diff --git a/api/src/main/java/org/openmrs/module/coreapps/utils/VisitTypeHelper.java b/api/src/main/java/org/openmrs/module/coreapps/utils/VisitTypeHelper.java index 4add28bd3..8d5b2d53f 100644 --- a/api/src/main/java/org/openmrs/module/coreapps/utils/VisitTypeHelper.java +++ b/api/src/main/java/org/openmrs/module/coreapps/utils/VisitTypeHelper.java @@ -1,5 +1,12 @@ package org.openmrs.module.coreapps.utils; +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + import org.apache.commons.lang3.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -8,6 +15,7 @@ import org.codehaus.jackson.map.JsonMappingException; import org.codehaus.jackson.map.ObjectMapper; import org.codehaus.jackson.node.ArrayNode; +import org.codehaus.jackson.type.TypeReference; import org.openmrs.VisitType; import org.openmrs.api.APIException; import org.openmrs.api.AdministrationService; @@ -17,13 +25,6 @@ import org.openmrs.module.emrapi.visit.VisitDomainWrapper; import org.springframework.stereotype.Component; -import java.io.IOException; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; - /** * Perform common {@link VisitType} functionality * @@ -35,13 +36,14 @@ public class VisitTypeHelper { /** * Returns a list of ordered visit types, provided by a global property - * If a visit type is present in the visitTypes argument, but not found in the global property string, + * If a visit type is present in the visitTypes argument, but not found in the + * global property string, * it will be returned unordered, at the end of the visitTypesOrdered list * * @param visitTypes * @return visitTypesOrdered */ - public List getOrderedVisitTypes (List visitTypes) { + public List getOrderedVisitTypes(List visitTypes) { AdministrationService adminService = Context.getAdministrationService(); String propertyValue = adminService.getGlobalProperty(CoreAppsConstants.VISIT_TYPES_ORDER_PROPERTY); VisitService vs = Context.getVisitService(); @@ -49,48 +51,55 @@ public List getOrderedVisitTypes (List visitTypes) { return getOrderedVisitTypes(visitTypes, propertyValue, vs); } - public boolean showVisitTypeOnPatientHeaderSection(){ + public boolean showVisitTypeOnPatientHeaderSection() { AdministrationService adminService = Context.getAdministrationService(); String propertyValue = adminService.getGlobalProperty(CoreAppsConstants.SHOW_VISIT_TYPE_PATIENT_HEADER_SECTION); - return new Boolean(propertyValue); + return Boolean.parseBoolean(propertyValue); } /** * Returns a list of ordered visit types. * - * @param visitTypes All the visit types + * @param visitTypes All the visit types * @param propertyValue The visit types to order in JSON-like format * @param visitService - * @return visitTypesOrdered The visit types ordered and merged with the input visit type list + * @return visitTypesOrdered The visit types ordered and merged with the input + * visit type list */ - public List getOrderedVisitTypes (List visitTypes, String propertyValue,VisitService visitService) { + public List getOrderedVisitTypes(List visitTypes, String propertyValue, + VisitService visitService) { - Map order = null; - List visitTypesOrdered = new ArrayList(); + Map order = null; + List visitTypesOrdered = new ArrayList<>(); if (propertyValue != null) { try { - order = new ObjectMapper().readValue(propertyValue, HashMap.class); + order = new ObjectMapper().readValue(propertyValue, new TypeReference>() { + }); } catch (JsonParseException e) { - VisitTypeHelper.LOG.error("Unable to parse global property \"" + CoreAppsConstants.VISIT_TYPES_ORDER_PROPERTY + "\""); + VisitTypeHelper.LOG.error( + "Unable to parse global property \"" + CoreAppsConstants.VISIT_TYPES_ORDER_PROPERTY + "\""); } catch (JsonMappingException e) { - VisitTypeHelper.LOG.error("Unable to map global property \"" + CoreAppsConstants.VISIT_TYPES_ORDER_PROPERTY + "\""); + VisitTypeHelper.LOG.error( + "Unable to map global property \"" + CoreAppsConstants.VISIT_TYPES_ORDER_PROPERTY + "\""); } catch (APIException e) { - VisitTypeHelper.LOG.error("Unable to load global property \"" + CoreAppsConstants.VISIT_TYPES_ORDER_PROPERTY + "\""); + VisitTypeHelper.LOG.error( + "Unable to load global property \"" + CoreAppsConstants.VISIT_TYPES_ORDER_PROPERTY + "\""); } catch (IOException e) { - VisitTypeHelper.LOG.error("Unable to read global property \"" + CoreAppsConstants.VISIT_TYPES_ORDER_PROPERTY + "\""); + VisitTypeHelper.LOG.error( + "Unable to read global property \"" + CoreAppsConstants.VISIT_TYPES_ORDER_PROPERTY + "\""); } } if (order != null) { - for (int i=1 ; i <= order.size() ; i++) { + for (int i = 1; i <= order.size(); i++) { String typeUuid = order.get(Integer.toString(i)); VisitType type = visitService.getVisitTypeByUuid(typeUuid); if (visitTypes.contains(type)) { visitTypesOrdered.add(visitService.getVisitTypeByUuid(typeUuid)); } } - for (VisitType type: visitTypes) { + for (VisitType type : visitTypes) { if (!order.containsValue(type.getUuid())) { visitTypesOrdered.add(type); } @@ -104,10 +113,10 @@ public List getOrderedVisitTypes (List visitTypes, String return visitTypesOrdered; } - public List getUnRetiredVisitTypes(){ + public List getUnRetiredVisitTypes() { VisitService visitService = Context.getVisitService(); - List visitTypes = new ArrayList(); - for(VisitType visitType : visitService.getAllVisitTypes()){ + List visitTypes = new ArrayList<>(); + for (VisitType visitType : visitService.getAllVisitTypes()) { if (!visitType.getRetired()) { visitTypes.add(visitType); } @@ -116,16 +125,16 @@ public List getUnRetiredVisitTypes(){ return visitTypes; } - private Map getVisitTypeColorCodes(VisitType visitType){ - Map colorCode = new HashMap(); + private Map getVisitTypeColorCodes(VisitType visitType) { + Map colorCode = new HashMap<>(); AdministrationService adminService = Context.getAdministrationService(); String propertyValue = adminService.getGlobalProperty(CoreAppsConstants.VISIT_TYPE_COLORS); - try{ - if(StringUtils.isNotEmpty(propertyValue)){ + try { + if (StringUtils.isNotEmpty(propertyValue)) { ArrayNode array = new ObjectMapper().readValue(propertyValue, ArrayNode.class); for (JsonNode node : array) { - String visitTypeUuid= node.path("uuid").getTextValue(); - if(visitType.getUuid().equalsIgnoreCase(visitTypeUuid)){ + String visitTypeUuid = node.path("uuid").getTextValue(); + if (visitType.getUuid().equalsIgnoreCase(visitTypeUuid)) { colorCode.put("uuid", visitTypeUuid); String color = node.path("color").getTextValue(); @@ -136,7 +145,7 @@ private Map getVisitTypeColorCodes(VisitType visitType){ } } } - } catch(IOException ex){ + } catch (IOException ex) { LOG.error("Error retrieving visit type color codes, " + ex); } @@ -154,7 +163,7 @@ public Map getVisitTypeColorAndShortName(VisitType type) { Map colorAndShortName = getVisitTypeColorCodes(type); // set default values - if (colorAndShortName.get("color") == null ) { + if (colorAndShortName.get("color") == null) { colorAndShortName.put("color", "grey"); } if (colorAndShortName.get("shortName") == null) { @@ -173,10 +182,10 @@ public Map getVisitTypeColorAndShortName(VisitType type) { */ public Map> getVisitColorAndShortName(List visits) { - Map> visitsWithAttr = new LinkedHashMap>(); + Map> visitsWithAttr = new LinkedHashMap<>(); for (VisitDomainWrapper visit : visits) { - MapvisitColorAndShortName = getVisitTypeColorAndShortName(visit.getVisit().getVisitType()); + Map visitColorAndShortName = getVisitTypeColorAndShortName(visit.getVisit().getVisitType()); visitsWithAttr.put(visit.getVisitId(), visitColorAndShortName); } return visitsWithAttr;