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