drugInventory.setDescription(postFields.get("description").toString());
}
if (postFields.get("drug") != null) {
Drug d = Context.getConceptService().getDrugByUuid(postFields.get("drug").toString());
if (d == null) {
throw new ResponseException(
"Drug uuid not found") {};
}
drugInventory.setDrugId(d.getDrugId());
drugInventory.setDrug(d);
}
if (postFields.get("quantity") != null) {
drugInventory.setQuantity(Integer.parseInt(postFields.get("quantity").toString()));
}
if (postFields.get("originalQuantity") != null) {
drugInventory.setOriginalQuantity(Integer.parseInt(postFields.get("originalQuantity").toString()));
}
if (postFields.get("expiryDate") != null) {
Date date = null;
String[] supportedFormats = { "EEE MMM dd yyyy HH:mm:ss z (zzzz)", "EEE MMM dd yyyy HH:mm:ss z (zzzz)",
"MMM dd, yyyy HH:mm:ss a", "EEE MMM dd yyyy HH:mm:ss", "yyyy-MM-dd'T'HH:mm:ss.SSSZ",
"yyyy-MM-dd'T'HH:mm:ss.SSS", "yyyy-MM-dd'T'HH:mm:ssZ", "yyyy-MM-dd'T'HH:mm:ss", "yyyy-MM-dd HH:mm:ss",
"yyyy-MM-dd" };
for (int i = 0; i < supportedFormats.length; i++) {
try {
date = new SimpleDateFormat(supportedFormats[i]).parse(postFields.get("expiryDate").toString());
drugInventory.setExpiryDate(date);
}
catch (Exception ex) {}
}
if (date == null) {
throw new ResponseException(
"Invalid date " + postFields.get("expiryDate")) {};
}
}
if (postFields.get("batch") != null) {
drugInventory.setBatch(postFields.get("batch").toString());
}
if (postFields.get("supplier") != null) {
drugInventory.setSupplier(postFields.get("supplier").toString());
}
if (postFields.get("roomLocation") != null) {
drugInventory.setRoomLocation(postFields.get("roomLocation").toString());
}
if (postFields.get("value") != null) {
drugInventory.setValue(Integer.parseInt(postFields.get("value").toString()));
}
if (postFields.get("status") != null) {
drugInventory.setStatus(postFields.get("status").toString());
}
if (postFields.get("provider") != null) {
Provider p = Context.getProviderService().getProviderByUuid(postFields.get("provider").toString());
if (p == null) {
throw new ResponseException(
"Provider uuid not found") {};
}
drugInventory.setProviderId(p.getId());
drugInventory.setProvider(p);
}
if (postFields.get("location") != null) {
Location l = Context.getLocationService().getLocationByUuid(postFields.get("location").toString());
if (l == null) {
throw new ResponseException(
"Location uuid not found") {};
}
drugInventory.setLocationId(l.getId());
drugInventory.setLocation(l);
}
if (postFields.get("drugPurchaseOrder") != null) {
DrugPurchaseOrder dPOrder = Context.getService(DrugPurchaseOrderService.class).getDrugPurchaseOrderByUuid(
postFields.get("drugPurchaseOrder").toString());
if (dPOrder == null) {
throw new ResponseException(
"DrugPurchaseOrder uuid not found") {};
}
drugInventory.setDrugPurchaseOrderId(dPOrder.getId());
drugInventory.setDrugPurchaseOrder(dPOrder);
}