diff --git a/omod-1.11/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_11/ObsResource1_11.java b/omod-1.11/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_11/ObsResource1_11.java index c5dca8ed0..14626f497 100644 --- a/omod-1.11/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_11/ObsResource1_11.java +++ b/omod-1.11/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_11/ObsResource1_11.java @@ -12,7 +12,11 @@ import io.swagger.models.Model; import io.swagger.models.ModelImpl; import io.swagger.models.properties.StringProperty; +import org.apache.commons.lang.BooleanUtils; +import org.openmrs.Concept; +import org.openmrs.ConceptNumeric; import org.openmrs.Obs; +import org.openmrs.api.db.hibernate.HibernateUtil; import org.openmrs.module.webservices.rest.web.RestConstants; import org.openmrs.module.webservices.rest.web.annotation.PropertySetter; import org.openmrs.module.webservices.rest.web.annotation.Resource; @@ -92,4 +96,22 @@ public static void setFormFieldNamespace(Obs obs, Object namespace) { public String getResourceVersion() { return RestConstants1_11.RESOURCE_VERSION; } + + /** + * @return a Double if the ConceptNumeric is configured as allowDecimal, but an Integer otherwise + * This changes the implementation from the pre-1.11 field named precise. + */ + protected Number getValueNumeric(Obs obs) { + if (obs.getValueNumeric() == null) { + return null; + } + Concept concept = HibernateUtil.getRealObjectFromProxy(obs.getConcept()); + if (concept instanceof ConceptNumeric) { + ConceptNumeric conceptNumeric = (ConceptNumeric) concept; + if (BooleanUtils.isFalse(conceptNumeric.getAllowDecimal())) { + return obs.getValueNumeric().intValue(); + } + } + return obs.getValueNumeric(); + } } diff --git a/omod-1.11/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_11/ObsResource1_11Test.java b/omod-1.11/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_11/ObsResource1_11Test.java index 9d525cbdf..3be6db0ea 100644 --- a/omod-1.11/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_11/ObsResource1_11Test.java +++ b/omod-1.11/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_11/ObsResource1_11Test.java @@ -9,9 +9,14 @@ */ package org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs1_11; +import org.junit.Assert; +import org.junit.Test; +import org.openmrs.ConceptNumeric; import org.openmrs.Obs; import org.openmrs.api.context.Context; +import org.openmrs.module.webservices.rest.SimpleObject; import org.openmrs.module.webservices.rest.web.RestTestConstants1_8; +import org.openmrs.module.webservices.rest.web.representation.Representation; import org.openmrs.module.webservices.rest.web.resource.impl.BaseDelegatingResourceTest; public class ObsResource1_11Test extends BaseDelegatingResourceTest { @@ -44,4 +49,22 @@ public String getDisplayProperty() { public String getUuidProperty() { return RestTestConstants1_8.OBS_UUID; } + + @Test + public void asRepresentation_shouldReturnNumericValueBasedOnConceptNumericAllowDecimal() { + Obs obs = getObject(); + obs.setValueNumeric(20.0); + ConceptNumeric cn = Context.getConceptService().getConceptNumeric(5497); + obs.setConcept(cn); + cn.setAllowDecimal(true); + SimpleObject rep = getResource().asRepresentation(getObject(), Representation.DEFAULT); + Object value = rep.get("value"); + Assert.assertEquals(20.0, value); + Assert.assertEquals(Double.class, value.getClass()); + cn.setAllowDecimal(false); + rep = getResource().asRepresentation(getObject(), Representation.DEFAULT); + value = rep.get("value"); + Assert.assertEquals(20, value); + Assert.assertEquals(Integer.class, value.getClass()); + } } diff --git a/omod-1.8/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/ObsResource1_8.java b/omod-1.8/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/ObsResource1_8.java index 1ed900c8b..00a5900e1 100644 --- a/omod-1.8/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/ObsResource1_8.java +++ b/omod-1.8/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/ObsResource1_8.java @@ -278,7 +278,7 @@ public String getDisplayString(Obs obs) { * @return */ @PropertyGetter("value") - public static Object getValue(Obs obs) throws ConversionException { + public Object getValue(Obs obs) throws ConversionException { if (obs.isComplex()) { //Note that complex obs value is handled by ObsComplexValueController1_8 SimpleObject so = new SimpleObject(); @@ -322,13 +322,20 @@ public static Object getValue(Obs obs) throws ConversionException { } } - + if (obs.getValueNumeric() != null) { - return obs.getValueNumeric(); + return getValueNumeric(obs); } return null; } + + /** + * @return the valueNumeric from the obs + */ + protected Number getValueNumeric(Obs obs) { + return obs.getValueNumeric(); + } /** * Sets the members of an obs group diff --git a/omod-1.9/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_9/ObsResource1_9Test.java b/omod-1.9/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_9/ObsResource1_9Test.java index f4e637738..ec560b4ce 100644 --- a/omod-1.9/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_9/ObsResource1_9Test.java +++ b/omod-1.9/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_9/ObsResource1_9Test.java @@ -220,7 +220,7 @@ public void setValue_shouldReturnUuidForConceptTrue() throws Exception { Obs obs = new Obs(); obs.setConcept(Context.getConceptService().getConceptByUuid(BOOLEAN_CONCEPT_UUID)); ObsResource1_8.setValue(obs, trueConcept); - assertEquals(trueConcept, ObsResource1_8.getValue(obs)); + assertEquals(trueConcept, new ObsResource1_8().getValue(obs)); } @Test @@ -232,7 +232,7 @@ public void setValue_shouldReturnDrug() throws Exception { String drugUuid = "3cfcf118-931c-46f7-8ff6-7b876f0d4202"; Drug drug = Context.getConceptService().getDrugByUuid(drugUuid); ObsResource1_8.setValue(obs, drugUuid); - assertEquals(drug, ObsResource1_8.getValue(obs)); + assertEquals(drug, new ObsResource1_8().getValue(obs)); } @Test @@ -240,7 +240,7 @@ public void setValue_shouldReturnUuidForConceptFalse() throws Exception { Obs obs = new Obs(); obs.setConcept(Context.getConceptService().getConceptByUuid(BOOLEAN_CONCEPT_UUID)); ObsResource1_8.setValue(obs, falseConcept); - assertEquals(falseConcept, ObsResource1_8.getValue(obs)); + assertEquals(falseConcept, new ObsResource1_8().getValue(obs)); } @Test(expected = ConversionException.class) @@ -255,7 +255,7 @@ public void setValue_shouldReturnUuidForPrimitiveTrue() throws Exception { Obs obs = new Obs(); obs.setConcept(Context.getConceptService().getConceptByUuid(BOOLEAN_CONCEPT_UUID)); ObsResource1_8.setValue(obs, true); - assertEquals(trueConcept, ObsResource1_8.getValue(obs)); + assertEquals(trueConcept, new ObsResource1_8().getValue(obs)); } @Test @@ -263,7 +263,7 @@ public void setValue_shouldReturnUuidForPrimitiveFalse() throws Exception { Obs obs = new Obs(); obs.setConcept(Context.getConceptService().getConceptByUuid(BOOLEAN_CONCEPT_UUID)); ObsResource1_8.setValue(obs, false); - assertEquals(falseConcept, ObsResource1_8.getValue(obs)); + assertEquals(falseConcept, new ObsResource1_8().getValue(obs)); } private void clearAndSetValue(Obs obs, ObsType type, Object value) { @@ -287,6 +287,6 @@ public void setConvertedProperties_shouldAllowAnyPropertyOrder() throws Exceptio propertyMap.put("obsDatetime", "2013-12-09T00:00:00.000+0100"); resource.setConvertedProperties(obs, propertyMap, resource.getUpdatableProperties(), false); - org.springframework.util.Assert.isTrue(((Double) ObsResource1_8.getValue(obs)) == 10.0); + org.springframework.util.Assert.isTrue(((Double) new ObsResource1_8().getValue(obs)) == 10.0); } }