// note that this step sets us up both for
  // displaying the data on the form again, in the event of an error
  // and sending the data to the database, in the event of no error
  //
  // we have to do this after adding the validations, so that we don't
  // overwrite the value the item data bean had from initial data entry
  // before the validator stores it as part of the Matches Initial Data Entry
  // Value validation
  // dib = loadFormValue(dib);
  // return dib;
  // }
  // should be from the db, we check here for a difference
  @Override
  protected List<DisplayItemGroupBean> validateDisplayItemGroupBean(
      DiscrepancyValidator v,
      DisplayItemGroupBean digb,
      List<DisplayItemGroupBean> digbs,
      List<DisplayItemGroupBean> formGroups,
      HttpServletRequest request,
      HttpServletResponse response) {
    EventDefinitionCRFBean edcb = (EventDefinitionCRFBean) request.getAttribute(EVENT_DEF_CRF_BEAN);
    HttpSession session = request.getSession();
    logger.info("===got this far");
    EventCRFBean ecb = (EventCRFBean) request.getAttribute(INPUT_EVENT_CRF);

    int keyId = ecb.getId();
    Integer validationCount = (Integer) session.getAttribute(COUNT_VALIDATE + keyId);

    formGroups = loadFormValueForItemGroup(digb, digbs, formGroups, edcb.getId(), request);
    logger.info(
        "found formgroups size for "
            + digb.getGroupMetaBean().getName()
            + ": "
            + formGroups.size()
            + " compare to db groups size: "
            + digbs.size());

    String inputName = "";
    for (int i = 0; i < formGroups.size(); i++) {
      DisplayItemGroupBean displayGroup = formGroups.get(i);

      List<DisplayItemBean> items = displayGroup.getItems();
      int order = displayGroup.getOrdinal();
      if (displayGroup.isAuto() && displayGroup.getFormInputOrdinal() > 0) {
        order = displayGroup.getFormInputOrdinal();
      }
      for (DisplayItemBean displayItem : items) {
        if (displayGroup.isAuto()) {
          inputName = getGroupItemInputName(displayGroup, order, displayItem);
        } else {
          inputName = getGroupItemManualInputName(displayGroup, order, displayItem);
        }
        validateDisplayItemBean(v, displayItem, inputName, request);
      }

      if (validationCount == null || validationCount.intValue() == 0) {
        if (i == 0 && formGroups.size() != digbs.size()) {
          v.addValidation(inputName + "group", Validator.DIFFERENT_NUMBER_OF_GROUPS_IN_DDE);
          // TODO internationalize this string, tbh
          v.setErrorMessage(
              "There are additional values here that were not present in the initial data entry. You have entered a different number of groups"
                  + " for the item groups containing "
                  + inputName);
        }
      }
    }

    return formGroups;
  }
  @Override
  protected DisplayItemBean validateDisplayItemBean(
      DiscrepancyValidator v, DisplayItemBean dib, String inputName) {

    org.akaza.openclinica.bean.core.ResponseType rt =
        dib.getMetadata().getResponseSet().getResponseType();

    boolean isSingleItem = false;
    if (StringUtil.isBlank(inputName)) { // for single items
      inputName = getInputName(dib);
      isSingleItem = true;
    }
    // we only give warning to user if data entered in DDE is different from
    // IDE when the first
    // time user hits 'save'
    int keyId = ecb.getId();
    Integer validationCount = (Integer) session.getAttribute(COUNT_VALIDATE + keyId);

    ItemDataBean valueToCompare = dib.getData();
    if (!isSingleItem) {
      valueToCompare = dib.getDbData();
    }
    boolean showOriginalItem =
        getItemMetadataService()
            .isShown(dib.getItem().getId(), ecb, valueToCompare); // was dib.getData()
    boolean showItem = dib.getMetadata().isShowItem();
    boolean showDuplicateItem =
        getItemMetadataService()
            .hasPassedDDE(
                dib.getMetadata(),
                ecb,
                valueToCompare); // .isShown(dib.getItem().getId(), ecb, dib.getDbData());// where
    // is the set db data?
    logger.debug(
        "*** show original item has value "
            + dib.getData().getValue()
            + " and show item has value "
            + valueToCompare.getValue());
    logger.debug(
        "--- show original: "
            + showOriginalItem
            + " show duplicate: "
            + showDuplicateItem
            + " and just show item: "
            + showItem);
    logger.debug("VALIDATION COUNT " + validationCount);
    if (showOriginalItem && showDuplicateItem || showItem) {
      // it should either be shown already, OR shown in the database?
      // logger.debug("=== we passed, adding validation here");
      Integer indValidationCount =
          (Integer) session.getAttribute(COUNT_VALIDATE + keyId + dib.getMetadata().getId());
      if (rt.equals(org.akaza.openclinica.bean.core.ResponseType.TEXT)
          || rt.equals(org.akaza.openclinica.bean.core.ResponseType.TEXTAREA)
          || rt.equals(org.akaza.openclinica.bean.core.ResponseType.FILE)) {
        dib = validateDisplayItemBeanText(v, dib, inputName);
        // necessary?
        // if (indValidationCount == null || validationCount == null || validationCount.intValue()
        // == 0) {
        logger.debug("=== we passed, adding validation here TEXT " + valueToCompare.getId());
        v.addValidation(
            inputName, Validator.MATCHES_INITIAL_DATA_ENTRY_VALUE, valueToCompare, false);
        v.setErrorMessage(
            respage.getString("value_you_specified")
                + " "
                + valueToCompare.getValue()
                + " "
                + respage.getString("from_initial_data_entry"));
        // session.setAttribute(COUNT_VALIDATE + keyId + dib.getMetadata().getId(), new Integer(1));
        // }

      } else if (rt.equals(org.akaza.openclinica.bean.core.ResponseType.RADIO)
          || rt.equals(org.akaza.openclinica.bean.core.ResponseType.SELECT)) {
        dib = validateDisplayItemBeanSingleCV(v, dib, inputName);
        // ItemFormMetadataBean ifmdb = dib.getMetadata();
        // ResponseSetBean rsBean = ifmdb.getResponseSet();
        // logger.info("### found a response set count of "+inputName+"
        // "+rsBean.getOptions().size());
        // TODO sees it at this end tbh 1878
        // if (indValidationCount == null || validationCount == null || validationCount.intValue()
        // == 0) {
        logger.debug("=== we passed, adding validation here RADIO " + valueToCompare.getId());
        v.addValidation(
            inputName, Validator.MATCHES_INITIAL_DATA_ENTRY_VALUE, valueToCompare, false);
        String errorValue = valueToCompare.getValue();

        java.util.ArrayList options = dib.getMetadata().getResponseSet().getOptions();

        for (int u = 0; u < options.size(); u++) {
          ResponseOptionBean rob = (ResponseOptionBean) options.get(u);
          if (rob.getValue().equals(errorValue)) {
            errorValue = rob.getText();
          }
        }
        v.setErrorMessage(
            respage.getString("value_you_specified")
                + " "
                + errorValue
                + " "
                + respage.getString("from_initial_data_entry"));
        // session.setAttribute(COUNT_VALIDATE + keyId + dib.getMetadata().getId(), new Integer(1));
        // }
      } else if (rt.equals(org.akaza.openclinica.bean.core.ResponseType.CHECKBOX)
          || rt.equals(org.akaza.openclinica.bean.core.ResponseType.SELECTMULTI)) {
        dib = validateDisplayItemBeanMultipleCV(v, dib, inputName);
        // if (indValidationCount == null || validationCount == null || validationCount.intValue()
        // == 0) {
        logger.debug("=== we passed, adding validation here CHECKBOX " + valueToCompare.getId());
        v.addValidation(
            inputName, Validator.MATCHES_INITIAL_DATA_ENTRY_VALUE, valueToCompare, true);
        // repeated from above, tbh 112007
        String errorValue = valueToCompare.getValue();
        String errorTexts = "";

        java.util.ArrayList options = dib.getMetadata().getResponseSet().getOptions();

        for (int u = 0; u < options.size(); u++) {
          ResponseOptionBean rob = (ResponseOptionBean) options.get(u);
          if (errorValue.contains(rob.getValue())) {
            errorTexts = errorTexts + rob.getText();
            if (u < options.size() - 1) {
              // the values for multi-select are seperated by
              // comma
              errorTexts = errorTexts + ", ";
            }
          }
        }
        v.setErrorMessage(
            respage.getString("value_you_specified")
                + " "
                + errorTexts
                + " "
                + respage.getString("from_initial_data_entry"));
        // session.setAttribute(COUNT_VALIDATE + keyId + dib.getMetadata().getId(), new Integer(1));
        // }
      }
    }
    // only load form value when an item is not in a group,
    // if in group, the value is already loaded
    // see formGroups = loadFormValueForItemGroup(digb,digbs,formGroups);
    if (isSingleItem) {
      dib = loadFormValue(dib);
    }

    return dib;
  }
  @Override
  protected DisplayItemBean validateDisplayItemBean(
      DiscrepancyValidator v, DisplayItemBean dib, String inputName, HttpServletRequest request) {

    org.akaza.openclinica.bean.core.ResponseType rt =
        dib.getMetadata().getResponseSet().getResponseType();
    HttpSession session = request.getSession();
    EventCRFBean ecb = (EventCRFBean) request.getAttribute(INPUT_EVENT_CRF);

    boolean isSingleItem = false;
    if (StringUtil.isBlank(inputName)) { // for single items
      inputName = getInputName(dib);
      isSingleItem = true;
    }

    // we only give warning to user if data entered in DDE is different from
    // IDE when the first
    // time user hits 'save'
    int keyId = ecb.getId();
    Integer validationCount = (Integer) session.getAttribute(COUNT_VALIDATE + keyId);

    // @pgawade 12-Aug-2011 issue 10601:
    // 1. Moved the call to loadFormValue method setting values
    // from form here. It was at the end
    // of this method before that is after validations and so an extra
    // validation for checking value entered
    // against the data type was getting implemented for non-repeating group
    // items.
    // 2. Copying the form data into new object for non repeating items to
    // pass to validation
    // for matching IDE and DDE values. Id same reference is used, the way
    // code here was written, it affetcs
    // the value used in validation of data entered in DDE against the
    // related data type
    ItemDataBean valueToCompareTmp = dib.getData();
    ItemDataBean valueToCompare = copyItemDataBean(valueToCompareTmp);

    if (!isSingleItem) {
      valueToCompare = dib.getDbData();
    }

    // only load form value when an item is not in a group,
    // if in group, the value is already loaded
    // see formGroups = loadFormValueForItemGroup(digb,digbs,formGroups);
    if (isSingleItem) {
      dib = loadFormValue(dib, request);
    }

    boolean showOriginalItem =
        getItemMetadataService()
            .isShown(dib.getItem().getId(), ecb, valueToCompare); // was dib.getData()
    boolean showItem = dib.getMetadata().isShowItem();
    if (!showItem && dib.getScdData().getScdItemMetadataBean().getScdItemFormMetadataId() > 0) {
      showItem = true;
    }
    boolean showDuplicateItem =
        getItemMetadataService()
            .hasPassedDDE(
                dib.getMetadata(),
                ecb,
                valueToCompare); // .isShown(dib.getItem().getId(), ecb, dib.getDbData());// where
                                 // is the set db data?
    logger.debug(
        "*** show original item has value "
            + dib.getData().getValue()
            + " and show item has value "
            + valueToCompare.getValue());
    logger.debug(
        "--- show original: "
            + showOriginalItem
            + " show duplicate: "
            + showDuplicateItem
            + " and just show item: "
            + showItem);
    logger.debug("VALIDATION COUNT " + validationCount);
    if (showOriginalItem && showDuplicateItem || showItem) {
      if (rt.equals(org.akaza.openclinica.bean.core.ResponseType.TEXT)
          || rt.equals(org.akaza.openclinica.bean.core.ResponseType.TEXTAREA)
          || rt.equals(org.akaza.openclinica.bean.core.ResponseType.FILE)) {
        dib = validateDisplayItemBeanText(v, dib, inputName, request);
        if (validationCount == null || validationCount.intValue() == 0) {
          v.addValidation(
              inputName, Validator.MATCHES_INITIAL_DATA_ENTRY_VALUE, valueToCompare, false);
          v.setErrorMessage(
              respage.getString("value_you_specified")
                  + " "
                  + valueToCompare.getValue()
                  + " "
                  + respage.getString("from_initial_data_entry"));
        }

      } else if (rt.equals(org.akaza.openclinica.bean.core.ResponseType.RADIO)
          || rt.equals(org.akaza.openclinica.bean.core.ResponseType.SELECT)) {
        dib = validateDisplayItemBeanSingleCV(v, dib, inputName);
        // ItemFormMetadataBean ifmdb = dib.getMetadata();
        // ResponseSetBean rsBean = ifmdb.getResponseSet();
        // logger.info("### found a response set count of "+inputName+"
        // "+rsBean.getOptions().size());
        // TODO sees it at this end tbh 1878

        if (validationCount == null || validationCount.intValue() == 0) {
          v.addValidation(
              inputName, Validator.MATCHES_INITIAL_DATA_ENTRY_VALUE, valueToCompare, false);
          String errorValue = valueToCompare.getValue();

          java.util.ArrayList options = dib.getMetadata().getResponseSet().getOptions();

          for (int u = 0; u < options.size(); u++) {
            ResponseOptionBean rob = (ResponseOptionBean) options.get(u);
            if (rob.getValue().equals(errorValue)) {
              errorValue = rob.getText();
            }
          }
          v.setErrorMessage(
              respage.getString("value_you_specified")
                  + " "
                  + errorValue
                  + " "
                  + respage.getString("from_initial_data_entry"));
        }
      } else if (rt.equals(org.akaza.openclinica.bean.core.ResponseType.CHECKBOX)
          || rt.equals(org.akaza.openclinica.bean.core.ResponseType.SELECTMULTI)) {
        dib = validateDisplayItemBeanMultipleCV(v, dib, inputName);

        if (validationCount == null || validationCount.intValue() == 0) {
          v.addValidation(
              inputName, Validator.MATCHES_INITIAL_DATA_ENTRY_VALUE, valueToCompare, true);
          // repeated from above, tbh 112007
          String errorValue = valueToCompare.getValue();
          String errorTexts = "";

          java.util.ArrayList options = dib.getMetadata().getResponseSet().getOptions();

          for (int u = 0; u < options.size(); u++) {
            ResponseOptionBean rob = (ResponseOptionBean) options.get(u);
            if (errorValue.contains(rob.getValue())) {
              errorTexts = errorTexts + rob.getText();
              if (u < options.size() - 1) {
                // the values for multi-select are seperated by
                // comma
                errorTexts = errorTexts + ", ";
              }
            }
          }
          v.setErrorMessage(
              respage.getString("value_you_specified")
                  + " "
                  + errorTexts
                  + " "
                  + respage.getString("from_initial_data_entry"));
        }
      }
    }
    // // only load form value when an item is not in a group,
    // // if in group, the value is already loaded
    // // see formGroups = loadFormValueForItemGroup(digb,digbs,formGroups);
    // if (isSingleItem) {
    // dib = loadFormValue(dib, request);
    // }

    return dib;
  }