1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.efaps.admin.datamodel.ui;
22
23 import java.io.Serializable;
24 import java.util.Map;
25 import java.util.TreeMap;
26
27 import org.apache.commons.lang3.BooleanUtils;
28 import org.efaps.admin.datamodel.Attribute;
29 import org.efaps.admin.dbproperty.DBProperties;
30 import org.efaps.admin.ui.AbstractUserInterfaceObject.TargetMode;
31 import org.efaps.admin.ui.field.Field;
32 import org.efaps.util.EFapsException;
33 import org.efaps.util.cache.CacheReloadException;
34
35
36
37
38
39
40
41
42
43
44
45
46
47 public class BooleanUI
48 extends AbstractUI
49 {
50
51
52
53 private static final long serialVersionUID = 1L;
54
55
56
57
58 @Override
59 public String getEditHtml(final FieldValue _fieldValue)
60 {
61 final StringBuilder ret = new StringBuilder();
62 final Field field = _fieldValue.getField();
63 final Attribute attribute = _fieldValue.getAttribute();
64
65 final Boolean bool;
66 if (_fieldValue.getValue() instanceof Boolean) {
67 bool = (Boolean) _fieldValue.getValue();
68 } else if (_fieldValue.getValue() instanceof String && ((String) _fieldValue.getValue()).length() > 0) {
69 if (((String) _fieldValue.getValue()).equalsIgnoreCase("TRUE")) {
70 bool = true;
71 } else {
72 bool = false;
73 }
74 } else {
75 bool = null;
76 }
77
78 if (_fieldValue.getTargetMode().equals(TargetMode.SEARCH)) {
79 ret.append("<script language=\"javascript\" type=\"text/javascript\">")
80 .append("var checked").append(field.getName()).append(";")
81 .append("function Clear").append(field.getName()).append("(_btn) {")
82 .append("if (checked").append(field.getName()).append(" == _btn){")
83 .append("_btn.checked = false;")
84 .append("checked").append(field.getName()).append(" = null;")
85 .append("} else { ")
86 .append("checked").append(field.getName()).append(" = _btn; }")
87 .append("}").append("</script>")
88 .append("<input type=\"radio\" ").append((bool != null && bool) ? "checked=\"checked\" " : "")
89 .append("name=\"").append(field.getName()).append("\" ").append("value=\"").append("TRUE")
90 .append("\" onclick=\"Clear").append(field.getName()).append("(this)\"/>")
91 .append(getTrue(attribute)).append("<br/>")
92 .append("<input type=\"radio\" ").append((bool != null && !bool) ? "checked=\"checked\" " : "")
93 .append("name=\"").append(field.getName()).append("\" ").append("value=\"").append("FALSE")
94 .append("\" onclick=\"Clear").append(field.getName()).append("(this)\"/>").append(getFalse(attribute));
95 } else {
96 ret.append("<input type=\"radio\" ").append((bool != null && bool) ? "checked=\"checked\" " : "")
97 .append("name=\"").append(field.getName()).append("\" ").append("value=\"").append("TRUE")
98 .append("\"/>").append(getTrue(attribute)).append("<br/>")
99 .append("<input type=\"radio\" ").append((bool != null && !bool) ? "checked=\"checked\" " : "")
100 .append("name=\"").append(field.getName()).append("\" ").append("value=\"").append("FALSE")
101 .append("\"/>").append(getFalse(attribute));
102 }
103 return ret.toString();
104 }
105
106
107
108
109 @Override
110 public String getReadOnlyHtml(final FieldValue _fieldValue)
111 {
112 String ret = null;
113 final Attribute attribute = _fieldValue.getAttribute();
114 if (_fieldValue.getValue() instanceof Boolean) {
115 final boolean bool = (Boolean) _fieldValue.getValue();
116 if (bool) {
117 ret = getTrue(attribute);
118 } else {
119 ret = getFalse(attribute);
120 }
121 }
122 return ret;
123 }
124
125
126
127
128 @Override
129 public Object getObject4Compare(final FieldValue _fieldValue)
130 {
131 Object ret = null;
132 if (_fieldValue.getValue() instanceof Boolean) {
133 final boolean bool = (Boolean) _fieldValue.getValue();
134 if (bool) {
135 ret = getTrue(_fieldValue.getAttribute());
136 } else {
137 ret = getFalse(_fieldValue.getAttribute());
138 }
139 }
140 return ret;
141 }
142
143
144
145
146 @Override
147 public int compare(final FieldValue _fieldValue,
148 final FieldValue _fieldValue2)
149 {
150 String value = null;
151 String value2 = null;
152
153 if (_fieldValue.getValue() instanceof Boolean && _fieldValue2.getValue() instanceof Boolean) {
154 if ((Boolean) _fieldValue.getValue()) {
155 value = getTrue(_fieldValue.getAttribute());
156 } else {
157 value = getFalse(_fieldValue.getAttribute());
158 }
159 if ((Boolean) _fieldValue2.getValue()) {
160 value2 = getTrue(_fieldValue.getAttribute());
161 } else {
162 value2 = getFalse(_fieldValue.getAttribute());
163 }
164 }
165
166 if (_fieldValue.getValue() instanceof String && _fieldValue2.getValue() instanceof String) {
167 value = (String) _fieldValue.getValue();
168 value2 = (String) _fieldValue2.getValue();
169 }
170 return value.compareTo(value2);
171 }
172
173
174
175
176
177
178
179 private String getFalse(final Attribute _attribute)
180 {
181 String ret;
182
183 if (DBProperties.hasProperty(_attribute.getKey() + ".false")) {
184 ret = DBProperties.getProperty(_attribute.getKey() + ".false");
185 } else {
186 ret = "FALSE";
187 }
188 return ret;
189 }
190
191
192
193
194
195
196
197 private String getTrue(final Attribute _attribute)
198 {
199 String ret;
200 if (DBProperties.hasProperty(_attribute.getKey() + ".true")) {
201 ret = DBProperties.getProperty(_attribute.getKey() + ".true");
202 } else {
203 ret = "TRUE";
204 }
205 return ret;
206 }
207
208
209
210
211
212
213
214
215 private String getLabel(final UIValue _uiValue,
216 final Boolean _key)
217 throws CacheReloadException
218 {
219 String ret = BooleanUtils.toStringTrueFalse(_key);
220 if (_uiValue.getAttribute() != null
221 && DBProperties.hasProperty(_uiValue.getAttribute().getKey() + "."
222 + BooleanUtils.toStringTrueFalse(_key))) {
223 ret = DBProperties.getProperty(_uiValue.getAttribute().getKey() + "."
224 + BooleanUtils.toStringTrueFalse(_key));
225 } else if (DBProperties
226 .hasProperty(_uiValue.getField().getLabel() + "." + BooleanUtils.toStringTrueFalse(_key))) {
227 ret = DBProperties.getProperty(_uiValue.getField().getLabel() + "." + BooleanUtils.toStringTrueFalse(_key));
228 }
229 return ret;
230 }
231
232 @Override
233 public Object getValue(final UIValue _uiValue)
234 throws EFapsException
235 {
236 final Map<Object, Object> ret = new TreeMap<Object, Object>();
237 ret.put(getLabel(_uiValue, Boolean.TRUE), Boolean.TRUE);
238 ret.put(getLabel(_uiValue, Boolean.FALSE), Boolean.FALSE);
239 return ret;
240 }
241
242
243
244
245 @Override
246 public Object transformObject(final UIValue _uiValue,
247 final Object _object)
248 throws EFapsException
249 {
250 Object ret = null;
251 if (_object instanceof Map) {
252 ret = _object;
253 } else if (_object instanceof Serializable) {
254 _uiValue.setDbValue((Serializable) _object);
255 ret = getValue(_uiValue);
256 }
257 return ret;
258 }
259 }