|
9 | 9 | */ |
10 | 10 | package org.openmrs.web.attribute.handler; |
11 | 11 |
|
12 | | -import java.util.Map; |
13 | | - |
| 12 | +import org.apache.commons.lang.StringUtils; |
| 13 | +import org.codehaus.jackson.map.ObjectMapper; |
14 | 14 | import org.openmrs.Concept; |
| 15 | +import org.openmrs.api.context.Context; |
15 | 16 | import org.openmrs.customdatatype.CustomDatatype; |
16 | 17 | import org.openmrs.customdatatype.datatype.ConceptDatatype; |
17 | 18 | import org.springframework.stereotype.Component; |
18 | 19 |
|
| 20 | +import java.util.HashMap; |
| 21 | +import java.util.Map; |
| 22 | + |
19 | 23 | /** |
20 | 24 | * Handler for the Concept custom datatype |
21 | 25 | */ |
22 | 26 | @Component |
23 | 27 | public class ConceptFieldGenDatatypeHandler extends SerializingFieldGenDatatypeHandler<ConceptDatatype, Concept> { |
24 | | - |
| 28 | + |
| 29 | + public static final String SHOW_ANSWERS = "showAnswers"; |
| 30 | + |
| 31 | + private ObjectMapper objectMapper = new ObjectMapper(); |
| 32 | + private Map<String, Object> widgetConfiguration = new HashMap<>(); |
| 33 | + |
25 | 34 | /** |
26 | 35 | * @see SerializingFieldGenDatatypeHandler#getWidgetName() |
27 | 36 | */ |
@@ -51,14 +60,32 @@ public CustomDatatype.Summary toHtmlSummary(CustomDatatype datatype, String valu |
51 | 60 | */ |
52 | 61 | @Override |
53 | 62 | public Map<String, Object> getWidgetConfiguration() { |
54 | | - return null; |
| 63 | + return widgetConfiguration; |
55 | 64 | } |
56 | 65 |
|
57 | 66 | /** |
58 | 67 | * @see SerializingFieldGenDatatypeHandler#setHandlerConfiguration(String) |
59 | 68 | */ |
60 | 69 | @Override |
61 | | - public void setHandlerConfiguration(String s) { |
62 | | - |
| 70 | + public void setHandlerConfiguration(String handlerConfig) { |
| 71 | + widgetConfiguration = new HashMap<>(); |
| 72 | + if (StringUtils.isNotBlank(handlerConfig)) { |
| 73 | + try { |
| 74 | + widgetConfiguration.putAll(objectMapper.readValue(handlerConfig, Map.class)); |
| 75 | + } |
| 76 | + catch (Exception e) { |
| 77 | + throw new IllegalArgumentException("Unable to parse widget configuration", e); |
| 78 | + } |
| 79 | + Object showAnswersRef = widgetConfiguration.remove(SHOW_ANSWERS); |
| 80 | + if (showAnswersRef != null) { |
| 81 | + Concept concept = Context.getConceptService().getConceptByUuid(showAnswersRef.toString()); |
| 82 | + if (concept != null) { |
| 83 | + widgetConfiguration.put(SHOW_ANSWERS, concept.getConceptId()); |
| 84 | + } |
| 85 | + else { |
| 86 | + widgetConfiguration.put(SHOW_ANSWERS, Integer.parseInt(showAnswersRef.toString())); |
| 87 | + } |
| 88 | + } |
| 89 | + } |
63 | 90 | } |
64 | 91 | } |
0 commit comments