Package org.openmrs

Examples of org.openmrs.PatientIdentifier


    if (identifierTypeValueWidget != null && identifierTypeWidget != null) {
      String identifier = (String) identifierTypeValueWidget.getValue(context, request);
      PatientIdentifierType identifierType = getIdentifierType((String) identifierTypeWidget.getValue(context, request));

      // Look for an existing identifier of this type
      PatientIdentifier patientIdentifier = patient.getPatientIdentifier(identifierType);

      if (StringUtils.hasText(identifier)) {
        // No existing identifier of this type, so create new
        if (patientIdentifier == null) {
          patientIdentifier = new PatientIdentifier();
          patientIdentifier.setIdentifierType(identifierType);

          // HACK: we need to set the date created  and uuid here as a hack around a hibernate flushing issue (see saving the Patient in FormEntrySession applyActions())
          patientIdentifier.setDateChanged(new Date());
          patientIdentifier.setUuid(UUID.randomUUID().toString());

          // For 1.9+ onwards patients require a preferred identifier
          if (patient.getPatientId() == null) {
            patientIdentifier.setPreferred(true);
          }

          patient.addIdentifier(patientIdentifier);
        }

        if (!identifier.equals(patientIdentifier.getIdentifier()) || !identifierType.equals(patientIdentifier.getIdentifierType())) {
          validateIdentifier(identifierType.getId(), identifier);
        }

        patientIdentifier.setIdentifier(identifier);
      }
      else if (patientIdentifier != null) {
        // If this field is not required, then we interpret a blank value as a request to avoid any existing identifier
        session.getSubmissionActions().getIdentifiersToVoid().add(patientIdentifier);
      }
    }

    //
    // TODO current behavior always updates location of the preferred identifier rather than
    // a specific identifier type being edited by the identifier widget. But identifier location
    // widget isn't aware of the identifier type widget
    //
    if (identifierLocationWidget != null) {
      PatientIdentifier patientIdentifier = patient.getPatientIdentifier();
      if (patientIdentifier == null) {
        patientIdentifier = new PatientIdentifier();
        patient.addIdentifier(patientIdentifier);
      }

      Object locationString = identifierLocationWidget.getValue(context, request);
      Location location = (Location) HtmlFormEntryUtil.convertToType(locationString.toString().trim(), Location.class);
      patientIdentifier.setLocation(location);
      patientIdentifier.setPreferred(true);

    }

    if (addressWidget != null) {
      PersonAddress personAddress = (PersonAddress) addressWidget.getValue(context, request);
View Full Code Here


  }

  private void validateIdentifier(Integer identifierType, String identifier) {
    if (identifierType != null && identifier != null) {
      try {
        PatientIdentifier pi = new PatientIdentifier();

        pi.setIdentifier(identifier);
        pi.setIdentifierType(getIdentifierType(identifierType.toString()));

        // note that this is a bit of a hack; we can't call the PatientIdentifierValidator.validateIdentifier(identifier) method
        // because it (as of 1.8) also tests to make sure that an identifier has a location associated with it; when validating an
        // individual identifier widget, there will be no location because the location is collected in another widget
        PatientIdentifierValidator.validateIdentifier(pi.getIdentifier(), pi.getIdentifierType());
       
        if (Context.getPatientService().isIdentifierInUseByAnotherPatient(pi)) {
          throw new IdentifierNotUniqueException(Context.getMessageSourceService().getMessage(
              "PatientIdentifier.error.notUniqueWithParameter", new Object[] { pi.getIdentifier() },
              Context.getLocale()));
        }
      }
      catch (Exception e) {
        throw new ValidationException(e.getMessage());
View Full Code Here

        // TODO wrap this in a transaction
        if (submissionActions.getPersonsToCreate() != null) {
            for (Person p : submissionActions.getPersonsToCreate()) {
                if (p instanceof Patient) {
                    Patient patient = (Patient) p;
                    PatientIdentifier patientIdentifier = patient.getPatientIdentifier();
                    if (!StringUtils.hasText(patient.getGivenName()) || !StringUtils.hasText(patient.getFamilyName())
                            || !StringUtils.hasText(patient.getGender()) || patient.getBirthdate() == null
                            || patientIdentifier == null || !StringUtils.hasText(patientIdentifier.getIdentifier())
                            || patientIdentifier.getIdentifierType() == null || patientIdentifier.getLocation() == null) {
                        throw new BadFormDesignException(
                                "Please check the design of your form to make sure the following fields are mandatory to create a patient: <br/><b>&lt;personName/&gt;</b>, <b>&lt;birthDateOrAge/&gt;</b>, <b>&lt;gender/&gt;</b>, <b>&lt;identifierType/&gt;</b>, <b>&lt;identifier/&gt;</b>, and <b>&lt;identifierLocation/&gt;</b>");
                    }
                }
                Context.getPersonService().savePerson(p);
            }
        }
        if (submissionActions.getEncountersToCreate() != null) {
            for (Encounter e : submissionActions.getEncountersToCreate()) {
                if (form != null) {
                    e.setForm(form);
                    if (form.getEncounterType() != null)
                        e.setEncounterType(form.getEncounterType());
                }
                Context.getEncounterService().saveEncounter(encounter);
            }
        }

        //deal with relationships
        if (submissionActions.getRelationshipsToCreate() != null) {
            for (Relationship r : submissionActions.getRelationshipsToCreate()) {
                if (log.isDebugEnabled()) {
                    log.debug("creating relationships" + r.getRelationshipType().getDescription());
                }
                Context.getPersonService().saveRelationship(r);
            }
        }

        if (submissionActions.getRelationshipsToVoid() != null) {
            for (Relationship r : submissionActions.getRelationshipsToVoid()) {
                if (log.isDebugEnabled()) {
                    log.debug("voiding relationships" + r.getId());
                }
                Context.getPersonService().voidRelationship(r, "htmlformentry");
            }
        }

        if (submissionActions.getRelationshipsToEdit() != null) {
            for (Relationship r : submissionActions.getRelationshipsToCreate()) {
                if (log.isDebugEnabled()) {
                    log.debug("editing relationships" + r.getId());
                }
                Context.getPersonService().saveRelationship(r);
            }
        }

        // program enrollments are trickier since we need to make sure the patient isn't already enrolled
        // 1. if the patient is already enrolled on the given date, just skip this
        // 2. if the patient is enrolled *after* the given date, shift the existing enrollment to start earlier. (TODO decide if this is right)
        // 3. otherwise just enroll them as requested
        if (submissionActions.getPatientProgramsToCreate() != null) {
            for (PatientProgram toCreate : submissionActions.getPatientProgramsToCreate()) {
                boolean skip = false;
                PatientProgram earliestAfter = null;
                List<PatientProgram> already = Context.getProgramWorkflowService().getPatientPrograms(toCreate.getPatient(),
                        toCreate.getProgram(), null, null, null, null, false);
                for (PatientProgram pp : already) {
                    if (pp.getActive(toCreate.getDateEnrolled())) {
                        skip = true;
                        break;
                    }
                    // if the existing one starts after toCreate
                    if (OpenmrsUtil.compare(pp.getDateEnrolled(), toCreate.getDateEnrolled()) > 0) {
                        if (earliestAfter == null
                                || OpenmrsUtil.compare(pp.getDateEnrolled(), earliestAfter.getDateEnrolled()) < 0) {
                            earliestAfter = pp;
                        }
                    }
                }
                if (skip) {
                    continue;
                }
                if (earliestAfter != null) {
                    // edit this enrollment to move its start date earlier
                    earliestAfter.setDateEnrolled(toCreate.getDateEnrolled());
                    Context.getProgramWorkflowService().savePatientProgram(earliestAfter);
                } else {
                    // just enroll as requested
                    Context.getProgramWorkflowService().savePatientProgram(toCreate);
                }
            }
        }

        //complete any necessary programs
        if (submissionActions.getPatientProgramsToComplete() != null) {
            for (PatientProgram toComplete : submissionActions.getPatientProgramsToComplete()) {
                Context.getProgramWorkflowService().savePatientProgram(toComplete);
            }
        }

        if (submissionActions.getPatientProgramsToUpdate() != null) {
            for (PatientProgram patientProgram : submissionActions.getPatientProgramsToUpdate()) {
                Context.getProgramWorkflowService().savePatientProgram(patientProgram);
            }
        }

        ObsService obsService = Context.getObsService();
       
        if (submissionActions.getObsToVoid() != null) {
            for (Obs o : submissionActions.getObsToVoid()) {
                if (log.isDebugEnabled())
                    log.debug("voiding obs: " + o.getObsId());
                obsService.voidObs(o, "htmlformentry");
                // if o was in a group and it has no obs left, void the group
        voidObsGroupIfAllChildObsVoided(o.getObsGroup());
            }
        }

        // If we're in EDIT mode, we have to save the encounter so that any new obs are created.
        // This feels a bit like a hack, but actually it's a good thing to update the encounter's dateChanged in this case. (PS- turns out there's no dateChanged on encounter up to 1.5.)
        // If there is no encounter (impossible at the time of writing this comment) we save the obs manually
        if (context.getMode() == Mode.EDIT) {
            if (encounter != null) {
                if (voidEncounter) {
                    try {
                        HtmlFormEntryUtil.voidEncounter(encounter, htmlForm, "voided via htmlformentry form submission");
                    } catch (Exception ex) {
                        throw new RuntimeException("Unable to void encounter.", ex);
                    }
                }
                Context.getEncounterService().saveEncounter(encounter);
            } else if (submissionActions.getObsToCreate() != null) {
                // this may not work right due to savehandlers (similar error to HTML-135) but this branch is
                // unreachable until html forms are allowed to edit data without an encounter
                for (Obs o : submissionActions.getObsToCreate())
                    obsService.saveObs(o, null);
            }
        }

        /*
           ObsService obsService = Context.getObsService();
           This should propagate from above
          if (submissionActions.getObsToCreate() != null) {
              for (Obs o : submissionActions.getObsToCreate())
                  Context.getObsService().saveObs(o, null);
          }
          */

    if (submissionActions.getIdentifiersToVoid() != null) {
      for (PatientIdentifier patientIdentifier : submissionActions.getIdentifiersToVoid()) {
        patientIdentifier.setVoided(true);
        patientIdentifier.setVoidedBy(Context.getAuthenticatedUser());
        patientIdentifier.setVoidReason(getForm().getName()); // Use form name as reason
        patientIdentifier.setDateVoided(new Date());
      }
    }

        // save the patient
        // TODO: we are having some issues here when updating a Patient and an Encounter via an HTML form due recently discovered problems with the way
View Full Code Here

    Patient demo = new Patient(12345);
    demo.addName(new PersonName("Demo", "The", "Person"));
    Location l = Context.getLocationService().getAllLocations().iterator().next();
    for (PatientIdentifierType pit : Context.getPatientService().getAllPatientIdentifierTypes()) {
      if (StringUtils.isEmpty(pit.getValidator())) {
        demo.addIdentifier(new PatientIdentifier("Testing" + pit.getName() + "123", pit, l));
      }
    }
    demo.setGender("F");
    demo.setUuid("testing-html-form-entry");
    {
View Full Code Here

      identifier = prefix + (int) (Math.random() * 100000);
      if (service.getPatients(identifier).isEmpty()) {
        identifierInUse = false;
      }
    }
    PatientIdentifier pi = new PatientIdentifier(identifier, service
            .getPatientIdentifierTypeByName("RaxaEMR Identifier Number"), location);
    pi.setPreferred(true);
    Patient patient = new Patient(person);
    patient.addIdentifier(pi);
    return service.savePatient(patient);
  }
View Full Code Here

      identifier = "NEW" + (int) (Math.random() * 100000);
      if (Context.getPatientService().getPatients(identifier).isEmpty()) {
        identifierInUse = false;
      }
    }
    PatientIdentifier pi = new PatientIdentifier(identifier, Context.getPatientService().getPatientIdentifierTypeByName(
        "RaxaEMR Identifier Number"), location);
    pi.setPreferred(true);
    Patient patient = new Patient(person);
    patient.addIdentifier(pi);
    System.out.println(patient.getPatientIdentifier());
    int personId = Context.getPatientService().savePatient(patient).getPersonId();
    return (Context.getPersonService().getPerson(personId));
View Full Code Here

TOP

Related Classes of org.openmrs.PatientIdentifier

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.