|
| 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 | +} |
0 commit comments