View Javadoc
1   /**
2    * Copyright 2005-2015 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.kew.api.validation;
17  
18  import org.apache.commons.collections.CollectionUtils;
19  import org.kuali.rice.core.api.CoreConstants;
20  import org.kuali.rice.core.api.mo.AbstractDataTransferObject;
21  import org.kuali.rice.core.api.mo.ModelBuilder;
22  import org.kuali.rice.core.api.util.jaxb.MapStringStringAdapter;
23  import org.kuali.rice.kew.api.rule.RuleContract;
24  import org.kuali.rice.kew.api.rule.RuleDelegationContract;
25  import org.kuali.rice.kew.api.rule.RuleResponsibility;
26  import org.kuali.rice.kew.api.rule.RuleResponsibilityContract;
27  import org.w3c.dom.Element;
28  
29  import javax.xml.bind.annotation.XmlAccessType;
30  import javax.xml.bind.annotation.XmlAccessorType;
31  import javax.xml.bind.annotation.XmlAnyElement;
32  import javax.xml.bind.annotation.XmlElement;
33  import javax.xml.bind.annotation.XmlRootElement;
34  import javax.xml.bind.annotation.XmlType;
35  import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
36  import java.io.Serializable;
37  import java.util.ArrayList;
38  import java.util.Collection;
39  import java.util.Collections;
40  import java.util.HashMap;
41  import java.util.List;
42  import java.util.Map;
43  
44  /**
45   * A set of results from validation of a field of data.
46   *
47   * @author Kuali Rice Team (rice.collab@kuali.org)
48   */
49  @XmlRootElement(name = ValidationResults.Constants.ROOT_ELEMENT_NAME)
50  @XmlAccessorType(XmlAccessType.NONE)
51  @XmlType(name = ValidationResults.Constants.TYPE_NAME, propOrder = {
52      ValidationResults.Elements.ERRORS,
53      CoreConstants.CommonElements.FUTURE_ELEMENTS
54  })
55  public class ValidationResults
56      extends AbstractDataTransferObject
57      implements ValidationResultsContract {
58  
59  	public static final String GLOBAL = "org.kuali.rice.kew.api.validation.ValidationResults.GLOBAL";
60  
61      @XmlElement(name = Elements.ERRORS, required = false)
62      @XmlJavaTypeAdapter(value = MapStringStringAdapter.class)
63      private final Map<String, String> errors;
64  
65      @SuppressWarnings("unused")
66      @XmlAnyElement
67      private final Collection<Element> _futureElements = null;
68  
69      /**
70       * Private constructor used only by JAXB.
71       */
72      private ValidationResults() {
73          this.errors = Collections.emptyMap();
74      }
75  
76      private ValidationResults(Builder builder) {
77         this.errors = Collections.unmodifiableMap(builder.getErrors());
78      }
79  
80      @Override
81  	public Map<String, String> getErrors() {
82  		return errors;
83  	}
84  
85      /**
86       * A builder which can be used to construct {@link ValidationResults} instances.  Enforces the constraints of the {@link ValidationResultsContract}.
87       *
88       */
89      public final static class Builder
90          implements Serializable, ModelBuilder, ValidationResultsContract
91      {
92  
93          private Map<String, String> errors = new HashMap<String, String>();
94  
95          private Builder() {
96          }
97  
98          public static Builder create() {
99              return new Builder();
100         }
101 
102         public static Builder create(ValidationResultsContract contract) {
103             if (contract == null) {
104                 throw new IllegalArgumentException("contract was null");
105             }
106             Builder builder = create();
107             if (contract.getErrors() != null) {
108                 builder.setErrors(contract.getErrors());
109             }
110             return builder;
111         }
112 
113         public ValidationResults build() {
114             return new ValidationResults(this);
115         }
116 
117         @Override
118         public Map<String, String> getErrors() {
119             return Collections.unmodifiableMap(this.errors);
120         }
121 
122         public void setErrors(Map<String, String> errors) {
123             this.errors = new HashMap<String, String>(errors);
124         }
125 
126         /**
127          * Convenience method for adding an error message
128          * @param errorMessage
129          */
130         public void addError(String errorMessage) {
131             addError(GLOBAL, errorMessage);
132         }
133 
134         /**
135          * Convenience method for adding an error message for a given field
136          * @param errorMessage
137          */
138         public void addError(String fieldName, String errorMessage) {
139             errors.put(fieldName, errorMessage);
140         }
141     }
142 
143     /**
144      * Defines some internal constants used on this class.
145      */
146     static class Constants {
147         final static String ROOT_ELEMENT_NAME = "validationResults";
148         final static String TYPE_NAME = "ValidationResultsType";
149     }
150     /**
151      * A private class which exposes constants which define the XML element names to use when this object is marshalled to XML.
152      */
153     static class Elements {
154         final static String ERRORS = "errors";
155     }
156 }