001/**
002 * Copyright 2005-2018 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.krad.datadictionary.validator;
017
018import java.util.ArrayList;
019
020import org.apache.commons.logging.Log;
021import org.apache.commons.logging.LogFactory;
022
023/**
024 * Collection of information regarding a single error detected within a dictionary bean
025 *
026 * @author Kuali Rice Team (rice.collab@kuali.org)
027 */
028public class ErrorReport {
029    private static final Log LOG = LogFactory.getLog(ErrorReport.class);
030
031    // Constant identifiers of the type of error
032    public static final int ERROR = 1;
033    public static final int WARNING = 2;
034
035    private static final String endl = System.getProperty("line.separator");
036
037    // Is type of error detailed in the report
038    private int errorStatus;
039    private String validationFailed;
040    private String beanLocation;
041    private ArrayList<String> currentValues;
042    private ArrayList<String> xmlPages;
043
044    /**
045     * Constructor creating a new report for an error
046     *
047     * @param ErrorStatus - The type of error being reported
048     */
049    public ErrorReport(int ErrorStatus) {
050        this.errorStatus = ErrorStatus;
051        this.validationFailed = "";
052        this.beanLocation = "";
053        this.currentValues = new ArrayList<String>();
054        this.xmlPages = new ArrayList<String>();
055    }
056
057    /**
058     * Constructor creating a new report for an error with set values
059     *
060     * @param errorStatus - The type of error being reported
061     * @param validationFailed - The validation that was failed
062     * @param beanLocation - The location of the bean in which error occured
063     * @param values - An array of the values effected
064     */
065    public ErrorReport(int errorStatus, String validationFailed, String beanLocation, String values[]) {
066        this.errorStatus = errorStatus;
067        this.validationFailed = validationFailed;
068        this.beanLocation = beanLocation;
069        this.currentValues = new ArrayList<String>();
070        this.xmlPages = new ArrayList<String>();
071
072        for (int i = 0; i < values.length; i++) {
073            addCurrentValue(values[i]);
074        }
075    }
076
077    /**
078     * Constructor creating a new report for an error with set values
079     *
080     * @param errorStatus - The type of error being reported
081     * @param validationFailed - The validation that was failed
082     * @param trace - ValidationTrace containing information on xml files and location
083     */
084    public ErrorReport(int errorStatus, String validationFailed, ValidationTrace trace) {
085        this.errorStatus = errorStatus;
086        this.validationFailed = validationFailed;
087        this.xmlPages = trace.getRelatedXmls();
088        this.beanLocation = trace.getBeanLocation();
089        this.currentValues = new ArrayList<String>();
090    }
091
092    /**
093     * Constructor creating a new report for an error with set values
094     *
095     * @param errorStatus - The type of error being reported
096     * @param validationFailed - The validation that was failed
097     * @param trace - ValidationTrace containing information on xml files and location
098     * @param values - An array of the values effected
099     */
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}