Search in sources :

Example 16 with APIException

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

the class PatientServiceImpl method processDeath.

/**
 * This is the way to establish that a patient has died. In addition to exiting the patient from
 * care (see above), this method will also set the appropriate patient characteristics to
 * indicate that they have died, when they died, etc.
 *
 * @param patient - the patient who has died
 * @param dateDied - the declared date/time of the patient's death
 * @param causeOfDeath - the concept that corresponds with the reason the patient died
 * @param otherReason - in case the causeOfDeath is 'other', a place to store more info
 * @throws APIException
 */
@Override
public void processDeath(Patient patient, Date dateDied, Concept causeOfDeath, String otherReason) throws APIException {
    if (patient != null && dateDied != null && causeOfDeath != null) {
        // set appropriate patient characteristics
        patient.setDead(true);
        patient.setDeathDate(dateDied);
        patient.setCauseOfDeath(causeOfDeath);
        this.savePatient(patient);
        saveCauseOfDeathObs(patient, dateDied, causeOfDeath, otherReason);
        // exit from program
        // first, need to get Concept for "Patient Died"
        String strPatientDied = Context.getAdministrationService().getGlobalProperty("concept.patientDied");
        Concept conceptPatientDied = Context.getConceptService().getConcept(strPatientDied);
        if (conceptPatientDied == null) {
            log.debug("ConceptPatientDied is null");
        }
        exitFromCare(patient, dateDied, conceptPatientDied);
    } else {
        if (patient == null) {
            throw new APIException("Patient.invalid.dead", (Object[]) null);
        }
        if (dateDied == null) {
            throw new APIException("Patient.no.valid.dateDied", (Object[]) null);
        }
        if (causeOfDeath == null) {
            throw new APIException("Patient.no.valid.causeOfDeath", (Object[]) null);
        }
    }
}
Also used : Concept(org.openmrs.Concept) APIException(org.openmrs.api.APIException)

Example 17 with APIException

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

the class AdministrationServiceImpl method saveGlobalProperty.

/**
 * @see org.openmrs.api.AdministrationService#saveGlobalProperty(org.openmrs.GlobalProperty)
 */
@Override
@CacheEvict(value = "userSearchLocales", allEntries = true)
public GlobalProperty saveGlobalProperty(GlobalProperty gp) throws APIException {
    // only try to save it if the global property has a key
    if (gp.getProperty() != null && gp.getProperty().length() > 0) {
        if (gp.getProperty().equals(OpenmrsConstants.GLOBAL_PROPERTY_LOCALE_ALLOWED_LIST)) {
            if (gp.getPropertyValue() != null) {
                List<Locale> localeList = new ArrayList<>();
                for (String localeString : gp.getPropertyValue().split(",")) {
                    localeList.add(LocaleUtility.fromSpecification(localeString.trim()));
                }
                if (!localeList.contains(LocaleUtility.getDefaultLocale())) {
                    gp.setPropertyValue(StringUtils.join(getAllowedLocales(), ", "));
                    throw new APIException(Context.getMessageSourceService().getMessage("general.locale.localeListNotIncludingDefaultLocale", new Object[] { LocaleUtility.getDefaultLocale() }, null));
                }
            }
        } else if (gp.getProperty().equals(OpenmrsConstants.GLOBAL_PROPERTY_DEFAULT_LOCALE) && gp.getPropertyValue() != null) {
            List<Locale> localeList = getAllowedLocales();
            if (!localeList.contains(LocaleUtility.fromSpecification(gp.getPropertyValue().trim()))) {
                String value = gp.getPropertyValue();
                gp.setPropertyValue(LocaleUtility.getDefaultLocale().toString());
                throw new APIException((Context.getMessageSourceService().getMessage("general.locale.defaultNotInAllowedLocalesList", new Object[] { value }, null)));
            }
        }
        CustomDatatypeUtil.saveIfDirty(gp);
        dao.saveGlobalProperty(gp);
        notifyGlobalPropertyChange(gp);
        return gp;
    }
    return gp;
}
Also used : Locale(java.util.Locale) APIException(org.openmrs.api.APIException) ArrayList(java.util.ArrayList) OpenmrsObject(org.openmrs.OpenmrsObject) ArrayList(java.util.ArrayList) List(java.util.List) CacheEvict(org.springframework.cache.annotation.CacheEvict)

Example 18 with APIException

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

the class ObsServiceImpl method handleExistingObsWithComplexConcept.

private void handleExistingObsWithComplexConcept(Obs obs) {
    ComplexData complexData = obs.getComplexData();
    Concept concept = obs.getConcept();
    if (null != concept && concept.isComplex() && null != complexData && null != complexData.getData()) {
        // save or update complexData object on this obs
        // this is done before the database save so that the obs.valueComplex
        // can be filled in by the handler.
        ComplexObsHandler handler = getHandler(obs);
        if (null != handler) {
            handler.saveObs(obs);
        } else {
            throw new APIException("unknown.handler", new Object[] { concept });
        }
    }
}
Also used : Concept(org.openmrs.Concept) APIException(org.openmrs.api.APIException) ComplexData(org.openmrs.obs.ComplexData) ComplexObsHandler(org.openmrs.obs.ComplexObsHandler)

Example 19 with APIException

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

the class EncounterServiceImpl method voidEncounter.

/**
 * @see org.openmrs.api.EncounterService#voidEncounter(org.openmrs.Encounter, java.lang.String)
 */
@Override
public Encounter voidEncounter(Encounter encounter, String reason) {
    // if authenticated user is not supposed to edit encounter of certain type
    if (!canEditEncounter(encounter, null)) {
        throw new APIException("Encounter.error.privilege.required.void", new Object[] { encounter.getEncounterType().getEditPrivilege() });
    }
    if (reason == null) {
        throw new IllegalArgumentException("The argument 'reason' is required and so cannot be null");
    }
    ObsService os = Context.getObsService();
    for (Obs o : encounter.getObsAtTopLevel(false)) {
        if (!o.getVoided()) {
            os.voidObs(o, reason);
        }
    }
    OrderService orderService = Context.getOrderService();
    for (Order o : encounter.getOrders()) {
        if (!o.getVoided()) {
            orderService.voidOrder(o, reason);
        }
    }
    encounter.setVoided(true);
    encounter.setVoidedBy(Context.getAuthenticatedUser());
    // unvoid handler to work
    if (encounter.getDateVoided() == null) {
        encounter.setDateVoided(new Date());
    }
    encounter.setVoidReason(reason);
    Context.getEncounterService().saveEncounter(encounter);
    return encounter;
}
Also used : Order(org.openmrs.Order) Obs(org.openmrs.Obs) APIException(org.openmrs.api.APIException) ObsService(org.openmrs.api.ObsService) OrderService(org.openmrs.api.OrderService) Date(java.util.Date)

Example 20 with APIException

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

the class PersonSaveHandler method handle.

/**
 * @see org.openmrs.api.handler.SaveHandler#handle(org.openmrs.OpenmrsObject, org.openmrs.User,
 *      java.util.Date, java.lang.String)
 */
@Override
public void handle(Person person, User creator, Date dateCreated, String other) {
    // address collection
    if (person.getAddresses() != null && !person.getAddresses().isEmpty()) {
        for (PersonAddress pAddress : person.getAddresses()) {
            if (pAddress.isBlank()) {
                person.removeAddress(pAddress);
                continue;
            }
            pAddress.setPerson(person);
        }
    }
    // name collection
    if (person.getNames() != null && !person.getNames().isEmpty()) {
        for (PersonName pName : person.getNames()) {
            pName.setPerson(person);
        }
    }
    // attribute collection
    if (person.getAttributes() != null && !person.getAttributes().isEmpty()) {
        for (PersonAttribute pAttr : person.getAttributes()) {
            pAttr.setPerson(person);
        }
    }
    // if the patient was marked as dead and reversed, drop the cause of death
    if (!person.getDead() && person.getCauseOfDeath() != null) {
        person.setCauseOfDeath(null);
    }
    // do the checks for voided attributes (also in PersonVoidHandler)
    if (person.getPersonVoided()) {
        if (!StringUtils.hasLength(person.getPersonVoidReason())) {
            throw new APIException("Person.voided.bit", new Object[] { person });
        }
        if (person.getPersonVoidedBy() == null) {
            person.setPersonVoidedBy(creator);
        }
        if (person.getPersonDateVoided() == null) {
            person.setPersonDateVoided(dateCreated);
        }
    } else {
        // voided is set to false
        person.setPersonVoidedBy(null);
        person.setPersonDateVoided(null);
        person.setPersonVoidReason(null);
    }
}
Also used : PersonName(org.openmrs.PersonName) APIException(org.openmrs.api.APIException) PersonAddress(org.openmrs.PersonAddress) PersonAttribute(org.openmrs.PersonAttribute)

Aggregations

APIException (org.openmrs.api.APIException)84 IOException (java.io.IOException)10 ArrayList (java.util.ArrayList)10 Date (java.util.Date)10 File (java.io.File)9 Obs (org.openmrs.Obs)7 List (java.util.List)6 Map (java.util.Map)6 FileInputStream (java.io.FileInputStream)5 FileOutputStream (java.io.FileOutputStream)5 Concept (org.openmrs.Concept)5 OpenmrsObject (org.openmrs.OpenmrsObject)5 User (org.openmrs.User)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