Program: 115094
Project: 115094
Buggy source:
/** 
 * This methods will create an array of DisplayItemGroupBean, which contains multiple rows for an item group on the data entry form.
 * @param digb The Item group which has multiple data rows
 * @param dbGroups The original array got from DB which contains multiple data rows
 * @param formGroups The new array got from front end which contains multiple data rows
 * @param request TODO
 * @return new constructed formGroups, compare to dbGroups, some rows are update, some new ones are added and some are removed
 */
protected List<DisplayItemGroupBean> loadFormValueForItemGroup(DisplayItemGroupBean digb,List<DisplayItemGroupBean> dbGroups,List<DisplayItemGroupBean> formGroups,int eventDefCRFId,HttpServletRequest request){
  SectionBean sb=(SectionBean)request.getAttribute(SECTION_BEAN);
  int repeatMax=digb.getGroupMetaBean().getRepeatMax();
  FormProcessor fp=new FormProcessor(request);
  ItemDAO idao=new ItemDAO(getDataSource());
  List<ItemBean> itBeans=idao.findAllItemsByGroupId(digb.getItemGroupBean().getId(),sb.getCRFVersionId());
  LOGGER.debug("String_Node_Str" + repeatMax);
  long timeCheck=System.currentTimeMillis();
  EventCRFBean ecb=(EventCRFBean)request.getAttribute(INPUT_EVENT_CRF);
  FormBeanUtil formBeanUtil=new FormBeanUtil();
  List<String> nullValuesList=new ArrayList<String>();
  nullValuesList=formBeanUtil.getNullValuesByEventCRFDefId(eventDefCRFId,getDataSource());
  long two=System.currentTimeMillis() - timeCheck;
  int firstLoopBreak=0;
  int secondLoopBreak=0;
  for (int i=0; i < repeatMax; i++) {
    DisplayItemGroupBean formGroup=new DisplayItemGroupBean();
    formGroup.setItemGroupBean(digb.getItemGroupBean());
    formGroup.setGroupMetaBean(runDynamicsCheck(digb.getGroupMetaBean(),request));
    ItemGroupBean igb=digb.getItemGroupBean();
    List<DisplayItemBean> dibs=FormBeanUtil.getDisplayBeansFromItems(itBeans,getDataSource(),ecb,sb.getId(),nullValuesList,getServletContext());
    if (fp.getStartsWith(igb.getOid() + "String_Node_Str" + i+ "String_Node_Str")) {
      formGroup.setOrdinal(i);
      formGroup.setFormInputOrdinal(i);
      formGroup.setAuto(false);
      formGroup.setInputId(igb.getOid() + "String_Node_Str" + i);
      LOGGER.debug("String_Node_Str" + igb.getOid() + "String_Node_Str"+ i);
      dibs=processInputForGroupItem(fp,dibs,i,digb,false);
      formGroup.setItems(dibs);
      formGroups.add(formGroup);
    }
 else     if (!StringUtil.isBlank(fp.getString(igb.getOid() + "String_Node_Str" + i+ "String_Node_Str"))) {
      formGroup.setOrdinal(i);
      formGroup.setFormInputOrdinal(i);
      formGroup.setInputId(igb.getOid() + "String_Node_Str" + i+ "String_Node_Str");
      formGroup.setAuto(false);
      LOGGER.debug("String_Node_Str" + igb.getOid() + "String_Node_Str"+ i);
      dibs=processInputForGroupItem(fp,dibs,i,digb,false);
      formGroup.setItems(dibs);
      formGroups.add(formGroup);
    }
 else {
      firstLoopBreak++;
    }
    if (firstLoopBreak > 14) {
      LOGGER.debug("String_Node_Str");
      break;
    }
  }
  LOGGER.trace("String_Node_Str" + repeatMax);
  two=System.currentTimeMillis() - timeCheck;
  LOGGER.trace("String_Node_Str" + two + "String_Node_Str");
  for (int i=0; i < repeatMax; i++) {
    DisplayItemGroupBean formGroup=new DisplayItemGroupBean();
    formGroup.setItemGroupBean(digb.getItemGroupBean());
    formGroup.setGroupMetaBean(runDynamicsCheck(digb.getGroupMetaBean(),request));
    ItemGroupBean igb=digb.getItemGroupBean();
    List<DisplayItemBean> dibs=FormBeanUtil.getDisplayBeansFromItems(itBeans,getDataSource(),ecb,sb.getId(),nullValuesList,getServletContext());
    LOGGER.trace("String_Node_Str" + dibs.size());
    two=System.currentTimeMillis() - timeCheck;
    if (fp.getStartsWith(igb.getOid() + "String_Node_Str" + i+ "String_Node_Str")) {
      formGroup.setInputId(igb.getOid() + "String_Node_Str" + i);
      if (i == 0) {
        formGroup.setOrdinal(i);
      }
 else {
        formGroup.setFormInputOrdinal(i);
      }
      formGroup.setAuto(true);
      LOGGER.debug("String_Node_Str" + igb.getOid() + "String_Node_Str"+ i);
      dibs=processInputForGroupItem(fp,dibs,i,digb,true);
      LOGGER.trace("String_Node_Str" + i + "String_Node_Str"+ igb.getName());
      formGroup.setItems(dibs);
      formGroups.add(formGroup);
    }
 else     if (!StringUtil.isBlank(fp.getString(igb.getOid() + "String_Node_Str" + i+ "String_Node_Str"))) {
      formGroup.setInputId(igb.getOid() + "String_Node_Str" + i);
      if (i == 0) {
        formGroup.setOrdinal(i);
      }
 else {
        formGroup.setFormInputOrdinal(i);
      }
      formGroup.setAuto(true);
      LOGGER.debug("String_Node_Str" + igb.getOid() + "String_Node_Str"+ i);
      dibs=processInputForGroupItem(fp,dibs,i,digb,true);
      LOGGER.trace("String_Node_Str" + i + "String_Node_Str"+ igb.getName());
      formGroup.setItems(dibs);
      formGroups.add(formGroup);
    }
 else {
      secondLoopBreak++;
    }
    if (secondLoopBreak > 14) {
      LOGGER.debug("String_Node_Str");
      break;
    }
  }
  LOGGER.debug("String_Node_Str" + firstLoopBreak);
  LOGGER.debug("String_Node_Str" + secondLoopBreak);
  LOGGER.trace("String_Node_Str" + repeatMax);
  two=System.currentTimeMillis() - timeCheck;
  LOGGER.trace("String_Node_Str" + two + "String_Node_Str");
  int manualRows=getManualRows(formGroups);
  LOGGER.debug("String_Node_Str" + manualRows + "String_Node_Str"+ formGroups.size());
  request.setAttribute("String_Node_Str",new Integer(manualRows));
  for (int j=0; j < formGroups.size(); j++) {
    DisplayItemGroupBean formItemGroup=formGroups.get(j);
    if (formItemGroup.isAuto() && formItemGroup.getFormInputOrdinal() > 0) {
      LOGGER.trace("String_Node_Str" + formItemGroup.getFormInputOrdinal());
      formItemGroup.setOrdinal(formItemGroup.getFormInputOrdinal() + manualRows);
    }
  }
  LOGGER.trace("String_Node_Str" + repeatMax);
  two=System.currentTimeMillis() - timeCheck;
  LOGGER.trace("String_Node_Str" + two + "String_Node_Str");
  Collections.sort(formGroups);
  LOGGER.trace("String_Node_Str" + formGroups.size());
  int previous=-1;
  for (int j=0; j < formGroups.size(); j++) {
    DisplayItemGroupBean formItemGroup=formGroups.get(j);
    LOGGER.trace("String_Node_Str" + formItemGroup.getOrdinal());
    if (formItemGroup.getOrdinal() == previous) {
      LOGGER.debug("String_Node_Str");
      formItemGroup.setEditFlag("String_Node_Str");
      formItemGroup.setOrdinal(previous + 1);
    }
    if (formItemGroup.getOrdinal() > dbGroups.size() - 1) {
      formItemGroup.setEditFlag("String_Node_Str");
    }
 else {
      for (int i=0; i < dbGroups.size(); i++) {
        DisplayItemGroupBean dbItemGroup=dbGroups.get(i);
        if (formItemGroup.getOrdinal() == i) {
          if ("String_Node_Str".equalsIgnoreCase(dbItemGroup.getEditFlag())) {
            formItemGroup.setEditFlag("String_Node_Str");
          }
 else {
            dbItemGroup.setEditFlag("String_Node_Str");
            for (            DisplayItemBean dib : dbItemGroup.getItems()) {
              ItemDataBean data=dib.getData();
              for (              DisplayItemBean formDib : formItemGroup.getItems()) {
                if (formDib.getItem().getId() == dib.getItem().getId()) {
                  formDib.getData().setId(data.getId());
                  formDib.setDbData(dib.getData());
                  LOGGER.debug("String_Node_Str" + data.getId());
                  break;
                }
              }
            }
            formItemGroup.setEditFlag("String_Node_Str");
          }
          break;
        }
      }
    }
    previous=formItemGroup.getOrdinal();
  }
  LOGGER.trace("String_Node_Str" + dbGroups.size());
  LOGGER.trace("String_Node_Str" + dbGroups.toString());
  for (int i=0; i < dbGroups.size(); i++) {
    DisplayItemGroupBean dbItemGroup=dbGroups.get(i);
    LOGGER.trace("String_Node_Str" + dbItemGroup.getEditFlag() + "String_Node_Str"+ dbItemGroup.getOrdinal());
    if (!"String_Node_Str".equalsIgnoreCase(dbItemGroup.getEditFlag()) && !"String_Node_Str".equalsIgnoreCase(dbItemGroup.getEditFlag())) {
      if (dbItemGroup.getGroupMetaBean().isShowGroup()) {
        LOGGER.trace("String_Node_Str" + dbItemGroup.getEditFlag());
        LOGGER.debug("String_Node_Str" + dbItemGroup.getEditFlag());
        dbItemGroup.setEditFlag("String_Node_Str");
      }
    }
  }
  LOGGER.debug("String_Node_Str" + formGroups.toString());
  for (int j=0; j < formGroups.size(); j++) {
    DisplayItemGroupBean formGroup=formGroups.get(j);
    formGroup.setIndex(j);
  }
  return formGroups;
}

Fixed version (ground truth):
/** 
 * This methods will create an array of DisplayItemGroupBean, which contains multiple rows for an item group on the data entry form.
 * @param digb The Item group which has multiple data rows
 * @param dbGroups The original array got from DB which contains multiple data rows
 * @param formGroups The new array got from front end which contains multiple data rows
 * @param request TODO
 * @return new constructed formGroups, compare to dbGroups, some rows are update, some new ones are added and some are removed
 */
protected List<DisplayItemGroupBean> loadFormValueForItemGroup(DisplayItemGroupBean digb,List<DisplayItemGroupBean> dbGroups,List<DisplayItemGroupBean> formGroups,int eventDefCRFId,HttpServletRequest request){
  SectionBean sb=(SectionBean)request.getAttribute(SECTION_BEAN);
  int repeatMax=digb.getGroupMetaBean().getRepeatMax();
  FormProcessor fp=new FormProcessor(request);
  ItemDAO idao=new ItemDAO(getDataSource());
  List<ItemBean> itBeans=idao.findAllItemsByGroupId(digb.getItemGroupBean().getId(),sb.getCRFVersionId());
  LOGGER.debug("String_Node_Str" + repeatMax);
  long timeCheck=System.currentTimeMillis();
  EventCRFBean ecb=(EventCRFBean)request.getAttribute(INPUT_EVENT_CRF);
  FormBeanUtil formBeanUtil=new FormBeanUtil();
  List<String> nullValuesList=new ArrayList<String>();
  nullValuesList=formBeanUtil.getNullValuesByEventCRFDefId(eventDefCRFId,getDataSource());
  long two=System.currentTimeMillis() - timeCheck;
  int firstLoopBreak=0;
  int secondLoopBreak=0;
  ItemDataDAO iddao=new ItemDataDAO(getDataSource(),locale);
  int maxOrdinal=iddao.getMaxOrdinalForGroup(ecb,sb,digb.getItemGroupBean());
  repeatMax=(repeatMax < maxOrdinal) ? maxOrdinal : repeatMax;
  for (int i=0; i < repeatMax; i++) {
    DisplayItemGroupBean formGroup=new DisplayItemGroupBean();
    formGroup.setItemGroupBean(digb.getItemGroupBean());
    formGroup.setGroupMetaBean(runDynamicsCheck(digb.getGroupMetaBean(),request));
    ItemGroupBean igb=digb.getItemGroupBean();
    List<DisplayItemBean> dibs=FormBeanUtil.getDisplayBeansFromItems(itBeans,getDataSource(),ecb,sb.getId(),nullValuesList,getServletContext());
    if (fp.getStartsWith(igb.getOid() + "String_Node_Str" + i+ "String_Node_Str")) {
      formGroup.setOrdinal(i);
      formGroup.setFormInputOrdinal(i);
      formGroup.setAuto(false);
      formGroup.setInputId(igb.getOid() + "String_Node_Str" + i);
      LOGGER.debug("String_Node_Str" + igb.getOid() + "String_Node_Str"+ i);
      dibs=processInputForGroupItem(fp,dibs,i,digb,false);
      formGroup.setItems(dibs);
      formGroups.add(formGroup);
    }
 else     if (!StringUtil.isBlank(fp.getString(igb.getOid() + "String_Node_Str" + i+ "String_Node_Str"))) {
      formGroup.setOrdinal(i);
      formGroup.setFormInputOrdinal(i);
      formGroup.setInputId(igb.getOid() + "String_Node_Str" + i+ "String_Node_Str");
      formGroup.setAuto(false);
      LOGGER.debug("String_Node_Str" + igb.getOid() + "String_Node_Str"+ i);
      dibs=processInputForGroupItem(fp,dibs,i,digb,false);
      formGroup.setItems(dibs);
      formGroups.add(formGroup);
    }
 else {
      firstLoopBreak++;
    }
    if (firstLoopBreak > 14) {
      LOGGER.debug("String_Node_Str");
      break;
    }
  }
  LOGGER.trace("String_Node_Str" + repeatMax);
  two=System.currentTimeMillis() - timeCheck;
  LOGGER.trace("String_Node_Str" + two + "String_Node_Str");
  for (int i=0; i < repeatMax; i++) {
    DisplayItemGroupBean formGroup=new DisplayItemGroupBean();
    formGroup.setItemGroupBean(digb.getItemGroupBean());
    formGroup.setGroupMetaBean(runDynamicsCheck(digb.getGroupMetaBean(),request));
    ItemGroupBean igb=digb.getItemGroupBean();
    List<DisplayItemBean> dibs=FormBeanUtil.getDisplayBeansFromItems(itBeans,getDataSource(),ecb,sb.getId(),nullValuesList,getServletContext());
    LOGGER.trace("String_Node_Str" + dibs.size());
    two=System.currentTimeMillis() - timeCheck;
    if (fp.getStartsWith(igb.getOid() + "String_Node_Str" + i+ "String_Node_Str")) {
      formGroup.setInputId(igb.getOid() + "String_Node_Str" + i);
      if (i == 0) {
        formGroup.setOrdinal(i);
      }
 else {
        formGroup.setFormInputOrdinal(i);
      }
      formGroup.setAuto(true);
      LOGGER.debug("String_Node_Str" + igb.getOid() + "String_Node_Str"+ i);
      dibs=processInputForGroupItem(fp,dibs,i,digb,true);
      LOGGER.trace("String_Node_Str" + i + "String_Node_Str"+ igb.getName());
      formGroup.setItems(dibs);
      formGroups.add(formGroup);
    }
 else     if (!StringUtil.isBlank(fp.getString(igb.getOid() + "String_Node_Str" + i+ "String_Node_Str"))) {
      formGroup.setInputId(igb.getOid() + "String_Node_Str" + i);
      if (i == 0) {
        formGroup.setOrdinal(i);
      }
 else {
        formGroup.setFormInputOrdinal(i);
      }
      formGroup.setAuto(true);
      LOGGER.debug("String_Node_Str" + igb.getOid() + "String_Node_Str"+ i);
      dibs=processInputForGroupItem(fp,dibs,i,digb,true);
      LOGGER.trace("String_Node_Str" + i + "String_Node_Str"+ igb.getName());
      formGroup.setItems(dibs);
      formGroups.add(formGroup);
    }
 else {
      secondLoopBreak++;
    }
    if (secondLoopBreak > 14) {
      LOGGER.debug("String_Node_Str");
      break;
    }
  }
  LOGGER.debug("String_Node_Str" + firstLoopBreak);
  LOGGER.debug("String_Node_Str" + secondLoopBreak);
  LOGGER.trace("String_Node_Str" + repeatMax);
  two=System.currentTimeMillis() - timeCheck;
  LOGGER.trace("String_Node_Str" + two + "String_Node_Str");
  int manualRows=getManualRows(formGroups);
  LOGGER.debug("String_Node_Str" + manualRows + "String_Node_Str"+ formGroups.size());
  request.setAttribute("String_Node_Str",new Integer(manualRows));
  for (int j=0; j < formGroups.size(); j++) {
    DisplayItemGroupBean formItemGroup=formGroups.get(j);
    if (formItemGroup.isAuto() && formItemGroup.getFormInputOrdinal() > 0) {
      LOGGER.trace("String_Node_Str" + formItemGroup.getFormInputOrdinal());
      formItemGroup.setOrdinal(formItemGroup.getFormInputOrdinal() + manualRows);
    }
  }
  LOGGER.trace("String_Node_Str" + repeatMax);
  two=System.currentTimeMillis() - timeCheck;
  LOGGER.trace("String_Node_Str" + two + "String_Node_Str");
  Collections.sort(formGroups);
  LOGGER.trace("String_Node_Str" + formGroups.size());
  int previous=-1;
  for (int j=0; j < formGroups.size(); j++) {
    DisplayItemGroupBean formItemGroup=formGroups.get(j);
    LOGGER.trace("String_Node_Str" + formItemGroup.getOrdinal());
    if (formItemGroup.getOrdinal() == previous) {
      LOGGER.debug("String_Node_Str");
      formItemGroup.setEditFlag("String_Node_Str");
      formItemGroup.setOrdinal(previous + 1);
    }
    if (formItemGroup.getOrdinal() > dbGroups.size() - 1) {
      formItemGroup.setEditFlag("String_Node_Str");
    }
 else {
      for (int i=0; i < dbGroups.size(); i++) {
        DisplayItemGroupBean dbItemGroup=dbGroups.get(i);
        if (formItemGroup.getOrdinal() == i) {
          if ("String_Node_Str".equalsIgnoreCase(dbItemGroup.getEditFlag())) {
            formItemGroup.setEditFlag("String_Node_Str");
          }
 else {
            dbItemGroup.setEditFlag("String_Node_Str");
            for (            DisplayItemBean dib : dbItemGroup.getItems()) {
              ItemDataBean data=dib.getData();
              for (              DisplayItemBean formDib : formItemGroup.getItems()) {
                if (formDib.getItem().getId() == dib.getItem().getId()) {
                  formDib.getData().setId(data.getId());
                  formDib.setDbData(dib.getData());
                  LOGGER.debug("String_Node_Str" + data.getId());
                  break;
                }
              }
            }
            formItemGroup.setEditFlag("String_Node_Str");
          }
          break;
        }
      }
    }
    previous=formItemGroup.getOrdinal();
  }
  LOGGER.trace("String_Node_Str" + dbGroups.size());
  LOGGER.trace("String_Node_Str" + dbGroups.toString());
  for (int i=0; i < dbGroups.size(); i++) {
    DisplayItemGroupBean dbItemGroup=dbGroups.get(i);
    LOGGER.trace("String_Node_Str" + dbItemGroup.getEditFlag() + "String_Node_Str"+ dbItemGroup.getOrdinal());
    if (!"String_Node_Str".equalsIgnoreCase(dbItemGroup.getEditFlag()) && !"String_Node_Str".equalsIgnoreCase(dbItemGroup.getEditFlag())) {
      if (dbItemGroup.getGroupMetaBean().isShowGroup()) {
        LOGGER.trace("String_Node_Str" + dbItemGroup.getEditFlag());
        LOGGER.debug("String_Node_Str" + dbItemGroup.getEditFlag());
        dbItemGroup.setEditFlag("String_Node_Str");
      }
    }
  }
  LOGGER.debug("String_Node_Str" + formGroups.toString());
  for (int j=0; j < formGroups.size(); j++) {
    DisplayItemGroupBean formGroup=formGroups.get(j);
    formGroup.setIndex(j);
  }
  return formGroups;
}