Program: 84579
Project: 84579
Buggy source:
/** 
 * The onSubmit function receives the form/command object that was modified by the input form and saves it to the db
 * @see org.springframework.web.servlet.mvc.SimpleFormController#onSubmit(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse,java.lang.Object,org.springframework.validation.BindException)
 */
protected ModelAndView onSubmit(HttpServletRequest request,HttpServletResponse response,Object obj,BindException errors) throws Exception {
  HttpSession httpSession=request.getSession();
  log.debug("String_Node_Str");
  if (Context.isAuthenticated()) {
    PatientService ps=Context.getPatientService();
    PersonService personService=Context.getPersonService();
    ShortPatientModel shortPatient=(ShortPatientModel)obj;
    String view=getSuccessView();
    boolean isError=false;
    String action=request.getParameter("String_Node_Str");
    MessageSourceAccessor msa=getMessageSourceAccessor();
    if (action != null && action.equals(msa.getMessage("String_Node_Str"))) {
      httpSession.setAttribute(WebConstants.OPENMRS_MSG_ATTR,"String_Node_Str");
      return new ModelAndView(new RedirectView("String_Node_Str"));
    }
    Patient patient=null;
    if (shortPatient.getPatientId() != null) {
      patient=ps.getPatient(shortPatient.getPatientId());
      if (patient == null) {
        try {
          Person p=personService.getPerson(shortPatient.getPatientId());
          patient=new Patient(p);
        }
 catch (        ObjectRetrievalFailureException noUserEx) {
        }
      }
    }
    if (patient == null)     patient=new Patient();
    boolean duplicate=false;
    PersonName newName=shortPatient.getName();
    if (log.isDebugEnabled())     log.debug("String_Node_Str" + newName.toString());
    for (    PersonName pn : patient.getNames()) {
      if (((pn.getGivenName() == null && newName.getGivenName() == null) || OpenmrsUtil.nullSafeEquals(pn.getGivenName(),newName.getGivenName())) && ((pn.getMiddleName() == null && newName.getMiddleName() == null) || OpenmrsUtil.nullSafeEquals(pn.getMiddleName(),newName.getMiddleName())) && ((pn.getFamilyName() == null && newName.getFamilyName() == null) || OpenmrsUtil.nullSafeEquals(pn.getFamilyName(),newName.getFamilyName())))       duplicate=true;
    }
    if (!duplicate) {
      if (patient.getPersonName() != null)       patient.getPersonName().setPreferred(false);
      newName.setPersonNameId(null);
      newName.setPreferred(true);
      patient.addName(newName);
    }
    if (log.isDebugEnabled())     log.debug("String_Node_Str" + shortPatient.getAddress());
    if (shortPatient.getAddress() != null && !shortPatient.getAddress().isBlank()) {
      duplicate=false;
      for (      PersonAddress pa : patient.getAddresses()) {
        if (pa.toString().equals(shortPatient.getAddress().toString()))         duplicate=true;
      }
      if (log.isDebugEnabled())       log.debug("String_Node_Str" + duplicate);
      if (!duplicate) {
        PersonAddress newAddress=shortPatient.getAddress();
        newAddress.setPersonAddressId(null);
        newAddress.setPreferred(true);
        patient.addAddress(newAddress);
      }
    }
    if (log.isDebugEnabled())     log.debug("String_Node_Str" + patient.getAddresses());
    if (patient.getIdentifiers() == null)     patient.setIdentifiers(new TreeSet<PatientIdentifier>());
    for (    PatientIdentifier pi : patient.getIdentifiers()) {
      pi.setPreferred(pref.equals(pi.getIdentifier() + pi.getIdentifierType().getPatientIdentifierTypeId()));
    }
    for (    PersonAttributeType type : personService.getPersonAttributeTypes(PERSON_TYPE.PATIENT,ATTR_VIEW_TYPE.VIEWING)) {
      String value=request.getParameter(type.getPersonAttributeTypeId().toString());
      patient.addAttribute(new PersonAttribute(type,value));
    }
    for (    PatientIdentifier identifier : newIdentifiers) {
      identifier.setPatient(patient);
      for (      PatientIdentifier currentIdentifier : patient.getActiveIdentifiers()) {
        if (currentIdentifier.equals(identifier)) {
          patient.removeIdentifier(currentIdentifier);
          Context.evictFromSession(currentIdentifier);
        }
      }
    }
    patient.addIdentifiers(newIdentifiers);
    List<PatientIdentifier> newIdentifiersList=new Vector<PatientIdentifier>();
    newIdentifiersList.addAll(newIdentifiers);
    for (    PatientIdentifier identifier : patient.getIdentifiers()) {
      if (!newIdentifiersList.contains(identifier)) {
        identifier.setVoided(true);
      }
    }
    patient.setBirthdate(shortPatient.getBirthdate());
    patient.setBirthdateEstimated(shortPatient.getBirthdateEstimated());
    patient.setGender(shortPatient.getGender());
    if (shortPatient.getTribe() == "String_Node_Str" || shortPatient.getTribe() == null)     patient.setTribe(null);
 else {
      Tribe t=ps.getTribe(Integer.valueOf(shortPatient.getTribe()));
      patient.setTribe(t);
    }
    patient.setDead(shortPatient.getDead());
    if (patient.isDead()) {
      patient.setDeathDate(shortPatient.getDeathDate());
      patient.setCauseOfDeath(shortPatient.getCauseOfDeath());
    }
 else {
      patient.setDeathDate(null);
      patient.setCauseOfDeath(null);
    }
    Patient newPatient=null;
    try {
      newPatient=ps.savePatient(patient);
    }
 catch (    InvalidIdentifierFormatException iife) {
      log.error(iife);
      patient.removeIdentifier(iife.getPatientIdentifier());
      httpSession.setAttribute(WebConstants.OPENMRS_ERROR_ATTR,"String_Node_Str");
      isError=true;
    }
catch (    InvalidCheckDigitException icde) {
      log.error(icde);
      patient.removeIdentifier(icde.getPatientIdentifier());
      httpSession.setAttribute(WebConstants.OPENMRS_ERROR_ATTR,"String_Node_Str");
      isError=true;
    }
catch (    IdentifierNotUniqueException inue) {
      log.error(inue);
      patient.removeIdentifier(inue.getPatientIdentifier());
      httpSession.setAttribute(WebConstants.OPENMRS_ERROR_ATTR,"String_Node_Str");
      isError=true;
    }
catch (    DuplicateIdentifierException die) {
      log.error(die);
      patient.removeIdentifier(die.getPatientIdentifier());
      httpSession.setAttribute(WebConstants.OPENMRS_ERROR_ATTR,"String_Node_Str");
      isError=true;
    }
catch (    InsufficientIdentifiersException iie) {
      log.error(iie);
      patient.removeIdentifier(iie.getPatientIdentifier());
      httpSession.setAttribute(WebConstants.OPENMRS_ERROR_ATTR,"String_Node_Str");
      isError=true;
    }
catch (    PatientIdentifierException pie) {
      log.error(pie);
      patient.removeIdentifier(pie.getPatientIdentifier());
      httpSession.setAttribute(WebConstants.OPENMRS_ERROR_ATTR,"String_Node_Str");
      isError=true;
    }
    if (!isError) {
      String[] personAs=request.getParameterValues("String_Node_Str");
      String[] types=request.getParameterValues("String_Node_Str");
      Person person=personService.getPerson(patient.getPatientId());
      List<Relationship> relationships;
      List<Person> newPersonAs=new Vector<Person>();
      if (person != null)       relationships=personService.getRelationshipsByPerson(person);
 else       relationships=new Vector<Relationship>();
      if (personAs != null) {
        for (int x=0; x < personAs.length; x++) {
          String personAString=personAs[x];
          String typeString=types[x];
          if (personAString != null && personAString.length() > 0 && typeString != null && typeString.length() > 0) {
            Person personA=personService.getPerson(Integer.valueOf(personAString));
            RelationshipType type=personService.getRelationshipType(Integer.valueOf(typeString));
            newPersonAs.add(personA);
            boolean found=false;
            for (            Relationship rel : relationships) {
              if (rel.getPersonA().equals(person))               found=true;
              if (rel.getPersonA().equals(personA)) {
                rel.setRelationshipType(type);
                found=true;
              }
            }
            if (!found) {
              Relationship r=new Relationship(personA,person,type);
              relationships.add(r);
            }
          }
        }
      }
      for (      Relationship rel : relationships) {
        if (newPersonAs.contains(rel.getPersonA()) || person.equals(rel.getPersonA()))         personService.saveRelationship(rel);
 else         personService.purgeRelationship(rel);
      }
      if (patient.getDead()) {
        log.debug("String_Node_Str");
        String codProp=Context.getAdministrationService().getGlobalProperty("String_Node_Str");
        Concept causeOfDeath=Context.getConceptService().getConcept(codProp);
        if (causeOfDeath != null) {
          List<Obs> obssDeath=Context.getObsService().getObservationsByPersonAndConcept(patient,causeOfDeath);
          if (obssDeath != null) {
            if (obssDeath.size() > 1) {
              log.error("String_Node_Str" + obssDeath.size() + "String_Node_Str");
            }
 else {
              Obs obsDeath=null;
              if (obssDeath.size() == 1) {
                log.debug("String_Node_Str");
                obsDeath=obssDeath.iterator().next();
              }
 else {
                log.debug("String_Node_Str");
                obsDeath=new Obs();
                obsDeath.setPerson(patient);
                obsDeath.setConcept(causeOfDeath);
                Location loc=Context.getLocationService().getLocation("String_Node_Str");
                if (loc == null)                 loc=Context.getLocationService().getLocation(new Integer(1));
                if (loc != null)                 obsDeath.setLocation(loc);
 else                 log.error("String_Node_Str");
              }
              Concept currCause=patient.getCauseOfDeath();
              if (currCause == null) {
                log.debug("String_Node_Str");
                String noneConcept=Context.getAdministrationService().getGlobalProperty("String_Node_Str");
                currCause=Context.getConceptService().getConcept(noneConcept);
              }
              if (currCause != null) {
                log.debug("String_Node_Str");
                obsDeath.setValueCoded(currCause);
                Date dateDeath=patient.getDeathDate();
                if (dateDeath == null)                 dateDeath=new Date();
                obsDeath.setObsDatetime(dateDeath);
                String otherConcept=Context.getAdministrationService().getGlobalProperty("String_Node_Str");
                Concept conceptOther=Context.getConceptService().getConcept(otherConcept);
                if (conceptOther != null) {
                  if (conceptOther.equals(currCause)) {
                    String otherInfo=ServletRequestUtils.getStringParameter(request,"String_Node_Str","String_Node_Str");
                    log.debug("String_Node_Str" + otherInfo);
                    obsDeath.setValueText(otherInfo);
                  }
 else {
                    log.debug("String_Node_Str");
                    obsDeath.setValueText("String_Node_Str");
                  }
                }
 else {
                  log.debug("String_Node_Str");
                  obsDeath.setValueText("String_Node_Str");
                }
                Context.getObsService().saveObs(obsDeath,null);
              }
 else {
                log.debug("String_Node_Str");
              }
            }
          }
        }
 else {
          log.debug("String_Node_Str");
        }
      }
    }
    if (isError) {
      log.error("String_Node_Str" + this.getFormView());
      Map<String,Object> model=new HashMap<String,Object>();
      model.put(getCommandName(),new ShortPatientModel(patient));
      return this.showForm(request,response,errors,model);
    }
 else {
      httpSession.setAttribute(WebConstants.OPENMRS_MSG_ATTR,"String_Node_Str");
      return new ModelAndView(new RedirectView(view + "String_Node_Str" + newPatient.getPatientId()));
    }
  }
 else {
    return new ModelAndView(new RedirectView(getFormView()));
  }
}

Fixed version (ground truth):
/** 
 * The onSubmit function receives the form/command object that was modified by the input form and saves it to the db
 * @see org.springframework.web.servlet.mvc.SimpleFormController#onSubmit(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse,java.lang.Object,org.springframework.validation.BindException)
 */
protected ModelAndView onSubmit(HttpServletRequest request,HttpServletResponse response,Object obj,BindException errors) throws Exception {
  HttpSession httpSession=request.getSession();
  log.debug("String_Node_Str");
  if (Context.isAuthenticated()) {
    PatientService ps=Context.getPatientService();
    PersonService personService=Context.getPersonService();
    ShortPatientModel shortPatient=(ShortPatientModel)obj;
    String view=getSuccessView();
    boolean isError=false;
    String action=request.getParameter("String_Node_Str");
    MessageSourceAccessor msa=getMessageSourceAccessor();
    if (action != null && action.equals(msa.getMessage("String_Node_Str"))) {
      httpSession.setAttribute(WebConstants.OPENMRS_MSG_ATTR,"String_Node_Str");
      return new ModelAndView(new RedirectView("String_Node_Str"));
    }
    Patient patient=null;
    if (shortPatient.getPatientId() != null) {
      patient=ps.getPatient(shortPatient.getPatientId());
      if (patient == null) {
        try {
          Person p=personService.getPerson(shortPatient.getPatientId());
          patient=new Patient(p);
        }
 catch (        ObjectRetrievalFailureException noUserEx) {
        }
      }
    }
    if (patient == null)     patient=new Patient();
    boolean duplicate=false;
    PersonName newName=shortPatient.getName();
    if (log.isDebugEnabled())     log.debug("String_Node_Str" + newName.toString());
    for (    PersonName pn : patient.getNames()) {
      if (((pn.getGivenName() == null && newName.getGivenName() == null) || OpenmrsUtil.nullSafeEquals(pn.getGivenName(),newName.getGivenName())) && ((pn.getMiddleName() == null && newName.getMiddleName() == null) || OpenmrsUtil.nullSafeEquals(pn.getMiddleName(),newName.getMiddleName())) && ((pn.getFamilyName() == null && newName.getFamilyName() == null) || OpenmrsUtil.nullSafeEquals(pn.getFamilyName(),newName.getFamilyName())))       duplicate=true;
    }
    if (!duplicate) {
      if (patient.getPersonName() != null)       patient.getPersonName().setPreferred(false);
      newName.setPersonNameId(null);
      newName.setPreferred(true);
      patient.addName(newName);
    }
    if (log.isDebugEnabled())     log.debug("String_Node_Str" + shortPatient.getAddress());
    if (shortPatient.getAddress() != null && !shortPatient.getAddress().isBlank()) {
      duplicate=false;
      for (      PersonAddress pa : patient.getAddresses()) {
        if (pa.toString().equals(shortPatient.getAddress().toString()))         duplicate=true;
      }
      if (log.isDebugEnabled())       log.debug("String_Node_Str" + duplicate);
      if (!duplicate) {
        PersonAddress newAddress=shortPatient.getAddress();
        newAddress.setPersonAddressId(null);
        newAddress.setPreferred(true);
        patient.addAddress(newAddress);
      }
    }
    if (log.isDebugEnabled())     log.debug("String_Node_Str" + patient.getAddresses());
    if (patient.getIdentifiers() == null)     patient.setIdentifiers(new TreeSet<PatientIdentifier>());
    for (    PatientIdentifier pi : patient.getIdentifiers()) {
      pi.setPreferred(pref.equals(pi.getIdentifier() + pi.getIdentifierType().getPatientIdentifierTypeId()));
    }
    for (    PersonAttributeType type : personService.getPersonAttributeTypes(PERSON_TYPE.PATIENT,ATTR_VIEW_TYPE.VIEWING)) {
      String value=request.getParameter(type.getPersonAttributeTypeId().toString());
      patient.addAttribute(new PersonAttribute(type,value));
    }
    for (    PatientIdentifier identifier : newIdentifiers) {
      identifier.setPatient(patient);
      for (      PatientIdentifier currentIdentifier : patient.getActiveIdentifiers()) {
        if (currentIdentifier.equals(identifier)) {
          patient.removeIdentifier(currentIdentifier);
          Context.evictFromSession(currentIdentifier);
        }
      }
    }
    patient.addIdentifiers(newIdentifiers);
    List<PatientIdentifier> newIdentifiersList=new Vector<PatientIdentifier>();
    newIdentifiersList.addAll(newIdentifiers);
    for (    PatientIdentifier identifier : patient.getIdentifiers()) {
      if (!newIdentifiersList.contains(identifier)) {
        identifier.setVoided(true);
      }
    }
    patient.setBirthdate(shortPatient.getBirthdate());
    patient.setBirthdateEstimated(shortPatient.getBirthdateEstimated());
    patient.setGender(shortPatient.getGender());
    if (shortPatient.getTribe() == "String_Node_Str" || shortPatient.getTribe() == null)     patient.setTribe(null);
 else {
      Tribe t=ps.getTribe(Integer.valueOf(shortPatient.getTribe()));
      patient.setTribe(t);
    }
    patient.setDead(shortPatient.getDead());
    if (patient.isDead()) {
      patient.setDeathDate(shortPatient.getDeathDate());
      patient.setCauseOfDeath(shortPatient.getCauseOfDeath());
    }
 else {
      patient.setDeathDate(null);
      patient.setCauseOfDeath(null);
    }
    Patient newPatient=null;
    try {
      newPatient=ps.savePatient(patient);
    }
 catch (    InvalidIdentifierFormatException iife) {
      log.error(iife);
      patient.removeIdentifier(iife.getPatientIdentifier());
      httpSession.setAttribute(WebConstants.OPENMRS_ERROR_ATTR,"String_Node_Str");
      isError=true;
    }
catch (    InvalidCheckDigitException icde) {
      log.error(icde);
      patient.removeIdentifier(icde.getPatientIdentifier());
      httpSession.setAttribute(WebConstants.OPENMRS_ERROR_ATTR,"String_Node_Str");
      isError=true;
    }
catch (    IdentifierNotUniqueException inue) {
      log.error(inue);
      patient.removeIdentifier(inue.getPatientIdentifier());
      httpSession.setAttribute(WebConstants.OPENMRS_ERROR_ATTR,"String_Node_Str");
      isError=true;
    }
catch (    DuplicateIdentifierException die) {
      log.error(die);
      patient.removeIdentifier(die.getPatientIdentifier());
      httpSession.setAttribute(WebConstants.OPENMRS_ERROR_ATTR,"String_Node_Str");
      isError=true;
    }
catch (    InsufficientIdentifiersException iie) {
      log.error(iie);
      patient.removeIdentifier(iie.getPatientIdentifier());
      httpSession.setAttribute(WebConstants.OPENMRS_ERROR_ATTR,"String_Node_Str");
      isError=true;
    }
catch (    PatientIdentifierException pie) {
      log.error(pie);
      patient.removeIdentifier(pie.getPatientIdentifier());
      httpSession.setAttribute(WebConstants.OPENMRS_ERROR_ATTR,"String_Node_Str");
      isError=true;
    }
    if (!isError) {
      String[] personAs=request.getParameterValues("String_Node_Str");
      String[] types=request.getParameterValues("String_Node_Str");
      Person person=personService.getPerson(patient.getPatientId());
      List<Relationship> relationships;
      List<Person> newPersonAs=new Vector<Person>();
      if (person != null)       relationships=personService.getRelationshipsByPerson(person);
 else       relationships=new Vector<Relationship>();
      if (personAs != null) {
        for (int x=0; x < personAs.length; x++) {
          String personAString=personAs[x];
          String typeString=types[x];
          if (personAString != null && personAString.length() > 0 && typeString != null && typeString.length() > 0) {
            Person personA=personService.getPerson(Integer.valueOf(personAString));
            RelationshipType type=personService.getRelationshipType(Integer.valueOf(typeString));
            newPersonAs.add(personA);
            boolean found=false;
            for (            Relationship rel : relationships) {
              if (rel.getPersonA().equals(person))               found=true;
              if (rel.getPersonA().equals(personA)) {
                rel.setRelationshipType(type);
                found=true;
              }
            }
            if (!found) {
              Relationship r=new Relationship(personA,person,type);
              relationships.add(r);
            }
          }
        }
      }
      for (      Relationship rel : relationships) {
        if (newPersonAs.contains(rel.getPersonA()) || person.equals(rel.getPersonA()))         personService.saveRelationship(rel);
 else         personService.purgeRelationship(rel);
      }
      if (patient.getDead()) {
        log.debug("String_Node_Str");
        String codProp=Context.getAdministrationService().getGlobalProperty("String_Node_Str");
        Concept causeOfDeath=Context.getConceptService().getConcept(codProp);
        if (causeOfDeath != null) {
          List<Obs> obssDeath=Context.getObsService().getObservationsByPersonAndConcept(patient,causeOfDeath);
          if (obssDeath != null) {
            if (obssDeath.size() > 1) {
              log.error("String_Node_Str" + obssDeath.size() + "String_Node_Str");
            }
 else {
              Obs obsDeath=null;
              if (obssDeath.size() == 1) {
                log.debug("String_Node_Str");
                obsDeath=obssDeath.iterator().next();
              }
 else {
                log.debug("String_Node_Str");
                obsDeath=new Obs();
                obsDeath.setPerson(patient);
                obsDeath.setConcept(causeOfDeath);
                Location loc=Context.getLocationService().getLocation("String_Node_Str");
                if (loc == null)                 loc=Context.getLocationService().getLocation(new Integer(1));
                if (loc != null)                 obsDeath.setLocation(loc);
 else                 log.error("String_Node_Str");
              }
              Concept currCause=patient.getCauseOfDeath();
              if (currCause == null) {
                log.debug("String_Node_Str");
                String noneConcept=Context.getAdministrationService().getGlobalProperty("String_Node_Str");
                currCause=Context.getConceptService().getConcept(noneConcept);
              }
              if (currCause != null) {
                log.debug("String_Node_Str");
                obsDeath.setValueCoded(currCause);
                Date dateDeath=patient.getDeathDate();
                if (dateDeath == null)                 dateDeath=new Date();
                obsDeath.setObsDatetime(dateDeath);
                String otherConcept=Context.getAdministrationService().getGlobalProperty("String_Node_Str");
                Concept conceptOther=Context.getConceptService().getConcept(otherConcept);
                if (conceptOther != null) {
                  if (conceptOther.equals(currCause)) {
                    String otherInfo=ServletRequestUtils.getStringParameter(request,"String_Node_Str","String_Node_Str");
                    log.debug("String_Node_Str" + otherInfo);
                    obsDeath.setValueText(otherInfo);
                  }
 else {
                    log.debug("String_Node_Str");
                    obsDeath.setValueText("String_Node_Str");
                  }
                }
 else {
                  log.debug("String_Node_Str");
                  obsDeath.setValueText("String_Node_Str");
                }
                Context.getObsService().saveObs(obsDeath,null);
              }
 else {
                log.debug("String_Node_Str");
              }
            }
          }
        }
 else {
          log.debug("String_Node_Str");
        }
      }
    }
    if (isError || errors.hasErrors()) {
      log.error("String_Node_Str" + this.getFormView());
      Map<String,Object> model=new HashMap<String,Object>();
      model.put(getCommandName(),new ShortPatientModel(patient));
      return this.showForm(request,response,errors,model);
    }
 else {
      httpSession.setAttribute(WebConstants.OPENMRS_MSG_ATTR,"String_Node_Str");
      return new ModelAndView(new RedirectView(view + "String_Node_Str" + newPatient.getPatientId()));
    }
  }
 else {
    return new ModelAndView(new RedirectView(getFormView()));
  }
}