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.List;
25
26 import org.efaps.admin.datamodel.Attribute;
27 import org.efaps.admin.event.EventDefinition;
28 import org.efaps.admin.event.EventType;
29 import org.efaps.admin.event.Parameter;
30 import org.efaps.admin.event.Parameter.ParameterValues;
31 import org.efaps.admin.event.Return;
32 import org.efaps.admin.event.Return.ReturnValues;
33 import org.efaps.admin.ui.AbstractUserInterfaceObject.TargetMode;
34 import org.efaps.admin.ui.field.Field;
35 import org.efaps.admin.ui.field.Field.Display;
36 import org.efaps.db.Context;
37 import org.efaps.db.Instance;
38 import org.efaps.util.EFapsException;
39 import org.efaps.util.cache.CacheReloadException;
40
41
42
43
44
45
46
47 public final class UIValue
48 implements Serializable, IUIValue
49 {
50
51
52
53
54 private static final long serialVersionUID = 1L;
55
56
57
58
59 private Serializable dbValue;
60
61
62
63
64 private final long fieldId;
65
66
67
68
69 private final long attributeId;
70
71
72
73
74 private Display display;
75
76
77
78
79 private TargetMode targetMode;
80
81
82
83
84
85
86 private Instance instance;
87
88
89
90
91 private Object classObject;
92
93
94
95
96 private Instance callInstance;
97
98
99
100
101 private Object requestInstances;
102
103
104
105
106 private Object readOnlyValue;
107
108
109
110
111 private Object editValue;
112
113
114
115
116 private Object hiddenValue;
117
118
119
120
121
122
123 private UIValue(final Field _field,
124 final Attribute _attribute,
125 final Serializable _value)
126 {
127 this.fieldId = _field == null ? 0 : _field.getId();
128 this.attributeId = _attribute == null ? 0 : _attribute.getId();
129 this.dbValue = _value;
130 }
131
132
133
134
135
136
137
138 public static UIValue get(final Field _field,
139 final Attribute _attribute,
140 final Object _value)
141 {
142 UIValue ret;
143 if (_value instanceof Serializable) {
144 ret = new UIValue(_field, _attribute, (Serializable) _value);
145 } else if (_value == null) {
146 ret = new UIValue(_field, _attribute, null);
147 } else {
148
149 ret = new UIValue(_field, _attribute, null);
150 }
151 return ret;
152 }
153
154
155
156
157 @Override
158 public Field getField()
159 {
160 return Field.get(this.fieldId);
161 }
162
163 @Override
164 public String toString()
165 {
166 return String.valueOf(this.dbValue);
167 }
168
169
170
171
172
173
174
175
176
177
178 public Object getEditValue(final TargetMode _mode)
179 throws EFapsException
180 {
181 this.display = Display.EDITABLE;
182 this.targetMode = _mode;
183 if (this.editValue == null) {
184 Object obj = executeEvents(EventType.UI_FIELD_VALUE, _mode);
185 if (obj == null) {
186 obj = executeEvents(EventType.UI_FIELD_FORMAT, _mode);
187 if (obj == null && getUIProvider() != null) {
188 obj = getUIProvider().getValue(this);
189 }
190 } else if (getUIProvider() != null) {
191 obj = getUIProvider().transformObject(this, obj);
192 }
193 this.editValue = obj;
194 }
195 return this.editValue;
196 }
197
198
199
200
201
202
203
204
205
206
207 public Object getHiddenValue(final TargetMode _mode)
208 throws EFapsException
209 {
210 this.display = Display.HIDDEN;
211 this.targetMode = _mode;
212 if (this.hiddenValue == null) {
213 Object obj = executeEvents(EventType.UI_FIELD_VALUE, _mode);
214 if (obj == null) {
215 obj = executeEvents(EventType.UI_FIELD_FORMAT, _mode);
216 if (obj == null && getUIProvider() != null) {
217 obj = getUIProvider().getValue(this);
218 }
219 } else if (getUIProvider() != null) {
220 obj = getUIProvider().transformObject(this, obj);
221 }
222 this.hiddenValue = obj;
223 }
224 return this.hiddenValue;
225 }
226
227
228
229
230
231
232
233
234
235
236 public Object getReadOnlyValue(final TargetMode _mode)
237 throws EFapsException
238 {
239 this.display = Display.READONLY;
240 this.targetMode = _mode;
241 if (this.readOnlyValue == null) {
242 Object obj = executeEvents(EventType.UI_FIELD_VALUE, _mode);
243 if (obj == null) {
244 obj = executeEvents(EventType.UI_FIELD_FORMAT, _mode);
245 if (obj == null && getUIProvider() != null) {
246 obj = getUIProvider().getValue(this);
247 }
248 } else if (getUIProvider() != null) {
249 obj = getUIProvider().transformObject(this, obj);
250 }
251 this.readOnlyValue = obj;
252 }
253 return this.readOnlyValue;
254 }
255
256
257
258
259
260 public IUIProvider getUIProvider()
261 throws CacheReloadException
262 {
263 IUIProvider ret;
264 if (this.attributeId > 0) {
265 ret = getAttribute().getAttributeType().getUIProvider();
266 } else {
267 ret = getField().getUIProvider();
268 }
269 return ret;
270 }
271
272
273
274
275
276
277 @Override
278 public Display getDisplay()
279 {
280 return this.display;
281 }
282
283
284
285
286
287
288 public Serializable getDbValue()
289 {
290 return this.dbValue;
291 }
292
293
294
295
296
297 public Attribute getAttribute()
298 throws CacheReloadException
299 {
300 return Attribute.get(this.attributeId);
301 }
302
303
304
305
306
307
308 public TargetMode getTargetMode()
309 {
310 return this.targetMode;
311 }
312
313
314
315
316
317
318
319
320
321
322
323 protected Object executeEvents(final EventType _eventType,
324 final TargetMode _targetMode)
325 throws EFapsException
326 {
327 Object ret = null;
328 if (this.fieldId > 0 && getField().hasEvents(_eventType)) {
329
330 final List<EventDefinition> events = getField().getEvents(_eventType);
331
332 final StringBuilder html = new StringBuilder();
333 if (events != null) {
334 final Parameter parameter = new Parameter();
335 parameter.put(ParameterValues.ACCESSMODE, _targetMode);
336 parameter.put(ParameterValues.INSTANCE, this.instance);
337 parameter.put(ParameterValues.CLASS, this.classObject);
338 parameter.put(ParameterValues.UIOBJECT, this);
339 parameter.put(ParameterValues.CALL_INSTANCE, getCallInstance());
340 parameter.put(ParameterValues.REQUEST_INSTANCES, getRequestInstances());
341 if (parameter.get(ParameterValues.PARAMETERS) == null) {
342 parameter.put(ParameterValues.PARAMETERS, Context.getThreadContext().getParameters());
343 }
344 for (final EventDefinition evenDef : events) {
345 final Return retu = evenDef.execute(parameter);
346 if (retu.get(ReturnValues.SNIPLETT) != null) {
347 html.append(retu.get(ReturnValues.SNIPLETT));
348 } else if (retu.get(ReturnValues.VALUES) != null) {
349 ret = retu.get(ReturnValues.VALUES);
350 if (retu.get(ReturnValues.INSTANCE) != null) {
351 final Instance inst = (Instance) retu.get(ReturnValues.INSTANCE);
352 if (inst != null && inst.isValid()) {
353 setInstance(inst);
354 } else {
355 setInstance(null);
356 }
357 }
358 }
359 }
360 }
361 if (html.length() > 0) {
362 ret = html.toString();
363 }
364 }
365 return ret;
366 }
367
368
369
370
371
372
373 protected void setDbValue(final Serializable _dbValue)
374 {
375 this.dbValue = _dbValue;
376 }
377
378
379
380
381
382
383 @Override
384 public Instance getInstance()
385 {
386 return this.instance;
387 }
388
389
390
391
392
393
394
395 public UIValue setInstance(final Instance _instance)
396 {
397 this.instance = _instance;
398 return this;
399 }
400
401
402
403
404
405
406
407 public UIValue setCallInstance(final Instance _instance)
408 {
409 this.callInstance = _instance;
410 return this;
411 }
412
413
414
415
416
417
418 public Instance getCallInstance()
419 {
420 return this.callInstance;
421 }
422
423
424
425
426
427
428 public Object getClassObject()
429 {
430 return this.classObject;
431 }
432
433
434
435
436
437
438
439 public UIValue setClassObject(final Object _classObject)
440 {
441 this.classObject = _classObject;
442 return this;
443 }
444
445
446
447
448
449
450 public Object getRequestInstances()
451 {
452 return this.requestInstances;
453 }
454
455
456
457
458
459
460
461 public UIValue setRequestInstances(final Object _requestInstances)
462 {
463 this.requestInstances = _requestInstances;
464 return this;
465 }
466
467
468
469
470
471
472 public UIValue reset()
473 {
474 this.editValue = null;
475 this.hiddenValue = null;
476 this.readOnlyValue = null;
477 return this;
478 }
479 }