Skip to content

Commit

Permalink
EA-191 - Support retrieving EMR API configurations via REST (#232)
Browse files Browse the repository at this point in the history
  • Loading branch information
mseaton authored Jun 28, 2024
1 parent 6a738a4 commit 893ced5
Show file tree
Hide file tree
Showing 9 changed files with 385 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,12 @@
import org.openmrs.Role;
import org.openmrs.VisitType;
import org.openmrs.module.emrapi.diagnosis.DiagnosisMetadata;
import org.openmrs.module.emrapi.disposition.Disposition;
import org.openmrs.module.emrapi.disposition.DispositionDescriptor;
import org.openmrs.module.emrapi.disposition.DispositionService;
import org.openmrs.module.metadatamapping.util.ModuleProperties;
import org.openmrs.util.OpenmrsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

Expand All @@ -47,6 +51,9 @@
@Component("emrApiProperties")
public class EmrApiProperties extends ModuleProperties {

@Autowired
protected DispositionService dispositionService;

@Override
public String getMetadataSourceName() {
return EmrApiConstants.EMR_METADATA_SOURCE_NAME;
Expand Down Expand Up @@ -191,7 +198,6 @@ public DiagnosisMetadata getDiagnosisMetadata() {
return new DiagnosisMetadata(conceptService, getEmrApiConceptSource());
}


public List<ConceptSource> getConceptSourcesForDiagnosisSearch() {
//The results can very well be cached to reduce calls to database.
//however the compatibility requirement to core 1.9.9 do not allow this currently
Expand Down Expand Up @@ -330,4 +336,11 @@ public Boolean getVisitAssignmentHandlerAdjustEncounterTimeOfDayIfNecessary() {
return "TRUE".equalsIgnoreCase(getGlobalProperty(GP_VISIT_ASSIGNMENT_HANDLER_ADJUST_ENCOUNTER_TIME_OF_DAY_IF_NECESSARY, false));
}

public List<Disposition> getDispositions() {
return dispositionService.getDispositions();
}

public DispositionDescriptor getDispositionDescriptor() {
return dispositionService.getDispositionDescriptor();
}
}
6 changes: 6 additions & 0 deletions api/src/test/resources/baseTestDataset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@
retired = "false" code = "emr.unknownProvider" name = "Unknown Provider"
/>

<metadatamapping_metadata_term_mapping metadata_term_mapping_id = "3246" metadata_source_id = "2134"
creator = "1" date_created="2008-08-18 12:38:58.0" uuid=""
retired = "false" code = "emr.unknownLocation" name = "Unknown Location"
metadata_uuid = "8d6c993e-c2cc-11de-8d13-0010c6dffd0f" metadata_class = "org.openmrs.Location"
/>

<metadatamapping_metadata_term_mapping metadata_term_mapping_id = "32245" metadata_source_id = "2134"
creator = "1" date_created="2008-08-18 12:38:58.0" uuid="cf25712e-f193-48d5-aaaa-5fd8d0244c71"
retired = "false" code = "emr.admissionForm" name = "Admission Form"
Expand Down
8 changes: 8 additions & 0 deletions omod/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@
<scope>compile</scope>
</dependency>

<dependency>
<groupId>${project.parent.groupId}</groupId>
<artifactId>${project.parent.artifactId}-api</artifactId>
<version>${project.parent.version}</version>
<scope>test</scope>
<type>test-jar</type>
</dependency>

<dependency>
<groupId>${project.parent.groupId}</groupId>
<artifactId>${project.parent.artifactId}-api-reporting</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package org.openmrs.module.emrapi.rest.converter;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.openmrs.annotation.Handler;
import org.openmrs.module.emrapi.EmrApiProperties;
import org.openmrs.module.emrapi.diagnosis.DiagnosisMetadata;
import org.openmrs.module.emrapi.disposition.Disposition;
import org.openmrs.module.emrapi.disposition.DispositionDescriptor;
import org.openmrs.module.emrapi.disposition.DispositionObs;
import org.openmrs.module.webservices.rest.SimpleObject;
import org.openmrs.module.webservices.rest.web.ConversionUtil;
import org.openmrs.module.webservices.rest.web.representation.CustomRepresentation;
import org.openmrs.module.webservices.rest.web.representation.Representation;
import org.openmrs.module.webservices.rest.web.resource.api.Converter;
import org.openmrs.module.webservices.rest.web.resource.impl.DelegatingResourceDescription;
import org.openmrs.module.webservices.rest.web.response.ConversionException;
import org.openmrs.module.webservices.rest.web.response.ResourceDoesNotSupportOperationException;

import java.beans.PropertyDescriptor;
import java.util.Map;

@Handler(supports = {
EmrApiProperties.class,
DiagnosisMetadata.class,
Disposition.class,
DispositionObs.class,
DispositionDescriptor.class
}, order = 0)
public class SimpleBeanConverter<T> implements Converter<T> {

private final Log log = LogFactory.getLog(getClass());

/**
* @return a resource description that represents a custom representation, or one that represents all bean properties in the class
*/
public DelegatingResourceDescription getResourceDescription(T o, Representation representation) {
if (representation instanceof CustomRepresentation) {
return ConversionUtil.getCustomRepresentationDescription((CustomRepresentation) representation);
}
DelegatingResourceDescription ret = new DelegatingResourceDescription();
for (PropertyDescriptor pd : PropertyUtils.getPropertyDescriptors(o.getClass())) {
if (pd.getReadMethod() != null && pd.getReadMethod().getDeclaringClass() == o.getClass()) {
String propName = pd.getName();
ret.addProperty(propName, representation);
}
}
return ret;
}

@Override
public SimpleObject asRepresentation(T o, Representation rep) throws ConversionException {
SimpleObject ret = new SimpleObject();
Map<String, DelegatingResourceDescription.Property> props = getResourceDescription(o, rep).getProperties();
for (String propName : props.keySet()) {
Object value = null;
// Log exception rather than fail if an exception is thrown while trying to retrieve a property
try {
value = PropertyUtils.getProperty(o, propName);
}
catch (Exception e) {
log.debug("Could not get property value " + propName + " from " + o.getClass(), e);
}
ret.put(propName, ConversionUtil.convertToRepresentation(value, props.get(propName).getRep()));
}
return ret;
}

@Override
public T newInstance(String s) {
throw new ResourceDoesNotSupportOperationException();
}

@Override
public T getByUniqueId(String s) {
throw new ResourceDoesNotSupportOperationException();
}

@Override
public Object getProperty(T o, String s) throws ConversionException {
throw new ResourceDoesNotSupportOperationException();
}

@Override
public void setProperty(Object o, String s, Object o1) throws ConversionException {
throw new ResourceDoesNotSupportOperationException();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.openmrs.module.emrapi.web.controller;

import org.openmrs.module.emrapi.EmrApiProperties;
import org.openmrs.module.webservices.rest.SimpleObject;
import org.openmrs.module.webservices.rest.web.ConversionUtil;
import org.openmrs.module.webservices.rest.web.RequestContext;
import org.openmrs.module.webservices.rest.web.RestUtil;
import org.openmrs.module.webservices.rest.web.representation.Representation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping(value = "/rest/emrapi/configuration")
public class EmrApiConfigurationController {

@Autowired
private EmrApiProperties emrApiProperties;

@RequestMapping(method = RequestMethod.GET)
@ResponseBody
public SimpleObject getEmrApiConfiguration(HttpServletRequest request, HttpServletResponse response) {
RequestContext context = RestUtil.getRequestContext(request, response, Representation.REF);
return (SimpleObject) ConversionUtil.convertToRepresentation(emrApiProperties, context.getRepresentation());
}
}
Loading

0 comments on commit 893ced5

Please sign in to comment.