Program: 114988
Project: 114988
Buggy source:
@Transactional public Map<String,Object> importOdmToOC(ODM odm,Page page,String boardId,HttpServletRequest request){
  DataBinder dataBinder=new DataBinder(new Study());
  errors=dataBinder.getBindingResult();
  printOdm(odm);
  CoreResources.setRequestSchemaByStudy(odm.getStudy().get(0).getOID(),dataSource);
  UserAccount userAccount=getCurrentUser();
  saveOrUpdatePageLayout(page,userAccount);
  ODMcomplexTypeDefinitionStudy odmStudy=odm.getStudy().get(0);
  Study study=retrieveStudy(odm,userAccount,odmStudy);
  study.setFilePath(study.getFilePath() + 1);
  String studyPath=Utils.getFilePath() + Utils.getStudyPath(study.getOc_oid(),study.getFilePath());
  if (new File(studyPath).exists()) {
    try {
      FileUtils.deleteDirectory(new File(studyPath));
    }
 catch (    IOException e) {
      e.printStackTrace();
    }
  }
  Form[] fmCrfs=getAllCrfsByProtIdFromFormManager(boardId,request);
  StudyEventDefinition studyEventDefinition=null;
  List<ODMcomplexTypeDefinitionMetaDataVersion> odmMetadataVersions=odmStudy.getMetaDataVersion();
  List<ODMcomplexTypeDefinitionStudyEventDef> odmStudyEventDefs=saveOrUpdateEvent(userAccount,study,odmMetadataVersions,errors);
  CrfBean crf=null;
  FormLayout formLayout=null;
  Set<Long> publishedVersions=saveOrUpdateCrf(userAccount,study,odmMetadataVersions,fmCrfs,errors,request);
  List<ODMcomplexTypeDefinitionStudyEventRef> odmStudyEventRefs=odmMetadataVersions.get(0).getProtocol().getStudyEventRef();
  for (  ODMcomplexTypeDefinitionStudyEventRef odmStudyEventRef : odmStudyEventRefs) {
    for (    ODMcomplexTypeDefinitionStudyEventDef odmStudyEventDef : odmStudyEventDefs) {
      if (odmStudyEventDef.getOID().equals(odmStudyEventRef.getStudyEventOID())) {
        studyEventDefinition=getStudyEventDefDao().findByOcOID(odmStudyEventDef.getOID());
        studyEventDefinition.setOrdinal(odmStudyEventRef.getOrderNumber().intValue());
        studyEventDefinition=getStudyEventDefDao().saveOrUpdate(studyEventDefinition);
        List<EventDefinitionCrf> jsonEventDefCrfList=new ArrayList<>();
        EventDefinitionCrf eventDefinitionCrf=null;
        for (        ODMcomplexTypeDefinitionFormRef odmFormRef : odmStudyEventDef.getFormRef()) {
          crf=getCrfDao().findByOcOID(odmFormRef.getFormOID());
          if (crf != null) {
            eventDefinitionCrf=getEventDefinitionCrfDao().findByStudyEventDefinitionIdAndCRFIdAndStudyId(studyEventDefinition.getStudyEventDefinitionId(),crf.getCrfId(),study.getStudyId());
            if (eventDefinitionCrf != null && !eventDefinitionCrf.getStatusId().equals(Status.AVAILABLE.getCode())) {
              eventDefinitionCrf.setStatusId(Status.AVAILABLE.getCode());
              eventService.restoreCrfFromEventDefinition(eventDefinitionCrf.getEventDefinitionCrfId(),studyEventDefinition.getStudyEventDefinitionId(),userAccount.getUserId());
            }
            String defaultVersionName=null;
            OCodmComplexTypeDefinitionConfigurationParameters conf=odmFormRef.getConfigurationParameters();
            List<OCodmComplexTypeDefinitionFormLayoutRef> formLayoutRefs=odmFormRef.getFormLayoutRef();
            if (formLayoutRefs.size() == 1 && formLayoutRefs.get(0).getIsDefaultVersion() == null) {
              defaultVersionName=formLayoutRefs.get(0).getOID();
            }
 else {
              for (              OCodmComplexTypeDefinitionFormLayoutRef formLayoutRef : formLayoutRefs) {
                if (formLayoutRef.getIsDefaultVersion().equalsIgnoreCase("String_Node_Str")) {
                  defaultVersionName=formLayoutRef.getOID();
                }
              }
            }
            if (defaultVersionName == null) {
              String formName="String_Node_Str";
              for (              ODMcomplexTypeDefinitionFormDef odmFormDef : odmMetadataVersions.get(0).getFormDef()) {
                if (odmFormDef.getOID().equals(odmFormRef.getFormOID())) {
                  formName=odmFormDef.getName();
                }
              }
              errors.rejectValue("String_Node_Str","String_Node_Str","String_Node_Str" + formName + "String_Node_Str"+ odmStudyEventDef.getName()+ "String_Node_Str");
              logger.info("String_Node_Str" + formName + "String_Node_Str"+ odmStudyEventDef.getName()+ "String_Node_Str");
              defaultVersionName=formLayoutRefs.get(0).getOID();
            }
            formLayout=getFormLayoutDao().findByNameCrfId(defaultVersionName,crf.getCrfId());
            EventDefinitionCrfDTO edcObj=new EventDefinitionCrfDTO();
            edcObj.setUserAccount(userAccount);
            edcObj.setConf(conf);
            edcObj.setCrf(crf);
            edcObj.setEventDefinitionCrf(eventDefinitionCrf);
            edcObj.setOdmFormRef(odmFormRef);
            edcObj.setStudy(study);
            edcObj.setFormLayout(formLayout);
            edcObj.setStudyEventDefinition(studyEventDefinition);
            edcObj.setOrdinal(odmFormRef.getOrderNumber().intValue());
            EDCTagDTO populateEDCTagParameter=new EDCTagDTO();
            populateEDCTagParameter.setConf(conf);
            populateEDCTagParameter.setConf(conf);
            populateEDCTagParameter.setEventDefinitionCrf(eventDefinitionCrf);
            populateEDCTagParameter.setUserAccount(userAccount);
            eventDefinitionCrf=saveOrUpdateEventDefnCrf(new EventDefinitionCrfDTO(edcObj));
            saveOrUpdateEDCTag(new EDCTagDTO(populateEDCTagParameter),studyEventDefinition,crf);
            jsonEventDefCrfList.add(eventDefinitionCrf);
          }
        }
        List<EventDefinitionCrf> ocEventDefCrfList=getEventDefinitionCrfDao().findAvailableByStudyEventDefStudy(studyEventDefinition.getStudyEventDefinitionId(),study.getStudyId());
        for (        EventDefinitionCrf ocEventDefCrf : ocEventDefCrfList) {
          if (!jsonEventDefCrfList.contains(ocEventDefCrf)) {
            ocEventDefCrf.setStatusId(Status.DELETED.getCode());
            ocEventDefCrf.setUpdateId(userAccount.getUserId());
            ocEventDefCrf.setDateUpdated(new Date());
            getEventDefinitionCrfDao().saveOrUpdate(ocEventDefCrf);
            eventService.removeCrfFromEventDefinition(ocEventDefCrf.getEventDefinitionCrfId(),studyEventDefinition.getStudyEventDefinitionId(),userAccount.getUserId(),study.getStudyId());
          }
        }
      }
    }
  }
  if (errors.hasErrors()) {
    List<ErrorObj> errList=getErrorList(errors.getAllErrors());
    throw new CustomRuntimeException("String_Node_Str",errList);
  }
  Map<String,Object> map=new HashMap<>();
  map.put("String_Node_Str",study);
  PublishingDTO publishingDTO=new PublishingDTO();
  publishingDTO.setVersionIds(publishedVersions);
  map.put("String_Node_Str",publishingDTO);
  return map;
}

Fixed version (ground truth):
@Transactional public Map<String,Object> importOdmToOC(ODM odm,Page page,String boardId,HttpServletRequest request){
  DataBinder dataBinder=new DataBinder(new Study());
  errors=dataBinder.getBindingResult();
  printOdm(odm);
  CoreResources.setRequestSchemaByStudy(odm.getStudy().get(0).getOID(),dataSource);
  UserAccount userAccount=getCurrentUser();
  saveOrUpdatePageLayout(page,userAccount);
  ODMcomplexTypeDefinitionStudy odmStudy=odm.getStudy().get(0);
  Study study=retrieveStudy(odm,userAccount,odmStudy);
  study.setFilePath(study.getFilePath() + 1);
  String studyPath=Utils.getFilePath() + Utils.getStudyPath(study.getOc_oid(),study.getFilePath());
  if (new File(studyPath).exists()) {
    try {
      FileUtils.deleteDirectory(new File(studyPath));
    }
 catch (    IOException e) {
      e.printStackTrace();
    }
  }
  Form[] fmCrfs=getAllCrfsByProtIdFromFormManager(boardId,request);
  StudyEventDefinition studyEventDefinition=null;
  List<ODMcomplexTypeDefinitionMetaDataVersion> odmMetadataVersions=odmStudy.getMetaDataVersion();
  List<ODMcomplexTypeDefinitionStudyEventDef> odmStudyEventDefs=saveOrUpdateEvent(userAccount,study,odmMetadataVersions,errors);
  CrfBean crf=null;
  FormLayout formLayout=null;
  Set<Long> publishedVersions=saveOrUpdateCrf(userAccount,study,odmMetadataVersions,fmCrfs,errors,request);
  List<ODMcomplexTypeDefinitionStudyEventRef> odmStudyEventRefs=odmMetadataVersions.get(0).getProtocol().getStudyEventRef();
  for (  ODMcomplexTypeDefinitionStudyEventRef odmStudyEventRef : odmStudyEventRefs) {
    for (    ODMcomplexTypeDefinitionStudyEventDef odmStudyEventDef : odmStudyEventDefs) {
      if (odmStudyEventDef.getOID().equals(odmStudyEventRef.getStudyEventOID())) {
        studyEventDefinition=getStudyEventDefDao().findByOcOID(odmStudyEventDef.getOID());
        studyEventDefinition.setOrdinal(odmStudyEventRef.getOrderNumber().intValue());
        studyEventDefinition=getStudyEventDefDao().saveOrUpdate(studyEventDefinition);
        List<EventDefinitionCrf> jsonEventDefCrfList=new ArrayList<>();
        EventDefinitionCrf eventDefinitionCrf=null;
        for (        ODMcomplexTypeDefinitionFormRef odmFormRef : odmStudyEventDef.getFormRef()) {
          crf=getCrfDao().findByOcOID(odmFormRef.getFormOID());
          if (crf != null) {
            eventDefinitionCrf=getEventDefinitionCrfDao().findByStudyEventDefinitionIdAndCRFIdAndStudyId(studyEventDefinition.getStudyEventDefinitionId(),crf.getCrfId(),study.getStudyId());
            if (eventDefinitionCrf != null && !eventDefinitionCrf.getStatusId().equals(Status.AVAILABLE.getCode())) {
              eventDefinitionCrf.setStatusId(Status.AVAILABLE.getCode());
              eventDefinitionCrf.setUpdateId(userAccount.getUserId());
              eventDefinitionCrf.setDateUpdated(new Date());
              eventDefinitionCrf=getEventDefinitionCrfDao().saveOrUpdate(eventDefinitionCrf);
              restoreSiteDefinitions(eventDefinitionCrf.getEventDefinitionCrfId(),userAccount.getUserId());
              eventService.restoreCrfFromEventDefinition(eventDefinitionCrf.getEventDefinitionCrfId(),studyEventDefinition.getStudyEventDefinitionId(),userAccount.getUserId());
            }
            String defaultVersionName=null;
            OCodmComplexTypeDefinitionConfigurationParameters conf=odmFormRef.getConfigurationParameters();
            List<OCodmComplexTypeDefinitionFormLayoutRef> formLayoutRefs=odmFormRef.getFormLayoutRef();
            if (formLayoutRefs.size() == 1 && formLayoutRefs.get(0).getIsDefaultVersion() == null) {
              defaultVersionName=formLayoutRefs.get(0).getOID();
            }
 else {
              for (              OCodmComplexTypeDefinitionFormLayoutRef formLayoutRef : formLayoutRefs) {
                if (formLayoutRef.getIsDefaultVersion().equalsIgnoreCase("String_Node_Str")) {
                  defaultVersionName=formLayoutRef.getOID();
                }
              }
            }
            if (defaultVersionName == null) {
              String formName="String_Node_Str";
              for (              ODMcomplexTypeDefinitionFormDef odmFormDef : odmMetadataVersions.get(0).getFormDef()) {
                if (odmFormDef.getOID().equals(odmFormRef.getFormOID())) {
                  formName=odmFormDef.getName();
                }
              }
              errors.rejectValue("String_Node_Str","String_Node_Str","String_Node_Str" + formName + "String_Node_Str"+ odmStudyEventDef.getName()+ "String_Node_Str");
              logger.info("String_Node_Str" + formName + "String_Node_Str"+ odmStudyEventDef.getName()+ "String_Node_Str");
              defaultVersionName=formLayoutRefs.get(0).getOID();
            }
            formLayout=getFormLayoutDao().findByNameCrfId(defaultVersionName,crf.getCrfId());
            EventDefinitionCrfDTO edcObj=new EventDefinitionCrfDTO();
            edcObj.setUserAccount(userAccount);
            edcObj.setConf(conf);
            edcObj.setCrf(crf);
            edcObj.setEventDefinitionCrf(eventDefinitionCrf);
            edcObj.setOdmFormRef(odmFormRef);
            edcObj.setStudy(study);
            edcObj.setFormLayout(formLayout);
            edcObj.setStudyEventDefinition(studyEventDefinition);
            edcObj.setOrdinal(odmFormRef.getOrderNumber().intValue());
            EDCTagDTO populateEDCTagParameter=new EDCTagDTO();
            populateEDCTagParameter.setConf(conf);
            populateEDCTagParameter.setEventDefinitionCrf(eventDefinitionCrf);
            populateEDCTagParameter.setUserAccount(userAccount);
            eventDefinitionCrf=saveOrUpdateEventDefnCrf(new EventDefinitionCrfDTO(edcObj));
            saveOrUpdateEDCTag(new EDCTagDTO(populateEDCTagParameter),studyEventDefinition,crf);
            jsonEventDefCrfList.add(eventDefinitionCrf);
          }
        }
        List<EventDefinitionCrf> ocEventDefCrfList=getEventDefinitionCrfDao().findAvailableByStudyEventDefStudy(studyEventDefinition.getStudyEventDefinitionId(),study.getStudyId());
        for (        EventDefinitionCrf ocEventDefCrf : ocEventDefCrfList) {
          if (!jsonEventDefCrfList.contains(ocEventDefCrf)) {
            ocEventDefCrf.setStatusId(Status.DELETED.getCode());
            ocEventDefCrf.setUpdateId(userAccount.getUserId());
            ocEventDefCrf.setDateUpdated(new Date());
            ocEventDefCrf=getEventDefinitionCrfDao().saveOrUpdate(ocEventDefCrf);
            removeSiteDefinitions(ocEventDefCrf.getEventDefinitionCrfId(),userAccount.getUserId());
            eventService.removeCrfFromEventDefinition(ocEventDefCrf.getEventDefinitionCrfId(),studyEventDefinition.getStudyEventDefinitionId(),userAccount.getUserId(),study.getStudyId());
          }
        }
      }
    }
  }
  if (errors.hasErrors()) {
    List<ErrorObj> errList=getErrorList(errors.getAllErrors());
    throw new CustomRuntimeException("String_Node_Str",errList);
  }
  Map<String,Object> map=new HashMap<>();
  map.put("String_Node_Str",study);
  PublishingDTO publishingDTO=new PublishingDTO();
  publishingDTO.setVersionIds(publishedVersions);
  map.put("String_Node_Str",publishingDTO);
  return map;
}