1 | |
package org.kuali.rice.core.api.uif; |
2 | |
|
3 | |
import java.io.Serializable; |
4 | |
import java.util.ArrayList; |
5 | |
import java.util.Arrays; |
6 | |
import java.util.Collection; |
7 | |
import java.util.Collections; |
8 | |
import java.util.HashMap; |
9 | |
import java.util.List; |
10 | |
import java.util.Map; |
11 | |
import javax.xml.bind.annotation.XmlAccessType; |
12 | |
import javax.xml.bind.annotation.XmlAccessorType; |
13 | |
import javax.xml.bind.annotation.XmlAnyElement; |
14 | |
import javax.xml.bind.annotation.XmlElement; |
15 | |
import javax.xml.bind.annotation.XmlElementWrapper; |
16 | |
import javax.xml.bind.annotation.XmlRootElement; |
17 | |
import javax.xml.bind.annotation.XmlType; |
18 | |
|
19 | |
import org.apache.commons.lang.StringUtils; |
20 | |
import org.kuali.rice.core.api.CoreConstants; |
21 | |
import org.kuali.rice.core.api.mo.AbstractDataTransferObject; |
22 | |
import org.kuali.rice.core.api.mo.ModelBuilder; |
23 | |
import org.w3c.dom.Element; |
24 | |
|
25 | |
@XmlRootElement(name = RemotableAttributeError.Constants.ROOT_ELEMENT_NAME) |
26 | |
@XmlAccessorType(XmlAccessType.NONE) |
27 | |
@XmlType(name = RemotableAttributeError.Constants.TYPE_NAME, |
28 | |
propOrder = {RemotableAttributeError.Elements.ATTRIBUTE_NAME, RemotableAttributeError.Elements.ERRORS, |
29 | |
CoreConstants.CommonElements.FUTURE_ELEMENTS}) |
30 | 0 | public final class RemotableAttributeError extends AbstractDataTransferObject implements AttributeError { |
31 | |
|
32 | |
@XmlElement(name = Elements.ATTRIBUTE_NAME, required = false) |
33 | |
private final String attributeName; |
34 | |
@XmlElementWrapper(name = Elements.ERRORS, required = true ) |
35 | |
@XmlElement(name = Elements.ERROR, required = false) |
36 | |
private final List<String> errors; |
37 | 0 | @SuppressWarnings("unused") @XmlAnyElement |
38 | |
private final Collection<Element> _futureElements = null; |
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | 0 | private RemotableAttributeError() { |
44 | 0 | this.attributeName = null; |
45 | 0 | this.errors = null; |
46 | 0 | } |
47 | |
|
48 | 0 | private RemotableAttributeError(Builder builder) { |
49 | 0 | this.attributeName = builder.getAttributeName(); |
50 | 0 | this.errors = builder.getErrors(); |
51 | 0 | } |
52 | |
|
53 | |
@Override |
54 | |
public String getAttributeName() { |
55 | 0 | return this.attributeName; |
56 | |
} |
57 | |
|
58 | |
@Override |
59 | |
public List<String> getErrors() { |
60 | 0 | return this.errors; |
61 | |
} |
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
public static boolean containsAttribute(String attributeName, Collection<RemotableAttributeError> errors) { |
73 | 0 | if (StringUtils.isBlank(attributeName)) { |
74 | 0 | throw new IllegalArgumentException("attributeName is blank"); |
75 | |
} |
76 | |
|
77 | 0 | if (errors == null) { |
78 | 0 | throw new IllegalArgumentException("errors is null"); |
79 | |
} |
80 | |
|
81 | 0 | for (AttributeError error : errors) { |
82 | 0 | if (attributeName.equals(error.getAttributeName())) { |
83 | 0 | return true; |
84 | |
} |
85 | |
} |
86 | 0 | return false; |
87 | |
} |
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
public static List<RemotableAttributeError> normalizeRemotableAttributes(List<RemotableAttributeError> errors) { |
97 | 0 | if (errors == null) { |
98 | 0 | throw new IllegalArgumentException("errors is null"); |
99 | |
} |
100 | |
|
101 | 0 | final List<RemotableAttributeError> normalized = new ArrayList<RemotableAttributeError>(); |
102 | 0 | final Map<String, Integer> map = new HashMap<String, Integer>(); |
103 | |
|
104 | 0 | for (final RemotableAttributeError error : errors) { |
105 | 0 | final Integer prevIndex = map.get(error.getAttributeName()); |
106 | 0 | if (prevIndex == null) { |
107 | 0 | final int insertIdx = normalized.size(); |
108 | 0 | map.put(error.getAttributeName(), insertIdx); |
109 | 0 | normalized.add(insertIdx, error); |
110 | 0 | } else { |
111 | 0 | final RemotableAttributeError prevError = normalized.get(prevIndex.intValue()); |
112 | 0 | final RemotableAttributeError.Builder b = RemotableAttributeError.Builder.create(error.getAttributeName(), error.getErrors()); |
113 | 0 | b.addErrors(prevError.getErrors()); |
114 | 0 | normalized.set(prevIndex.intValue(), b.build()); |
115 | |
} |
116 | 0 | } |
117 | 0 | return normalized; |
118 | |
} |
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | 0 | public static final class Builder implements Serializable, ModelBuilder, AttributeError { |
125 | |
|
126 | |
private String attributeName; |
127 | 0 | private List<String> errors = new ArrayList<String>(); |
128 | |
|
129 | 0 | private Builder(String attributeName) { |
130 | 0 | this.attributeName = attributeName; |
131 | 0 | } |
132 | |
|
133 | |
public static Builder create(String attributeName) { |
134 | 0 | return new Builder(attributeName); |
135 | |
} |
136 | |
|
137 | |
public static Builder create(String attributeName, List<String> errors) { |
138 | 0 | Builder b = new Builder(attributeName); |
139 | 0 | b.setErrors(errors); |
140 | 0 | return b; |
141 | |
} |
142 | |
|
143 | |
public static Builder create(String attributeName, String... errors) { |
144 | 0 | Builder b = new Builder(attributeName); |
145 | 0 | b.addErrors(errors); |
146 | 0 | return b; |
147 | |
} |
148 | |
|
149 | |
public static Builder create(AttributeError contract) { |
150 | 0 | if (contract == null) { |
151 | 0 | throw new IllegalArgumentException("contract was null"); |
152 | |
} |
153 | 0 | Builder builder = create(contract.getAttributeName()); |
154 | 0 | builder.setErrors(contract.getErrors()); |
155 | 0 | return builder; |
156 | |
} |
157 | |
|
158 | |
public RemotableAttributeError build() { |
159 | 0 | if (errors.isEmpty()) { |
160 | 0 | throw new IllegalStateException("must contain at least one error"); |
161 | |
} |
162 | |
|
163 | 0 | for (String err : errors) { |
164 | 0 | if (StringUtils.isBlank(err)) { |
165 | 0 | throw new IllegalStateException("contains a blank error"); |
166 | |
} |
167 | |
} |
168 | |
|
169 | 0 | return new RemotableAttributeError(this); |
170 | |
} |
171 | |
|
172 | |
@Override |
173 | |
public String getAttributeName() { |
174 | 0 | return this.attributeName; |
175 | |
} |
176 | |
|
177 | |
@Override |
178 | |
public List<String> getErrors() { |
179 | 0 | return Collections.unmodifiableList(this.errors); |
180 | |
} |
181 | |
|
182 | |
public void setAttributeName(String attributeName) { |
183 | 0 | if (StringUtils.isBlank(attributeName)) { |
184 | 0 | throw new IllegalArgumentException("attributeName is blank"); |
185 | |
} |
186 | |
|
187 | 0 | this.attributeName = attributeName; |
188 | 0 | } |
189 | |
|
190 | |
public void setErrors(List<String> errors) { |
191 | 0 | if (errors == null) { |
192 | 0 | throw new IllegalArgumentException("errors is null"); |
193 | |
} |
194 | |
|
195 | 0 | this.errors = new ArrayList<String>(errors); |
196 | 0 | } |
197 | |
|
198 | |
|
199 | |
|
200 | |
|
201 | |
|
202 | |
|
203 | |
public void addErrors(String... errors) { |
204 | 0 | if (errors == null) { |
205 | 0 | throw new IllegalArgumentException("errors is null"); |
206 | |
} |
207 | |
|
208 | 0 | this.errors.addAll(Arrays.asList(errors)); |
209 | 0 | } |
210 | |
|
211 | |
|
212 | |
|
213 | |
|
214 | |
|
215 | |
|
216 | |
public void addErrors(List<String> errors) { |
217 | 0 | if (errors == null) { |
218 | 0 | throw new IllegalArgumentException("errors is null"); |
219 | |
} |
220 | |
|
221 | 0 | this.errors.addAll(errors); |
222 | 0 | } |
223 | |
} |
224 | |
|
225 | |
|
226 | |
|
227 | |
|
228 | 0 | static class Constants { |
229 | |
|
230 | |
final static String ROOT_ELEMENT_NAME = "attributeError"; |
231 | |
final static String TYPE_NAME = "attributeErrorType"; |
232 | |
|
233 | |
} |
234 | |
|
235 | |
|
236 | |
|
237 | |
|
238 | |
|
239 | 0 | static class Elements { |
240 | |
|
241 | |
final static String ATTRIBUTE_NAME = "attributeName"; |
242 | |
final static String ERROR = "error"; |
243 | |
final static String ERRORS = "errors"; |
244 | |
|
245 | |
} |
246 | |
|
247 | |
} |