Program: 115081
Project: 115081
Buggy source:
/** 
 * Read from Pform Submission Payload or the Body
 * @param body
 * @param errors
 * @param studyBean
 * @param studyEventBean
 * @param studySubjectBean
 * @return
 * @throws Exception
 */
private Errors readDownloadFile(String body,Errors errors,StudyBean studyBean,StudyEventBean studyEventBean,StudySubjectBean studySubjectBean,StudyEventDefinitionBean studyEventDefinitionBean) throws Exception {
  DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
  DocumentBuilder db=dbf.newDocumentBuilder();
  InputSource is=new InputSource();
  is.setCharacterStream(new StringReader(body));
  Document doc=db.parse(is);
  Integer itemOrdinal=1;
  String itemOID;
  String itemValue;
  String groupNodeName="String_Node_Str";
  NodeList instanceNodeList=doc.getElementsByTagName("String_Node_Str");
  for (int i=0; i < instanceNodeList.getLength(); i=i + 1) {
    Node instanceNode=instanceNodeList.item(i);
    if (instanceNode instanceof Element) {
      NodeList crfNodeList=instanceNode.getChildNodes();
      for (int j=0; j < crfNodeList.getLength(); j=j + 1) {
        Node crfNode=crfNodeList.item(j);
        if (crfNode instanceof Element) {
          String crfVersionOID=crfNode.getNodeName().trim();
          logger.info("String_Node_Str" + crfVersionOID + "String_Node_Str");
          EventCRFBean eventCrfBean=getCrfVersionCheck(crfVersionOID,errors,studyBean,studyEventBean,studySubjectBean);
          ArrayList<ItemDataBean> itemDataBeanList=new ArrayList<ItemDataBean>();
          iddao=new ItemDataDAO(ds);
          if (eventCrfBean == null)           return errors;
          if (eventCrfBean != null) {
            NodeList groupNodeList=crfNode.getChildNodes();
            for (int k=0; k < groupNodeList.getLength(); k=k + 1) {
              Node groupNode=groupNodeList.item(k);
              if (groupNode instanceof Element && !groupNode.getNodeName().startsWith("String_Node_Str")) {
                if (groupNode.getNodeName() != groupNodeName) {
                  itemOrdinal=1;
                }
 else {
                  itemOrdinal++;
                }
                groupNodeName=groupNode.getNodeName();
                NodeList itemNodeList=groupNode.getChildNodes();
                for (int m=0; m < itemNodeList.getLength(); m=m + 1) {
                  Node itemNode=itemNodeList.item(m);
                  if (itemNode instanceof Element && !itemNode.getNodeName().endsWith("String_Node_Str") && !itemNode.getNodeName().endsWith("String_Node_Str")) {
                    itemOID=itemNode.getNodeName().trim();
                    itemValue=itemNode.getTextContent();
                    ArrayList<ItemBean> iBean=getItemRecord(itemOID);
                    CRFVersionBean cvBean=getCRFVersion(crfVersionOID);
                    Integer itemId=iBean.get(0).getId();
                    Integer crfVersionId=cvBean.getId();
                    ItemFormMetadataBean ifmBean=getItemFromMetadata(itemId,crfVersionId);
                    Integer responseTypeId=ifmBean.getResponseSet().getResponseType().getId();
                    if (responseTypeId == 3 || responseTypeId == 7) {
                      itemValue=itemValue.replaceAll("String_Node_Str","String_Node_Str");
                    }
                    idao=new ItemDAO(ds);
                    ArrayList<ItemBean> itemBeanList=(ArrayList<ItemBean>)idao.findByOid(itemOID);
                    ItemBean itemBean=itemBeanList.get(0);
                    ItemDataBean itemDataBean=createItemData(itemBean,itemValue,itemOrdinal,eventCrfBean,studyBean,studySubjectBean);
                    errors=validateItemData(itemDataBean,itemBean,responseTypeId);
                    if (errors.hasErrors()) {
                      return errors;
                    }
 else {
                      itemDataBeanList.add(itemDataBean);
                    }
                    if (!errors.hasErrors()) {
                      for (                      ItemDataBean itemDataBean1 : itemDataBeanList) {
                        iddao.create(itemDataBean1);
                        eventCrfBean=updateEventCRF(eventCrfBean,studyBean,studySubjectBean);
                        if (getCountCompletedEventCrfsInAStudyEvent(studyEventBean) == getCountCrfsInAEventDefCrf(studyEventDefinitionBean.getId())) {
                          updateStudyEvent(studyEventBean,SubjectEventStatus.COMPLETED,studyBean,studySubjectBean);
                        }
 else {
                          updateStudyEvent(studyEventBean,SubjectEventStatus.DATA_ENTRY_STARTED,studyBean,studySubjectBean);
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
  return errors;
}

Fixed version (ground truth):
/** 
 * Read from Pform Submission Payload or the Body
 * @param body
 * @param errors
 * @param studyBean
 * @param studyEventBean
 * @param studySubjectBean
 * @return
 * @throws Exception
 */
private Errors readDownloadFile(String body,Errors errors,StudyBean studyBean,StudyEventBean studyEventBean,StudySubjectBean studySubjectBean,StudyEventDefinitionBean studyEventDefinitionBean) throws Exception {
  DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
  DocumentBuilder db=dbf.newDocumentBuilder();
  InputSource is=new InputSource();
  is.setCharacterStream(new StringReader(body));
  Document doc=db.parse(is);
  Integer itemOrdinal=1;
  String itemOID;
  String itemValue;
  String groupNodeName="String_Node_Str";
  NodeList instanceNodeList=doc.getElementsByTagName("String_Node_Str");
  for (int i=0; i < instanceNodeList.getLength(); i=i + 1) {
    Node instanceNode=instanceNodeList.item(i);
    if (instanceNode instanceof Element) {
      NodeList crfNodeList=instanceNode.getChildNodes();
      for (int j=0; j < crfNodeList.getLength(); j=j + 1) {
        Node crfNode=crfNodeList.item(j);
        if (crfNode instanceof Element) {
          String crfVersionOID=crfNode.getNodeName().trim();
          logger.info("String_Node_Str" + crfVersionOID + "String_Node_Str");
          EventCRFBean eventCrfBean=getCrfVersionCheck(crfVersionOID,errors,studyBean,studyEventBean,studySubjectBean);
          ArrayList<ItemDataBean> itemDataBeanList=new ArrayList<ItemDataBean>();
          iddao=new ItemDataDAO(ds);
          if (eventCrfBean == null)           return errors;
          if (eventCrfBean != null) {
            NodeList groupNodeList=crfNode.getChildNodes();
            for (int k=0; k < groupNodeList.getLength(); k=k + 1) {
              Node groupNode=groupNodeList.item(k);
              if (groupNode instanceof Element && !groupNode.getNodeName().startsWith("String_Node_Str")) {
                if (groupNode.getNodeName() != groupNodeName) {
                  itemOrdinal=1;
                }
 else {
                  itemOrdinal++;
                }
                groupNodeName=groupNode.getNodeName();
                NodeList itemNodeList=groupNode.getChildNodes();
                for (int m=0; m < itemNodeList.getLength(); m=m + 1) {
                  Node itemNode=itemNodeList.item(m);
                  if (itemNode instanceof Element && !itemNode.getNodeName().endsWith("String_Node_Str") && !itemNode.getNodeName().endsWith("String_Node_Str")) {
                    itemOID=itemNode.getNodeName().trim();
                    itemValue=itemNode.getTextContent();
                    ArrayList<ItemBean> iBean=getItemRecord(itemOID);
                    CRFVersionBean cvBean=getCRFVersion(crfVersionOID);
                    Integer itemId=iBean.get(0).getId();
                    Integer crfVersionId=cvBean.getId();
                    ItemFormMetadataBean ifmBean=getItemFromMetadata(itemId,crfVersionId);
                    Integer responseTypeId=ifmBean.getResponseSet().getResponseType().getId();
                    if (responseTypeId == 3 || responseTypeId == 7) {
                      itemValue=itemValue.replaceAll("String_Node_Str","String_Node_Str");
                    }
                    idao=new ItemDAO(ds);
                    ArrayList<ItemBean> itemBeanList=(ArrayList<ItemBean>)idao.findByOid(itemOID);
                    ItemBean itemBean=itemBeanList.get(0);
                    ItemDataBean itemDataBean=createItemData(itemBean,itemValue,itemOrdinal,eventCrfBean,studyBean,studySubjectBean);
                    errors=validateItemData(itemDataBean,itemBean,responseTypeId);
                    if (errors.hasErrors()) {
                      return errors;
                    }
 else {
                      itemDataBeanList.add(itemDataBean);
                    }
                  }
                }
              }
            }
            if (!errors.hasErrors()) {
              for (              ItemDataBean itemDataBean1 : itemDataBeanList) {
                iddao.create(itemDataBean1);
                eventCrfBean=updateEventCRF(eventCrfBean,studyBean,studySubjectBean);
                if (getCountCompletedEventCrfsInAStudyEvent(studyEventBean) == getCountCrfsInAEventDefCrf(studyEventDefinitionBean.getId())) {
                  updateStudyEvent(studyEventBean,SubjectEventStatus.COMPLETED,studyBean,studySubjectBean);
                }
 else {
                  updateStudyEvent(studyEventBean,SubjectEventStatus.DATA_ENTRY_STARTED,studyBean,studySubjectBean);
                }
              }
            }
          }
        }
      }
    }
  }
  return errors;
}