View Javadoc

1   /**
2    * 2008, Digitalis Informatica. All rights reserved. Distribuicao e Gestao de Informatica, Lda. Estrada de Paco de Arcos
3    * num.9 - Piso -1 2780-666 Paco de Arcos Telefone: (351) 21 4408990 Fax: (351) 21 4408999 http://www.digitalis.pt
4    */
5   
6   package pt.digitalis.dif.dem.objects.parameters.rules;
7   
8   import java.util.ArrayList;
9   import java.util.Arrays;
10  import java.util.List;
11  import java.util.Map;
12  
13  import pt.digitalis.dif.dem.annotations.AnnotationTags;
14  import pt.digitalis.dif.dem.interfaces.IStageInstance;
15  import pt.digitalis.dif.dem.managers.IMessageManager;
16  import pt.digitalis.dif.dem.objects.parameters.IParameter;
17  import pt.digitalis.dif.exception.objects.ParameterException;
18  import pt.digitalis.dif.ioc.DIFIoCRegistry;
19  import pt.digitalis.dif.utils.ObjectFormatter;
20  import pt.digitalis.utils.common.StringUtils;
21  
22  /**
23   * A base implementation for all parameter rules
24   * 
25   * @author Pedro Viegas <a href="mailto:pviegas@digitalis.pt">pviegas@digitalis.pt</a><br/>
26   * @param <T>
27   *            the parameter base type
28   * @created Mar 31, 2009
29   */
30  public abstract class AbstractParameterRule<T> implements IParameterRule<T> {
31  
32      /** The rule action */
33      private ParameterRuleAction action;
34  
35      /** The description for end value */
36      private String descriptionEndValue = null;
37  
38      /** The description for start value */
39      private String descriptionStartValue = null;
40  
41      /** The description value */
42      private String descriptionValue = null;
43  
44      /** The end value for validations */
45      private String endValue = null;
46  
47      /** The parameter that this rule is associated to */
48      protected String parameterID;
49  
50      /** the rule parameters */
51      private List<String> parameters = new ArrayList<String>();
52  
53      /** The start value for validations */
54      private String startValue = null;
55  
56      /** The value for validation */
57      private String value = null;
58  
59      /**
60       * @see pt.digitalis.dif.dem.objects.parameters.rules.IParameterRule#getAction()
61       */
62      public ParameterRuleAction getAction()
63      {
64          return action;
65      }
66  
67      /**
68       * @see pt.digitalis.dif.dem.objects.parameters.rules.IParameterRule#getDescriptionEndValue()
69       */
70      public String getDescriptionEndValue()
71      {
72          return descriptionEndValue;
73      }
74  
75      /**
76       * @see pt.digitalis.dif.dem.objects.parameters.rules.IParameterRule#getDescriptionStartValue()
77       */
78      public String getDescriptionStartValue()
79      {
80          return descriptionStartValue;
81      }
82  
83      /**
84       * @see pt.digitalis.dif.dem.objects.parameters.rules.IParameterRule#getDescriptionValue()
85       */
86      public String getDescriptionValue()
87      {
88          return descriptionValue;
89      }
90  
91      /**
92       * @see pt.digitalis.dif.dem.objects.parameters.rules.IParameterRule#getEndValue()
93       */
94      public String getEndValue()
95      {
96          return endValue;
97      }
98  
99      /**
100      * Returns the list of messages available for the parameter rule
101      * 
102      * @param stage
103      *            the stage instance that the parameter is associated to
104      * @return the parameter rule Messages
105      */
106     protected Map<String, String> getMessages(IStageInstance stage)
107     {
108         return DIFIoCRegistry.getRegistry().getImplementation(IMessageManager.class)
109                 .collectEntityMessagesFromRepository(this.getClass()).getMessages(stage.getContext().getLanguage());
110     }
111 
112     /**
113      * Inspector for the 'parameter' attribute.
114      * 
115      * @param stage
116      *            the stage instance that the parameter is associated to
117      * @return the parameter value
118      * @throws ParameterException
119      *             if the parameter does not exist
120      */
121     @SuppressWarnings("unchecked")
122     public IParameter<T> getParameter(IStageInstance stage) throws ParameterException
123     {
124         return (IParameter<T>) stage.getParameters().getAllAvailableParameters().getParameter(parameterID);
125     }
126 
127     /**
128      * Get's the name of a given parameter from the stage messages. When missing the parameter ID is returned (camel
129      * case interpreted)
130      * 
131      * @param stage
132      *            the parameter's current stage
133      * @param parameterID
134      *            the parameter ID to fetch the name
135      * @return the parameter name
136      */
137     public String getParameterName(IStageInstance stage, String parameterID)
138     {
139         String message = stage.getMessage(parameterID);
140 
141         if (message == null)
142             message = StringUtils.camelCaseToString(parameterID);
143 
144         return message;
145     }
146 
147     /**
148      * @see pt.digitalis.dif.dem.objects.parameters.rules.IParameterRule#getParameters()
149      */
150     public List<String> getParameters()
151     {
152         return parameters;
153     }
154 
155     /**
156      * @see pt.digitalis.dif.dem.objects.parameters.rules.IParameterRule#getStartValue()
157      */
158     public String getStartValue()
159     {
160         return startValue;
161     }
162 
163     /**
164      * @see pt.digitalis.dif.dem.objects.parameters.rules.IParameterRule#getValue()
165      */
166     public String getValue()
167     {
168         return value;
169     }
170 
171     /**
172      * @see pt.digitalis.dif.dem.objects.parameters.rules.IParameterRule#init(java.lang.String, java.lang.String,
173      *      pt.digitalis.dif.dem.objects.parameters.rules.ParameterRuleAction, java.lang.String, java.lang.String,
174      *      java.lang.String)
175      */
176     public AbstractParameterRule<T> init(String parameterID, String parameters, ParameterRuleAction action,
177             String value, String first, String last)
178     {
179         this.parameterID = parameterID;
180         this.action = action;
181 
182         if (parameters != null && !"".equals(parameters))
183         {
184             List<String> parametersPassed = Arrays.asList(parameters.split(","));
185 
186             for (String parameterPassed: parametersPassed)
187                 this.parameters.add(parameterPassed.trim().toLowerCase());
188         }
189 
190         if (value != null && !value.contains(AnnotationTags.NONE.toString()) && value.contains(":"))
191         {
192             String[] splitCommaValues = value.split(",");
193             for (String v: splitCommaValues)
194             {
195                 String[] splitValue = v.split(":");
196                 if (this.value != null)
197                 {
198                     this.value += ",";
199                 }
200                 else
201                 {
202                     this.value = "";
203                 }
204                 this.value += splitValue[0];
205 
206                 if (this.descriptionValue != null)
207                 {
208                     this.descriptionValue += ",";
209                 }
210                 else
211                 {
212                     this.descriptionValue = "";
213                 }
214                 this.descriptionValue += splitValue[1];
215             }
216         }
217         else
218         {
219             this.value = value;
220         }
221 
222         if (first != null && !first.contains(AnnotationTags.NONE.toString()) && first.contains(":"))
223         {
224             String[] splitValue = first.split(":");
225             this.startValue = splitValue[0];
226             this.descriptionStartValue = splitValue[1];
227         }
228         else
229         {
230             this.startValue = first;
231         }
232 
233         if (last != null && !last.contains(AnnotationTags.NONE.toString()) && last.contains(":"))
234         {
235             String[] splitValue = last.split(":");
236             this.endValue = splitValue[0];
237             this.descriptionEndValue = splitValue[1];
238         }
239         else
240         {
241             this.endValue = last;
242         }
243 
244         return this;
245     }
246 
247     /**
248      * @see pt.digitalis.dif.dem.objects.parameters.rules.IParameterRule#setDescriptionEndValue(java.lang.String)
249      */
250     public void setDescriptionEndValue(String descriptionEndValue)
251     {
252         this.descriptionEndValue = descriptionEndValue;
253     }
254 
255     /**
256      * @see pt.digitalis.dif.dem.objects.parameters.rules.IParameterRule#setDescriptionStartValue(java.lang.String)
257      */
258     public void setDescriptionStartValue(String descriptionStartValue)
259     {
260         this.descriptionStartValue = descriptionStartValue;
261     }
262 
263     /**
264      * @see pt.digitalis.dif.dem.objects.parameters.rules.IParameterRule#setDescriptionValue(java.lang.String)
265      */
266     public void setDescriptionValue(String descriptionValue)
267     {
268         this.descriptionValue = descriptionValue;
269     }
270 
271     /**
272      * @see pt.digitalis.dif.dem.objects.parameters.rules.IParameterRule#setEndValue(java.lang.String)
273      */
274     public AbstractParameterRule<T> setEndValue(String endValue)
275     {
276         this.endValue = endValue;
277 
278         return this;
279     }
280 
281     /**
282      * @see pt.digitalis.dif.dem.objects.parameters.rules.IParameterRule#setStartValue(java.lang.String)
283      */
284     public AbstractParameterRule<T> setStartValue(String startValue)
285     {
286         this.startValue = startValue;
287 
288         return this;
289     }
290 
291     /**
292      * @see pt.digitalis.dif.dem.objects.parameters.rules.IParameterRule#setValue(java.lang.String)
293      */
294     public AbstractParameterRule<T> setValue(String value)
295     {
296         this.value = value;
297 
298         return this;
299     }
300 
301     /**
302      * @see java.lang.Object#toString()
303      */
304     @Override
305     public String toString()
306     {
307         ObjectFormatter formatter = new ObjectFormatter();
308         formatter.addItem("Action", this.action);
309         formatter.addItem("End Value", this.endValue);
310         formatter.addItem("Parameter ID", this.parameterID);
311         formatter.addItem("Parameters", this.parameters);
312         formatter.addItem("Start Value", this.startValue);
313         formatter.addItem("Value", this.value);
314         formatter.addItem("Description Value", this.descriptionValue);
315         formatter.addItem("Description Start Value", this.descriptionStartValue);
316         formatter.addItem("Description End Value", this.descriptionEndValue);
317 
318         return formatter.getFormatedObject();
319     }
320 }