View Javadoc
1   /**
2    * Copyright 2005-2016 The Kuali Foundation
3    *
4    * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
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  package org.kuali.rice.krad.datadictionary.validator;
17  
18  import java.util.ArrayList;
19  
20  import org.apache.commons.logging.Log;
21  import org.apache.commons.logging.LogFactory;
22  
23  /**
24   * Collection of information regarding a single error detected within a dictionary bean
25   *
26   * @author Kuali Rice Team (rice.collab@kuali.org)
27   */
28  public class ErrorReport {
29      private static final Log LOG = LogFactory.getLog(ErrorReport.class);
30  
31      // Constant identifiers of the type of error
32      public static final int ERROR = 1;
33      public static final int WARNING = 2;
34  
35      private static final String endl = System.getProperty("line.separator");
36  
37      // Is type of error detailed in the report
38      private int errorStatus;
39      private String validationFailed;
40      private String beanLocation;
41      private ArrayList<String> currentValues;
42      private ArrayList<String> xmlPages;
43  
44      /**
45       * Constructor creating a new report for an error
46       *
47       * @param ErrorStatus - The type of error being reported
48       */
49      public ErrorReport(int ErrorStatus) {
50          this.errorStatus = ErrorStatus;
51          this.validationFailed = "";
52          this.beanLocation = "";
53          this.currentValues = new ArrayList<String>();
54          this.xmlPages = new ArrayList<String>();
55      }
56  
57      /**
58       * Constructor creating a new report for an error with set values
59       *
60       * @param errorStatus - The type of error being reported
61       * @param validationFailed - The validation that was failed
62       * @param beanLocation - The location of the bean in which error occured
63       * @param values - An array of the values effected
64       */
65      public ErrorReport(int errorStatus, String validationFailed, String beanLocation, String values[]) {
66          this.errorStatus = errorStatus;
67          this.validationFailed = validationFailed;
68          this.beanLocation = beanLocation;
69          this.currentValues = new ArrayList<String>();
70          this.xmlPages = new ArrayList<String>();
71  
72          for (int i = 0; i < values.length; i++) {
73              addCurrentValue(values[i]);
74          }
75      }
76  
77      /**
78       * Constructor creating a new report for an error with set values
79       *
80       * @param errorStatus - The type of error being reported
81       * @param validationFailed - The validation that was failed
82       * @param trace - ValidationTrace containing information on xml files and location
83       */
84      public ErrorReport(int errorStatus, String validationFailed, ValidationTrace trace) {
85          this.errorStatus = errorStatus;
86          this.validationFailed = validationFailed;
87          this.xmlPages = trace.getRelatedXmls();
88          this.beanLocation = trace.getBeanLocation();
89          this.currentValues = new ArrayList<String>();
90      }
91  
92      /**
93       * Constructor creating a new report for an error with set values
94       *
95       * @param errorStatus - The type of error being reported
96       * @param validationFailed - The validation that was failed
97       * @param trace - ValidationTrace containing information on xml files and location
98       * @param values - An array of the values effected
99       */
100     public ErrorReport(int errorStatus, String validationFailed, ValidationTrace trace, String values[]) {
101         this.errorStatus = errorStatus;
102         this.validationFailed = validationFailed;
103         this.beanLocation = trace.getBeanLocation();
104         this.xmlPages = trace.getRelatedXmls();
105         this.currentValues = new ArrayList<String>();
106 
107         for (int i = 0; i < values.length; i++) {
108             addCurrentValue(values[i]);
109         }
110     }
111 
112     /**
113      * Constructor creating a new report for an error with set values
114      *
115      * @param errorStatus - The type of error being reported
116      * @param validationFailed - The validation that was failed
117      * @param beanLocation - The location of the bean in which error occured
118      */
119     public ErrorReport(int errorStatus, String validationFailed, String beanLocation) {
120         this.errorStatus = errorStatus;
121         this.validationFailed = validationFailed;
122         this.beanLocation = beanLocation;
123         this.currentValues = new ArrayList<String>();
124         this.xmlPages = new ArrayList<String>();
125     }
126 
127     /**
128      * Creates a new ErrorReport of ERROR status
129      *
130      * @param validationFailed - The validation that was failed
131      * @param trace - ValidationTrace containing information on xml files and location
132      * @return Returns a new ErrorReport of ERROR status
133      */
134     public static ErrorReport createError(String validationFailed, ValidationTrace trace) {
135         return new ErrorReport(ERROR, validationFailed, trace);
136     }
137 
138     /**
139      * Creates a new ErrorReport of WARNING status
140      *
141      * @param validationFailed - The validation that was failed
142      * @param trace - ValidationTrace containing information on xml files and location
143      * @return Returns a new ErrorReport of WARNING status
144      */
145     public static ErrorReport createWarning(String validationFailed, ValidationTrace trace) {
146         return new ErrorReport(WARNING, validationFailed, trace);
147     }
148 
149     /**
150      * Adds a value involved in the error
151      *
152      * @param value - Value involved ("Name of Value = Its Value")
153      */
154     public void addCurrentValue(String value) {
155         currentValues.add(value);
156     }
157 
158     /**
159      * Adds a xml page involved in the error
160      *
161      * @param page - The file path of the xml page involved
162      */
163     public void addXmlPage(String page) {
164         xmlPages.add(page);
165     }
166 
167     /**
168      * Add a list of xml page involved in the error
169      *
170      * @param pages - The file path of the xml page involved
171      */
172     public void addXmlPages(ArrayList<String> pages) {
173         xmlPages.addAll(pages);
174     }
175 
176     /**
177      * Removes a value from the list of those involved
178      *
179      * @param index - The index of the value
180      */
181     public void removeCurrentValue(int index) {
182         currentValues.remove(index);
183     }
184 
185     /**
186      * Removes a xml page from the list of those involved
187      *
188      * @param index - The index of the xml page
189      */
190     public void removeXmlPage(int index) {
191         xmlPages.remove(index);
192     }
193 
194     /**
195      * Replaces a value in the list of those involved
196      *
197      * @param index - The index of the value
198      * @param value - The value to replace the value with
199      */
200     public void modifyCurrentValue(int index, String value) {
201         currentValues.set(index, value);
202     }
203 
204     /**
205      * Replaces a xml page in the list of those involved
206      *
207      * @param index - The index of the page
208      * @param page - The page to replace the xml page with
209      */
210     public void modifyXmlPage(int index, String page) {
211         xmlPages.set(index, page);
212     }
213 
214     /**
215      * Creates a message for the error being reported
216      *
217      * @return A compiled message about the error in the report
218      */
219     public String errorMessage() {
220         String message = "";
221 
222         if (errorStatus == ERROR) {
223             message = message + ("Dictionary Error Detected: " + getValidationFailed() + endl);
224         } else if (errorStatus == WARNING) {
225             message = message + ("Dictionary Warning Detected: " + getValidationFailed() + endl);
226         }
227 
228         message = message + ("Bean: " + getBeanLocation() + endl);
229         message = message + ("Values involved:" + endl);
230         for (int i = 0; i < getCurrentValueSize(); i++) {
231             message = message + (getCurrentValue(i) + endl);
232         }
233 
234         return message;
235     }
236 
237     /**
238      * Creates a message for the xml pages involved
239      *
240      * @return A compiled list of the xml pages involved
241      */
242     public String errorPageList() {
243         String pages = "Xml Pages Involved" + endl;
244 
245         for (int i = 0; i < getXmlPageSize(); i++) {
246             pages = pages + getXmlPage(i) + endl;
247         }
248 
249         return pages;
250     }
251 
252     /**
253      * Sets the validation that was failed
254      *
255      * @param validation - The validation that failed
256      */
257     public void setValidationFailed(String validation) {
258         validationFailed = validation;
259     }
260 
261     /**
262      * Sets the location of the bean in the trace
263      *
264      * @param location - The Bean location
265      */
266     public void setBeanLocation(String location) {
267         beanLocation = location;
268     }
269 
270     /**
271      * Retrieves the type of error
272      *
273      * @return Integer value of the type of error
274      */
275     public int getErrorStatus() {
276         return errorStatus;
277     }
278 
279     /**
280      * Retrieves the validation that was failed
281      *
282      * @return The failed validation
283      */
284     public String getValidationFailed() {
285         return validationFailed;
286     }
287 
288     /**
289      * Retrieves the location of the bean in the trace
290      *
291      * @return The location of the bean
292      */
293     public String getBeanLocation() {
294         return beanLocation;
295     }
296 
297     /**
298      * Retrieves a value involved in the error
299      *
300      * @param index - The index of the value
301      * @return The value involved at the provided index
302      */
303     public String getCurrentValue(int index) {
304         return currentValues.get(index);
305     }
306 
307     /**
308      * Retrieves a xml page file location involved in the error
309      *
310      * @param index - The index of the page
311      * @return The xml file involved at the provided index
312      */
313     public String getXmlPage(int index) {
314         return xmlPages.get(index);
315     }
316 
317     /**
318      * Retrieves the number of values involved in the error.
319      *
320      * @return The number of values involved
321      */
322     public int getCurrentValueSize() {
323         return currentValues.size();
324     }
325 
326     /**
327      * Retrieves the number of xml pages involved in the error
328      *
329      * @return The number of xml pages involved
330      */
331     public int getXmlPageSize() {
332         return xmlPages.size();
333     }
334 
335     /**
336      * Returns whether this message represents an error per its errorStatus.
337      * 
338      * @return true if the message represents an error
339      */
340     public boolean isError() {
341         return errorStatus == ERROR;
342     }
343 
344     /**
345      * Returns whether this message represents a warning per its errorStatus.
346      * 
347      * @return true if the message represents a warning
348      */
349     public boolean isWarning() {
350         return errorStatus == WARNING;
351     }
352 }