Skip to content

Commit

Permalink
Change orders by adding a revised order, not editing in place.
Browse files Browse the repository at this point in the history
  • Loading branch information
zestyping committed Aug 6, 2015
1 parent 667fac4 commit caeeb14
Showing 1 changed file with 62 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@
import org.openmrs.module.webservices.rest.web.response.ResponseException;
import org.projectbuendia.openmrs.webservices.rest.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.*;

/**
* Rest API for orders.
Expand Down Expand Up @@ -119,7 +116,6 @@ private SimpleObject getAllInner() throws ResponseException {
return getSimpleObjectWithResults(getAllOrders());
}


@Override
public SimpleObject search(RequestContext context) throws ResponseException {
try {
Expand All @@ -139,15 +135,20 @@ SimpleObject searchInner(Patient patient) throws ResponseException {
orderService.getAllOrdersByPatient(patient));
}

public List<Order> getAllOrders() {
List<Order> orders = new ArrayList<>();
public Collection<Order> getAllOrders() {
Map<String, Order> orders = new HashMap<>();
Set<String> previousOrderUuids = new HashSet<>();
for (Encounter encounter : encounterService.getEncounters(
null, null, null, null, null, null, null, null, null, false)) {
for (Order order : encounter.getOrders()) {
orders.add(order);
orders.put(order.getUuid(), order);
previousOrderUuids.add(order.getPreviousOrder().getUuid());
}
}
return orders;
for (String uuid : previousOrderUuids) {
orders.remove(uuid);
}
return orders.values();
}

public Object create(SimpleObject json, RequestContext context) throws ResponseException {
Expand All @@ -168,6 +169,27 @@ Object createInner(SimpleObject json) throws ResponseException {
return orderToJson(order);
}

Encounter createEncounter(Patient patient) {
Encounter encounter = new Encounter();
encounter.setCreator(CREATOR); // TODO: do this properly from authentication
encounter.setEncounterDatetime(new Date());
encounter.setPatient(patient);
encounter.setLocation(Context.getLocationService().getDefaultLocation());
encounter.setEncounterType(encounterService.getEncounterType("ADULTRETURN"));
encounterService.saveEncounter(encounter);
return encounter;
}

Concept getFreeTextOrderConcept() {
return DbUtil.getConcept(
"Order described in free text instructions",
FREE_TEXT_ORDER_UUID, "N/A", "Misc");
}

Provider getProvider() {
return providerService.getAllProviders(false).get(0); // omit retired
}

/** Creates a new Order and a corresponding Encounter containing it. */
protected Order jsonToOrder(SimpleObject json) {
String patientUuid = (String) json.get("patient_uuid");
Expand All @@ -178,27 +200,27 @@ protected Order jsonToOrder(SimpleObject json) {
if (patient == null) {
throw new ObjectNotFoundException();
}
Encounter encounter = new Encounter();
encounter.setEncounterDatetime(new Date());
encounter.setPatient(patient);
encounter.setLocation(Context.getLocationService().getDefaultLocation());
encounter.setEncounterType(encounterService.getEncounterType("ADULTRETURN"));
encounterService.saveEncounter(encounter);
String instructions = (String) json.get("instructions");
if (instructions == null || instructions.isEmpty()) {
throw new IllegalArgumentException("Required key 'instructions' is missing or empty");
}
Integer startMillis = (Integer) json.get("start");
Date startDate = startMillis == null ? new Date() : new Date(startMillis);
Integer stopMillis = (Integer) json.get("stop");
Date stopDate = stopMillis == null ? null : new Date(stopMillis);

Order order = new Order(); // an excellent band
order.setCreator(CREATOR); // TODO: do this properly from authentication
List<Provider> providers = providerService.getAllProviders(false); // omit retired
order.setOrderer(providers.get(0));
order.setEncounter(createEncounter(patient));
order.setOrderer(getProvider());
order.setOrderType(DbUtil.getMiscOrderType());
order.setCareSetting(orderService.getCareSettingByName("Outpatient"));
order.setConcept(DbUtil.getConcept(
"Order described in free text instructions",
FREE_TEXT_ORDER_UUID, "N/A", "Misc"));
order.setConcept(getFreeTextOrderConcept());
order.setDateCreated(new Date());
order.setPatient(patient);
order.setInstructions((String) json.get("instructions"));
order.setDateActivated(new Date());
order.setEncounter(encounter);
order.setInstructions(instructions);
order.setDateActivated(startDate);
order.setAutoExpireDate(stopDate);
return order;
}

Expand Down Expand Up @@ -235,7 +257,7 @@ public List<Representation> getAvailableRepresentations() {
return Arrays.asList(Representation.DEFAULT);
}

SimpleObject getSimpleObjectWithResults(List<Order> orders) {
SimpleObject getSimpleObjectWithResults(Collection<Order> orders) {
List<SimpleObject> jsonResults = new ArrayList<>();
for (Order order : orders) {
jsonResults.add(orderToJson(order));
Expand Down Expand Up @@ -275,24 +297,28 @@ Object updateInner(String uuid, SimpleObject simpleObject) throws ResponseExcept
throw new ObjectNotFoundException();
}

if (applyEdits(order, simpleObject)) {
orderService.saveOrder(order, null);
Order revisedOrder = reviseOrder(order, simpleObject);
if (revisedOrder != null) {
orderService.saveOrder(revisedOrder, null);
order = revisedOrder;
}
return orderToJson(order);
}

/** Applies edits to an Order. Returns true if any changes were made. */
protected boolean applyEdits(Order order, SimpleObject edits) {
/** Revises an order. Returns null if no changes were made. */
protected Order reviseOrder(Order order, SimpleObject edits) {
Order newOrder = order.cloneForRevision();
boolean changed = false;

for (String key : edits.keySet()) {
Object value = edits.get(key);
switch (key) {
case "stop":
if (value == null) {
order.setAutoExpireDate(null);
newOrder.setAutoExpireDate(null);
changed = true;
} else if (value instanceof Integer) {
order.setAutoExpireDate(new Date((Integer) value));
newOrder.setAutoExpireDate(new Date((Integer) value));
changed = true;
} else {
log.warn("Key '" + key + "' has value of invalid type: " + value);
Expand All @@ -301,7 +327,7 @@ protected boolean applyEdits(Order order, SimpleObject edits) {

case "instructions":
if (value instanceof String) {
order.setInstructions((String) value);
newOrder.setInstructions((String) value);
changed = true;
} else {
log.warn("Key '" + key + "' has value of invalid type: " + value);
Expand All @@ -313,7 +339,11 @@ protected boolean applyEdits(Order order, SimpleObject edits) {
break;
}
}
return changed;

if (!changed) return null;
newOrder.setEncounter(createEncounter(order.getPatient()));
newOrder.setOrderer(getProvider());
return newOrder;
}

/** Serializes an order to JSON. */
Expand Down

0 comments on commit caeeb14

Please sign in to comment.