Program: 115113
Project: 115113
Buggy source:
/** 
 * Constructs a list of DisplayItemWithGroupBean, which is used for display a section of items on the UI
 * @param dsb
 * @param hasItemGroup
 * @param request TODO
 * @return
 */
protected List<DisplayItemWithGroupBean> createItemWithGroups(DisplaySectionBean dsb,boolean hasItemGroup,int eventCRFDefId,HttpServletRequest request){
  HttpSession session=request.getSession();
  List<DisplayItemWithGroupBean> displayItemWithGroups=new ArrayList<DisplayItemWithGroupBean>();
  EventCRFBean ecb=(EventCRFBean)request.getAttribute(INPUT_EVENT_CRF);
  ItemDAO idao=new ItemDAO(getDataSource());
  FormBeanUtil formBeanUtil=new FormBeanUtil();
  List<String> nullValuesList=new ArrayList<String>();
  SectionBean sb=(SectionBean)request.getAttribute(SECTION_BEAN);
  EventDefinitionCRFBean edcb=(EventDefinitionCRFBean)request.getAttribute(EVENT_DEF_CRF_BEAN);
  nullValuesList=formBeanUtil.getNullValuesByEventCRFDefId(eventCRFDefId,getDataSource());
  ArrayList items=dsb.getItems();
  logger.trace("String_Node_Str" + items.size());
  for (int i=0; i < items.size(); i++) {
    DisplayItemBean item=(DisplayItemBean)items.get(i);
    DisplayItemWithGroupBean newOne=new DisplayItemWithGroupBean();
    newOne.setSingleItem(runDynamicsItemCheck(item,null,request));
    newOne.setOrdinal(item.getMetadata().getOrdinal());
    newOne.setInGroup(false);
    newOne.setPageNumberLabel(item.getMetadata().getPageNumberLabel());
    displayItemWithGroups.add(newOne);
  }
  if (hasItemGroup) {
    ItemDataDAO iddao=new ItemDataDAO(getDataSource(),locale);
    ArrayList data=iddao.findAllActiveBySectionIdAndEventCRFId(sb.getId(),ecb.getId());
    if (data != null && data.size() > 0) {
      session.setAttribute(HAS_DATA_FLAG,true);
    }
    logger.trace("String_Node_Str" + data.size());
    logger.trace("String_Node_Str" + data.toString());
    for (    DisplayItemGroupBean itemGroup : dsb.getDisplayFormGroups()) {
      logger.debug("String_Node_Str");
      DisplayItemWithGroupBean newOne=new DisplayItemWithGroupBean();
      DisplayItemBean firstItem=itemGroup.getItems().get(0);
      DisplayItemBean checkItem=firstItem;
      boolean noNeedToSwitch=false;
      for (int i=0; i < data.size(); i++) {
        ItemDataBean idb=(ItemDataBean)data.get(i);
        if (idb.getItemId() == firstItem.getItem().getId()) {
          noNeedToSwitch=true;
        }
      }
      if (!noNeedToSwitch) {
        checkItem=itemGroup.getItems().get(itemGroup.getItems().size() - 1);
      }
      newOne.setPageNumberLabel(firstItem.getMetadata().getPageNumberLabel());
      newOne.setItemGroup(itemGroup);
      newOne.setInGroup(true);
      newOne.setOrdinal(itemGroup.getGroupMetaBean().getOrdinal());
      List<ItemBean> itBeans=idao.findAllItemsByGroupId(itemGroup.getItemGroupBean().getId(),sb.getCRFVersionId());
      boolean hasData=false;
      int checkAllColumns=0;
      for (int i=0; i < data.size(); i++) {
        ItemDataBean idb=(ItemDataBean)data.get(i);
        logger.debug("String_Node_Str" + checkAllColumns);
        if (idb.getItemId() == checkItem.getItem().getId()) {
          hasData=true;
          logger.debug("String_Node_Str");
          checkAllColumns=0;
          logger.debug("String_Node_Str");
          DisplayItemGroupBean digb=new DisplayItemGroupBean();
          List<DisplayItemBean> dibs=FormBeanUtil.getDisplayBeansFromItems(itBeans,getDataSource(),ecb,sb.getId(),edcb,0,getServletContext());
          digb.setItems(dibs);
          logger.trace("String_Node_Str" + dibs.size());
          digb.setGroupMetaBean(runDynamicsCheck(itemGroup.getGroupMetaBean(),request));
          digb.setItemGroupBean(itemGroup.getItemGroupBean());
          newOne.getItemGroups().add(digb);
          newOne.getDbItemGroups().add(digb);
        }
      }
      List<DisplayItemGroupBean> groupRows=newOne.getItemGroups();
      logger.trace("String_Node_Str" + groupRows.size());
      logger.trace("String_Node_Str" + data.size());
      if (hasData) {
        session.setAttribute(GROUP_HAS_DATA,Boolean.TRUE);
        for (int i=0; i < groupRows.size(); i++) {
          DisplayItemGroupBean displayGroup=groupRows.get(i);
          for (          DisplayItemBean dib : displayGroup.getItems()) {
            for (int j=0; j < data.size(); j++) {
              ItemDataBean idb=(ItemDataBean)data.get(j);
              if (idb.getItemId() == dib.getItem().getId() && !idb.isSelected()) {
                idb.setSelected(true);
                dib.setData(idb);
                logger.debug("String_Node_Str" + idb.getId() + "String_Node_Str"+ idb.getValue());
                if (shouldLoadDBValues(dib)) {
                  logger.debug("String_Node_Str");
                  dib.loadDBValue();
                  logger.debug("String_Node_Str" + idb.getName() + "String_Node_Str"+ idb.getOrdinal()+ "String_Node_Str"+ idb.getValue());
                  logger.debug("String_Node_Str" + dib.getItem().getOid());
                }
                break;
              }
            }
          }
        }
      }
 else {
        session.setAttribute(GROUP_HAS_DATA,Boolean.FALSE);
        DisplayItemGroupBean digb2=new DisplayItemGroupBean();
        List<DisplayItemBean> dibs=FormBeanUtil.getDisplayBeansFromItems(itBeans,getDataSource(),ecb,sb.getId(),nullValuesList,getServletContext());
        digb2.setItems(dibs);
        logger.trace("String_Node_Str" + nullValuesList);
        digb2.setEditFlag("String_Node_Str");
        digb2.setGroupMetaBean(itemGroup.getGroupMetaBean());
        digb2.setItemGroupBean(itemGroup.getItemGroupBean());
        newOne.getItemGroups().add(digb2);
        newOne.getDbItemGroups().add(digb2);
      }
      displayItemWithGroups.add(newOne);
    }
  }
  return displayItemWithGroups;
}

Fixed version (ground truth):
protected List<DisplayItemWithGroupBean> createItemWithGroups(DisplaySectionBean dsb,boolean hasItemGroup,int eventCRFDefId,HttpServletRequest request){
  HttpSession session=request.getSession();
  List<DisplayItemWithGroupBean> displayItemWithGroups=new ArrayList<DisplayItemWithGroupBean>();
  EventCRFBean ecb=(EventCRFBean)request.getAttribute(INPUT_EVENT_CRF);
  ItemDAO idao=new ItemDAO(getDataSource());
  FormBeanUtil formBeanUtil=new FormBeanUtil();
  SectionBean sb=(SectionBean)request.getAttribute(SECTION_BEAN);
  EventDefinitionCRFBean edcb=(EventDefinitionCRFBean)request.getAttribute(EVENT_DEF_CRF_BEAN);
  ArrayList items=dsb.getItems();
  logger.trace("String_Node_Str" + items.size());
  for (int i=0; i < items.size(); i++) {
    DisplayItemBean item=(DisplayItemBean)items.get(i);
    DisplayItemWithGroupBean newOne=new DisplayItemWithGroupBean();
    newOne.setSingleItem(runDynamicsItemCheck(item,null,request));
    newOne.setOrdinal(item.getMetadata().getOrdinal());
    newOne.setInGroup(false);
    newOne.setPageNumberLabel(item.getMetadata().getPageNumberLabel());
    displayItemWithGroups.add(newOne);
  }
  if (hasItemGroup) {
    ItemDataDAO iddao=new ItemDataDAO(getDataSource(),locale);
    HashMap<String,ItemDataBean> dataMap=(HashMap<String,ItemDataBean>)iddao.findAllActiveMap(sb.getId(),ecb.getId());
    ArrayList<ItemDataBean> data=iddao.findAllActiveBySectionIdAndEventCRFId(sb.getId(),ecb.getId());
    if (data != null && data.size() > 0) {
      session.setAttribute(HAS_DATA_FLAG,true);
    }
    logger.trace("String_Node_Str" + data.size());
    logger.trace("String_Node_Str" + data.toString());
    for (    DisplayItemGroupBean itemGroup : dsb.getDisplayFormGroups()) {
      logger.debug("String_Node_Str");
      DisplayItemWithGroupBean newOne=new DisplayItemWithGroupBean();
      DisplayItemBean firstItem=itemGroup.getItems().get(0);
      newOne.setPageNumberLabel(firstItem.getMetadata().getPageNumberLabel());
      newOne.setItemGroup(itemGroup);
      newOne.setInGroup(true);
      newOne.setOrdinal(itemGroup.getGroupMetaBean().getOrdinal());
      List<ItemBean> itBeans=idao.findAllItemsByGroupId(itemGroup.getItemGroupBean().getId(),sb.getCRFVersionId());
      List<DisplayItemBean> dibs=new ArrayList();
      DisplayItemGroupBean digb=new DisplayItemGroupBean();
      boolean hasData=false;
      int checkAllColumns=0;
      if (data.size() > 0)       hasData=true;
      newOne=buildMatrixForRepeatingGroups(newOne,itemGroup,ecb,sb,itBeans,dataMap);
      if (hasData) {
        session.setAttribute(GROUP_HAS_DATA,Boolean.TRUE);
      }
 else {
        session.setAttribute(GROUP_HAS_DATA,Boolean.FALSE);
        DisplayItemGroupBean digb2=new DisplayItemGroupBean();
        digb2.setItems(dibs);
        digb2.setEditFlag("String_Node_Str");
      }
      displayItemWithGroups.add(newOne);
    }
  }
  return displayItemWithGroups;
}