-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
EA-191 - Support retrieving EMR API configurations via REST (#232)
- Loading branch information
Showing
9 changed files
with
385 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
omod/src/main/java/org/openmrs/module/emrapi/rest/converter/SimpleBeanConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
27 changes: 0 additions & 27 deletions
27
omod/src/main/java/org/openmrs/module/emrapi/web/controller/EmrApiConfigController.java
This file was deleted.
Oops, something went wrong.
31 changes: 31 additions & 0 deletions
31
...src/main/java/org/openmrs/module/emrapi/web/controller/EmrApiConfigurationController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
Oops, something went wrong.