Search in sources :

Example 91 with Order

use of org.openmrs.Order in project openmrs-core by openmrs.

the class OrderServiceImpl method setProperty.

private void setProperty(Order order, String propertyName, Object value) {
    Boolean isAccessible = null;
    Field field = null;
    try {
        field = Order.class.getDeclaredField(propertyName);
        field.setAccessible(true);
        field.set(order, value);
    } catch (Exception e) {
        throw new APIException("Order.failed.set.property", new Object[] { propertyName, order }, e);
    } finally {
        if (field != null && isAccessible != null) {
            field.setAccessible(isAccessible);
        }
    }
}
Also used : DrugOrder(org.openmrs.DrugOrder) TestOrder(org.openmrs.TestOrder) Order(org.openmrs.Order) Field(java.lang.reflect.Field) APIException(org.openmrs.api.APIException) CannotStopDiscontinuationOrderException(org.openmrs.api.CannotStopDiscontinuationOrderException) MissingRequiredPropertyException(org.openmrs.api.MissingRequiredPropertyException) CannotDeleteObjectInUseException(org.openmrs.api.CannotDeleteObjectInUseException) CannotUpdateObjectInUseException(org.openmrs.api.CannotUpdateObjectInUseException) OrderEntryException(org.openmrs.api.OrderEntryException) APIException(org.openmrs.api.APIException) EditedOrderDoesNotMatchPreviousException(org.openmrs.api.EditedOrderDoesNotMatchPreviousException) AmbiguousOrderException(org.openmrs.api.AmbiguousOrderException) CannotUnvoidOrderException(org.openmrs.api.CannotUnvoidOrderException) CannotStopInactiveOrderException(org.openmrs.api.CannotStopInactiveOrderException) UnchangeableObjectException(org.openmrs.api.UnchangeableObjectException)

Example 92 with Order

use of org.openmrs.Order 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 93 with Order

use of org.openmrs.Order in project openmrs-core by openmrs.

the class EncounterServiceImpl method unvoidEncounter.

/**
 * @see org.openmrs.api.EncounterService#unvoidEncounter(org.openmrs.Encounter)
 */
@Override
public Encounter unvoidEncounter(Encounter encounter) throws APIException {
    // if authenticated user is not supposed to edit encounter of certain type
    if (!canEditEncounter(encounter, null)) {
        throw new APIException("Encounter.error.privilege.required.unvoid", new Object[] { encounter.getEncounterType().getEditPrivilege() });
    }
    String voidReason = encounter.getVoidReason();
    if (voidReason == null) {
        voidReason = "";
    }
    ObsService os = Context.getObsService();
    for (Obs o : encounter.getObsAtTopLevel(true)) {
        if (voidReason.equals(o.getVoidReason())) {
            os.unvoidObs(o);
        }
    }
    OrderService orderService = Context.getOrderService();
    for (Order o : encounter.getOrders()) {
        if (voidReason.equals(o.getVoidReason())) {
            orderService.unvoidOrder(o);
        }
    }
    encounter.setVoided(false);
    encounter.setVoidedBy(null);
    encounter.setDateVoided(null);
    encounter.setVoidReason(null);
    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)

Example 94 with Order

use of org.openmrs.Order in project openmrs-core by openmrs.

the class EncounterServiceImpl method purgeEncounter.

/**
 * @see org.openmrs.api.EncounterService#purgeEncounter(Encounter, boolean)
 */
@Override
public void purgeEncounter(Encounter encounter, boolean cascade) throws APIException {
    // if authenticated user is not supposed to edit encounter of certain type
    if (!canEditEncounter(encounter, null)) {
        throw new APIException("Encounter.error.privilege.required.purge", new Object[] { encounter.getEncounterType().getEditPrivilege() });
    }
    if (cascade) {
        ObsService obsService = Context.getObsService();
        List<Encounter> justThisEncounter = new ArrayList<>();
        justThisEncounter.add(encounter);
        List<Obs> observations = new ArrayList<>(obsService.getObservations(null, justThisEncounter, null, null, null, null, null, null, null, null, null, true));
        for (Obs o : observations) {
            obsService.purgeObs(o);
        }
        Set<Order> orders = encounter.getOrders();
        for (Order o : orders) {
            Context.getOrderService().purgeOrder(o);
        }
    }
    Context.getEncounterService().purgeEncounter(encounter);
}
Also used : Order(org.openmrs.Order) Obs(org.openmrs.Obs) APIException(org.openmrs.api.APIException) ArrayList(java.util.ArrayList) Encounter(org.openmrs.Encounter) ObsService(org.openmrs.api.ObsService)

Example 95 with Order

use of org.openmrs.Order in project openmrs-core by openmrs.

the class EncounterServiceImpl method saveEncounter.

/**
 * @see org.openmrs.api.EncounterService#saveEncounter(org.openmrs.Encounter)
 */
@Override
public Encounter saveEncounter(Encounter encounter) throws APIException {
    // if authenticated user is not supposed to edit encounter of certain type
    failIfDeniedToEdit(encounter);
    // If new encounter, try to assign a visit using the registered visit assignment handler.
    createVisitForNewEncounter(encounter);
    // check permissions
    boolean isNewEncounter = requirePrivilege(encounter);
    // This must be done after setting dateCreated etc on the obs because
    // of the way the ORM tools flush things and check for nullity
    // This also must be done before the save encounter so we can use the
    // orig date
    // after the save
    Patient p = encounter.getPatient();
    Date originalDate;
    Location originalLocation = null;
    if (!isNewEncounter) {
        // fetch the datetime from the database prior to saving for this
        // encounter
        // to see if it has changed and change all obs after saving if so
        originalDate = dao.getSavedEncounterDatetime(encounter);
        if (encounter.getLocation() != null) {
            originalLocation = dao.getSavedEncounterLocation(encounter);
        }
        // Our data model duplicates the patient column to allow for
        // observations to
        // not have to look up the parent Encounter to find the patient
        // Therefore, encounter.patient must always equal
        // encounter.observations[0-n].patient
        // If we are changing encounter.encounterDatetime, then we need to
        // also apply that
        // to Obs that inherited their obsDatetime from the encounter in the
        // first place
        Date newDate = encounter.getEncounterDatetime();
        Location newLocation = encounter.getLocation();
        for (Obs obs : encounter.getAllObs(true)) {
            // if the date was changed
            if (OpenmrsUtil.compare(originalDate, newDate) != 0 && OpenmrsUtil.compare(obs.getObsDatetime(), originalDate) == 0) {
                // if the obs datetime is the same as the
                // original encounter datetime, fix it
                obs.setObsDatetime(newDate);
            }
            if (!OpenmrsUtil.nullSafeEquals(newLocation, originalLocation) && obs.getLocation().equals(originalLocation)) {
                obs.setLocation(newLocation);
            }
            // encounter, fix it
            if (!obs.getPerson().getPersonId().equals(p.getPatientId())) {
                obs.setPerson(p);
            }
        }
    }
    // same goes for Orders
    for (Order o : encounter.getOrders()) {
        if (!p.equals(o.getPatient())) {
            o.setPatient(p);
        }
    }
    // do the actual saving to the database
    dao.saveEncounter(encounter);
    // save the new orderGroups
    for (OrderGroup orderGroup : encounter.getOrderGroups()) {
        Context.getOrderService().saveOrderGroup(orderGroup);
    }
    // save the new orders which do not have order groups
    for (Order o : encounter.getOrdersWithoutOrderGroups()) {
        if (o.getOrderId() == null) {
            Context.getOrderService().saveOrder(o, null);
        }
    }
    // save the Obs
    String changeMessage = Context.getMessageSourceService().getMessage("Obs.void.reason.default");
    ObsService os = Context.getObsService();
    List<Obs> obsToRemove = new ArrayList<>();
    List<Obs> obsToAdd = new ArrayList<>();
    for (Obs o : encounter.getObsAtTopLevel(true)) {
        if (o.getId() == null) {
            os.saveObs(o, null);
        } else {
            Obs newObs = os.saveObs(o, changeMessage);
            // The logic in saveObs evicts the old obs instance, so we need to update the collection
            // with the newly loaded and voided instance, apparently reloading the encounter
            // didn't do the tick
            obsToRemove.add(o);
            obsToAdd.add(os.getObs(o.getId()));
            obsToAdd.add(newObs);
        }
    }
    removeGivenObsAndTheirGroupMembersFromEncounter(obsToRemove, encounter);
    addGivenObsAndTheirGroupMembersToEncounter(obsToAdd, encounter);
    return encounter;
}
Also used : Order(org.openmrs.Order) Obs(org.openmrs.Obs) OrderGroup(org.openmrs.OrderGroup) ArrayList(java.util.ArrayList) Patient(org.openmrs.Patient) ObsService(org.openmrs.api.ObsService) Date(java.util.Date) Location(org.openmrs.Location)

Aggregations

Order (org.openmrs.Order)155 Test (org.junit.Test)133 BaseContextSensitiveTest (org.openmrs.test.BaseContextSensitiveTest)115 TestOrder (org.openmrs.TestOrder)114 DrugOrder (org.openmrs.DrugOrder)106 OrderUtilTest (org.openmrs.order.OrderUtilTest)97 Matchers.containsInAnyOrder (org.hamcrest.Matchers.containsInAnyOrder)77 Date (java.util.Date)67 Encounter (org.openmrs.Encounter)41 Patient (org.openmrs.Patient)36 BindException (org.springframework.validation.BindException)20 Errors (org.springframework.validation.Errors)20 Concept (org.openmrs.Concept)15 CareSetting (org.openmrs.CareSetting)11 Obs (org.openmrs.Obs)11 SimpleDateFormat (java.text.SimpleDateFormat)10 OrderGroup (org.openmrs.OrderGroup)10 ArrayList (java.util.ArrayList)9 Calendar (java.util.Calendar)9 OrderBuilder (org.openmrs.api.builder.OrderBuilder)9