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.event;
22  
23  import java.io.Serializable;
24  import java.util.HashMap;
25  import java.util.Map;
26  import java.util.Set;
27  
28  import org.apache.commons.lang3.builder.ToStringBuilder;
29  import org.efaps.db.Instance;
30  
31  /**
32   * Class witch is used for parsing Parameters to the Events.
33   *
34   * @author The eFaps Team
35   * @version $Id$
36   */
37  public class Parameter
38      implements Serializable
39  {
40      /**
41       * Serializable.
42       */
43      private static final long serialVersionUID = 1L;
44  
45      /**
46       * This enum holds the definitions of Parameters, to be accessed.
47       */
48      public enum ParameterValues
49      {
50          /**
51           * Holds an AccessType, used for AccessCheck-Programs.
52           */
53          ACCESSTYPE,
54          /**
55           * Holds the mode of the access for the ui.
56           */
57          ACCESSMODE,
58          /**
59           * BPM TaskInformation.
60           */
61          BPM_TASK,
62          /**
63           * BPM Values Map.
64           */
65          BPM_VALUES,
66          /**
67           * BPM Decision boolean.
68           */
69          BPM_DECISION,
70          /**
71           * Call instance, means
72           * <ul>
73           * <li>for a web table, the instance for which the table values are
74           * evaluated</li>
75           * <li>for a web form, on which the web form is executed (if exists);
76           * e.g. in edit mode it is the instance of the called object</li>
77           * <li>for a command the instance on which the command was executed</li>
78           * </ul>
79           */
80          CALL_INSTANCE,
81          /**
82           * The cmd that initiated the call. (e.g. The cmd that opened the form)
83           */
84          CALL_CMD,
85          /**
86           * Contains the class that called the esjp.
87           */
88          CLASS,
89          /**
90           * Contains a list of classifcations.
91           */
92          CLASSIFICATIONS,
93          /**
94           * Holds an Instance.
95           * */
96          INSTANCE,
97          /**
98           * Holds the new Values for an Instance, used e.g. by Creation of a new
99           * Object
100          */
101         NEW_VALUES,
102         /**
103          * Holds an Map used to obfuscate the oids for presentation in the UserInterface.
104          */
105         OIDMAP4UI,
106         /**
107          * Further Parameters as map (key is string, value is string array),
108          * e.g. from called form, command etc.
109          */
110         PARAMETERS,
111         /**
112          * Parameters that were stored from a parent so that the child can used them also.
113          * Contains a map (key is string, value is string array),
114          * e.g. used by pickers to have the parameters from the form that opened it
115          */
116         PARENTPARAMETERS,
117         /**
118          * Holds the Properties of the trigger.
119          */
120         PROPERTIES,
121         /**
122          * Place mark for additional Informations.
123          */
124         OTHERS,
125         /**
126          * Instances that where retrieved in the same request as the instance.
127          */
128         REQUEST_INSTANCES,
129         /**
130          * Holds the UserInterfaceObject on which the event is called.
131          */
132         UIOBJECT;
133     }
134 
135     /**
136      * Map used as the store for this Parameter.
137      */
138     private final Map<Parameter.ParameterValues, Object> map = new HashMap<Parameter.ParameterValues, Object>();
139 
140     /**
141      * Put an object into the underlying map.
142      *
143      * @param _key key to the object
144      * @param _value object
145      */
146     public void put(final ParameterValues _key, final Object _value)
147     {
148         this.map.put(_key, _value);
149     }
150 
151     /**
152      * Method to get an object from the underlying map.
153      *
154      * @param _key key to the object
155      * @return object from the underlying map
156      */
157     public Object get(final ParameterValues _key)
158     {
159         return this.map.get(_key);
160     }
161 
162     /**
163      * Returns the value of map with the key
164      * {@link ParameterValues#CALL_INSTANCE}.
165      *
166      * @return call instance of this parameter; or if not defined
167      *         <code>null</code>
168      */
169     public Instance getCallInstance()
170     {
171         return (Instance) this.map.get(Parameter.ParameterValues.CALL_INSTANCE);
172     }
173 
174     /**
175      * Returns the value of map with the key {@link ParameterValues#INSTANCE}.
176      *
177      * @return instance of this parameter; or if not defined <code>null</code>
178      */
179     public Instance getInstance()
180     {
181         return (Instance) this.map.get(Parameter.ParameterValues.INSTANCE);
182     }
183 
184     /**
185      * Returns the value of map with the key {@link ParameterValues#PARAMETERS}.
186      *
187      * @return further parameters of this parameter; or if not defined
188      *         <code>null</code>
189      */
190     @SuppressWarnings("unchecked")
191     public Map<String, String[]> getParameters()
192     {
193         return (Map<String, String[]>) this.map.get(Parameter.ParameterValues.PARAMETERS);
194     }
195 
196     /**
197      * Evaluates with given key in the list of all parameters for given key and
198      * returns them (if found) as string array. If not found a <code>null</code>
199      * is returned.
200      *
201      * @param _key name of parameter values which should returned
202      * @return array of parameter values for given key of <code>null</code> if
203      *         not exists
204      * @see #getParameters to get the map of parameters
205      */
206     public String[] getParameterValues(final String _key)
207     {
208         final Map<String, String[]> params = getParameters();
209         return params != null ? params.get(_key) : null;
210     }
211 
212     /**
213      * Evaluates with given key in the list of all parameters for given key and
214      * returns them (if found) as string with index 0 in the string array of the
215      * parameter values. If not found a <code>null</code> is returned.
216      *
217      * @param _key name of parameter value which should returned
218      * @return value for given key or <code>null</code> if not exists
219      * @see #getParameterValues to get the string array for given key
220      */
221     public String getParameterValue(final String _key)
222     {
223         final String[] paramValues = getParameterValues(_key);
224         return paramValues != null && paramValues.length > 0 ? paramValues[0] : null;
225     }
226 
227     /**
228      * Method to get the entry set of the underlying map.
229      *
230      * @return entry set
231      */
232     public Set<?> entrySet()
233     {
234         return this.map.entrySet();
235     }
236 
237     /**
238      * Returns a string representation of this parameter instance.
239      *
240      * @return string representation of this parameter instance.
241      */
242     @Override
243     public String toString()
244     {
245         return new ToStringBuilder(this).appendSuper(super.toString()).append("map", this.map.toString()).toString();
246     }
247 }