1   /*
2    * Copyright 2003 - 2013 The eFaps Team
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *
16   * Revision:        $Rev$
17   * Last Changed:    $Date$
18   * Last Changed By: $Author$
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   * Wrapper Class to get a value for a UserInterface.
43   *
44   * @author The eFaps Team
45   * @version $Id$
46   */
47  public final class UIValue
48      implements Serializable, IUIValue
49  {
50  
51      /**
52       *Needed foer serialization.
53       */
54      private static final long serialVersionUID = 1L;
55  
56      /**
57       * Value form the database.
58       */
59      private Serializable dbValue;
60  
61      /**
62       * Id of the field this Value belongs to.
63       */
64      private final long fieldId;
65  
66      /**
67       * Id of the Attribute this value belongs to.
68       */
69      private final long attributeId;
70  
71      /**
72       * Stores he display mode for this field.
73       */
74      private Display display;
75  
76      /**
77       * The TargetMode the value is wanted for.
78       */
79      private TargetMode targetMode;
80  
81      /**
82       * The variable stores the field instance for this value.
83       *
84       * @see #getInstance
85       */
86      private Instance instance;
87  
88      /**
89       * Object that will be passed to the event as CLASS.
90       */
91      private Object classObject;
92  
93      /**
94       * Object that will be passed to the event as CLASS.
95       */
96      private Instance callInstance;
97  
98      /**
99       * Object that will be passed to the event as CLASS.
100      */
101     private Object requestInstances;
102 
103     /**
104      *  Value in case of readonly.
105      */
106     private Object readOnlyValue;
107 
108     /**
109      *  Value in case of edit.
110      */
111     private Object editValue;
112 
113     /**
114      * Value in case of hidden.
115      */
116     private Object hiddenValue;
117 
118     /**
119      * @param _field        Field
120      * @param _attribute    attribute
121      * @param _value        value
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      * @param _field        Field
134      * @param _attribute    attribute
135      * @param _value        value
136      * @return  UIValue
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             // throw warning!
149             ret = new UIValue(_field, _attribute, null);
150         }
151         return ret;
152     }
153 
154     /**
155      * @return the field belonging to this UIValue
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      * Method to get a plain string for this FieldValue .
171      *
172      * @see #executeEvents
173      * @param _mode target mode
174      * @throws EFapsException on error
175      * @return plain string
176      * @throws EFapsException
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      * Method to get a plain string for this FieldValue .
200      *
201      * @see #executeEvents
202      * @param _mode target mode
203      * @throws EFapsException on error
204      * @return plain string
205      * @throws EFapsException
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      * Method to get a plain string for this FieldValue .
229      *
230      * @see #executeEvents
231      * @param _mode target mode
232      * @throws EFapsException on error
233      * @return plain string
234      * @throws EFapsException
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      * @return the UIProvider for this value
258      * @throws CacheReloadException on eror
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      * Getter method for the instance variable {@link #display}.
274      *
275      * @return value of instance variable {@link #display}
276      */
277     @Override
278     public Display getDisplay()
279     {
280         return this.display;
281     }
282 
283     /**
284      * Getter method for the instance variable {@link #dbValue}.
285      *
286      * @return value of instance variable {@link #dbValue}
287      */
288     public Serializable getDbValue()
289     {
290         return this.dbValue;
291     }
292 
293     /**
294      * @return Attribute
295      * @throws CacheReloadException on error
296      */
297     public Attribute getAttribute()
298         throws CacheReloadException
299     {
300         return Attribute.get(this.attributeId);
301     }
302 
303     /**
304      * Getter method for the instance variable {@link #targetMode}.
305      *
306      * @return value of instance variable {@link #targetMode}
307      */
308     public TargetMode getTargetMode()
309     {
310         return this.targetMode;
311     }
312 
313     /**
314      * Executes the field value events for a field.
315      *
316      * @param _eventType    type of event to be executed
317      * @param _targetMode   targetmode
318      * @throws EFapsException on error
319      * @return string from called field value events or <code>null</code> if no
320      *         field value event is defined
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      * Setter method for instance variable {@link #dbValue}.
370      *
371      * @param _dbValue value for instance variable {@link #dbValue}
372      */
373     protected void setDbValue(final Serializable _dbValue)
374     {
375         this.dbValue = _dbValue;
376     }
377 
378     /**
379      * Getter method for the instance variable {@link #instance}.
380      *
381      * @return value of instance variable {@link #instance}
382      */
383     @Override
384     public Instance getInstance()
385     {
386         return this.instance;
387     }
388 
389     /**
390      * Setter method for instance variable {@link #instance}.
391      *
392      * @param _instance value for instance variable {@link #instance}
393      * @return this, for chaining
394      */
395     public UIValue setInstance(final Instance _instance)
396     {
397         this.instance = _instance;
398         return this;
399     }
400 
401     /**
402      * Setter method for instance variable {@link #instance}.
403      *
404      * @param _instance value for instance variable {@link #instance}
405      * @return this, for chaining
406      */
407     public UIValue setCallInstance(final Instance _instance)
408     {
409         this.callInstance = _instance;
410         return this;
411     }
412 
413     /**
414      * Getter method for the instance variable {@link #callInstance}.
415      *
416      * @return value of instance variable {@link #callInstance}
417      */
418     public Instance getCallInstance()
419     {
420         return this.callInstance;
421     }
422 
423     /**
424      * Getter method for the instance variable {@link #classObject}.
425      *
426      * @return value of instance variable {@link #classObject}
427      */
428     public Object getClassObject()
429     {
430         return this.classObject;
431     }
432 
433     /**
434      * Setter method for instance variable {@link #classObject}.
435      *
436      * @param _classObject value for instance variable {@link #classObject}
437      * @return this, for chaining
438      */
439     public UIValue setClassObject(final Object _classObject)
440     {
441         this.classObject = _classObject;
442         return this;
443     }
444 
445     /**
446      * Getter method for the instance variable {@link #requestInstances}.
447      *
448      * @return value of instance variable {@link #requestInstances}
449      */
450     public Object getRequestInstances()
451     {
452         return this.requestInstances;
453     }
454 
455     /**
456      * Setter method for instance variable {@link #requestInstances}.
457      *
458      * @param _requestInstances value for instance variable {@link #requestInstances}
459      * @return this, for chaining
460      */
461     public UIValue setRequestInstances(final Object _requestInstances)
462     {
463         this.requestInstances = _requestInstances;
464         return this;
465     }
466 
467     /**
468      * Reset the UIValue, meaning that the values will be evaluated again.
469      *
470      * @return this, for chaining
471      */
472     public UIValue reset()
473     {
474         this.editValue = null;
475         this.hiddenValue = null;
476         this.readOnlyValue = null;
477         return this;
478     }
479 }