private AnalyzerTestMapping validateAnalyzerAndTestName(
      String analyzerId,
      String analyzerTestName,
      String testId,
      ActionMessages errors,
      boolean newMapping) {
    // This is not very efficient but this is a very low usage action
    if (GenericValidator.isBlankOrNull(analyzerId)
        || GenericValidator.isBlankOrNull(analyzerTestName)
        || GenericValidator.isBlankOrNull(testId)) {
      errors.add(ActionErrors.GLOBAL_MESSAGE, new ActionError("error.all.required"));
      return null;
    }

    AnalyzerTestMapping existingMapping = null;
    AnalyzerTestMappingDAO mappingDAO = new AnalyzerTestMappingDAOImpl();
    List<AnalyzerTestMapping> testMappingList = mappingDAO.getAllAnalyzerTestMappings();
    for (AnalyzerTestMapping testMapping : testMappingList) {
      if (analyzerId.equals(testMapping.getAnalyzerId())
          && analyzerTestName.equals(testMapping.getAnalyzerTestName())) {
        if (newMapping) {
          errors.add(
              ActionErrors.GLOBAL_MESSAGE, new ActionError("error.analyzer.test.name.duplicate"));
          return null;
        } else {
          existingMapping = testMapping;
          testMapping.setTestId(testId);
          break;
        }
      }
    }

    return existingMapping;
  }
  public String validateAndUpdateAnalyzerTestName(
      ActionMapping mapping, HttpServletRequest request, BaseActionForm dynaForm) {
    String forward = FWD_SUCCESS_INSERT;
    String analyzerId = dynaForm.getString("analyzerId");
    String testId = dynaForm.getString("testId");
    String analyzerTestName = dynaForm.getString("analyzerTestName");
    boolean newMapping = "0".equals(request.getParameter("ID"));

    ActionMessages errors = new ActionMessages();

    AnalyzerTestMapping analyzerTestNameMapping =
        validateAnalyzerAndTestName(analyzerId, analyzerTestName, testId, errors, newMapping);

    if (errors.size() > 0) {
      saveErrors(request, errors);
      return FWD_FAIL;
    }

    if (newMapping) {
      analyzerTestNameMapping = new AnalyzerTestMapping();
      analyzerTestNameMapping.setAnalyzerId(analyzerId);
      analyzerTestNameMapping.setAnalyzerTestName(analyzerTestName);
      analyzerTestNameMapping.setTestId(testId);
    }

    AnalyzerTestMappingDAO mappingDAO = new AnalyzerTestMappingDAOImpl();

    Transaction tx = HibernateUtil.getSession().beginTransaction();

    try {
      if (newMapping) {
        mappingDAO.insertData(analyzerTestNameMapping, currentUserId);
      } else {
        mappingDAO.updateMapping(analyzerTestNameMapping, currentUserId);
      }

    } catch (LIMSRuntimeException lre) {
      tx.rollback();

      ActionError error = null;
      if (lre.getException() instanceof org.hibernate.StaleObjectStateException) {
        error = new ActionError("errors.OptimisticLockException", null, null);
      } else {
        error = new ActionError("errors.UpdateException", null, null);
      }

      persisteError(request, error);

      disableNavigationButtons(request);
      forward = FWD_FAIL;
    } finally {
      if (!tx.wasRolledBack()) {
        tx.commit();
      }
      HibernateUtil.closeSession();
    }

    AnalyzerTestNameCache.instance().reloadCache();

    return forward;
  }