// populate values drug order from database (VIEW, EDIT)
if (context.getMode() != Mode.ENTER && context.getExistingOrders() != null) {
for (Drug drug : drugsUsedAsKey) {
if (context.getExistingOrders().containsKey(drug.getConcept())) {
//this will return null if Order is not a DrugOrder even if matched by Concept
DrugOrder drugOrder = (DrugOrder) context.removeExistingDrugOrder(drug);
if (drugOrder != null) {
//start from the first order for that drug
while (drugOrder.getPreviousOrder() != null) {
drugOrder = (DrugOrder) drugOrder.getPreviousOrder();
}
//get the latest revision or discontinuation order
DrugOrder lastRevision = drugOrder;
while (true) {
DrugOrder revisedOrder = (DrugOrder) Context.getOrderService().getRevisionOrder(drugOrder);
if (revisedOrder != null) {
drugOrder = revisedOrder;
lastRevision = revisedOrder;
continue;
}
DrugOrder discontinuationOrder = (DrugOrder) Context.getOrderService().getDiscontinuationOrder(
drugOrder);
if (discontinuationOrder != null) {
drugOrder = discontinuationOrder;
continue;
}
break;
}
existingOrder = drugOrder;
if (drugWidget instanceof DropdownWidget) {
drugWidget.setInitialValue(drugOrder.getDrug().getDrugId());
} else {
if (((CheckboxWidget) drugWidget).getValue().equals(drugOrder.getDrug().getDrugId().toString()))
((CheckboxWidget) drugWidget).setInitialValue("CHECKED");
}
if (!existingOrder.getAction().equals(Action.DISCONTINUE)) {
lastRevision = drugOrder;
}
startDateWidget.setInitialValue(lastRevision.getDateActivated());
routeWidget.setInitialValue(lastRevision.getRoute().getId());
careSettingWidget.setInitialValue(lastRevision.getCareSetting().getId());
dosingTypeWidget.setInitialValue(lastRevision.getDosingType().toString());
doseWidget.setInitialValue(lastRevision.getDose());
numRefillsWidget.setInitialValue(lastRevision.getNumRefills());
if (lastRevision.getDoseUnits() != null) {
doseUnitsWidget.setInitialValue(lastRevision.getDoseUnits().getId());
}
quantityWidget.setInitialValue(lastRevision.getQuantity());
if (lastRevision.getQuantityUnits() != null) {
quantityUnitsWidget.setInitialValue(lastRevision.getQuantityUnits().getId());
}
durationWidget.setInitialValue(lastRevision.getDuration());
if (lastRevision.getDurationUnits() != null) {
durationUnitsWidget.setInitialValue(lastRevision.getDurationUnits().getId());
}
frequencyWidget.setInitialValue(lastRevision.getFrequency().getConcept().getId());
if (!usingDurationField) {
discontinuedDateWidget.setInitialValue(drugOrder.getDateStopped());
Order discontinuationOrder = Context.getOrderService().getDiscontinuationOrder(drugOrder);
if (discontinuedReasonWidget != null && discontinuationOrder != null)
discontinuedReasonWidget.setInitialValue(discontinuationOrder.getOrderReason()
.getConceptId());
}
break;
}