Search in sources :

Example 46 with APIException

use of org.openmrs.api.APIException in project openmrs-core by openmrs.

the class PatientServiceImpl method retirePatientIdentifierType.

/**
 * @see org.openmrs.api.PatientService#retirePatientIdentifierType(org.openmrs.PatientIdentifierType,
 *      String)
 */
@Override
public PatientIdentifierType retirePatientIdentifierType(PatientIdentifierType patientIdentifierType, String reason) throws APIException {
    checkIfPatientIdentifierTypesAreLocked();
    if (reason == null || reason.length() < 1) {
        throw new APIException("Patient.identifier.retire.reason", (Object[]) null);
    }
    patientIdentifierType.setRetired(true);
    patientIdentifierType.setRetiredBy(Context.getAuthenticatedUser());
    patientIdentifierType.setDateRetired(new Date());
    patientIdentifierType.setRetireReason(reason);
    return Context.getPatientService().savePatientIdentifierType(patientIdentifierType);
}
Also used : APIException(org.openmrs.api.APIException) Date(java.util.Date)

Example 47 with APIException

use of org.openmrs.api.APIException in project openmrs-core by openmrs.

the class PatientServiceImpl method saveReasonForExitObs.

/**
 * TODO: Patients should actually be allowed to exit multiple times
 *
 * @param patient
 * @param exitDate
 * @param cause
 */
private void saveReasonForExitObs(Patient patient, Date exitDate, Concept cause) throws APIException {
    if (patient == null) {
        throw new APIException("Patient.null", (Object[]) null);
    }
    if (exitDate == null) {
        throw new APIException("Patient.exit.date.null", (Object[]) null);
    }
    if (cause == null) {
        throw new APIException("Patient.cause.null", (Object[]) null);
    }
    // need to make sure there is an Obs that represents the patient's
    // exit
    log.debug("Patient is exiting, so let's make sure there's an Obs for it");
    String codProp = Context.getAdministrationService().getGlobalProperty("concept.reasonExitedCare");
    Concept reasonForExit = Context.getConceptService().getConcept(codProp);
    if (reasonForExit != null) {
        List<Obs> obssExit = Context.getObsService().getObservationsByPersonAndConcept(patient, reasonForExit);
        if (obssExit != null) {
            if (obssExit.size() > 1) {
                log.error("Multiple reasons for exit (" + obssExit.size() + ")?  Shouldn't be...");
            } else {
                Obs obsExit;
                if (obssExit.size() == 1) {
                    // already has a reason for exit - let's edit it.
                    log.debug("Already has a reason for exit, so changing it");
                    obsExit = obssExit.iterator().next();
                } else {
                    // no reason for exit obs yet, so let's make one
                    log.debug("No reason for exit yet, let's create one.");
                    obsExit = new Obs();
                    obsExit.setPerson(patient);
                    obsExit.setConcept(reasonForExit);
                    Location loc = Context.getLocationService().getDefaultLocation();
                    if (loc != null) {
                        obsExit.setLocation(loc);
                    } else {
                        log.error("Could not find a suitable location for which to create this new Obs");
                    }
                }
                if (obsExit != null) {
                    // put the right concept and (maybe) text in this
                    // obs
                    obsExit.setValueCoded(cause);
                    // ABKTODO: presume current locale?
                    obsExit.setValueCodedName(cause.getName());
                    obsExit.setObsDatetime(exitDate);
                    Context.getObsService().saveObs(obsExit, "updated by PatientService.saveReasonForExit");
                }
            }
        }
    } else {
        log.debug("Reason for exit is null - should not have gotten here without throwing an error on the form.");
    }
}
Also used : Concept(org.openmrs.Concept) Obs(org.openmrs.Obs) APIException(org.openmrs.api.APIException) Location(org.openmrs.Location)

Example 48 with APIException

use of org.openmrs.api.APIException in project openmrs-core by openmrs.

the class PatientServiceImpl method requireNoActiveOrderOfSameType.

private void requireNoActiveOrderOfSameType(Patient patient1, Patient patient2) {
    String messageKey = "Patient.merge.cannotHaveSameTypeActiveOrders";
    List<Order> ordersByPatient1 = Context.getOrderService().getAllOrdersByPatient(patient1);
    List<Order> ordersByPatient2 = Context.getOrderService().getAllOrdersByPatient(patient2);
    ordersByPatient1.forEach((Order order1) -> ordersByPatient2.forEach((Order order2) -> {
        if (order1.isActive() && order2.isActive() && order1.getOrderType().equals(order2.getOrderType())) {
            Object[] parameters = { patient1.getPatientId(), patient2.getPatientId(), order1.getOrderType() };
            String message = Context.getMessageSourceService().getMessage(messageKey, parameters, Context.getLocale());
            log.debug(message);
            throw new APIException(message);
        }
    }));
}
Also used : Order(org.openmrs.Order) APIException(org.openmrs.api.APIException)

Example 49 with APIException

use of org.openmrs.api.APIException in project openmrs-core by openmrs.

the class PatientServiceImpl method mergePatients.

/**
 * 1) Moves object (encounters/obs) pointing to <code>nonPreferred</code> to
 * <code>preferred</code> 2) Copies data (gender/birthdate/names/ids/etc) from
 * <code>nonPreferred</code> to <code>preferred</code> iff the data is missing or null in
 * <code>preferred</code> 3) <code>notPreferred</code> is marked as voided
 *
 * @param preferred
 * @param notPreferred
 * @throws APIException
 * @see org.openmrs.api.PatientService#mergePatients(org.openmrs.Patient, org.openmrs.Patient)
 */
@Override
public void mergePatients(Patient preferred, Patient notPreferred) throws APIException, SerializationException {
    log.debug("Merging patients: (preferred)" + preferred.getPatientId() + ", (notPreferred) " + notPreferred.getPatientId());
    if (preferred.getPatientId().equals(notPreferred.getPatientId())) {
        log.debug("Merge operation cancelled: Cannot merge user" + preferred.getPatientId() + " to self");
        throw new APIException("Patient.merge.cancelled", new Object[] { preferred.getPatientId() });
    }
    requireNoActiveOrderOfSameType(preferred, notPreferred);
    PersonMergeLogData mergedData = new PersonMergeLogData();
    mergeVisits(preferred, notPreferred, mergedData);
    mergeEncounters(preferred, notPreferred, mergedData);
    mergeProgramEnrolments(preferred, notPreferred, mergedData);
    mergeRelationships(preferred, notPreferred, mergedData);
    mergeObservationsNotContainedInEncounters(preferred, notPreferred, mergedData);
    mergeIdentifiers(preferred, notPreferred, mergedData);
    mergeNames(preferred, notPreferred, mergedData);
    mergeAddresses(preferred, notPreferred, mergedData);
    mergePersonAttributes(preferred, notPreferred, mergedData);
    mergeGenderInformation(preferred, notPreferred, mergedData);
    mergeDateOfBirth(preferred, notPreferred, mergedData);
    mergeDateOfDeath(preferred, notPreferred, mergedData);
    // void the non preferred patient
    Context.getPatientService().voidPatient(notPreferred, "Merged with patient #" + preferred.getPatientId());
    // void the person associated with not preferred patient
    Context.getPersonService().voidPerson(notPreferred, "The patient corresponding to this person has been voided and Merged with patient #" + preferred.getPatientId());
    // associate the Users associated with the not preferred person, to the preferred person.
    changeUserAssociations(preferred, notPreferred, mergedData);
    // Save the newly update preferred patient
    // This must be called _after_ voiding the nonPreferred patient so that
    // a "Duplicate Identifier" error doesn't pop up.
    savePatient(preferred);
    // save the person merge log
    PersonMergeLog personMergeLog = new PersonMergeLog();
    personMergeLog.setWinner(preferred);
    personMergeLog.setLoser(notPreferred);
    personMergeLog.setPersonMergeLogData(mergedData);
    Context.getPersonService().savePersonMergeLog(personMergeLog);
}
Also used : APIException(org.openmrs.api.APIException) PersonMergeLog(org.openmrs.person.PersonMergeLog) PersonMergeLogData(org.openmrs.person.PersonMergeLogData)

Example 50 with APIException

use of org.openmrs.api.APIException in project openmrs-core by openmrs.

the class PatientServiceImpl method setAllergies.

/**
 * @see org.openmrs.api.PatientService#setAllergies(org.openmrs.Patient,
 *      org.openmrs.Allergies)
 */
@Override
public Allergies setAllergies(Patient patient, Allergies allergies) {
    // NOTE We neither delete nor edit allergies. We instead void them.
    // Because we shield the API users from this business logic,
    // we end up with the complicated code below. :)
    // get the current allergies as stored in the database
    List<Allergy> dbAllergyList = getAllergies(patient);
    for (Allergy originalAllergy : dbAllergyList) {
        // check if we still have each allergy, else it has just been deleted
        if (allergies.contains(originalAllergy)) {
            // we still have this allergy, check if it has been edited/changed
            Allergy potentiallyEditedAllergy = allergies.getAllergy(originalAllergy.getAllergyId());
            if (!potentiallyEditedAllergy.hasSameValues(originalAllergy)) {
                // allergy has been edited, so void it and create a new one with the current values
                Allergy newAllergy = new Allergy();
                try {
                    // remove the edited allergy from our current list, and void id
                    allergies.remove(potentiallyEditedAllergy);
                    // copy values from edited allergy, and add it to the current list
                    newAllergy.copy(potentiallyEditedAllergy);
                    allergies.add(newAllergy);
                    // we void its original values, as came from the database,
                    // instead the current ones which have just been copied
                    // into the new allergy we have just created above
                    voidAllergy(originalAllergy);
                } catch (Exception ex) {
                    throw new APIException("Failed to copy edited values", ex);
                }
            }
            continue;
        }
        // void the allergy that has been deleted
        voidAllergy(originalAllergy);
    }
    for (Allergy allergy : allergies) {
        if (allergy.getAllergyId() == null && allergy.getAllergen().getCodedAllergen() == null && StringUtils.isNotBlank(allergy.getAllergen().getNonCodedAllergen())) {
            Concept otherNonCoded = Context.getConceptService().getConceptByUuid(Allergen.getOtherNonCodedConceptUuid());
            if (otherNonCoded == null) {
                throw new APIException("Can't find concept with uuid:" + Allergen.getOtherNonCodedConceptUuid());
            }
            allergy.getAllergen().setCodedAllergen(otherNonCoded);
        }
    }
    return dao.saveAllergies(patient, allergies);
}
Also used : Concept(org.openmrs.Concept) Allergy(org.openmrs.Allergy) APIException(org.openmrs.api.APIException) DuplicateIdentifierException(org.openmrs.api.DuplicateIdentifierException) BlankIdentifierException(org.openmrs.api.BlankIdentifierException) PatientIdentifierTypeLockedException(org.openmrs.api.PatientIdentifierTypeLockedException) InsufficientIdentifiersException(org.openmrs.api.InsufficientIdentifiersException) APIException(org.openmrs.api.APIException) MissingRequiredIdentifierException(org.openmrs.api.MissingRequiredIdentifierException) SerializationException(org.openmrs.serialization.SerializationException) PatientIdentifierException(org.openmrs.api.PatientIdentifierException)

Aggregations

APIException (org.openmrs.api.APIException)86 Date (java.util.Date)11 IOException (java.io.IOException)10 ArrayList (java.util.ArrayList)10 File (java.io.File)9 Obs (org.openmrs.Obs)7 List (java.util.List)6 Map (java.util.Map)6 User (org.openmrs.User)6 FileInputStream (java.io.FileInputStream)5 FileOutputStream (java.io.FileOutputStream)5 Concept (org.openmrs.Concept)5 OpenmrsObject (org.openmrs.OpenmrsObject)5 InputStream (java.io.InputStream)4 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)4 Order (org.openmrs.Order)4 FileNotFoundException (java.io.FileNotFoundException)3 OutputStreamWriter (java.io.OutputStreamWriter)3 MessageDigest (java.security.MessageDigest)3 HashMap (java.util.HashMap)3