Advertisement
Guest User

Untitled

a guest
Feb 28th, 2013
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.24 KB | None | 0 0
  1. /**
  2.  
  3. * The contents of this file are subject to the OpenMRS Public License
  4.  
  5. * Version 1.0 (the "License"); you may not use this file except in
  6.  
  7. * compliance with the License. You may obtain a copy of the License at
  8.  
  9. * http://license.openmrs.org
  10.  
  11. *
  12.  
  13. * Software distributed under the License is distributed on an "AS IS"
  14.  
  15. * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
  16.  
  17. * License for the specific language governing rights and limitations
  18.  
  19. * under the License.
  20.  
  21. *
  22.  
  23. * Copyright (C) OpenMRS, LLC. All Rights Reserved.
  24.  
  25. */
  26.  
  27. package org.openmrs.customdatatype.datatype;
  28.  
  29.  
  30.  
  31. import org.apache.commons.lang.StringUtils;
  32.  
  33. import org.openmrs.Concept;
  34.  
  35. import org.openmrs.api.ConceptService;
  36.  
  37. import org.openmrs.api.context.Context;
  38.  
  39. import org.openmrs.Obs;
  40.  
  41. import org.openmrs.customdatatype.SerializingCustomDatatype;
  42.  
  43. import org.springframework.stereotype.Component;
  44.  
  45.  
  46.  
  47. /**
  48.  
  49. * Datatype for Concept, represented by org.openmrs.Concept.
  50.  
  51. * @since 1.9
  52.  
  53. */
  54.  
  55. @Component
  56.  
  57. public class ConceptDatatype extends SerializingCustomDatatype<Concept> {
  58.  
  59.  
  60.  
  61. ConceptService cs = Context.getConceptService();
  62.  
  63.  
  64.  
  65. /**
  66.  
  67. * @see org.openmrs.customdatatype.SerializingCustomDatatype#doGetTextSummary(java.lang.Object)
  68.  
  69. */
  70.  
  71. @Override
  72.  
  73. public String serialize(Concept typedValue) {
  74.  
  75. if (typedValue == null)
  76.  
  77. return null;
  78.  
  79. return typedValue.getUuid();
  80.  
  81. }
  82.  
  83.  
  84.  
  85. /**
  86.  
  87. * @see org.openmrs.customdatatype.SerializingCustomDatatype#doGetTextSummary(java.lang.Object)
  88.  
  89. */
  90.  
  91. @Override
  92.  
  93. public Concept deserialize(String serializedValue) {
  94.  
  95. if (StringUtils.isEmpty(serializedValue))
  96.  
  97. return null;
  98.  
  99. return cs.getConceptByUuid(serializedValue);
  100.  
  101. }
  102.  
  103. }
  104.  
  105.  
  106.  
  107. /**
  108.  
  109. * The contents of this file are subject to the OpenMRS Public License
  110.  
  111. * Version 1.0 (the "License"); you may not use this file except in
  112.  
  113. * compliance with the License. You may obtain a copy of the License at
  114.  
  115. * http://license.openmrs.org
  116.  
  117. *
  118.  
  119. * Software distributed under the License is distributed on an "AS IS"
  120.  
  121. * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
  122.  
  123. * License for the specific language governing rights and limitations
  124.  
  125. * under the License.
  126.  
  127. *
  128.  
  129. * Copyright (C) OpenMRS, LLC. All Rights Reserved.
  130.  
  131. */
  132.  
  133. package org.openmrs.web.attribute.handler;
  134.  
  135.  
  136.  
  137. import java.util.HashMap;
  138.  
  139. import java.util.Map;
  140.  
  141.  
  142.  
  143. import javax.servlet.http.HttpServletRequest;
  144.  
  145.  
  146.  
  147. import org.apache.commons.lang.StringUtils;
  148.  
  149. import org.openmrs.Concept;
  150.  
  151. import org.openmrs.api.context.Context;
  152.  
  153. import org.openmrs.customdatatype.CustomDatatype;
  154.  
  155. import org.openmrs.customdatatype.InvalidCustomValueException;
  156.  
  157. import org.openmrs.customdatatype.datatype.ConceptDatatype;
  158.  
  159. import org.openmrs.messagesource.MessageSourceService;
  160.  
  161. import org.springframework.stereotype.Component;
  162.  
  163.  
  164.  
  165. /**
  166.  
  167. * Handler for the concept custom datatype
  168.  
  169. */
  170.  
  171. @Component
  172.  
  173. public class ConceptDatatypeHandler implements FieldGenDatatypeHandler<ConceptDatatype, Concept> {
  174.  
  175.  
  176.  
  177. /**
  178.  
  179. * @see org.openmrs.customdatatype.CustomDatatypeHandler#setHandlerConfiguration(java.lang.String)
  180.  
  181. */
  182.  
  183. @Override
  184.  
  185. public void setHandlerConfiguration(String arg0) {
  186.  
  187. // not used
  188.  
  189. }
  190.  
  191.  
  192.  
  193. /**
  194.  
  195. * @see org.openmrs.web.attribute.handler.FieldGenDatatypeHandler#getWidgetName()
  196.  
  197. */
  198.  
  199. @Override
  200.  
  201. public String getWidgetName() {
  202.  
  203. return "concept";
  204.  
  205. }
  206.  
  207.  
  208.  
  209. /**
  210.  
  211. * @see org.openmrs.web.attribute.handler.FieldGenDatatypeHandler#getWidgetConfiguration()
  212.  
  213. */
  214.  
  215. @Override
  216.  
  217. public Map<String, Object> getWidgetConfiguration() {
  218.  
  219. MessageSourceService mss = Context.getMessageSourceService();
  220.  
  221. Map<String, Object> ret = new HashMap<String, Object>();
  222.  
  223. ret.put("isNullable", "false");
  224.  
  225. ret.put("trueLabel", mss.getMessage("general.true"));
  226.  
  227. ret.put("falseLabel", mss.getMessage("general.false"));
  228.  
  229. return ret;
  230.  
  231. }
  232.  
  233.  
  234.  
  235. /**
  236.  
  237. * @see org.openmrs.web.attribute.handler.FieldGenDatatypeHandler#getValue(org.openmrs.customdatatype.CustomDatatype, javax.servlet.http.HttpServletRequest, java.lang.String)
  238.  
  239. */
  240.  
  241. @Override
  242.  
  243. public Concept getValue(org.openmrs.customdatatype.datatype.ConceptDatatype datatype, HttpServletRequest request,
  244.  
  245. String formFieldName) throws InvalidCustomValueException {
  246.  
  247. String result = request.getParameter(formFieldName);
  248.  
  249. if (StringUtils.isBlank(result))
  250.  
  251. return null;
  252.  
  253. try {
  254.  
  255. return new Concept();
  256.  
  257. }
  258.  
  259. catch (Exception e) {
  260.  
  261. throw new InvalidCustomValueException("Invalid concept: " + result);
  262.  
  263. }
  264.  
  265. }
  266.  
  267.  
  268.  
  269. /**
  270.  
  271. * @see org.openmrs.web.attribute.handler.HtmlDisplayableDatatypeHandler#toHtmlSummary(org.openmrs.customdatatype.CustomDatatype, java.lang.String)
  272.  
  273. */
  274.  
  275. @Override
  276.  
  277. public CustomDatatype.Summary toHtmlSummary(CustomDatatype<Concept> datatype, String valueReference) {
  278.  
  279. return new CustomDatatype.Summary(valueReference, true);
  280.  
  281. }
  282.  
  283.  
  284.  
  285. /**
  286.  
  287. * @see org.openmrs.web.attribute.handler.HtmlDisplayableDatatypeHandler#toHtml(org.openmrs.customdatatype.CustomDatatype, java.lang.String)
  288.  
  289. */
  290.  
  291. @Override
  292.  
  293. public String toHtml(CustomDatatype<Concept> datatype, String valueReference) {
  294.  
  295. return valueReference;
  296.  
  297. }
  298.  
  299. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement