Skip to content

Commit 24a9cdc

Browse files
authored
RESTWS-977: Add a conceptreferencerange endpoint for 2.6 and below (#654)
1 parent afaea6f commit 24a9cdc

File tree

5 files changed

+349
-0
lines changed

5 files changed

+349
-0
lines changed

omod-2.5/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,12 @@
163163
<groupId>org.openmrs.web</groupId>
164164
<artifactId>openmrs-web</artifactId>
165165
<version>${openmrs.version.2.5.0}</version> <!-- $NO-MVN-MAN-VER$ -->
166+
<exclusions>
167+
<exclusion>
168+
<groupId>javax.servlet</groupId>
169+
<artifactId>servlet-api</artifactId>
170+
</exclusion>
171+
</exclusions>
166172
</dependency>
167173

168174
<dependency>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
/**
2+
* This Source Code Form is subject to the terms of the Mozilla Public License,
3+
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
4+
* obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
5+
* the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
6+
*
7+
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
8+
* graphic logo is a trademark of OpenMRS Inc.
9+
*/
10+
package org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs2_5;
11+
12+
import java.util.ArrayList;
13+
import java.util.List;
14+
15+
import org.apache.commons.lang3.StringUtils;
16+
import org.openmrs.Concept;
17+
import org.openmrs.ConceptName;
18+
import org.openmrs.ConceptNumeric;
19+
import org.openmrs.api.ConceptService;
20+
import org.openmrs.api.context.Context;
21+
import org.openmrs.module.webservices.rest.web.RequestContext;
22+
import org.openmrs.module.webservices.rest.web.RestConstants;
23+
import org.openmrs.module.webservices.rest.web.RestUtil;
24+
import org.openmrs.module.webservices.rest.web.annotation.PropertyGetter;
25+
import org.openmrs.module.webservices.rest.web.annotation.Resource;
26+
import org.openmrs.module.webservices.rest.web.representation.DefaultRepresentation;
27+
import org.openmrs.module.webservices.rest.web.representation.FullRepresentation;
28+
import org.openmrs.module.webservices.rest.web.representation.RefRepresentation;
29+
import org.openmrs.module.webservices.rest.web.representation.Representation;
30+
import org.openmrs.module.webservices.rest.web.resource.api.PageableResult;
31+
import org.openmrs.module.webservices.rest.web.resource.impl.DelegatingCrudResource;
32+
import org.openmrs.module.webservices.rest.web.resource.impl.DelegatingResourceDescription;
33+
import org.openmrs.module.webservices.rest.web.resource.impl.NeedsPaging;
34+
import org.openmrs.module.webservices.rest.web.response.ResponseException;
35+
36+
/**
37+
* {@link Resource} for conceptreferencerange on platform versions before 2.7
38+
*/
39+
@Resource(name = RestConstants.VERSION_1 + "/conceptreferencerange", supportedClass = ConceptNumeric.class, supportedOpenmrsVersions = "2.5.* - 2.6.*")
40+
public class ConceptReferenceRangeResource2_5 extends DelegatingCrudResource<ConceptNumeric> {
41+
42+
@Override
43+
public ConceptNumeric newDelegate() {
44+
return new ConceptNumeric();
45+
}
46+
47+
@Override
48+
public DelegatingResourceDescription getRepresentationDescription(Representation rep) {
49+
if (rep instanceof DefaultRepresentation) {
50+
DelegatingResourceDescription description = new DelegatingResourceDescription();
51+
description.addProperty("uuid");
52+
description.addProperty("display");
53+
description.addProperty("concept");
54+
description.addProperty("hiNormal");
55+
description.addProperty("hiAbsolute");
56+
description.addProperty("hiCritical");
57+
description.addProperty("lowNormal");
58+
description.addProperty("lowAbsolute");
59+
description.addProperty("lowCritical");
60+
description.addProperty("units");
61+
description.addProperty("allowDecimal");
62+
description.addSelfLink();
63+
description.addLink("full", ".?v=" + RestConstants.REPRESENTATION_FULL);
64+
return description;
65+
}
66+
else if (rep instanceof FullRepresentation) {
67+
DelegatingResourceDescription description = new DelegatingResourceDescription();
68+
description.addProperty("uuid");
69+
description.addProperty("display");
70+
description.addProperty("concept");
71+
description.addProperty("hiNormal");
72+
description.addProperty("hiAbsolute");
73+
description.addProperty("hiCritical");
74+
description.addProperty("lowNormal");
75+
description.addProperty("lowAbsolute");
76+
description.addProperty("lowCritical");
77+
description.addProperty("units");
78+
description.addProperty("allowDecimal");
79+
description.addSelfLink();
80+
return description;
81+
}
82+
else if (rep instanceof RefRepresentation) {
83+
DelegatingResourceDescription description = new DelegatingResourceDescription();
84+
description.addProperty("uuid");
85+
description.addProperty("display");
86+
description.addSelfLink();
87+
description.addLink("full", ".?v=" + RestConstants.REPRESENTATION_FULL);
88+
return description;
89+
}
90+
91+
return null;
92+
}
93+
94+
@PropertyGetter("concept")
95+
public String getConcept(ConceptNumeric instance) {
96+
return instance.getUuid();
97+
}
98+
99+
@PropertyGetter("display")
100+
public String getDisplayString(ConceptNumeric instance) {
101+
String localization = getLocalization("Concept", instance.getUuid());
102+
if (StringUtils.isNotBlank(localization)) {
103+
return localization;
104+
} else {
105+
ConceptName cn = instance.getName();
106+
if (cn != null) {
107+
return cn.getName();
108+
} else {
109+
return instance.toString();
110+
}
111+
}
112+
}
113+
114+
@Override
115+
public ConceptNumeric getByUniqueId(String uniqueId) {
116+
return Context.getConceptService().getConceptNumericByUuid(uniqueId);
117+
}
118+
119+
@Override
120+
public ConceptNumeric save(ConceptNumeric delegate) {
121+
throw new UnsupportedOperationException("resource does not support this operation");
122+
}
123+
124+
@Override
125+
protected void delete(ConceptNumeric delegate, String reason, RequestContext context) throws ResponseException {
126+
throw new UnsupportedOperationException("resource does not support this operation");
127+
}
128+
129+
@Override
130+
public void purge(ConceptNumeric delegate, RequestContext context) throws ResponseException {
131+
throw new UnsupportedOperationException("resource does not support this operation");
132+
}
133+
134+
@Override
135+
protected PageableResult doSearch(RequestContext context) {
136+
137+
ConceptService conceptService = Context.getConceptService();
138+
List<ConceptNumeric> referenceRanges = new ArrayList<ConceptNumeric>();
139+
140+
String conceptUuid = context.getParameter("concept");
141+
if (StringUtils.isBlank(conceptUuid)) {
142+
throw new IllegalArgumentException("concept is required");
143+
}
144+
145+
String[] conceptReferenceStrings = conceptUuid.split(",");
146+
for (String conceptReference : conceptReferenceStrings) {
147+
if (StringUtils.isBlank(conceptReference)) {
148+
continue;
149+
}
150+
// handle UUIDs
151+
if (RestUtil.isValidUuid(conceptReference)) {
152+
ConceptNumeric conceptNumeric = conceptService.getConceptNumericByUuid(conceptReference.trim());
153+
if (conceptNumeric != null) {
154+
referenceRanges.add(conceptNumeric);
155+
continue;
156+
}
157+
}
158+
// handle mappings
159+
int idx = conceptReference.indexOf(':');
160+
if (idx >= 0 && idx < conceptReference.length() - 1) {
161+
String conceptSource = conceptReference.substring(0, idx);
162+
String conceptCode = conceptReference.substring(idx + 1);
163+
Concept concept = conceptService.getConceptByMapping(conceptCode.trim(), conceptSource.trim(), false);
164+
if (concept != null && concept instanceof ConceptNumeric) {
165+
referenceRanges.add((ConceptNumeric)concept);
166+
}
167+
}
168+
}
169+
170+
return new NeedsPaging<ConceptNumeric>(referenceRanges, context);
171+
}
172+
173+
@Override
174+
public String getResourceVersion() {
175+
return RestConstants2_5.RESOURCE_VERSION;
176+
}
177+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* This Source Code Form is subject to the terms of the Mozilla Public License,
3+
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
4+
* obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
5+
* the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
6+
*
7+
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
8+
* graphic logo is a trademark of OpenMRS Inc.
9+
*/
10+
package org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs2_5;
11+
12+
public class RestConstants2_5 {
13+
14+
public static final String RESOURCE_VERSION = "2.5";
15+
16+
public static final String CONCEPT_NUMERIC_UUID = "c607c80f-1ea9-4da3-bb88-6276ce8868dd";
17+
18+
public static final String DISPLAY_WEIGHT = "WEIGHT (KG)";
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/**
2+
* This Source Code Form is subject to the terms of the Mozilla Public License,
3+
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
4+
* obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
5+
* the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
6+
*
7+
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
8+
* graphic logo is a trademark of OpenMRS Inc.
9+
*/
10+
package org.openmrs.module.webservices.rest.web.v1_0.controller.openmrs2_5;
11+
12+
import java.util.List;
13+
14+
import org.apache.commons.beanutils.PropertyUtils;
15+
import org.junit.Assert;
16+
import org.junit.jupiter.api.Test;
17+
import org.openmrs.module.webservices.rest.SimpleObject;
18+
import org.openmrs.module.webservices.rest.test.Util;
19+
import org.openmrs.module.webservices.rest.web.v1_0.controller.jupiter.MainResourceControllerTest;
20+
import org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs2_5.RestConstants2_5;
21+
import org.springframework.mock.web.MockHttpServletRequest;
22+
23+
public class ConceptReferenceRangeController2_5Test extends MainResourceControllerTest {
24+
25+
@Override
26+
public String getURI() {
27+
return "conceptreferencerange";
28+
}
29+
30+
@Override
31+
public String getUuid() {
32+
return RestConstants2_5.CONCEPT_NUMERIC_UUID;
33+
}
34+
35+
@Override
36+
public long getAllCount() {
37+
return 0;
38+
}
39+
40+
@Test
41+
public void shouldGetConceptReferenceRangeEvenWithSpacesAroundConceptUuid() throws Exception {
42+
MockHttpServletRequest request = newGetRequest(getURI());
43+
request.setParameter("concept", RestConstants2_5.CONCEPT_NUMERIC_UUID + " ");
44+
request.setParameter("v", "full");
45+
SimpleObject response = deserialize(handle(request));
46+
List<Object> resultsList = Util.getResultsList(response);
47+
Assert.assertEquals(1, resultsList.size());
48+
Assert.assertEquals(250.0, PropertyUtils.getProperty(resultsList.get(0), "hiNormal"));
49+
Assert.assertEquals(0.0, PropertyUtils.getProperty(resultsList.get(0), "lowCritical"));
50+
Assert.assertEquals("kg", PropertyUtils.getProperty(resultsList.get(0), "units"));
51+
Assert.assertEquals(true, PropertyUtils.getProperty(resultsList.get(0), "allowDecimal"));
52+
}
53+
54+
@Override
55+
public void shouldGetDefaultByUuid() throws Exception {
56+
super.shouldGetDefaultByUuid();
57+
}
58+
59+
@Override
60+
public void shouldGetRefByUuid() throws Exception {
61+
super.shouldGetRefByUuid();
62+
}
63+
64+
@Override
65+
public void shouldGetFullByUuid() throws Exception {
66+
super.shouldGetFullByUuid();
67+
}
68+
69+
@Override
70+
public void shouldGetAll() throws Exception {
71+
super.shouldGetAll();
72+
}
73+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/**
2+
* This Source Code Form is subject to the terms of the Mozilla Public License,
3+
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
4+
* obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
5+
* the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
6+
*
7+
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
8+
* graphic logo is a trademark of OpenMRS Inc.
9+
*/
10+
package org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs2_5;
11+
12+
import org.openmrs.ConceptNumeric;
13+
import org.openmrs.api.context.Context;
14+
import org.openmrs.module.webservices.rest.web.resource.impl.BaseDelegatingResourceTest;
15+
16+
/**
17+
* Tests functionality of {@link ConceptReferenceRangeResource2_5}.
18+
*/
19+
public class ConceptReferenceRangeResource2_5Test extends BaseDelegatingResourceTest<ConceptReferenceRangeResource2_5, ConceptNumeric> {
20+
21+
@Override
22+
public ConceptNumeric newObject() {
23+
return Context.getConceptService().getConceptNumericByUuid(RestConstants2_5.CONCEPT_NUMERIC_UUID);
24+
}
25+
26+
@Override
27+
public void validateDefaultRepresentation() throws Exception {
28+
super.validateDefaultRepresentation();
29+
assertPropEquals("display", RestConstants2_5.DISPLAY_WEIGHT);
30+
assertPropEquals("uuid", getObject().getUuid());
31+
assertPropEquals("concept", getObject().getUuid());
32+
assertPropEquals("hiNormal", getObject().getHiNormal());
33+
assertPropEquals("hiAbsolute", getObject().getHiAbsolute());
34+
assertPropEquals("hiCritical", getObject().getHiCritical());
35+
assertPropEquals("lowNormal", getObject().getLowNormal());
36+
assertPropEquals("lowAbsolute", getObject().getLowAbsolute());
37+
assertPropEquals("lowCritical", getObject().getLowCritical());
38+
assertPropEquals("units", getObject().getUnits());
39+
assertPropEquals("allowDecimal", getObject().getAllowDecimal());
40+
}
41+
42+
@Override
43+
public void validateFullRepresentation() throws Exception {
44+
super.validateFullRepresentation();
45+
assertPropEquals("display", RestConstants2_5.DISPLAY_WEIGHT);
46+
assertPropEquals("uuid", getObject().getUuid());
47+
assertPropEquals("concept", getObject().getUuid());
48+
assertPropEquals("hiNormal", getObject().getHiNormal());
49+
assertPropEquals("hiAbsolute", getObject().getHiAbsolute());
50+
assertPropEquals("hiCritical", getObject().getHiCritical());
51+
assertPropEquals("lowNormal", getObject().getLowNormal());
52+
assertPropEquals("lowAbsolute", getObject().getLowAbsolute());
53+
assertPropEquals("lowCritical", getObject().getLowCritical());
54+
assertPropEquals("units", getObject().getUnits());
55+
assertPropEquals("allowDecimal", getObject().getAllowDecimal());
56+
}
57+
58+
@Override
59+
public void validateRefRepresentation() throws Exception {
60+
super.validateRefRepresentation();
61+
assertPropEquals("display", RestConstants2_5.DISPLAY_WEIGHT);
62+
assertPropEquals("uuid", getObject().getUuid());
63+
}
64+
65+
@Override
66+
public String getDisplayProperty() {
67+
return RestConstants2_5.DISPLAY_WEIGHT;
68+
}
69+
70+
@Override
71+
public String getUuidProperty() {
72+
return RestConstants2_5.CONCEPT_NUMERIC_UUID;
73+
}
74+
}

0 commit comments

Comments
 (0)