/**
   * Re-do calculations if funcs include changed item(s) and funcs are not included in the current
   * section. If calculation can not sucessfully redo, old value will be erased and "<erased>" will
   * be saved in database. <br>
   * The parameter 'itemdata' might be overwritten.
   *
   * @param itemGroupSizes
   * @param items
   * @param itemdata
   * @param oldItemdata
   * @param updatedData
   * @param sectionId
   * @return ArrayList<String> which records left_item_text of items who failed to be updated into
   *     database.
   */
  public ArrayList<String> redoCalculations(
      HashMap<String, ItemBean> items,
      HashMap<String, String> itemdata,
      TreeSet<String> changedItems,
      HashMap<Integer, TreeSet<Integer>> itemOrdinals,
      int sectionId) {
    ArrayList<String> updateFailedItems = new ArrayList<String>();
    if (itemdata == null) {
      logger.error("In ScoreCalculator redoCalculations(), itemdata is empty!");
      errors.add("In ScoreCalculator redoCalculations(), 'itemdata' map is empty!");
      return updateFailedItems;
    }
    if (changedItems == null) {
      logger.error("In ScoreCalculator redoCalculations(), 'changeItems' set is empty!");
      errors.add("In ScoreCalculator redoCalculations(), 'changeItems' set is empty!");
      return updateFailedItems;
    }
    ItemFormMetadataDAO ifmdao = new ItemFormMetadataDAO(sm.getDataSource());
    ItemDAO idao = new ItemDAO(sm.getDataSource());
    ItemDataDAO iddao = new ItemDataDAO(sm.getDataSource());

    NumberFormat nf = NumberFormat.getInstance();
    Parser parser = new Parser(items, itemdata);
    try {
      // for calculation type
      List<ItemFormMetadataBean> derivedItemList =
          ifmdao.findAllByCRFVersionIdAndResponseTypeId(
              ecb.getCRFVersionId(), ResponseType.CALCULATION.getId());
      if (derivedItemList.size() > 0) {
        Collections.sort(derivedItemList);
        for (ItemFormMetadataBean ifmb : derivedItemList) {
          if (ifmb.getSectionId() != sectionId) {
            ItemBean ib = (ItemBean) idao.findByPK(ifmb.getItemId());
            ResponseOptionBean rob = (ResponseOptionBean) ifmb.getResponseSet().getOptions().get(0);
            int groupsize = 1;
            if (itemOrdinals.containsKey(ib.getId())) {
              groupsize = itemOrdinals.get(ib.getId()).size();
            }
            String value = "";
            ArrayList<ScoreToken> parsedExp = new ArrayList<ScoreToken>();
            for (int i = 0; i < groupsize; ++i) {
              ItemDataBean idb =
                  iddao.findByItemIdAndEventCRFIdAndOrdinal(ifmb.getItemId(), ecb.getId(), i + 1);
              // is there any changed item
              Parser p = new Parser(items, itemdata);
              parsedExp = parser.parseScoreTokens(rob.getValue());
              if (p.isChanged(changedItems, parsedExp)) {
                StringBuffer err = new StringBuffer();
                parsedExp = parser.assignVariables(parsedExp, i + 1);
                // if parser has error and has been calculated
                // before, set "<erased>"
                if (parser.getErrors().length() > 0) {
                  err.append(parser.getErrors());
                  if (idb.isActive()) {
                    idb.setValue("<erased>");
                    idb.setStatus(Status.UNAVAILABLE);
                    idb = (ItemDataBean) iddao.update(idb);
                    if (!idb.isActive()) {
                      String key =
                          i + 1 > 1
                              ? ifmb.getLeftItemText() + "_" + (i + 1)
                              : ifmb.getLeftItemText();
                      updateFailedItems.add(key);
                    }
                  }
                  parser.setErrors(new StringBuffer());
                }
                // otherwise do calculation
                else {
                  try {
                    value = ScoreUtil.eval(parsedExp);
                  } catch (ScoreException se) {
                    logger.error(se.getMessage());
                  }
                  String exp = rob.getValue();
                  exp = exp.replace("##", ",");
                  if (writeToDB(ib, ifmb, idb, exp, value, err)) {
                    changedItems.add(ib.getName());
                    itemdata.put(ib.getId() + "_" + (i + 1), idb.getValue());
                  } else {
                    String key =
                        i + 1 > 1 ? ifmb.getLeftItemText() + "_" + (i + 1) : ifmb.getLeftItemText();
                    updateFailedItems.add(key);
                  }
                }
                if (err.length() > 0) {
                  String key =
                      i + 1 > 1 ? ifmb.getLeftItemText() + "_" + (i + 1) : ifmb.getLeftItemText();
                  errors.add("Item " + key + " contains calculation errors: " + err.toString());
                }
              }
            }
          }
        }
      }

      List<ItemFormMetadataBean> itemList =
          ifmdao.findAllByCRFVersionIdAndResponseTypeId(
              ecb.getCRFVersionId(), ResponseType.GROUP_CALCULATION.getId());
      if (itemList.size() > 0) {
        Collections.sort(itemList);
        for (ItemFormMetadataBean ifmb : itemList) {
          if (ifmb.getSectionId() != sectionId) {
            ItemBean ib = (ItemBean) idao.findByPK(ifmb.getItemId());
            ResponseOptionBean rob = (ResponseOptionBean) ifmb.getResponseSet().getOptions().get(0);
            String value = "";
            Parser p = new Parser(items, itemdata);
            ArrayList<ScoreToken> parsedExp = parser.parseScoreTokens(rob.getValue());
            if (p.isChanged(changedItems, parsedExp)) {
              StringBuffer err = new StringBuffer();
              parser.setErrors(err);
              parsedExp = parser.assignVariables(parsedExp, itemOrdinals);
              ItemDataBean idb =
                  iddao.findByItemIdAndEventCRFIdAndOrdinal(ifmb.getItemId(), ecb.getId(), 1);
              if (parser.getErrors().length() > 0) {
                err.append(parser.getErrors());
                if (idb.isActive()) {
                  idb.setValue("<erased>");
                  idb.setStatus(Status.UNAVAILABLE);
                  idb = (ItemDataBean) iddao.update(idb);
                  if (!idb.isActive()) {
                    updateFailedItems.add(ifmb.getLeftItemText());
                  }
                }
              } else {
                try {
                  value = ScoreUtil.eval(parsedExp);
                } catch (ScoreException se) {
                  logger.error(se.getMessage());
                }
                String exp = rob.getValue();
                exp = exp.replace("##", ",");
                if (writeToDB(ib, ifmb, idb, exp, value, err)) {
                  changedItems.add(ib.getName());
                  itemdata.put(ib.getId() + "_" + idb.getOrdinal(), idb.getValue());
                } else {
                  updateFailedItems.add(ifmb.getLeftItemText());
                }
              }
              if (err.length() > 0) {
                errors.add(
                    "Item "
                        + ifmb.getLeftItemText()
                        + " contains calculation errors: "
                        + err.toString());
              }
            }
          }
        }
      }
    } catch (OpenClinicaException e) {
      logger.error(e.getMessage());
    }

    return updateFailedItems;
  }
  @Override
  @SuppressWarnings(value = "unchecked")
  public void processRequest() throws Exception {
    ItemDataDAO itemDataDao = new ItemDataDAO(sm.getDataSource());
    EventCRFDAO eventCrfDao = new EventCRFDAO(sm.getDataSource());
    CrfBusinessLogicHelper crfBusinessLogicHelper = new CrfBusinessLogicHelper(sm.getDataSource());
    String action = request.getParameter("action");

    FormProcessor fp = new FormProcessor(request);

    // checks which module the requests are from
    String module = fp.getString(MODULE);
    request.setAttribute(MODULE, module);

    resetPanel();
    panel.setStudyInfoShown(false);
    panel.setOrderedData(true);

    setToPanel(resword.getString("create_CRF"), respage.getString("br_create_new_CRF_entering"));

    setToPanel(
        resword.getString("create_CRF_version"), respage.getString("br_create_new_CRF_uploading"));
    setToPanel(
        resword.getString("revise_CRF_version"), respage.getString("br_if_you_owner_CRF_version"));
    setToPanel(
        resword.getString("CRF_spreadsheet_template"),
        respage.getString("br_download_blank_CRF_spreadsheet_from"));
    setToPanel(
        resword.getString("example_CRF_br_spreadsheets"),
        respage.getString("br_download_example_CRF_instructions_from"));

    if ("confirm".equalsIgnoreCase(action)) {
      List<DisplayItemBeanWrapper> displayItemBeanWrappers =
          (List<DisplayItemBeanWrapper>) session.getAttribute("importedData");
      logger.info("Size of displayItemBeanWrappers : " + displayItemBeanWrappers.size());
      forwardPage(Page.VERIFY_IMPORT_CRF_DATA);
    }

    if ("save".equalsIgnoreCase(action)) {
      List<DisplayItemBeanWrapper> displayItemBeanWrappers =
          (List<DisplayItemBeanWrapper>) session.getAttribute("importedData");
      // System.out.println("Size of displayItemBeanWrappers : " + displayItemBeanWrappers.size());

      for (DisplayItemBeanWrapper wrapper : displayItemBeanWrappers) {

        int eventCrfBeanId = -1;
        EventCRFBean eventCrfBean = new EventCRFBean();

        // TODO : tom , the wrapper object has all the necessary data -
        // as you see we check the
        // is to see if this data is Savable if it is then we go ahead
        // and save it. if not we discard.
        // So the change needs to happen here , instead of discarding we
        // need to file discrepancy notes
        // and save the data. If you look in the
        // Page.VERIFY_IMPORT_CRF_DATA jsp file you can see how I am
        // pulling the errors. and use that in the same way.

        logger.info("right before we check to make sure it is savable: " + wrapper.isSavable());
        if (wrapper.isSavable()) {
          ArrayList<Integer> eventCrfInts = new ArrayList<Integer>();
          // based on the use case: "If any of the data does not meet
          // validations specified in the CRF
          // Template, a discrepancy note is automatically logged.
          // The DN will have a type of Failed Validation Check, and
          // a message of Failed Validation check."
          // System.out.println("wrapper problems found : " +
          // wrapper.getValidationErrors().toString());
          for (DisplayItemBean displayItemBean : wrapper.getDisplayItemBeans()) {
            eventCrfBeanId = displayItemBean.getData().getEventCRFId();
            eventCrfBean = (EventCRFBean) eventCrfDao.findByPK(eventCrfBeanId);
            logger.info("found value here: " + displayItemBean.getData().getValue());
            logger.info("found status here: " + eventCrfBean.getStatus().getName());
            // System.out.println("found event crf bean name here: "
            // +
            // eventCrfBean.getEventName()+" id "+eventCrfBean.getId
            // ());
            // SO, items can be created in a wrapper which is set to
            // overwrite
            // we get around this by checking the bean first, to
            // make sure it's not null
            ItemDataBean itemDataBean = new ItemDataBean();
            itemDataBean =
                itemDataDao.findByItemIdAndEventCRFIdAndOrdinal(
                    displayItemBean.getItem().getId(),
                    eventCrfBean.getId(),
                    displayItemBean.getData().getOrdinal());
            if (wrapper.isOverwrite() && itemDataBean.getStatus() != null) {
              // ItemDataBean itemDataBean = new ItemDataBean();
              // itemDataBean =
              // itemDataDao.findByItemIdAndEventCRFIdAndOrdinal(
              // displayItemBean.getItem().getId(),
              // eventCrfBean.getId(), displayItemBean
              // .getData().getOrdinal());
              // itemDataBean =
              // itemDataDao.findByEventCRFIdAndItemName(
              // eventCrfBean,
              // displayItemBean.getItem().getName());
              logger.info(
                  "just tried to find item data bean on item name "
                      + displayItemBean.getItem().getName());
              itemDataBean.setUpdatedDate(new Date());
              itemDataBean.setUpdater(ub);
              itemDataBean.setValue(displayItemBean.getData().getValue());
              // set status?
              itemDataDao.update(itemDataBean);
              logger.info("updated: " + itemDataBean.getItemId());
              // need to set pk here in order to create dn
              displayItemBean.getData().setId(itemDataBean.getId());
            } else {
              itemDataDao.create(displayItemBean.getData());
              logger.info(
                  "created: "
                      + displayItemBean.getData().getItemId()
                      + "event CRF ID = "
                      + eventCrfBean.getId()
                      + "CRF VERSION ID ="
                      + eventCrfBean.getCRFVersionId());

              // does this dao function work for repeating
              // events/groups?
              // ItemDataBean itemDataBean =
              // itemDataDao.findByEventCRFIdAndItemName(
              // eventCrfBean,
              // displayItemBean.getItem().getName());
              ItemDataBean itemDataBean2 =
                  itemDataDao.findByItemIdAndEventCRFIdAndOrdinal(
                      displayItemBean.getItem().getId(),
                      eventCrfBean.getId(),
                      displayItemBean.getData().getOrdinal());
              logger.info(
                  "found: id " + itemDataBean2.getId() + " name " + itemDataBean2.getName());
              displayItemBean.getData().setId(itemDataBean2.getId());
            }
            // logger.info("created item data bean:
            // "+displayItemBean.getData().getId());
            // logger.info("created:
            // "+displayItemBean.getData().getName());
            // logger.info("continued:
            // "+displayItemBean.getData().getItemId());
            ItemDAO idao = new ItemDAO(sm.getDataSource());
            ItemBean ibean = (ItemBean) idao.findByPK(displayItemBean.getData().getItemId());
            // logger.info("continued2: getName " +
            // ibean.getName());
            // System.out.println("*** checking for validation errors: " + ibean.getName());
            String itemOid =
                displayItemBean.getItem().getOid()
                    + "_"
                    + wrapper.getStudyEventRepeatKey()
                    + "_"
                    + displayItemBean.getData().getOrdinal()
                    + "_"
                    + wrapper.getStudySubjectOid();
            if (wrapper.getValidationErrors().containsKey(itemOid)) {
              ArrayList messageList = (ArrayList) wrapper.getValidationErrors().get(itemOid);
              // if
              // (wrapper.getValidationErrors().containsKey(ibean
              // .getName())) {
              // ArrayList messageList = (ArrayList)
              // wrapper.getValidationErrors
              // ().get(ibean.getName());
              // could be more then one will have to iterate
              // could it be more than one? tbh 08/2008
              for (int iter = 0; iter < messageList.size(); iter++) {
                String message = (String) messageList.get(iter);
                DiscrepancyNoteBean parentDn =
                    ImportSpringJob.createDiscrepancyNote(
                        ibean,
                        message,
                        eventCrfBean,
                        displayItemBean,
                        null,
                        ub,
                        sm.getDataSource(),
                        currentStudy);
                ImportSpringJob.createDiscrepancyNote(
                    ibean,
                    message,
                    eventCrfBean,
                    displayItemBean,
                    parentDn.getId(),
                    ub,
                    sm.getDataSource(),
                    currentStudy);
                // System.out.println("*** created disc note with message: " + message);
                // displayItemBean);
              }
            }
            // logger.info("created:
            // "+displayItemBean.getDbData().getName());
            if (!eventCrfInts.contains(new Integer(eventCrfBean.getId()))) {
              crfBusinessLogicHelper.markCRFComplete(eventCrfBean, ub);
              // System.out.println("*** just updated event crf bean: " + eventCrfBean.getId());
              eventCrfInts.add(new Integer(eventCrfBean.getId()));
            }
          }
          // end of item datas, tbh
          // crfBusinessLogicHelper.markCRFComplete(eventCrfBean, ub);
          // System .out.println("*** just updated event crf bean: "+
          // eventCrfBean.getId());
          // need to update the study event status as well, tbh
          // crfBusinessLogicHelper.updateStudyEvent(eventCrfBean,
          // ub);
          // above should do it for us, tbh 08/2008
        }
      }

      addPageMessage(respage.getString("data_has_been_successfully_import"));
      // forwardPage(Page.SUBMIT_DATA_SERVLET);
      forwardPage(Page.LIST_STUDY_SUBJECTS_SERVLET);
      // replaced tbh, 06/2009
    }
  }