Skip to content

Commit

Permalink
Spice up the inventory / formulary code
Browse files Browse the repository at this point in the history
-Separate ViewModels for custom/existing/formulary pages
-Update instructions
-Better error handeling for custom medications
  • Loading branch information
Kevin Zurek committed May 18, 2017
1 parent 53f1693 commit 66d17ca
Show file tree
Hide file tree
Showing 12 changed files with 421 additions and 198 deletions.
102 changes: 81 additions & 21 deletions app/femr/ui/controllers/admin/InventoryController.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import femr.ui.helpers.security.FEMRAuthenticated;
import femr.ui.models.admin.inventory.*;
import femr.common.models.MedicationItem;
import femr.ui.views.html.admin.inventory.manage;
import femr.ui.views.html.admin.inventory.*;
import play.data.DynamicForm;
import play.data.Form;
import play.data.FormFactory;
Expand Down Expand Up @@ -68,23 +68,29 @@ public InventoryController(FormFactory formFactory,
this.sessionService = sessionService;
}

/**
* Serves up the inventory homepage. POST communication for updating quantity
* is handled by AJAX calls
*/
public Result manageGet() {
CurrentUser currentUser = sessionService.retrieveCurrentUserSession();


InventoryViewModelGet viewModel = new InventoryViewModelGet();
ManageViewModelGet viewModel = new ManageViewModelGet();

// If the use does not have a trip ID, we cannot retrieve the list of medications
// since they are tied to a trip
if( currentUser.getTripId() != null ){

ServiceResponse<List<MedicationItem>> medicationServiceResponse = inventoryService.retrieveMedicationInventorysByTripId(currentUser.getTripId());
if (medicationServiceResponse.hasErrors()) {

throw new RuntimeException();
} else {

viewModel.setMedications(medicationServiceResponse.getResponseObject());
}


ServiceResponse<MissionTripItem> missionTripServiceResponse = missionTripService.retrieveAllTripInformationByTripId(currentUser.getTripId());
if (missionTripServiceResponse.hasErrors()) {

Expand All @@ -94,17 +100,28 @@ public Result manageGet() {
viewModel.setMissionTripItem(missionTripServiceResponse.getResponseObject());
}

}
else{
} else {

viewModel.setMedications( new ArrayList<>() );
viewModel.setMedications(new ArrayList<>());
}

ServiceResponse<List<MedicationItem>> conceptMedicationServiceResponse = conceptService.retrieveAllMedicationConcepts();
if (conceptMedicationServiceResponse.hasErrors()) {
return ok(manage.render(currentUser, viewModel));
}


/**
* Page for adding a new custom medication
*/
public Result customGet() {
CurrentUser currentUser = sessionService.retrieveCurrentUserSession();

CustomViewModelGet viewModel = new CustomViewModelGet();

ServiceResponse<List<String>> availableMedicationFormsResponse = medicationService.retrieveAvailableMedicationForms();
if (availableMedicationFormsResponse.hasErrors()) {
throw new RuntimeException();
} else {
viewModel.setConceptMedications(conceptMedicationServiceResponse.getResponseObject());
viewModel.setAvailableForms(availableMedicationFormsResponse.getResponseObject());
}

ServiceResponse<List<String>> availableMedicationUnitsResponse = medicationService.retrieveAvailableMedicationUnits();
Expand All @@ -114,25 +131,28 @@ public Result manageGet() {
viewModel.setAvailableUnits(availableMedicationUnitsResponse.getResponseObject());
}

ServiceResponse<List<String>> availableMedicationFormsResponse = medicationService.retrieveAvailableMedicationForms();
if (availableMedicationFormsResponse.hasErrors()) {
ServiceResponse<MissionTripItem> missionTripServiceResponse = missionTripService.retrieveAllTripInformationByTripId(currentUser.getTripId());
if (missionTripServiceResponse.hasErrors()) {

throw new RuntimeException();
} else {
viewModel.setAvailableForms(availableMedicationFormsResponse.getResponseObject());

viewModel.setMissionTripItem(missionTripServiceResponse.getResponseObject());
}

return ok(manage.render(currentUser, viewModel));
return ok(custom.render(currentUser, viewModel));
}

/**
* Handles the submission of a new medication from the Admin Inventory Tracking screen.
* Handles the submission of a new custom medication from an Admin
*/
public Result managePost() {

public Result customPost(){
CurrentUser currentUser = sessionService.retrieveCurrentUserSession();

final Form<InventoryViewModelPost> inventoryViewModelPostForm = formFactory.form(InventoryViewModelPost.class);
Form<InventoryViewModelPost> form = inventoryViewModelPostForm.bindFromRequest();
final Form<CustomViewModelPost> inventoryViewModelPostForm = formFactory.form(CustomViewModelPost.class);

Form<CustomViewModelPost> form = inventoryViewModelPostForm.bindFromRequest();

if (form.hasErrors()) {
return redirect("/admin/inventory");

Expand All @@ -145,7 +165,9 @@ public Result managePost() {

}

InventoryViewModelPost inventoryViewModelPost = form.bindFromRequest().get();
CustomViewModelPost inventoryViewModelPost = form.bindFromRequest().get();



// create a new medicationItem for managing the compilation of active ingredients
// (this could potentially be moved into the service layer)
Expand Down Expand Up @@ -191,18 +213,56 @@ public Result managePost() {
}


return redirect("/admin/inventory");
}

/**
* Page for adding a new medication from the concept dictionary
*/
public Result existingGet() {
CurrentUser currentUser = sessionService.retrieveCurrentUserSession();

ExistingViewModelGet viewModel = new ExistingViewModelGet();

ServiceResponse<List<MedicationItem>> conceptMedicationServiceResponse = conceptService.retrieveAllMedicationConcepts();
if (conceptMedicationServiceResponse.hasErrors()) {
throw new RuntimeException();
} else {
viewModel.setConceptMedications(conceptMedicationServiceResponse.getResponseObject());
}

ServiceResponse<MissionTripItem> missionTripServiceResponse = missionTripService.retrieveAllTripInformationByTripId(currentUser.getTripId());
if (missionTripServiceResponse.hasErrors()) {

throw new RuntimeException();
} else {

viewModel.setMissionTripItem(missionTripServiceResponse.getResponseObject());
}

return ok(existing.render(currentUser, viewModel));
}

/**
* Handles the submission of an existing medication from an Admin
*/
public Result existingPost() {
CurrentUser currentUser = sessionService.retrieveCurrentUserSession();

final Form<ExistingViewModelPost> existingViewModelPostForm = formFactory.form(ExistingViewModelPost.class);
Form<ExistingViewModelPost> existingForm = existingViewModelPostForm.bindFromRequest();
ExistingViewModelPost existingViewModelPost = existingForm.bindFromRequest().get();

//if just adding medications from the concept dictionary, there won't be any amount involved
//and knowledge of the ingredients already exists.
if (inventoryViewModelPost.getNewConceptMedicationsForInventory() != null) {
if (existingViewModelPost.getNewConceptMedicationsForInventory() != null) {

ServiceResponse<MedicationItem> conceptMedicationServiceResponse;
ServiceResponse<MedicationItem> medicationItemServiceResponse;
MedicationItem conceptMedicationItem;

//for each concept medication id that was sent from the select2 form
for (Integer conceptMedicationId : inventoryViewModelPost.getNewConceptMedicationsForInventory()) {
for (Integer conceptMedicationId : existingViewModelPost.getNewConceptMedicationsForInventory()) {

//get the actual concept MedicationItem from the id that was sent in
conceptMedicationServiceResponse = conceptService.retrieveConceptMedication(conceptMedicationId);
Expand Down
39 changes: 39 additions & 0 deletions app/femr/ui/models/admin/inventory/CustomViewModelGet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package femr.ui.models.admin.inventory;

import femr.common.models.MissionTripItem;

import java.util.List;

/**
* Created by kevin on 14/05/17.
*/
public class CustomViewModelGet {

private List<String> availableUnits;
private List<String> availableForms;
private MissionTripItem missionTripItem;

public List<String> getAvailableUnits() {
return availableUnits;
}

public void setAvailableUnits(List<String> availableUnits) {
this.availableUnits = availableUnits;
}

public List<String> getAvailableForms() {
return availableForms;
}

public void setAvailableForms(List<String> availableForms) {
this.availableForms = availableForms;
}

public MissionTripItem getMissionTripItem() {
return missionTripItem;
}

public void setMissionTripItem(MissionTripItem missionTripItem) {
this.missionTripItem = missionTripItem;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,57 +24,39 @@
import java.util.ArrayList;
import java.util.List;

public class InventoryViewModelPost {
public class CustomViewModelPost {

private Integer medicationQuantity;
private String medicationForm;
private String medicationName;
private List<Double> medicationStrength;
private List<String> medicationUnit;
private List<String> medicationIngredient;
//this is a list of IDs that come out of the select2 textbox for adding
//existing medicine.
private List<Integer> newConceptMedicationsForInventory;


public List<ValidationError> validate(){

List<ValidationError> errors = new ArrayList<>();

//if nothing is entered for quantity, default to 0
if (medicationQuantity == null) {

medicationQuantity = 0;
//errors.add(new ValidationError("medicationQuantity", "quantity is a required field"));
}
// Based on fEMR-95 in JIRA. medicationForm is used to be able to add medication to inventory.
if (StringUtils.isNullOrWhiteSpace(medicationName) && newConceptMedicationsForInventory == null)
if (StringUtils.isNullOrWhiteSpace(medicationName))
errors.add(new ValidationError("medicationName", "name is a required field"));
if (StringUtils.isNullOrWhiteSpace(medicationForm))
errors.add(new ValidationError("medicationForm", "a form is required"));

// Based on the error from JIRA fEMR-278, generic name is required
if (newConceptMedicationsForInventory.isEmpty()){
for(int i = 0; i < medicationIngredient.size(); i++){
if(StringUtils.isNullOrWhiteSpace(medicationIngredient.get(i)) && medicationStrength.get(i) > 0.0){
errors.add(new ValidationError("medicationGeneric", "a generic name is required"));
}
for (int i = 0; i < medicationIngredient.size(); i++) {
if (StringUtils.isNullOrWhiteSpace(medicationIngredient.get(i)) || medicationStrength.get(i) == null) {
errors.add(new ValidationError("medicationGeneric", "a generic name is required"));
}
}


/*
for (Integer i : medicationStrength){
if (i == null)
errors.add(new ValidationError("medicationStrength", "all strength fields are required"));
}
for (String ms : medicationUnit){
if (StringUtils.isNullOrWhiteSpace(ms))
errors.add(new ValidationError("medicationUnit", "all units are required"));
}
for (String mi : medicationIngredient){
if (StringUtils.isNullOrWhiteSpace(mi))
errors.add(new ValidationError("medicationIngredient", "all ingredients required"));
}
*/
return errors.isEmpty() ? null : errors;
}

Expand Down Expand Up @@ -127,11 +109,5 @@ public void setMedicationIngredient(List<String> medicationIngredient) {
this.medicationIngredient = medicationIngredient;
}

public List<Integer> getNewConceptMedicationsForInventory() {
return newConceptMedicationsForInventory;
}

public void setNewConceptMedicationsForInventory(List<Integer> newConceptMedicationsForInventory) {
this.newConceptMedicationsForInventory = newConceptMedicationsForInventory;
}
}
31 changes: 31 additions & 0 deletions app/femr/ui/models/admin/inventory/ExistingViewModelGet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package femr.ui.models.admin.inventory;

import femr.common.models.MedicationItem;
import femr.common.models.MissionTripItem;

import java.util.List;

/**
* Created by kevin on 14/05/17.
*/
public class ExistingViewModelGet {

private List<MedicationItem> conceptMedications;
private MissionTripItem missionTripItem;

public List<MedicationItem> getConceptMedications() {
return conceptMedications;
}

public void setConceptMedications(List<MedicationItem> conceptMedications) {
this.conceptMedications = conceptMedications;
}

public MissionTripItem getMissionTripItem() {
return missionTripItem;
}

public void setMissionTripItem(MissionTripItem missionTripItem) {
this.missionTripItem = missionTripItem;
}
}
18 changes: 18 additions & 0 deletions app/femr/ui/models/admin/inventory/ExistingViewModelPost.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package femr.ui.models.admin.inventory;

import java.util.List;

public class ExistingViewModelPost {

//this is a list of IDs that come out of the select2 textbox for adding
//existing medicine.
private List<Integer> newConceptMedicationsForInventory;

public List<Integer> getNewConceptMedicationsForInventory() {
return newConceptMedicationsForInventory;
}

public void setNewConceptMedicationsForInventory(List<Integer> newConceptMedicationsForInventory) {
this.newConceptMedicationsForInventory = newConceptMedicationsForInventory;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

import java.util.List;

public class InventoryViewModelGet {
public class ManageViewModelGet {
private List<MedicationItem> medications;
private List<MedicationItem> conceptMedications;
private List<String> availableUnits;
Expand Down
Loading

0 comments on commit 66d17ca

Please sign in to comment.