Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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
*
Expand All @@ -35,62 +36,70 @@ 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<VisitType> getOrderedVisitTypes (List<VisitType> visitTypes) {
public List<VisitType> getOrderedVisitTypes(List<VisitType> visitTypes) {
AdministrationService adminService = Context.getAdministrationService();
String propertyValue = adminService.getGlobalProperty(CoreAppsConstants.VISIT_TYPES_ORDER_PROPERTY);
VisitService vs = Context.getVisitService();

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<VisitType> getOrderedVisitTypes (List<VisitType> visitTypes, String propertyValue,VisitService visitService) {
public List<VisitType> getOrderedVisitTypes(List<VisitType> visitTypes, String propertyValue,
VisitService visitService) {

Map<Integer,String> order = null;
List<VisitType> visitTypesOrdered = new ArrayList<VisitType>();
Map<String, String> order = null;
List<VisitType> visitTypesOrdered = new ArrayList<>();

if (propertyValue != null) {
try {
order = new ObjectMapper().readValue(propertyValue, HashMap.class);
order = new ObjectMapper().readValue(propertyValue, new TypeReference<Map<String, String>>() {
});
} 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);
}
Expand All @@ -104,10 +113,10 @@ public List<VisitType> getOrderedVisitTypes (List<VisitType> visitTypes, String
return visitTypesOrdered;
}

public List<VisitType> getUnRetiredVisitTypes(){
public List<VisitType> getUnRetiredVisitTypes() {
VisitService visitService = Context.getVisitService();
List<VisitType> visitTypes = new ArrayList<VisitType>();
for(VisitType visitType : visitService.getAllVisitTypes()){
List<VisitType> visitTypes = new ArrayList<>();
for (VisitType visitType : visitService.getAllVisitTypes()) {
if (!visitType.getRetired()) {
visitTypes.add(visitType);
}
Expand All @@ -116,16 +125,16 @@ public List<VisitType> getUnRetiredVisitTypes(){
return visitTypes;
}

private Map<String, Object> getVisitTypeColorCodes(VisitType visitType){
Map<String, Object> colorCode = new HashMap<String, Object>();
private Map<String, Object> getVisitTypeColorCodes(VisitType visitType) {
Map<String, Object> 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();
Expand All @@ -136,7 +145,7 @@ private Map<String, Object> getVisitTypeColorCodes(VisitType visitType){
}
}
}
} catch(IOException ex){
} catch (IOException ex) {
LOG.error("Error retrieving visit type color codes, " + ex);
}

Expand All @@ -154,7 +163,7 @@ public Map<String, Object> getVisitTypeColorAndShortName(VisitType type) {
Map<String, Object> 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) {
Expand All @@ -173,10 +182,10 @@ public Map<String, Object> getVisitTypeColorAndShortName(VisitType type) {
*/
public Map<Integer, Map<String, Object>> getVisitColorAndShortName(List<VisitDomainWrapper> visits) {

Map<Integer, Map<String, Object>> visitsWithAttr = new LinkedHashMap<Integer, Map<String, Object>>();
Map<Integer, Map<String, Object>> visitsWithAttr = new LinkedHashMap<>();

for (VisitDomainWrapper visit : visits) {
Map<String,Object>visitColorAndShortName = getVisitTypeColorAndShortName(visit.getVisit().getVisitType());
Map<String, Object> visitColorAndShortName = getVisitTypeColorAndShortName(visit.getVisit().getVisitType());
visitsWithAttr.put(visit.getVisitId(), visitColorAndShortName);
}
return visitsWithAttr;
Expand Down