Program: 115295
Project: 115295
Buggy source:
@Override protected void processRequest() throws Exception {
  checkStudyLocked(Page.LIST_STUDY_SUBJECTS,respage.getString("String_Node_Str"));
  checkStudyFrozen(Page.LIST_STUDY_SUBJECTS,respage.getString("String_Node_Str"));
  StudySubjectDAO ssd=new StudySubjectDAO(sm.getDataSource());
  StudyDAO stdao=new StudyDAO(sm.getDataSource());
  StudyGroupClassDAO sgcdao=new StudyGroupClassDAO(sm.getDataSource());
  ArrayList classes=new ArrayList();
  panel.setStudyInfoShown(false);
  FormProcessor fp=new FormProcessor(request);
  FormDiscrepancyNotes discNotes;
  SimpleDateFormat sdf=new SimpleDateFormat("String_Node_Str");
  int parentStudyId=currentStudy.getParentStudyId();
  if (parentStudyId <= 0) {
    parentStudyId=currentStudy.getId();
    classes=sgcdao.findAllActiveByStudy(currentStudy);
  }
 else {
    StudyBean parentStudy=(StudyBean)stdao.findByPK(parentStudyId);
    classes=sgcdao.findAllActiveByStudy(parentStudy);
  }
  StudyParameterValueDAO spvdao=new StudyParameterValueDAO(sm.getDataSource());
  StudyParameterValueBean parentSPV=spvdao.findByHandleAndStudy(parentStudyId,"String_Node_Str");
  currentStudy.getStudyParameterConfig().setCollectDob(parentSPV.getValue());
  parentSPV=spvdao.findByHandleAndStudy(parentStudyId,"String_Node_Str");
  currentStudy.getStudyParameterConfig().setGenderRequired(parentSPV.getValue());
  StudyParameterValueBean checkPersonId=spvdao.findByHandleAndStudy(parentStudyId,"String_Node_Str");
  currentStudy.getStudyParameterConfig().setSubjectPersonIdRequired(checkPersonId.getValue());
  if (!fp.isSubmitted()) {
    if (fp.getBoolean("String_Node_Str")) {
      session.removeAttribute(FORM_DISCREPANCY_NOTES_NAME);
      forwardPage(Page.INSTRUCTIONS_ENROLL_SUBJECT);
    }
 else {
      setUpBeans(classes);
      Date today=new Date(System.currentTimeMillis());
      String todayFormatted=local_df.format(today);
      fp.addPresetValue(INPUT_ENROLLMENT_DATE,todayFormatted);
      String idSetting="String_Node_Str";
      if (currentStudy.getParentStudyId() > 0) {
        parentSPV=spvdao.findByHandleAndStudy(parentStudyId,"String_Node_Str");
        currentStudy.getStudyParameterConfig().setSubjectIdGeneration(parentSPV.getValue());
      }
      idSetting=currentStudy.getStudyParameterConfig().getSubjectIdGeneration();
      logger.info("String_Node_Str" + idSetting);
      if (idSetting.equals("String_Node_Str") || idSetting.equals("String_Node_Str")) {
        fp.addPresetValue(INPUT_LABEL,AUTO_LABEL);
      }
      setPresetValues(fp.getPresetValues());
      discNotes=new FormDiscrepancyNotes();
      session.setAttribute(FORM_DISCREPANCY_NOTES_NAME,discNotes);
      forwardPage(Page.ADD_NEW_SUBJECT);
    }
  }
 else {
    if (!fp.getBoolean(EXISTING_SUB_SHOWN)) {
      DOB=fp.getString(INPUT_DOB);
      YOB=fp.getString(INPUT_YOB);
      GENDER=fp.getString(INPUT_GENDER);
    }
    discNotes=(FormDiscrepancyNotes)session.getAttribute(FORM_DISCREPANCY_NOTES_NAME);
    if (discNotes == null) {
      discNotes=new FormDiscrepancyNotes();
    }
    DiscrepancyValidator v=new DiscrepancyValidator(request,discNotes);
    v.addValidation(INPUT_LABEL,Validator.NO_BLANKS);
    v.addValidation(INPUT_LABEL,Validator.LENGTH_NUMERIC_COMPARISON,NumericComparisonOperator.LESS_THAN_OR_EQUAL_TO,30);
    if (currentStudy.getStudyParameterConfig().getSubjectPersonIdRequired().equals("String_Node_Str")) {
      v.addValidation(INPUT_UNIQUE_IDENTIFIER,Validator.NO_BLANKS);
    }
    v.addValidation(INPUT_UNIQUE_IDENTIFIER,Validator.LENGTH_NUMERIC_COMPARISON,NumericComparisonOperator.LESS_THAN_OR_EQUAL_TO,255);
    if (!StringUtil.isBlank(fp.getString(INPUT_SECONDARY_LABEL))) {
      v.addValidation(INPUT_SECONDARY_LABEL,Validator.LENGTH_NUMERIC_COMPARISON,NumericComparisonOperator.LESS_THAN_OR_EQUAL_TO,30);
    }
    String dobSetting=currentStudy.getStudyParameterConfig().getCollectDob();
    if (dobSetting.equals("String_Node_Str")) {
      v.addValidation(INPUT_DOB,Validator.IS_A_DATE);
      if (!StringUtil.isBlank(fp.getString("String_Node_Str"))) {
        v.alwaysExecuteLastValidation(INPUT_DOB);
      }
      v.addValidation(INPUT_DOB,Validator.DATE_IN_PAST);
    }
 else     if (dobSetting.equals("String_Node_Str")) {
      v.addValidation(INPUT_YOB,Validator.IS_AN_INTEGER);
      v.alwaysExecuteLastValidation(INPUT_YOB);
      v.addValidation(INPUT_YOB,Validator.COMPARES_TO_STATIC_VALUE,NumericComparisonOperator.GREATER_THAN_OR_EQUAL_TO,1000);
      Date today=new Date();
      Calendar c=Calendar.getInstance();
      c.setTime(today);
      int currentYear=c.get(Calendar.YEAR);
      v.addValidation(INPUT_YOB,Validator.COMPARES_TO_STATIC_VALUE,NumericComparisonOperator.LESS_THAN_OR_EQUAL_TO,currentYear);
    }
 else {
      logger.info("String_Node_Str");
    }
    ArrayList acceptableGenders=new ArrayList();
    acceptableGenders.add("String_Node_Str");
    acceptableGenders.add("String_Node_Str");
    if (!currentStudy.getStudyParameterConfig().getGenderRequired().equals("String_Node_Str")) {
      v.addValidation(INPUT_GENDER,Validator.IS_IN_SET,acceptableGenders);
    }
    v.addValidation(INPUT_ENROLLMENT_DATE,Validator.IS_A_DATE);
    v.alwaysExecuteLastValidation(INPUT_ENROLLMENT_DATE);
    v.addValidation(INPUT_ENROLLMENT_DATE,Validator.DATE_IN_PAST);
    boolean locationError=false;
    if (fp.getBoolean("String_Node_Str")) {
      v.addValidation(INPUT_EVENT_START_DATE,Validator.IS_A_DATE);
      v.alwaysExecuteLastValidation(INPUT_EVENT_START_DATE);
      if (currentStudy.getStudyParameterConfig().getEventLocationRequired().equalsIgnoreCase("String_Node_Str")) {
        v.addValidation("String_Node_Str",Validator.NO_BLANKS);
        locationError=true;
      }
    }
    HashMap errors=v.validate();
    SubjectDAO sdao=new SubjectDAO(sm.getDataSource());
    String uniqueIdentifier=fp.getString(INPUT_UNIQUE_IDENTIFIER);
    SubjectBean subjectWithSameId=new SubjectBean();
    SubjectBean subjectWithSameIdInParent=new SubjectBean();
    boolean showExistingRecord=false;
    if (!uniqueIdentifier.equals("String_Node_Str")) {
      boolean subjectWithSameIdInCurrentStudyTree=false;
      subjectWithSameId=sdao.findByUniqueIdentifierAndStudy(uniqueIdentifier,currentStudy.getId());
      if (subjectWithSameId.isActive()) {
        Validator.addError(errors,INPUT_UNIQUE_IDENTIFIER,resexception.getString("String_Node_Str") + "String_Node_Str" + uniqueIdentifier+ "String_Node_Str"+ resexception.getString("String_Node_Str"));
        subjectWithSameIdInCurrentStudyTree=true;
        logger.info("String_Node_Str");
      }
 else {
        subjectWithSameId=sdao.findByUniqueIdentifierAndParentStudy(uniqueIdentifier,currentStudy.getId());
        if (subjectWithSameId.isActive()) {
          StudySubjectBean ssub=ssd.findBySubjectIdAndStudy(subjectWithSameId.getId(),currentStudy);
          StudyBean site=(StudyBean)stdao.findByPK(ssub.getStudyId());
          Validator.addError(errors,INPUT_UNIQUE_IDENTIFIER,resexception.getString("String_Node_Str") + "String_Node_Str" + uniqueIdentifier+ resexception.getString("String_Node_Str")+ site.getName()+ resexception.getString("String_Node_Str")+ resexception.getString("String_Node_Str"));
          subjectWithSameIdInCurrentStudyTree=true;
        }
 else {
          subjectWithSameId=sdao.findByUniqueIdentifierAndStudy(uniqueIdentifier,currentStudy.getParentStudyId());
          if (subjectWithSameId.isActive()) {
            Validator.addError(errors,INPUT_UNIQUE_IDENTIFIER,resexception.getString("String_Node_Str") + "String_Node_Str" + uniqueIdentifier+ resexception.getString("String_Node_Str"));
            subjectWithSameIdInCurrentStudyTree=true;
          }
 else {
            subjectWithSameId=sdao.findByUniqueIdentifierAndParentStudy(uniqueIdentifier,currentStudy.getParentStudyId());
            if (subjectWithSameId.isActive()) {
              Validator.addError(errors,INPUT_UNIQUE_IDENTIFIER,resexception.getString("String_Node_Str") + "String_Node_Str" + uniqueIdentifier+ resexception.getString("String_Node_Str"));
              subjectWithSameIdInCurrentStudyTree=true;
            }
          }
        }
      }
      if (!subjectWithSameIdInCurrentStudyTree) {
        subjectWithSameId=sdao.findByUniqueIdentifier(uniqueIdentifier);
        if (subjectWithSameId.isActive()) {
          showExistingRecord=true;
        }
      }
    }
    boolean insertWithParents=fp.getInt(INPUT_MOTHER) > 0 || fp.getInt(INPUT_FATHER) > 0;
    if (fp.getInt(INPUT_MOTHER) > 0) {
      SubjectBean mother=(SubjectBean)sdao.findByPK(fp.getInt(INPUT_MOTHER));
      if (mother == null || !mother.isActive() || mother.getGender() != 'f') {
        Validator.addError(errors,INPUT_MOTHER,resexception.getString("String_Node_Str"));
      }
    }
    if (fp.getInt(INPUT_FATHER) > 0) {
      SubjectBean father=(SubjectBean)sdao.findByPK(fp.getInt(INPUT_FATHER));
      if (father == null || !father.isActive() || father.getGender() != 'm') {
        Validator.addError(errors,INPUT_FATHER,resexception.getString("String_Node_Str"));
      }
    }
    String label=fp.getString(INPUT_LABEL);
    if (!label.equalsIgnoreCase(AUTO_LABEL)) {
      StudySubjectBean subjectWithSameLabel=ssd.findByLabelAndStudy(label,currentStudy);
      StudySubjectBean subjectWithSameLabelInParent=new StudySubjectBean();
      if (currentStudy.getParentStudyId() > 0) {
        subjectWithSameLabelInParent=ssd.findSameByLabelAndStudy(label,currentStudy.getParentStudyId(),0);
      }
      if (subjectWithSameLabel.isActive() || subjectWithSameLabelInParent.isActive()) {
        Validator.addError(errors,INPUT_LABEL,resexception.getString("String_Node_Str"));
      }
    }
    if (!classes.isEmpty()) {
      for (int i=0; i < classes.size(); i++) {
        StudyGroupClassBean sgc=(StudyGroupClassBean)classes.get(i);
        int groupId=fp.getInt("String_Node_Str" + i);
        String notes=fp.getString("String_Node_Str" + i);
        if ("String_Node_Str".equals(sgc.getSubjectAssignment()) && groupId == 0) {
          Validator.addError(errors,"String_Node_Str" + i,resexception.getString("String_Node_Str"));
        }
        if (notes.trim().length() > 255) {
          Validator.addError(errors,"String_Node_Str" + i,resexception.getString("String_Node_Str"));
        }
        sgc.setStudyGroupId(groupId);
        sgc.setGroupNotes(notes);
      }
    }
    if (!errors.isEmpty()) {
      addPageMessage(respage.getString("String_Node_Str"));
      if (locationError) {
        addPageMessage(respage.getString("String_Node_Str"));
      }
      setInputMessages(errors);
      fp.addPresetValue(INPUT_DOB,fp.getString(INPUT_DOB));
      fp.addPresetValue(INPUT_YOB,fp.getString(INPUT_YOB));
      fp.addPresetValue(INPUT_GENDER,fp.getString(INPUT_GENDER));
      fp.addPresetValue(INPUT_UNIQUE_IDENTIFIER,uniqueIdentifier);
      fp.addPresetValue(INPUT_LABEL,label);
      fp.addPresetValue(INPUT_SECONDARY_LABEL,fp.getString(INPUT_SECONDARY_LABEL));
      fp.addPresetValue(INPUT_ENROLLMENT_DATE,fp.getString(INPUT_ENROLLMENT_DATE));
      fp.addPresetValue(INPUT_EVENT_START_DATE,fp.getString(INPUT_EVENT_START_DATE));
      if (currentStudy.isGenetic()) {
        String intFields[]={INPUT_GROUP,INPUT_FATHER,INPUT_MOTHER};
        fp.setCurrentIntValuesAsPreset(intFields);
      }
      fp.addPresetValue(EDIT_DOB,fp.getString(EDIT_DOB));
      setPresetValues(fp.getPresetValues());
      setUpBeans(classes);
      boolean existingSubShown=fp.getBoolean(EXISTING_SUB_SHOWN);
      if (!existingSubShown) {
        forwardPage(Page.ADD_NEW_SUBJECT);
      }
 else {
        forwardPage(Page.ADD_EXISTING_SUBJECT);
      }
    }
 else {
      StudySubjectBean studySubject=new StudySubjectBean();
      SubjectBean subject=new SubjectBean();
      boolean existingSubShown=fp.getBoolean(EXISTING_SUB_SHOWN);
      if (showExistingRecord && !existingSubShown) {
        needUpdate=false;
        subject=subjectWithSameId;
        Calendar cal=Calendar.getInstance();
        int year=0;
        if (subject.getDateOfBirth() != null) {
          cal.setTime(subject.getDateOfBirth());
          year=cal.get(Calendar.YEAR);
          fp.addPresetValue(INPUT_DOB,local_df.format(subject.getDateOfBirth()));
        }
 else {
          fp.addPresetValue(INPUT_DOB,"String_Node_Str");
        }
        if (currentStudy.getStudyParameterConfig().getCollectDob().equals("String_Node_Str") && !subject.isDobCollected()) {
          fp.addPresetValue(INPUT_DOB,fp.getString(INPUT_DOB));
        }
        fp.addPresetValue(INPUT_YOB,String.valueOf(year));
        if (!currentStudy.getStudyParameterConfig().getGenderRequired().equals("String_Node_Str")) {
          fp.addPresetValue(INPUT_GENDER,subject.getGender() + "String_Node_Str");
        }
 else {
          fp.addPresetValue(INPUT_GENDER,"String_Node_Str");
        }
        if (!label.equalsIgnoreCase(AUTO_LABEL)) {
          fp.addPresetValue(INPUT_LABEL,label);
        }
        fp.addPresetValue(INPUT_SECONDARY_LABEL,fp.getString(INPUT_SECONDARY_LABEL));
        fp.addPresetValue(INPUT_ENROLLMENT_DATE,fp.getString(INPUT_ENROLLMENT_DATE));
        fp.addPresetValue(INPUT_EVENT_START_DATE,fp.getString(INPUT_EVENT_START_DATE));
        fp.addPresetValue(INPUT_UNIQUE_IDENTIFIER,subject.getUniqueIdentifier());
        fp.addPresetValue(INPUT_FATHER,subject.getFatherId());
        fp.addPresetValue(INPUT_MOTHER,subject.getMotherId());
        setPresetValues(fp.getPresetValues());
        setUpBeans(classes);
        int warningCount=0;
        if (currentStudy.getStudyParameterConfig().getGenderRequired().equalsIgnoreCase("String_Node_Str")) {
          if (String.valueOf(subjectWithSameId.getGender()).equals("String_Node_Str")) {
            fp.addPresetValue(G_WARNING,"String_Node_Str");
            fp.addPresetValue(INPUT_GENDER,GENDER);
            needUpdate=true;
            updateSubject=subjectWithSameId;
            updateSubject.setGender(GENDER.toCharArray()[0]);
            warningCount++;
          }
 else           if (!String.valueOf(subjectWithSameId.getGender()).equals(GENDER)) {
            fp.addPresetValue(G_WARNING,"String_Node_Str");
            warningCount++;
          }
 else {
            fp.addPresetValue(G_WARNING,"String_Node_Str");
          }
        }
 else {
          fp.addPresetValue(G_WARNING,"String_Node_Str");
        }
        if (currentStudy.getStudyParameterConfig().getCollectDob().equals("String_Node_Str")) {
          if (subjectWithSameId.isDobCollected() == false) {
            needUpdate=true;
            updateSubject=subjectWithSameId;
            updateSubject.setDobCollected(true);
            if (subjectWithSameId.getDateOfBirth() == null) {
              fp.addPresetValue(INPUT_DOB,DOB);
              updateSubject.setDateOfBirth(new Date(DOB));
            }
 else {
              String y=String.valueOf(subjectWithSameId.getDateOfBirth()).split("String_Node_Str")[0];
              String[] d=DOB.split("String_Node_Str");
              if (!y.equals("String_Node_Str")) {
                if (!y.equals(d[2])) {
                  fp.addPresetValue(D_WARNING,"String_Node_Str");
                  fp.addPresetValue(INPUT_DOB,d[0] + "String_Node_Str" + d[1]+ "String_Node_Str"+ y);
                  updateSubject.setDateOfBirth(sdf.parse(d[0] + "String_Node_Str" + d[1]+ "String_Node_Str"+ y));
                }
 else {
                  fp.addPresetValue(D_WARNING,"String_Node_Str");
                  fp.addPresetValue(INPUT_DOB,DOB);
                  updateSubject.setDateOfBirth(sdf.parse(DOB));
                }
              }
 else {
                fp.addPresetValue(D_WARNING,"String_Node_Str");
                fp.addPresetValue(INPUT_DOB,DOB);
                updateSubject.setDateOfBirth(sdf.parse(DOB));
              }
            }
            warningCount++;
          }
 else           if (!local_df.format(subjectWithSameId.getDateOfBirth()).toString().equals(DOB)) {
            fp.addPresetValue(D_WARNING,"String_Node_Str");
            warningCount++;
          }
 else {
            fp.addPresetValue(D_WARNING,"String_Node_Str");
          }
        }
 else         if (currentStudy.getStudyParameterConfig().getCollectDob().equals("String_Node_Str")) {
          String y=String.valueOf(subjectWithSameId.getDateOfBirth()).split("String_Node_Str")[0];
          if (!y.equals("String_Node_Str")) {
            if (!y.equals(YOB)) {
              fp.addPresetValue(Y_WARNING,"String_Node_Str");
              warningCount++;
            }
 else {
              fp.addPresetValue(Y_WARNING,"String_Node_Str");
            }
          }
 else {
            needUpdate=true;
            updateSubject=subjectWithSameId;
            fp.addPresetValue(Y_WARNING,"String_Node_Str");
            fp.addPresetValue(INPUT_YOB,YOB);
            updateSubject.setDateOfBirth(sdf.parse("String_Node_Str" + YOB));
            warningCount++;
          }
        }
 else {
          fp.addPresetValue(Y_WARNING,"String_Node_Str");
        }
        if (warningCount > 0) {
          warningCount=0;
          forwardPage(Page.ADD_EXISTING_SUBJECT);
          return;
        }
      }
      if (!showExistingRecord) {
        if (!StringUtil.isBlank(fp.getString(INPUT_GENDER))) {
          subject.setGender(fp.getString(INPUT_GENDER).charAt(0));
        }
 else {
          subject.setGender(' ');
        }
        subject.setUniqueIdentifier(uniqueIdentifier);
        if (currentStudy.getStudyParameterConfig().getCollectDob().equals("String_Node_Str")) {
          if (!StringUtil.isBlank(fp.getString(INPUT_DOB))) {
            subject.setDateOfBirth(fp.getDate(INPUT_DOB));
            subject.setDobCollected(true);
          }
 else {
            subject.setDateOfBirth(null);
            subject.setDobCollected(false);
          }
        }
 else         if (currentStudy.getStudyParameterConfig().getCollectDob().equals("String_Node_Str")) {
          subject.setDobCollected(false);
          int yob=fp.getInt(INPUT_YOB);
          Date fakeDate=new Date("String_Node_Str" + yob);
          String dobString=local_df.format(fakeDate);
          try {
            Date fakeDOB=local_df.parse(dobString);
            subject.setDateOfBirth(fakeDOB);
          }
 catch (          ParseException pe) {
            subject.setDateOfBirth(new Date());
            addPageMessage(respage.getString("String_Node_Str"));
          }
        }
        subject.setStatus(Status.AVAILABLE);
        subject.setOwner(ub);
        if (insertWithParents) {
          subject.setFatherId(fp.getInt(INPUT_FATHER));
          subject.setMotherId(fp.getInt(INPUT_MOTHER));
          subject=sdao.create(subject);
        }
 else {
          subject=sdao.create(subject);
        }
        if (!subject.isActive()) {
          throw new OpenClinicaException(resexception.getString("String_Node_Str"),"String_Node_Str");
        }
      }
 else       if (existingSubShown) {
        if (!needUpdate) {
          subject=subjectWithSameId;
        }
 else {
          updateSubject.setUpdater(ub);
          updateSubject=(SubjectBean)sdao.update(updateSubject);
          if (!updateSubject.isActive()) {
            throw new OpenClinicaException("String_Node_Str","String_Node_Str");
          }
          subject=updateSubject;
          needUpdate=false;
        }
      }
      studySubject.setSubjectId(subject.getId());
      studySubject.setStudyId(currentStudy.getId());
      studySubject.setLabel(fp.getString(INPUT_LABEL));
      studySubject.setSecondaryLabel(fp.getString(INPUT_SECONDARY_LABEL));
      studySubject.setStatus(Status.AVAILABLE);
      studySubject.setEnrollmentDate(fp.getDate(INPUT_ENROLLMENT_DATE));
      if (fp.getBoolean("String_Node_Str")) {
        studySubject.setEventStartDate(fp.getDate(INPUT_EVENT_START_DATE));
      }
      studySubject.setOwner(ub);
      if (fp.getString(INPUT_LABEL).equalsIgnoreCase(AUTO_LABEL)) {
synchronized (simpleLockObj) {
          int nextLabel=ssd.findTheGreatestLabel() + 1;
          studySubject.setLabel(nextLabel + "String_Node_Str");
          studySubject=ssd.createWithoutGroup(studySubject);
          if (showExistingRecord && !existingSubShown) {
            fp.addPresetValue(INPUT_LABEL,label);
          }
        }
      }
 else {
        studySubject=ssd.createWithoutGroup(studySubject);
      }
      if (!classes.isEmpty() && studySubject.isActive()) {
        SubjectGroupMapDAO sgmdao=new SubjectGroupMapDAO(sm.getDataSource());
        for (int i=0; i < classes.size(); i++) {
          StudyGroupClassBean group=(StudyGroupClassBean)classes.get(i);
          int studyGroupId=group.getStudyGroupId();
          String notes=group.getGroupNotes();
          SubjectGroupMapBean map=new SubjectGroupMapBean();
          map.setNotes(group.getGroupNotes());
          map.setStatus(Status.AVAILABLE);
          map.setStudyGroupId(group.getStudyGroupId());
          map.setStudySubjectId(studySubject.getId());
          map.setStudyGroupClassId(group.getId());
          map.setOwner(ub);
          if (map.getStudyGroupId() > 0) {
            sgmdao.create(map);
          }
        }
      }
      if (!studySubject.isActive()) {
        throw new OpenClinicaException(resexception.getString("String_Node_Str"),"String_Node_Str");
      }
      FormDiscrepancyNotes fdn=(FormDiscrepancyNotes)session.getAttribute(FORM_DISCREPANCY_NOTES_NAME);
      DiscrepancyNoteDAO dndao=new DiscrepancyNoteDAO(sm.getDataSource());
      String[] subjectFields={INPUT_DOB,INPUT_YOB,INPUT_GENDER};
      for (      String element : subjectFields) {
        saveFieldNotes(element,fdn,dndao,subject.getId(),"String_Node_Str",currentStudy);
      }
      saveFieldNotes(INPUT_ENROLLMENT_DATE,fdn,dndao,studySubject.getId(),"String_Node_Str",currentStudy);
      request.removeAttribute(FormProcessor.FIELD_SUBMITTED);
      request.setAttribute(CreateNewStudyEventServlet.INPUT_STUDY_SUBJECT,studySubject);
      request.setAttribute(CreateNewStudyEventServlet.INPUT_REQUEST_STUDY_SUBJECT,"String_Node_Str");
      request.setAttribute(FormProcessor.FIELD_SUBMITTED,"String_Node_Str");
      addPageMessage(respage.getString("String_Node_Str") + studySubject.getLabel() + respage.getString("String_Node_Str"));
      if (fp.getBoolean("String_Node_Str")) {
        createStudyEvent(fp,studySubject);
        request.setAttribute("String_Node_Str",studySubject.getId() + "String_Node_Str");
        response.encodeRedirectURL("String_Node_Str" + studySubject.getId());
        forwardPage(Page.VIEW_STUDY_SUBJECT_SERVLET);
        return;
      }
      String submitEvent=fp.getString(SUBMIT_EVENT_BUTTON);
      String submitEnroll=fp.getString(SUBMIT_ENROLL_BUTTON);
      String submitDone=fp.getString(SUBMIT_DONE_BUTTON);
      session.removeAttribute(FORM_DISCREPANCY_NOTES_NAME);
      if (!StringUtil.isBlank(submitEvent)) {
        forwardPage(Page.CREATE_NEW_STUDY_EVENT_SERVLET);
      }
 else       if (!StringUtil.isBlank(submitEnroll)) {
        setUpBeans(classes);
        Date today=new Date(System.currentTimeMillis());
        String todayFormatted=local_df.format(today);
        fp.addPresetValue(INPUT_ENROLLMENT_DATE,todayFormatted);
        String idSetting="String_Node_Str";
        if (currentStudy.getParentStudyId() > 0) {
          parentSPV=spvdao.findByHandleAndStudy(parentStudyId,"String_Node_Str");
          currentStudy.getStudyParameterConfig().setSubjectIdGeneration(parentSPV.getValue());
        }
        idSetting=currentStudy.getStudyParameterConfig().getSubjectIdGeneration();
        logger.info("String_Node_Str" + idSetting);
        if (idSetting.equals("String_Node_Str") || idSetting.equals("String_Node_Str")) {
          fp.addPresetValue(INPUT_LABEL,AUTO_LABEL);
        }
        setPresetValues(fp.getPresetValues());
        discNotes=new FormDiscrepancyNotes();
        session.setAttribute(FORM_DISCREPANCY_NOTES_NAME,discNotes);
        forwardPage(Page.ADD_NEW_SUBJECT);
      }
 else {
        request.setAttribute("String_Node_Str",studySubject.getId() + "String_Node_Str");
        response.encodeRedirectURL("String_Node_Str" + studySubject.getId());
        forwardPage(Page.VIEW_STUDY_SUBJECT_SERVLET);
        return;
      }
    }
  }
}

Fixed version (ground truth):
@Override protected void processRequest() throws Exception {
  checkStudyLocked(Page.LIST_STUDY_SUBJECTS,respage.getString("String_Node_Str"));
  checkStudyFrozen(Page.LIST_STUDY_SUBJECTS,respage.getString("String_Node_Str"));
  StudySubjectDAO ssd=new StudySubjectDAO(sm.getDataSource());
  StudyDAO stdao=new StudyDAO(sm.getDataSource());
  StudyGroupClassDAO sgcdao=new StudyGroupClassDAO(sm.getDataSource());
  ArrayList classes=new ArrayList();
  panel.setStudyInfoShown(false);
  FormProcessor fp=new FormProcessor(request);
  FormDiscrepancyNotes discNotes;
  SimpleDateFormat sdf=new SimpleDateFormat("String_Node_Str");
  int parentStudyId=currentStudy.getParentStudyId();
  if (parentStudyId <= 0) {
    parentStudyId=currentStudy.getId();
    classes=sgcdao.findAllActiveByStudy(currentStudy);
  }
 else {
    StudyBean parentStudy=(StudyBean)stdao.findByPK(parentStudyId);
    classes=sgcdao.findAllActiveByStudy(parentStudy);
  }
  StudyParameterValueDAO spvdao=new StudyParameterValueDAO(sm.getDataSource());
  StudyParameterValueBean parentSPV=spvdao.findByHandleAndStudy(parentStudyId,"String_Node_Str");
  currentStudy.getStudyParameterConfig().setCollectDob(parentSPV.getValue());
  parentSPV=spvdao.findByHandleAndStudy(parentStudyId,"String_Node_Str");
  currentStudy.getStudyParameterConfig().setGenderRequired(parentSPV.getValue());
  StudyParameterValueBean checkPersonId=spvdao.findByHandleAndStudy(parentStudyId,"String_Node_Str");
  currentStudy.getStudyParameterConfig().setSubjectPersonIdRequired(checkPersonId.getValue());
  if (!fp.isSubmitted()) {
    if (fp.getBoolean("String_Node_Str")) {
      session.removeAttribute(FORM_DISCREPANCY_NOTES_NAME);
      forwardPage(Page.INSTRUCTIONS_ENROLL_SUBJECT);
    }
 else {
      setUpBeans(classes);
      Date today=new Date(System.currentTimeMillis());
      String todayFormatted=local_df.format(today);
      fp.addPresetValue(INPUT_ENROLLMENT_DATE,todayFormatted);
      String idSetting="String_Node_Str";
      if (currentStudy.getParentStudyId() > 0) {
        parentSPV=spvdao.findByHandleAndStudy(parentStudyId,"String_Node_Str");
        currentStudy.getStudyParameterConfig().setSubjectIdGeneration(parentSPV.getValue());
      }
      idSetting=currentStudy.getStudyParameterConfig().getSubjectIdGeneration();
      logger.info("String_Node_Str" + idSetting);
      if (idSetting.equals("String_Node_Str") || idSetting.equals("String_Node_Str")) {
        fp.addPresetValue(INPUT_LABEL,AUTO_LABEL);
      }
      setPresetValues(fp.getPresetValues());
      discNotes=new FormDiscrepancyNotes();
      session.setAttribute(FORM_DISCREPANCY_NOTES_NAME,discNotes);
      forwardPage(Page.ADD_NEW_SUBJECT);
    }
  }
 else {
    if (!fp.getBoolean(EXISTING_SUB_SHOWN)) {
      DOB=fp.getString(INPUT_DOB);
      YOB=fp.getString(INPUT_YOB);
      GENDER=fp.getString(INPUT_GENDER);
    }
    discNotes=(FormDiscrepancyNotes)session.getAttribute(FORM_DISCREPANCY_NOTES_NAME);
    if (discNotes == null) {
      discNotes=new FormDiscrepancyNotes();
    }
    DiscrepancyValidator v=new DiscrepancyValidator(request,discNotes);
    v.addValidation(INPUT_LABEL,Validator.NO_BLANKS);
    v.addValidation(INPUT_LABEL,Validator.LENGTH_NUMERIC_COMPARISON,NumericComparisonOperator.LESS_THAN_OR_EQUAL_TO,30);
    if (currentStudy.getStudyParameterConfig().getSubjectPersonIdRequired().equals("String_Node_Str")) {
      v.addValidation(INPUT_UNIQUE_IDENTIFIER,Validator.NO_BLANKS);
    }
    v.addValidation(INPUT_UNIQUE_IDENTIFIER,Validator.LENGTH_NUMERIC_COMPARISON,NumericComparisonOperator.LESS_THAN_OR_EQUAL_TO,255);
    if (!StringUtil.isBlank(fp.getString(INPUT_SECONDARY_LABEL))) {
      v.addValidation(INPUT_SECONDARY_LABEL,Validator.LENGTH_NUMERIC_COMPARISON,NumericComparisonOperator.LESS_THAN_OR_EQUAL_TO,30);
    }
    String dobSetting=currentStudy.getStudyParameterConfig().getCollectDob();
    if (dobSetting.equals("String_Node_Str")) {
      v.addValidation(INPUT_DOB,Validator.IS_A_DATE);
      if (!StringUtil.isBlank(fp.getString("String_Node_Str"))) {
        v.alwaysExecuteLastValidation(INPUT_DOB);
      }
      v.addValidation(INPUT_DOB,Validator.DATE_IN_PAST);
    }
 else     if (dobSetting.equals("String_Node_Str")) {
      v.addValidation(INPUT_YOB,Validator.IS_AN_INTEGER);
      v.alwaysExecuteLastValidation(INPUT_YOB);
      v.addValidation(INPUT_YOB,Validator.COMPARES_TO_STATIC_VALUE,NumericComparisonOperator.GREATER_THAN_OR_EQUAL_TO,1000);
      Date today=new Date();
      Calendar c=Calendar.getInstance();
      c.setTime(today);
      int currentYear=c.get(Calendar.YEAR);
      v.addValidation(INPUT_YOB,Validator.COMPARES_TO_STATIC_VALUE,NumericComparisonOperator.LESS_THAN_OR_EQUAL_TO,currentYear);
    }
 else {
      logger.info("String_Node_Str");
    }
    ArrayList acceptableGenders=new ArrayList();
    acceptableGenders.add("String_Node_Str");
    acceptableGenders.add("String_Node_Str");
    if (!currentStudy.getStudyParameterConfig().getGenderRequired().equals("String_Node_Str")) {
      v.addValidation(INPUT_GENDER,Validator.IS_IN_SET,acceptableGenders);
    }
    v.addValidation(INPUT_ENROLLMENT_DATE,Validator.IS_A_DATE);
    v.alwaysExecuteLastValidation(INPUT_ENROLLMENT_DATE);
    v.addValidation(INPUT_ENROLLMENT_DATE,Validator.DATE_IN_PAST);
    boolean locationError=false;
    if (fp.getBoolean("String_Node_Str")) {
      v.addValidation(INPUT_EVENT_START_DATE,Validator.IS_A_DATE);
      v.alwaysExecuteLastValidation(INPUT_EVENT_START_DATE);
      if (currentStudy.getStudyParameterConfig().getEventLocationRequired().equalsIgnoreCase("String_Node_Str")) {
        v.addValidation("String_Node_Str",Validator.NO_BLANKS);
        locationError=true;
      }
    }
    HashMap errors=v.validate();
    SubjectDAO sdao=new SubjectDAO(sm.getDataSource());
    String uniqueIdentifier=fp.getString(INPUT_UNIQUE_IDENTIFIER);
    SubjectBean subjectWithSameId=new SubjectBean();
    SubjectBean subjectWithSameIdInParent=new SubjectBean();
    boolean showExistingRecord=false;
    if (!uniqueIdentifier.equals("String_Node_Str")) {
      boolean subjectWithSameIdInCurrentStudyTree=false;
      subjectWithSameId=sdao.findByUniqueIdentifierAndStudy(uniqueIdentifier,currentStudy.getId());
      if (subjectWithSameId.isActive()) {
        Validator.addError(errors,INPUT_UNIQUE_IDENTIFIER,resexception.getString("String_Node_Str") + "String_Node_Str" + uniqueIdentifier+ "String_Node_Str"+ resexception.getString("String_Node_Str"));
        subjectWithSameIdInCurrentStudyTree=true;
        logger.info("String_Node_Str");
      }
 else {
        subjectWithSameId=sdao.findByUniqueIdentifierAndParentStudy(uniqueIdentifier,currentStudy.getId());
        if (subjectWithSameId.isActive()) {
          StudySubjectBean ssub=ssd.findBySubjectIdAndStudy(subjectWithSameId.getId(),currentStudy);
          StudyBean site=(StudyBean)stdao.findByPK(ssub.getStudyId());
          Validator.addError(errors,INPUT_UNIQUE_IDENTIFIER,resexception.getString("String_Node_Str") + "String_Node_Str" + uniqueIdentifier+ resexception.getString("String_Node_Str")+ site.getName()+ resexception.getString("String_Node_Str")+ resexception.getString("String_Node_Str"));
          subjectWithSameIdInCurrentStudyTree=true;
        }
 else {
          subjectWithSameId=sdao.findByUniqueIdentifierAndStudy(uniqueIdentifier,currentStudy.getParentStudyId());
          if (subjectWithSameId.isActive()) {
            Validator.addError(errors,INPUT_UNIQUE_IDENTIFIER,resexception.getString("String_Node_Str") + "String_Node_Str" + uniqueIdentifier+ resexception.getString("String_Node_Str"));
            subjectWithSameIdInCurrentStudyTree=true;
          }
 else {
            subjectWithSameId=sdao.findByUniqueIdentifierAndParentStudy(uniqueIdentifier,currentStudy.getParentStudyId());
            if (subjectWithSameId.isActive()) {
              Validator.addError(errors,INPUT_UNIQUE_IDENTIFIER,resexception.getString("String_Node_Str") + "String_Node_Str" + uniqueIdentifier+ resexception.getString("String_Node_Str"));
              subjectWithSameIdInCurrentStudyTree=true;
            }
          }
        }
      }
      if (!subjectWithSameIdInCurrentStudyTree) {
        subjectWithSameId=sdao.findByUniqueIdentifier(uniqueIdentifier);
        if (subjectWithSameId.isActive()) {
          showExistingRecord=true;
        }
      }
    }
    boolean insertWithParents=fp.getInt(INPUT_MOTHER) > 0 || fp.getInt(INPUT_FATHER) > 0;
    if (fp.getInt(INPUT_MOTHER) > 0) {
      SubjectBean mother=(SubjectBean)sdao.findByPK(fp.getInt(INPUT_MOTHER));
      if (mother == null || !mother.isActive() || mother.getGender() != 'f') {
        Validator.addError(errors,INPUT_MOTHER,resexception.getString("String_Node_Str"));
      }
    }
    if (fp.getInt(INPUT_FATHER) > 0) {
      SubjectBean father=(SubjectBean)sdao.findByPK(fp.getInt(INPUT_FATHER));
      if (father == null || !father.isActive() || father.getGender() != 'm') {
        Validator.addError(errors,INPUT_FATHER,resexception.getString("String_Node_Str"));
      }
    }
    String label=fp.getString(INPUT_LABEL);
    if (!label.equalsIgnoreCase(AUTO_LABEL)) {
      StudySubjectBean subjectWithSameLabel=ssd.findByLabelAndStudy(label,currentStudy);
      StudySubjectBean subjectWithSameLabelInParent=new StudySubjectBean();
      if (currentStudy.getParentStudyId() > 0) {
        subjectWithSameLabelInParent=ssd.findSameByLabelAndStudy(label,currentStudy.getParentStudyId(),0);
      }
      if (subjectWithSameLabel.isActive() || subjectWithSameLabelInParent.isActive()) {
        Validator.addError(errors,INPUT_LABEL,resexception.getString("String_Node_Str"));
      }
    }
    if (!classes.isEmpty()) {
      for (int i=0; i < classes.size(); i++) {
        StudyGroupClassBean sgc=(StudyGroupClassBean)classes.get(i);
        int groupId=fp.getInt("String_Node_Str" + i);
        String notes=fp.getString("String_Node_Str" + i);
        if ("String_Node_Str".equals(sgc.getSubjectAssignment()) && groupId == 0) {
          Validator.addError(errors,"String_Node_Str" + i,resexception.getString("String_Node_Str"));
        }
        if (notes.trim().length() > 255) {
          Validator.addError(errors,"String_Node_Str" + i,resexception.getString("String_Node_Str"));
        }
        sgc.setStudyGroupId(groupId);
        sgc.setGroupNotes(notes);
      }
    }
    if (!errors.isEmpty()) {
      addPageMessage(respage.getString("String_Node_Str"));
      if (locationError) {
        addPageMessage(respage.getString("String_Node_Str"));
      }
      setInputMessages(errors);
      fp.addPresetValue(INPUT_DOB,fp.getString(INPUT_DOB));
      fp.addPresetValue(INPUT_YOB,fp.getString(INPUT_YOB));
      fp.addPresetValue(INPUT_GENDER,fp.getString(INPUT_GENDER));
      fp.addPresetValue(INPUT_UNIQUE_IDENTIFIER,uniqueIdentifier);
      fp.addPresetValue(INPUT_LABEL,label);
      fp.addPresetValue(INPUT_SECONDARY_LABEL,fp.getString(INPUT_SECONDARY_LABEL));
      fp.addPresetValue(INPUT_ENROLLMENT_DATE,fp.getString(INPUT_ENROLLMENT_DATE));
      fp.addPresetValue(INPUT_EVENT_START_DATE,fp.getString(INPUT_EVENT_START_DATE));
      if (currentStudy.isGenetic()) {
        String intFields[]={INPUT_GROUP,INPUT_FATHER,INPUT_MOTHER};
        fp.setCurrentIntValuesAsPreset(intFields);
      }
      fp.addPresetValue(EDIT_DOB,fp.getString(EDIT_DOB));
      setPresetValues(fp.getPresetValues());
      setUpBeans(classes);
      boolean existingSubShown=fp.getBoolean(EXISTING_SUB_SHOWN);
      if (!existingSubShown) {
        Object isSubjectOverlay=fp.getRequest().getParameter("String_Node_Str");
        if (isSubjectOverlay != null) {
          request.setAttribute("String_Node_Str",true);
          forwardPage(Page.LIST_STUDY_SUBJECTS_SERVLET);
        }
 else {
          forwardPage(Page.ADD_NEW_SUBJECT);
        }
      }
 else {
        forwardPage(Page.ADD_EXISTING_SUBJECT);
      }
    }
 else {
      StudySubjectBean studySubject=new StudySubjectBean();
      SubjectBean subject=new SubjectBean();
      boolean existingSubShown=fp.getBoolean(EXISTING_SUB_SHOWN);
      if (showExistingRecord && !existingSubShown) {
        needUpdate=false;
        subject=subjectWithSameId;
        Calendar cal=Calendar.getInstance();
        int year=0;
        if (subject.getDateOfBirth() != null) {
          cal.setTime(subject.getDateOfBirth());
          year=cal.get(Calendar.YEAR);
          fp.addPresetValue(INPUT_DOB,local_df.format(subject.getDateOfBirth()));
        }
 else {
          fp.addPresetValue(INPUT_DOB,"String_Node_Str");
        }
        if (currentStudy.getStudyParameterConfig().getCollectDob().equals("String_Node_Str") && !subject.isDobCollected()) {
          fp.addPresetValue(INPUT_DOB,fp.getString(INPUT_DOB));
        }
        fp.addPresetValue(INPUT_YOB,String.valueOf(year));
        if (!currentStudy.getStudyParameterConfig().getGenderRequired().equals("String_Node_Str")) {
          fp.addPresetValue(INPUT_GENDER,subject.getGender() + "String_Node_Str");
        }
 else {
          fp.addPresetValue(INPUT_GENDER,"String_Node_Str");
        }
        if (!label.equalsIgnoreCase(AUTO_LABEL)) {
          fp.addPresetValue(INPUT_LABEL,label);
        }
        fp.addPresetValue(INPUT_SECONDARY_LABEL,fp.getString(INPUT_SECONDARY_LABEL));
        fp.addPresetValue(INPUT_ENROLLMENT_DATE,fp.getString(INPUT_ENROLLMENT_DATE));
        fp.addPresetValue(INPUT_EVENT_START_DATE,fp.getString(INPUT_EVENT_START_DATE));
        fp.addPresetValue(INPUT_UNIQUE_IDENTIFIER,subject.getUniqueIdentifier());
        fp.addPresetValue(INPUT_FATHER,subject.getFatherId());
        fp.addPresetValue(INPUT_MOTHER,subject.getMotherId());
        setPresetValues(fp.getPresetValues());
        setUpBeans(classes);
        int warningCount=0;
        if (currentStudy.getStudyParameterConfig().getGenderRequired().equalsIgnoreCase("String_Node_Str")) {
          if (String.valueOf(subjectWithSameId.getGender()).equals("String_Node_Str")) {
            fp.addPresetValue(G_WARNING,"String_Node_Str");
            fp.addPresetValue(INPUT_GENDER,GENDER);
            needUpdate=true;
            updateSubject=subjectWithSameId;
            updateSubject.setGender(GENDER.toCharArray()[0]);
            warningCount++;
          }
 else           if (!String.valueOf(subjectWithSameId.getGender()).equals(GENDER)) {
            fp.addPresetValue(G_WARNING,"String_Node_Str");
            warningCount++;
          }
 else {
            fp.addPresetValue(G_WARNING,"String_Node_Str");
          }
        }
 else {
          fp.addPresetValue(G_WARNING,"String_Node_Str");
        }
        if (currentStudy.getStudyParameterConfig().getCollectDob().equals("String_Node_Str")) {
          if (subjectWithSameId.isDobCollected() == false) {
            needUpdate=true;
            updateSubject=subjectWithSameId;
            updateSubject.setDobCollected(true);
            if (subjectWithSameId.getDateOfBirth() == null) {
              fp.addPresetValue(INPUT_DOB,DOB);
              updateSubject.setDateOfBirth(new Date(DOB));
            }
 else {
              String y=String.valueOf(subjectWithSameId.getDateOfBirth()).split("String_Node_Str")[0];
              String[] d=DOB.split("String_Node_Str");
              if (!y.equals("String_Node_Str")) {
                if (!y.equals(d[2])) {
                  fp.addPresetValue(D_WARNING,"String_Node_Str");
                  fp.addPresetValue(INPUT_DOB,d[0] + "String_Node_Str" + d[1]+ "String_Node_Str"+ y);
                  updateSubject.setDateOfBirth(sdf.parse(d[0] + "String_Node_Str" + d[1]+ "String_Node_Str"+ y));
                }
 else {
                  fp.addPresetValue(D_WARNING,"String_Node_Str");
                  fp.addPresetValue(INPUT_DOB,DOB);
                  updateSubject.setDateOfBirth(sdf.parse(DOB));
                }
              }
 else {
                fp.addPresetValue(D_WARNING,"String_Node_Str");
                fp.addPresetValue(INPUT_DOB,DOB);
                updateSubject.setDateOfBirth(sdf.parse(DOB));
              }
            }
            warningCount++;
          }
 else           if (!local_df.format(subjectWithSameId.getDateOfBirth()).toString().equals(DOB)) {
            fp.addPresetValue(D_WARNING,"String_Node_Str");
            warningCount++;
          }
 else {
            fp.addPresetValue(D_WARNING,"String_Node_Str");
          }
        }
 else         if (currentStudy.getStudyParameterConfig().getCollectDob().equals("String_Node_Str")) {
          String y=String.valueOf(subjectWithSameId.getDateOfBirth()).split("String_Node_Str")[0];
          if (!y.equals("String_Node_Str")) {
            if (!y.equals(YOB)) {
              fp.addPresetValue(Y_WARNING,"String_Node_Str");
              warningCount++;
            }
 else {
              fp.addPresetValue(Y_WARNING,"String_Node_Str");
            }
          }
 else {
            needUpdate=true;
            updateSubject=subjectWithSameId;
            fp.addPresetValue(Y_WARNING,"String_Node_Str");
            fp.addPresetValue(INPUT_YOB,YOB);
            updateSubject.setDateOfBirth(sdf.parse("String_Node_Str" + YOB));
            warningCount++;
          }
        }
 else {
          fp.addPresetValue(Y_WARNING,"String_Node_Str");
        }
        if (warningCount > 0) {
          warningCount=0;
          forwardPage(Page.ADD_EXISTING_SUBJECT);
          return;
        }
      }
      if (!showExistingRecord) {
        if (!StringUtil.isBlank(fp.getString(INPUT_GENDER))) {
          subject.setGender(fp.getString(INPUT_GENDER).charAt(0));
        }
 else {
          subject.setGender(' ');
        }
        subject.setUniqueIdentifier(uniqueIdentifier);
        if (currentStudy.getStudyParameterConfig().getCollectDob().equals("String_Node_Str")) {
          if (!StringUtil.isBlank(fp.getString(INPUT_DOB))) {
            subject.setDateOfBirth(fp.getDate(INPUT_DOB));
            subject.setDobCollected(true);
          }
 else {
            subject.setDateOfBirth(null);
            subject.setDobCollected(false);
          }
        }
 else         if (currentStudy.getStudyParameterConfig().getCollectDob().equals("String_Node_Str")) {
          subject.setDobCollected(false);
          int yob=fp.getInt(INPUT_YOB);
          Date fakeDate=new Date("String_Node_Str" + yob);
          String dobString=local_df.format(fakeDate);
          try {
            Date fakeDOB=local_df.parse(dobString);
            subject.setDateOfBirth(fakeDOB);
          }
 catch (          ParseException pe) {
            subject.setDateOfBirth(new Date());
            addPageMessage(respage.getString("String_Node_Str"));
          }
        }
        subject.setStatus(Status.AVAILABLE);
        subject.setOwner(ub);
        if (insertWithParents) {
          subject.setFatherId(fp.getInt(INPUT_FATHER));
          subject.setMotherId(fp.getInt(INPUT_MOTHER));
          subject=sdao.create(subject);
        }
 else {
          subject=sdao.create(subject);
        }
        if (!subject.isActive()) {
          throw new OpenClinicaException(resexception.getString("String_Node_Str"),"String_Node_Str");
        }
      }
 else       if (existingSubShown) {
        if (!needUpdate) {
          subject=subjectWithSameId;
        }
 else {
          updateSubject.setUpdater(ub);
          updateSubject=(SubjectBean)sdao.update(updateSubject);
          if (!updateSubject.isActive()) {
            throw new OpenClinicaException("String_Node_Str","String_Node_Str");
          }
          subject=updateSubject;
          needUpdate=false;
        }
      }
      studySubject.setSubjectId(subject.getId());
      studySubject.setStudyId(currentStudy.getId());
      studySubject.setLabel(fp.getString(INPUT_LABEL));
      studySubject.setSecondaryLabel(fp.getString(INPUT_SECONDARY_LABEL));
      studySubject.setStatus(Status.AVAILABLE);
      studySubject.setEnrollmentDate(fp.getDate(INPUT_ENROLLMENT_DATE));
      if (fp.getBoolean("String_Node_Str")) {
        studySubject.setEventStartDate(fp.getDate(INPUT_EVENT_START_DATE));
      }
      studySubject.setOwner(ub);
      if (fp.getString(INPUT_LABEL).equalsIgnoreCase(AUTO_LABEL)) {
synchronized (simpleLockObj) {
          int nextLabel=ssd.findTheGreatestLabel() + 1;
          studySubject.setLabel(nextLabel + "String_Node_Str");
          studySubject=ssd.createWithoutGroup(studySubject);
          if (showExistingRecord && !existingSubShown) {
            fp.addPresetValue(INPUT_LABEL,label);
          }
        }
      }
 else {
        studySubject=ssd.createWithoutGroup(studySubject);
      }
      if (!classes.isEmpty() && studySubject.isActive()) {
        SubjectGroupMapDAO sgmdao=new SubjectGroupMapDAO(sm.getDataSource());
        for (int i=0; i < classes.size(); i++) {
          StudyGroupClassBean group=(StudyGroupClassBean)classes.get(i);
          int studyGroupId=group.getStudyGroupId();
          String notes=group.getGroupNotes();
          SubjectGroupMapBean map=new SubjectGroupMapBean();
          map.setNotes(group.getGroupNotes());
          map.setStatus(Status.AVAILABLE);
          map.setStudyGroupId(group.getStudyGroupId());
          map.setStudySubjectId(studySubject.getId());
          map.setStudyGroupClassId(group.getId());
          map.setOwner(ub);
          if (map.getStudyGroupId() > 0) {
            sgmdao.create(map);
          }
        }
      }
      if (!studySubject.isActive()) {
        throw new OpenClinicaException(resexception.getString("String_Node_Str"),"String_Node_Str");
      }
      FormDiscrepancyNotes fdn=(FormDiscrepancyNotes)session.getAttribute(FORM_DISCREPANCY_NOTES_NAME);
      DiscrepancyNoteDAO dndao=new DiscrepancyNoteDAO(sm.getDataSource());
      String[] subjectFields={INPUT_DOB,INPUT_YOB,INPUT_GENDER};
      for (      String element : subjectFields) {
        saveFieldNotes(element,fdn,dndao,subject.getId(),"String_Node_Str",currentStudy);
      }
      saveFieldNotes(INPUT_ENROLLMENT_DATE,fdn,dndao,studySubject.getId(),"String_Node_Str",currentStudy);
      request.removeAttribute(FormProcessor.FIELD_SUBMITTED);
      request.setAttribute(CreateNewStudyEventServlet.INPUT_STUDY_SUBJECT,studySubject);
      request.setAttribute(CreateNewStudyEventServlet.INPUT_REQUEST_STUDY_SUBJECT,"String_Node_Str");
      request.setAttribute(FormProcessor.FIELD_SUBMITTED,"String_Node_Str");
      addPageMessage(respage.getString("String_Node_Str") + studySubject.getLabel() + respage.getString("String_Node_Str"));
      if (fp.getBoolean("String_Node_Str")) {
        createStudyEvent(fp,studySubject);
        request.setAttribute("String_Node_Str",studySubject.getId() + "String_Node_Str");
        response.encodeRedirectURL("String_Node_Str" + studySubject.getId());
        forwardPage(Page.VIEW_STUDY_SUBJECT_SERVLET);
        return;
      }
      String submitEvent=fp.getString(SUBMIT_EVENT_BUTTON);
      String submitEnroll=fp.getString(SUBMIT_ENROLL_BUTTON);
      String submitDone=fp.getString(SUBMIT_DONE_BUTTON);
      session.removeAttribute(FORM_DISCREPANCY_NOTES_NAME);
      if (!StringUtil.isBlank(submitEvent)) {
        forwardPage(Page.CREATE_NEW_STUDY_EVENT_SERVLET);
      }
 else       if (!StringUtil.isBlank(submitEnroll)) {
        setUpBeans(classes);
        Date today=new Date(System.currentTimeMillis());
        String todayFormatted=local_df.format(today);
        fp.addPresetValue(INPUT_ENROLLMENT_DATE,todayFormatted);
        String idSetting="String_Node_Str";
        if (currentStudy.getParentStudyId() > 0) {
          parentSPV=spvdao.findByHandleAndStudy(parentStudyId,"String_Node_Str");
          currentStudy.getStudyParameterConfig().setSubjectIdGeneration(parentSPV.getValue());
        }
        idSetting=currentStudy.getStudyParameterConfig().getSubjectIdGeneration();
        logger.info("String_Node_Str" + idSetting);
        if (idSetting.equals("String_Node_Str") || idSetting.equals("String_Node_Str")) {
          fp.addPresetValue(INPUT_LABEL,AUTO_LABEL);
        }
        setPresetValues(fp.getPresetValues());
        discNotes=new FormDiscrepancyNotes();
        session.setAttribute(FORM_DISCREPANCY_NOTES_NAME,discNotes);
        forwardPage(Page.ADD_NEW_SUBJECT);
      }
 else {
        request.setAttribute("String_Node_Str",studySubject.getId() + "String_Node_Str");
        response.encodeRedirectURL("String_Node_Str" + studySubject.getId());
        forwardPage(Page.VIEW_STUDY_SUBJECT_SERVLET);
        return;
      }
    }
  }
}