/
WidgetFactory.java
178 lines (163 loc) · 6.62 KB
/
WidgetFactory.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package org.openmrs.module.htmlformentry.widget;
import java.io.File;
import org.openmrs.ConceptComplex;
import org.openmrs.ConceptDatatype;
import org.openmrs.FormField;
import org.openmrs.Obs;
import org.openmrs.api.context.Context;
import org.openmrs.module.htmlformentry.HtmlFormEntryConstants;
import org.openmrs.obs.handler.AbstractHandler;
import org.openmrs.obs.handler.ImageHandler;
import org.openmrs.web.WebConstants;
/**
* Contains shortcut methods to instantiate Widgets, and related utility methods.
*/
public class WidgetFactory {
/**
* Used by {@see inferWidgetTypeHint(FormField)}.
*/
public enum WidgetTypeHint {
NUMBER,
TEXT,
TEXTAREA,
CHECKBOX,
RADIO_BUTTONS,
DROPDOWN,
CHECKBOX_LIST,
MULTISELECT,
UPLOAD_WIDGET,
DATE,
DATE_TIME
}
/**
* Given a FormField, infers the related widget from the ConceptDatatype associated with that FormField.
*/
public static WidgetTypeHint inferWidgetTypeHint(FormField formField) {
ConceptDatatype dt = formField.getField().getConcept().getDatatype();
if (dt.isText()) {
return WidgetTypeHint.TEXT;
} else if (dt.isNumeric()) {
return WidgetTypeHint.NUMBER;
} else if (dt.isDate()) {
return WidgetTypeHint.DATE;
} else if (dt.isCoded()) {
if (formField.getField().isSelectMultiple())
return WidgetTypeHint.CHECKBOX_LIST;
else if (formField.getField().getDefaultValue() != null)
return WidgetTypeHint.CHECKBOX;
else
return WidgetTypeHint.DROPDOWN;
} else if (HtmlFormEntryConstants.COMPLEX_UUID.equals(dt.getUuid())) {
return WidgetTypeHint.UPLOAD_WIDGET;
} else {
throw new IllegalArgumentException(
"Autodetecting widget type from concept datatype not yet implemented for "
+ dt.getName());
}
}
/*
public static Widget createWidget(FormEntryContext context, Map<String, Object> hints) {
if (hints == null)
throw new NullPointerException("hints must be provided");
WidgetTypeHint typeHint = (WidgetTypeHint) hints.get("widgetType");
if (typeHint == WidgetTypeHint.TEXT) {
Widget w = new TextFieldWidget();
context.registerWidget(w);
return w;
} else if (typeHint == WidgetTypeHint.DATE) {
Widget w = new DateWidget();
context.registerWidget(w);
return w;
} else if (typeHint == WidgetTypeHint.CHECKBOX) {
String value = "true";
String label = "";
if (hints.containsKey("value"))
value = hints.get("value").toString();
if (hints.containsKey("label"))
label = hints.get("label").toString();
Widget w = new CheckboxWidget(label, value);
context.registerWidget(w);
return w;
} else if (typeHint == WidgetTypeHint.NUMBER) {
NumberFieldWidget w = new NumberFieldWidget((ConceptNumeric) hints.get("concept"));
context.registerWidget(w);
return w;
} else if (typeHint == WidgetTypeHint.DROPDOWN) {
DropdownWidget w = new DropdownWidget((Concept) hints.get("concept"));
context.registerWidget(w);
return w;
} else {
throw new IllegalArgumentException(typeHint + " not yet implemented");
}
}
public static Widget createWidget(FormEntryContext context, WidgetTypeHint widgetType) {
Map<String, Object> hints = new HashMap<String, Object>();
hints.put("widgetType", widgetType);
return createWidget(context, hints);
}
*/
/**
* Formats a value for display as HTML.
*
* @param the value to display
* @return the HTML to display the value
*/
public static String displayValue(String value) {
value = value.replace("<", "<");
value = value.replace(">", ">");
value = value.replace("\n", "<br/>");
return "<span class=\"value\">" + value + "</span>";
}
/**
* Returns the HTML to display an empty value.
*
* @param value
* @return the HTML to display the empty value
*/
public static String displayEmptyValue(String value) {
value = value.replace("<", "<");
value = value.replace(">", ">");
value = value.replace("\n", "<br/>");
return "<span class=\"emptyValue\">" + value + "</span>";
}
public static String displayDefaultEmptyValue() {
return displayEmptyValue("____________");
}
/**
* Returns the HTML to display the complex value. If the value is an image it is displayed by
* <img> </img> tag else it is displayed as hyperlink that can be downloaded by the user.
*
* @param obs the obs whose complex value needs to be displayed
* @return the HTML code that renders the complex obs
*/
public static String displayComplexValue(Obs obs) {
String hyperlink = getViewHyperlink(obs);
// check if concept is complex and uses ImageHandler. If so, display image
ConceptComplex complex = Context.getConceptService().getConceptComplex(obs.getConcept().getId());
if (complex != null) {
try {
if (ImageHandler.class.isAssignableFrom(Class.forName(AbstractHandler.class.getPackage().getName() + "."
+ complex.getHandler()))) {
String imgTag = "<img src='" + hyperlink + "' class=\"complexImage\" />";
return "<a href=\"" + hyperlink + "\" target=\"_blank\">" + imgTag + "</a><br/><a href=\"" + getDownloadHyperlink(obs)
+ "\" target=\"_blank\">" + Context.getMessageSourceService().getMessage("htmlformentry.form.complex.download")
+ "</a>";
}
}
catch (ClassNotFoundException e) {}
}
File file = AbstractHandler.getComplexDataFile(obs);
String fileName = file.getName();
String value = "<p class=\"value\">" + fileName + "<br/><a href=\"" + hyperlink + "\" target=\"_blank\">"
+ Context.getMessageSourceService().getMessage("htmlformentry.form.complex.view") + "</a> | <a href=\""
+ getDownloadHyperlink(obs) + "\" target=\"_blank\">"
+ Context.getMessageSourceService().getMessage("htmlformentry.form.complex.download") + "</a></p>";
return "<span>" + value + "</span>";
}
private static String getViewHyperlink(Obs obs) {
return "/" + WebConstants.WEBAPP_NAME + "/complexObsServlet?obsId=" + obs.getObsId();
}
private static String getDownloadHyperlink(Obs obs) {
return getViewHyperlink(obs) + "&view=download&viewType=download";
}
}