View Javadoc

1   /**
2    * 2007, 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   package pt.digitalis.dif.dem.objects.parameters.errors;
6   
7   import java.util.ArrayList;
8   import java.util.List;
9   
10  import pt.digitalis.dif.dem.objects.parameters.constraints.IParameterConstraint;
11  import pt.digitalis.dif.dem.objects.parameters.constraints.ParameterConstraintResult;
12  import pt.digitalis.dif.dem.objects.parameters.rules.IParameterRule;
13  import pt.digitalis.dif.dem.objects.parameters.validators.IParameterValidator;
14  import pt.digitalis.dif.utils.ObjectFormatter;
15  
16  /**
17   * Defines a type that represents an error on a parameter.
18   * 
19   * @author Rodrigo Gonçalves <a href="mailto:rgoncalves@digitalis.pt">rgoncalves@digitalis.pt</a><br/>
20   * @author Pedro Viegas <a href="mailto:pviegas@digitalis.pt">pviegas@digitalis.pt</a>
21   */
22  public class ParameterError {
23  
24      /** The constraint that caused the error */
25      private IParameterConstraint constraint;
26  
27      /** The parameter error IDs */
28      private List<String> errorIDs = new ArrayList<String>();
29  
30      /** The type of the error */
31      private ParameterErrorType errorType;
32  
33      /** The generated exception if it is the case */
34      private Exception exception;
35  
36      /** Always report these errors */
37      private boolean forceShowError = false;
38  
39      /** The id of the error. ID of the constrain/validator or key that identifies the other kind of error */
40      private String id;
41  
42      /**
43       * The message that describes the error
44       */
45      private String message;
46  
47      /** The rule that caused the error */
48      private IParameterRule<?> rule;
49  
50      /** The validator that caused the error */
51      private IParameterValidator validator;
52  
53      /**
54       * Constructor
55       */
56      public ParameterError()
57      {
58          super();
59      }
60  
61      /**
62       * Constructor
63       * 
64       * @param constraintId
65       *            The constraint Id
66       * @param constraintResult
67       *            The constraint result
68       */
69      public ParameterError(String constraintId, ParameterConstraintResult constraintResult)
70      {
71          if (constraintResult.getLanguage() != null)
72          {
73              this.message = constraintResult.getConstraint().validationErrorMessage(constraintResult.getLanguage());
74          }
75          else
76          {
77              this.message = constraintResult.getConstraint().validationErrorMessage();
78          }
79  
80          this.errorType = ParameterErrorType.CONSTRAINT;
81          this.id = constraintId;
82          this.constraint = constraintResult.getConstraint();
83          this.validator = null;
84          if (constraintResult.getErrorIDs() != null)
85          {
86              this.errorIDs.addAll(constraintResult.getErrorIDs());
87          }
88      }
89  
90      /**
91       * Constructor.
92       * 
93       * @param message
94       *            the error message.
95       * @param errorType
96       *            the error type
97       */
98      public ParameterError(String message, ParameterErrorType errorType)
99      {
100         this.message = message;
101         this.errorType = errorType;
102         this.id = null;
103         this.constraint = null;
104         this.validator = null;
105     }
106 
107     /**
108      * Inspector for the error constraint.
109      * 
110      * @return the constraint the constraint
111      */
112     public IParameterConstraint getConstraint()
113     {
114         return constraint;
115     }
116 
117     /**
118      * Inspector for the 'errorIDs' attribute.
119      * 
120      * @return the errorIDs value
121      */
122     public List<String> getErrorIDs()
123     {
124         return errorIDs;
125     }
126 
127     /**
128      * Inspector for the error type.
129      * 
130      * @return the errorType the error type
131      */
132     public ParameterErrorType getErrorType()
133     {
134         return errorType;
135     }
136 
137     /**
138      * Inspector for the error exception.
139      * 
140      * @return the exception the error exception
141      */
142     public Exception getException()
143     {
144         return exception;
145     }
146 
147     /**
148      * Inspector for the error ID.
149      * 
150      * @return the id the error ID.
151      */
152     public String getId()
153     {
154         return id;
155     }
156 
157     /**
158      * Inspector for the error message.
159      * 
160      * @return the message the error message
161      */
162     public String getMessage()
163     {
164         return message;
165     }
166 
167     /**
168      * Inspector for the 'rule' attribute.
169      * 
170      * @return the rule value
171      */
172     public IParameterRule<?> getRule()
173     {
174         return rule;
175     }
176 
177     /**
178      * Inspector for the validator.
179      * 
180      * @return the validator
181      */
182     public IParameterValidator getValidator()
183     {
184         return validator;
185     }
186 
187     /**
188      * Inspector for the 'forceShowError' attribute.
189      * 
190      * @return the forceShowError value
191      */
192     public boolean isForceShowError()
193     {
194         return forceShowError;
195     }
196 
197     /**
198      * Modifier for the constraint.
199      * 
200      * @param id
201      *            the id of the constraint
202      * @param constraint
203      *            the constraint to set
204      */
205     public void setConstraint(String id, IParameterConstraint constraint)
206     {
207         this.id = id;
208         this.constraint = constraint;
209     }
210 
211     /**
212      * Modifier for the 'errorIDs' attribute.
213      * 
214      * @param errorIDs
215      *            the new errorIDs value to set
216      */
217     public void setErrorIDs(List<String> errorIDs)
218     {
219         this.errorIDs = errorIDs;
220     }
221 
222     /**
223      * Modifier for the exception.
224      * 
225      * @param exception
226      *            the exception to set
227      */
228     public void setException(Exception exception)
229     {
230         this.exception = exception;
231     }
232 
233     /**
234      * Modifier for the 'forceShowError' attribute.
235      * 
236      * @param forceShowError
237      *            the new forceShowError value to set
238      */
239     public void setForceShowError(boolean forceShowError)
240     {
241         this.forceShowError = forceShowError;
242     }
243 
244     /**
245      * Inspector for the error ID.
246      * 
247      * @param newID
248      *            the new ID.
249      */
250     public void setId(String newID)
251     {
252         this.id = newID;
253     }
254 
255     /**
256      * Modifier for the 'message' attribute.
257      * 
258      * @param message
259      *            the new message value to set
260      */
261     public void setMessage(String message)
262     {
263         this.message = message;
264     }
265 
266     /**
267      * Modifier for the 'rule' attribute.
268      * 
269      * @param rule
270      *            the new rule value to set
271      */
272     public void setRule(IParameterRule<?> rule)
273     {
274         this.rule = rule;
275     }
276 
277     /**
278      * Modifier for the validator.
279      * 
280      * @param id
281      *            the id of the validator
282      * @param validator
283      *            the validator to set
284      */
285     public void setValidator(String id, IParameterValidator validator)
286     {
287         this.id = id;
288         this.validator = validator;
289     }
290 
291     /**
292      * @see java.lang.Object#toString()
293      */
294     @Override
295     public String toString()
296     {
297         ObjectFormatter formatter = new ObjectFormatter();
298         formatter.addItem("Message", message);
299         formatter.addItem("Error Type", errorType);
300         formatter.addItemIfNotNull("ID", id);
301         formatter.addItemIfNotNull("Constraint", constraint);
302         formatter.addItemIfNotNull("Validator", validator);
303         formatter.addItemIfNotNull("Exception", exception);
304 
305         return formatter.getFormatedObject();
306     }
307 }