Program: 39082
Project: 39082
Buggy source:
private int addValidationEcrfFieldStatusEntries(ECRFStatusEntry statusEntry,boolean addSeries,Timestamp now,User user) throws Exception {
  ProbandListEntry listEntry=statusEntry.getListEntry();
  ECRF ecrf=statusEntry.getEcrf();
  ProbandListEntryOutVO listEntryVO=this.getProbandListEntryDao().toProbandListEntryOutVO(listEntry);
  if (listEntryVO.getProband().getDecrypted()) {
    throw L10nUtil.initServiceException(ServiceExceptionCodes.CANNOT_DECRYPT_PROBAND);
  }
  UserOutVO userVO=this.getUserDao().toUserOutVO(user);
  Collection visitScheduleItems=null;
  if (listEntry.getGroup() != null) {
    VisitScheduleItemDao visitScheduleItemDao=this.getVisitScheduleItemDao();
    visitScheduleItems=visitScheduleItemDao.findByTrialGroupVisitProbandTravel(listEntry.getTrial().getId(),listEntry.getGroup().getId(),null,listEntry.getProband().getId(),null,null);
    visitScheduleItemDao.toVisitScheduleItemOutVOCollection(visitScheduleItems);
  }
  InputFieldSelectionSetValueDao inputFieldSelectionSetValueDao=this.getInputFieldSelectionSetValueDao();
  ProbandListEntryTagValueDao probandListEntryTagValueDao=this.getProbandListEntryTagValueDao();
  Collection<ProbandListEntryTagValueJsonVO> probandListEntryTagValues=ServiceUtil.getProbandListEntryTagJsonValues(probandListEntryTagValueDao.findByListEntryJs(listEntryVO.getId(),true,null,null),false,probandListEntryTagValueDao,inputFieldSelectionSetValueDao);
  HashMap<String,Long> maxSeriesIndexMap=null;
  HashMap<String,Long> fieldMaxPositionMap=null;
  HashMap<String,Long> fieldMinPositionMap=null;
  HashMap<String,Set<ECRFField>> seriesEcrfFieldMap=null;
  ECRFFieldValueDao ecrfFieldValueDao=this.getECRFFieldValueDao();
  if (addSeries) {
    maxSeriesIndexMap=new HashMap<String,Long>();
    fieldMaxPositionMap=new HashMap<String,Long>();
    fieldMinPositionMap=new HashMap<String,Long>();
    seriesEcrfFieldMap=new HashMap<String,Set<ECRFField>>();
    ServiceUtil.initSeriesEcrfFieldMaps(this.getECRFFieldDao().findByTrialEcrfSeriesJs(null,ecrf.getId(),true,true,true,null),listEntryVO.getId(),ecrf.getId(),maxSeriesIndexMap,fieldMaxPositionMap,fieldMinPositionMap,seriesEcrfFieldMap,ecrfFieldValueDao);
  }
  Collection<ECRFFieldValueJsonVO> jsValues=ServiceUtil.getEcrfFieldJsonValues(ecrfFieldValueDao.findByListEntryEcrfJs(listEntry.getId(),ecrf.getId(),true,true,null),maxSeriesIndexMap,fieldMaxPositionMap,fieldMinPositionMap,seriesEcrfFieldMap,false,ecrfFieldValueDao,inputFieldSelectionSetValueDao);
  FieldCalculation fieldCalculation=new FieldCalculation();
  fieldCalculation.setProbandListEntry(listEntryVO);
  fieldCalculation.setActiveUser(userVO);
  fieldCalculation.setLocale(Locales.AUDIT_TRAIL);
  fieldCalculation.setProbandListEntryTagValues(probandListEntryTagValues);
  fieldCalculation.setVisitScheduleItems(visitScheduleItems);
  fieldCalculation.setECRFFieldInputFieldVariableValues(jsValues);
  Exception scriptException=null;
  int errorCount=0;
  HashMap<Long,HashMap<Long,ValidationError>> validationErrorMap=new HashMap<Long,HashMap<Long,ValidationError>>();
  try {
    Iterator<ValidationError> it=fieldCalculation.initInputFieldVariables().iterator();
    while (it.hasNext()) {
      ValidationError msg=it.next();
      if (validationErrorMap.containsKey(msg.getEcrfFieldId())) {
        validationErrorMap.get(msg.getEcrfFieldId()).put(msg.getIndex(),msg);
      }
 else {
        HashMap<Long,ValidationError> indexErrorMap=new HashMap<Long,ValidationError>();
        indexErrorMap.put(msg.getIndex(),msg);
        validationErrorMap.put(msg.getEcrfFieldId(),indexErrorMap);
      }
      errorCount++;
    }
  }
 catch (  Exception e) {
    scriptException=e;
  }
  Iterator<Map> ecrfFieldValuesIt=ecrfFieldValueDao.findByListEntryEcrfJs(listEntry.getId(),ecrf.getId(),true,null,null).iterator();
  while (ecrfFieldValuesIt.hasNext()) {
    Map<String,Object> entities=(Map<String,Object>)ecrfFieldValuesIt.next();
    ECRFFieldValue ecrfFieldValue=(ECRFFieldValue)entities.get(ServiceUtil.ECRF_FIELD_VALUE_DAO_ECRF_FIELD_VALUE_ALIAS);
    ECRFField ecrfField;
    Long index;
    if (ecrfFieldValue == null) {
      ecrfField=(ECRFField)entities.get(ServiceUtil.ECRF_FIELD_VALUE_DAO_ECRF_FIELD_ALIAS);
      index=(ecrfField.isSeries() ? 0l : null);
    }
 else {
      ecrfField=ecrfFieldValue.getEcrfField();
      index=ecrfFieldValue.getIndex();
    }
    ECRFFieldStatusEntryInVO newEcrfFieldStatusEntry=new ECRFFieldStatusEntryInVO();
    newEcrfFieldStatusEntry.setEcrfFieldId(ecrfField.getId());
    newEcrfFieldStatusEntry.setListEntryId(listEntry.getId());
    newEcrfFieldStatusEntry.setIndex(index);
    ValidationError msg;
    HashMap<Long,ValidationError> indexErrorMap;
    if (scriptException != null) {
      ECRFFieldStatusEntry lastStatus=this.getECRFFieldStatusEntryDao().findLastStatus(ECRFFieldStatusQueue.VALIDATION,listEntry.getId(),ecrfField.getId(),index);
      if (lastStatus == null || lastStatus.getStatus().getTransitions().contains(this.getECRFFieldStatusTypeDao().getValidationFailed())) {
        newEcrfFieldStatusEntry.setStatusId(this.getECRFFieldStatusTypeDao().getValidationFailed().getId());
        newEcrfFieldStatusEntry.setComment(scriptException.getMessage());
      }
 else {
        continue;
      }
    }
 else     if ((indexErrorMap=validationErrorMap.get(ecrfField.getId())) != null && (msg=indexErrorMap.get(index)) != null) {
      ECRFFieldStatusEntry lastStatus=this.getECRFFieldStatusEntryDao().findLastStatus(ECRFFieldStatusQueue.VALIDATION,listEntry.getId(),ecrfField.getId(),index);
      if (lastStatus == null || lastStatus.getStatus().getTransitions().contains(this.getECRFFieldStatusTypeDao().getValidationError())) {
        newEcrfFieldStatusEntry.setStatusId(this.getECRFFieldStatusTypeDao().getValidationError().getId());
        newEcrfFieldStatusEntry.setComment(msg.getOutput());
      }
 else {
        continue;
      }
    }
 else {
      ECRFFieldStatusEntry lastStatus=this.getECRFFieldStatusEntryDao().findLastStatus(ECRFFieldStatusQueue.VALIDATION,listEntry.getId(),ecrfField.getId(),index);
      if (lastStatus != null && lastStatus.getStatus().getTransitions().contains(this.getECRFFieldStatusTypeDao().getValidationSuccess())) {
        newEcrfFieldStatusEntry.setStatusId(this.getECRFFieldStatusTypeDao().getValidationSuccess().getId());
      }
 else {
        continue;
      }
    }
    addEcrfFieldStatusEntry(newEcrfFieldStatusEntry,ECRFFieldStatusQueue.VALIDATION,now,user,false,false,true);
  }
  statusEntry.setValidationTimestamp(now);
  if (scriptException != null) {
    statusEntry.setValidationStatus(ECRFValidationStatus.FAILED);
    statusEntry.setValidationResponseMsg(L10nUtil.getMessage(Locales.NOTIFICATION,MessageCodes.ECRF_VALIDATION_FAILED_RESPONSE,DefaultMessages.ECRF_VALIDATION_FAILED_RESPONSE,scriptException.getMessage()));
  }
 else   if (errorCount > 0) {
    statusEntry.setValidationStatus(ECRFValidationStatus.OK);
    statusEntry.setValidationResponseMsg(L10nUtil.getMessage(Locales.NOTIFICATION,MessageCodes.ECRF_VALIDATION_OK_ERRORS_RESPONSE,DefaultMessages.ECRF_VALIDATION_OK_ERRORS_RESPONSE,errorCount));
  }
 else {
    statusEntry.setValidationStatus(ECRFValidationStatus.OK);
    statusEntry.setValidationResponseMsg(L10nUtil.getMessage(Locales.NOTIFICATION,MessageCodes.ECRF_VALIDATION_OK_NO_ERROR_RESPONSE,DefaultMessages.ECRF_VALIDATION_OK_NO_ERROR_RESPONSE));
  }
  this.getECRFStatusEntryDao().update(statusEntry);
  return errorCount;
}

Fixed version (ground truth):
private int addValidationEcrfFieldStatusEntries(ECRFStatusEntry statusEntry,boolean addSeries,Timestamp now,User user) throws Exception {
  ProbandListEntry listEntry=statusEntry.getListEntry();
  ECRF ecrf=statusEntry.getEcrf();
  ProbandListEntryOutVO listEntryVO=this.getProbandListEntryDao().toProbandListEntryOutVO(listEntry);
  if (!listEntryVO.getProband().getDecrypted()) {
    throw L10nUtil.initServiceException(ServiceExceptionCodes.CANNOT_DECRYPT_PROBAND);
  }
  UserOutVO userVO=this.getUserDao().toUserOutVO(user);
  Collection visitScheduleItems=null;
  if (listEntry.getGroup() != null) {
    VisitScheduleItemDao visitScheduleItemDao=this.getVisitScheduleItemDao();
    visitScheduleItems=visitScheduleItemDao.findByTrialGroupVisitProbandTravel(listEntry.getTrial().getId(),listEntry.getGroup().getId(),null,listEntry.getProband().getId(),null,null);
    visitScheduleItemDao.toVisitScheduleItemOutVOCollection(visitScheduleItems);
  }
  InputFieldSelectionSetValueDao inputFieldSelectionSetValueDao=this.getInputFieldSelectionSetValueDao();
  ProbandListEntryTagValueDao probandListEntryTagValueDao=this.getProbandListEntryTagValueDao();
  Collection<ProbandListEntryTagValueJsonVO> probandListEntryTagValues=ServiceUtil.getProbandListEntryTagJsonValues(probandListEntryTagValueDao.findByListEntryJs(listEntryVO.getId(),true,null,null),false,probandListEntryTagValueDao,inputFieldSelectionSetValueDao);
  HashMap<String,Long> maxSeriesIndexMap=null;
  HashMap<String,Long> fieldMaxPositionMap=null;
  HashMap<String,Long> fieldMinPositionMap=null;
  HashMap<String,Set<ECRFField>> seriesEcrfFieldMap=null;
  ECRFFieldValueDao ecrfFieldValueDao=this.getECRFFieldValueDao();
  if (addSeries) {
    maxSeriesIndexMap=new HashMap<String,Long>();
    fieldMaxPositionMap=new HashMap<String,Long>();
    fieldMinPositionMap=new HashMap<String,Long>();
    seriesEcrfFieldMap=new HashMap<String,Set<ECRFField>>();
    ServiceUtil.initSeriesEcrfFieldMaps(this.getECRFFieldDao().findByTrialEcrfSeriesJs(null,ecrf.getId(),true,true,true,null),listEntryVO.getId(),ecrf.getId(),maxSeriesIndexMap,fieldMaxPositionMap,fieldMinPositionMap,seriesEcrfFieldMap,ecrfFieldValueDao);
  }
  Collection<ECRFFieldValueJsonVO> jsValues=ServiceUtil.getEcrfFieldJsonValues(ecrfFieldValueDao.findByListEntryEcrfJs(listEntry.getId(),ecrf.getId(),true,true,null),maxSeriesIndexMap,fieldMaxPositionMap,fieldMinPositionMap,seriesEcrfFieldMap,false,ecrfFieldValueDao,inputFieldSelectionSetValueDao);
  FieldCalculation fieldCalculation=new FieldCalculation();
  fieldCalculation.setProbandListEntry(listEntryVO);
  fieldCalculation.setActiveUser(userVO);
  fieldCalculation.setLocale(Locales.AUDIT_TRAIL);
  fieldCalculation.setProbandListEntryTagValues(probandListEntryTagValues);
  fieldCalculation.setVisitScheduleItems(visitScheduleItems);
  fieldCalculation.setECRFFieldInputFieldVariableValues(jsValues);
  Exception scriptException=null;
  int errorCount=0;
  HashMap<Long,HashMap<Long,ValidationError>> validationErrorMap=new HashMap<Long,HashMap<Long,ValidationError>>();
  try {
    Iterator<ValidationError> it=fieldCalculation.initInputFieldVariables().iterator();
    while (it.hasNext()) {
      ValidationError msg=it.next();
      if (validationErrorMap.containsKey(msg.getEcrfFieldId())) {
        validationErrorMap.get(msg.getEcrfFieldId()).put(msg.getIndex(),msg);
      }
 else {
        HashMap<Long,ValidationError> indexErrorMap=new HashMap<Long,ValidationError>();
        indexErrorMap.put(msg.getIndex(),msg);
        validationErrorMap.put(msg.getEcrfFieldId(),indexErrorMap);
      }
      errorCount++;
    }
  }
 catch (  Exception e) {
    scriptException=e;
  }
  Iterator<Map> ecrfFieldValuesIt=ecrfFieldValueDao.findByListEntryEcrfJs(listEntry.getId(),ecrf.getId(),true,null,null).iterator();
  while (ecrfFieldValuesIt.hasNext()) {
    Map<String,Object> entities=(Map<String,Object>)ecrfFieldValuesIt.next();
    ECRFFieldValue ecrfFieldValue=(ECRFFieldValue)entities.get(ServiceUtil.ECRF_FIELD_VALUE_DAO_ECRF_FIELD_VALUE_ALIAS);
    ECRFField ecrfField;
    Long index;
    if (ecrfFieldValue == null) {
      ecrfField=(ECRFField)entities.get(ServiceUtil.ECRF_FIELD_VALUE_DAO_ECRF_FIELD_ALIAS);
      index=(ecrfField.isSeries() ? 0l : null);
    }
 else {
      ecrfField=ecrfFieldValue.getEcrfField();
      index=ecrfFieldValue.getIndex();
    }
    ECRFFieldStatusEntryInVO newEcrfFieldStatusEntry=new ECRFFieldStatusEntryInVO();
    newEcrfFieldStatusEntry.setEcrfFieldId(ecrfField.getId());
    newEcrfFieldStatusEntry.setListEntryId(listEntry.getId());
    newEcrfFieldStatusEntry.setIndex(index);
    ValidationError msg;
    HashMap<Long,ValidationError> indexErrorMap;
    if (scriptException != null) {
      ECRFFieldStatusEntry lastStatus=this.getECRFFieldStatusEntryDao().findLastStatus(ECRFFieldStatusQueue.VALIDATION,listEntry.getId(),ecrfField.getId(),index);
      if (lastStatus == null || lastStatus.getStatus().getTransitions().contains(this.getECRFFieldStatusTypeDao().getValidationFailed())) {
        newEcrfFieldStatusEntry.setStatusId(this.getECRFFieldStatusTypeDao().getValidationFailed().getId());
        newEcrfFieldStatusEntry.setComment(scriptException.getMessage());
      }
 else {
        continue;
      }
    }
 else     if ((indexErrorMap=validationErrorMap.get(ecrfField.getId())) != null && (msg=indexErrorMap.get(index)) != null) {
      ECRFFieldStatusEntry lastStatus=this.getECRFFieldStatusEntryDao().findLastStatus(ECRFFieldStatusQueue.VALIDATION,listEntry.getId(),ecrfField.getId(),index);
      if (lastStatus == null || lastStatus.getStatus().getTransitions().contains(this.getECRFFieldStatusTypeDao().getValidationError())) {
        newEcrfFieldStatusEntry.setStatusId(this.getECRFFieldStatusTypeDao().getValidationError().getId());
        newEcrfFieldStatusEntry.setComment(msg.getOutput());
      }
 else {
        continue;
      }
    }
 else {
      ECRFFieldStatusEntry lastStatus=this.getECRFFieldStatusEntryDao().findLastStatus(ECRFFieldStatusQueue.VALIDATION,listEntry.getId(),ecrfField.getId(),index);
      if (lastStatus != null && lastStatus.getStatus().getTransitions().contains(this.getECRFFieldStatusTypeDao().getValidationSuccess())) {
        newEcrfFieldStatusEntry.setStatusId(this.getECRFFieldStatusTypeDao().getValidationSuccess().getId());
      }
 else {
        continue;
      }
    }
    addEcrfFieldStatusEntry(newEcrfFieldStatusEntry,ECRFFieldStatusQueue.VALIDATION,now,user,false,false,true);
  }
  statusEntry.setValidationTimestamp(now);
  if (scriptException != null) {
    statusEntry.setValidationStatus(ECRFValidationStatus.FAILED);
    statusEntry.setValidationResponseMsg(L10nUtil.getMessage(Locales.NOTIFICATION,MessageCodes.ECRF_VALIDATION_FAILED_RESPONSE,DefaultMessages.ECRF_VALIDATION_FAILED_RESPONSE,scriptException.getMessage()));
  }
 else   if (errorCount > 0) {
    statusEntry.setValidationStatus(ECRFValidationStatus.OK);
    statusEntry.setValidationResponseMsg(L10nUtil.getMessage(Locales.NOTIFICATION,MessageCodes.ECRF_VALIDATION_OK_ERRORS_RESPONSE,DefaultMessages.ECRF_VALIDATION_OK_ERRORS_RESPONSE,errorCount));
  }
 else {
    statusEntry.setValidationStatus(ECRFValidationStatus.OK);
    statusEntry.setValidationResponseMsg(L10nUtil.getMessage(Locales.NOTIFICATION,MessageCodes.ECRF_VALIDATION_OK_NO_ERROR_RESPONSE,DefaultMessages.ECRF_VALIDATION_OK_NO_ERROR_RESPONSE));
  }
  this.getECRFStatusEntryDao().update(statusEntry);
  return errorCount;
}