1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.kuali.rice.kns.uif.util; |
12 | |
|
13 | |
import org.apache.commons.lang.StringUtils; |
14 | |
import org.kuali.rice.core.util.type.TypeUtils; |
15 | |
import org.kuali.rice.kns.uif.UifConstants; |
16 | |
import org.kuali.rice.kns.uif.container.Container; |
17 | |
import org.kuali.rice.kns.uif.core.Component; |
18 | |
import org.kuali.rice.kns.uif.core.DataBinding; |
19 | |
import org.kuali.rice.kns.uif.core.Ordered; |
20 | |
import org.kuali.rice.kns.uif.field.AttributeField; |
21 | |
import org.kuali.rice.kns.uif.field.Field; |
22 | |
import org.kuali.rice.kns.uif.field.GroupField; |
23 | |
import org.kuali.rice.kns.uif.layout.LayoutManager; |
24 | |
import org.kuali.rice.kns.util.ObjectUtils; |
25 | |
import org.springframework.beans.BeanUtils; |
26 | |
import org.springframework.core.OrderComparator; |
27 | |
|
28 | |
import java.beans.PropertyDescriptor; |
29 | |
import java.io.Serializable; |
30 | |
import java.util.ArrayList; |
31 | |
import java.util.Collections; |
32 | |
import java.util.HashSet; |
33 | |
import java.util.List; |
34 | |
import java.util.Map; |
35 | |
import java.util.Set; |
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | 0 | public class ComponentUtils { |
44 | |
|
45 | |
public static <T extends Component> T copy(T component) { |
46 | 0 | return copy(component, null); |
47 | |
} |
48 | |
|
49 | |
public static <T extends Component> T copy(T component, String idSuffix) { |
50 | 0 | T copy = copyObject(component); |
51 | |
|
52 | 0 | if (StringUtils.isNotBlank(idSuffix)) { |
53 | 0 | updateIdsWithSuffixNested(copy, idSuffix); |
54 | |
} |
55 | |
|
56 | 0 | return copy; |
57 | |
} |
58 | |
|
59 | |
public static <T extends Object> T copyObject(T object) { |
60 | 0 | if (object == null) { |
61 | 0 | return null; |
62 | |
} |
63 | |
|
64 | 0 | T copy = null; |
65 | |
try { |
66 | 0 | copy = CloneUtils.deepClone(object); |
67 | |
} |
68 | 0 | catch (Exception e) { |
69 | 0 | throw new RuntimeException(e); |
70 | 0 | } |
71 | |
|
72 | 0 | return copy; |
73 | |
} |
74 | |
|
75 | |
protected static Object getCopyPropertyValue(Set<String> propertiesForReferenceCopy, String propertyName, |
76 | |
Object propertyValue) { |
77 | 0 | if (propertyValue == null) { |
78 | 0 | return null; |
79 | |
} |
80 | |
|
81 | 0 | Object copyValue = propertyValue; |
82 | |
|
83 | 0 | Class<?> valuePropertyType = propertyValue.getClass(); |
84 | 0 | if (propertiesForReferenceCopy.contains(propertyName) || TypeUtils.isSimpleType(valuePropertyType) |
85 | |
|| TypeUtils.isClassClass(valuePropertyType)) { |
86 | 0 | return copyValue; |
87 | |
} |
88 | |
|
89 | 0 | if (Component.class.isAssignableFrom(valuePropertyType) |
90 | |
|| LayoutManager.class.isAssignableFrom(valuePropertyType)) { |
91 | 0 | copyValue = copyObject(propertyValue); |
92 | |
} |
93 | |
else { |
94 | 0 | copyValue = ObjectUtils.deepCopy((Serializable) propertyValue); |
95 | |
} |
96 | |
|
97 | 0 | return copyValue; |
98 | |
} |
99 | |
|
100 | |
@SuppressWarnings("unchecked") |
101 | |
protected static <T extends Object> T getNewInstance(T object) { |
102 | 0 | T copy = null; |
103 | |
try { |
104 | 0 | copy = (T) object.getClass().newInstance(); |
105 | |
} |
106 | 0 | catch (Exception e) { |
107 | 0 | throw new RuntimeException("Unable to create new instance of class: " + object.getClass()); |
108 | 0 | } |
109 | |
|
110 | 0 | return copy; |
111 | |
} |
112 | |
|
113 | |
public static <T extends Field> List<T> copyFieldList(List<T> fields, String addBindingPrefix, String idSuffix) { |
114 | 0 | List<T> copiedFieldList = copyFieldList(fields, idSuffix); |
115 | |
|
116 | 0 | prefixBindingPath(copiedFieldList, addBindingPrefix); |
117 | |
|
118 | 0 | return copiedFieldList; |
119 | |
} |
120 | |
|
121 | |
public static <T extends Field> List<T> copyFieldList(List<T> fields, String idSuffix) { |
122 | 0 | List<T> copiedFieldList = new ArrayList<T>(); |
123 | |
|
124 | 0 | for (T field : fields) { |
125 | 0 | T copiedField = copy(field, idSuffix); |
126 | 0 | copiedFieldList.add(copiedField); |
127 | 0 | } |
128 | |
|
129 | 0 | return copiedFieldList; |
130 | |
} |
131 | |
|
132 | |
public static <T extends Component> T copyComponent(T component, String addBindingPrefix, String idSuffix) { |
133 | 0 | T copy = copy(component, idSuffix); |
134 | |
|
135 | 0 | prefixBindingPathNested(component, addBindingPrefix); |
136 | |
|
137 | 0 | return copy; |
138 | |
} |
139 | |
|
140 | |
public static <T extends Component> List<T> copyComponentList(List<T> components, String idSuffix) { |
141 | 0 | List<T> copiedComponentList = new ArrayList<T>(); |
142 | |
|
143 | 0 | for (T field : components) { |
144 | 0 | T copiedComponent = copy(field, idSuffix); |
145 | 0 | copiedComponentList.add(copiedComponent); |
146 | 0 | } |
147 | |
|
148 | 0 | return copiedComponentList; |
149 | |
} |
150 | |
|
151 | |
@SuppressWarnings("unchecked") |
152 | |
public static <T extends Component> List<T> getComponentsOfType(List<? extends Component> items, |
153 | |
Class<T> componentType) { |
154 | 0 | List<T> typeComponents = new ArrayList<T>(); |
155 | |
|
156 | 0 | for (Component component : items) { |
157 | 0 | if (componentType.isAssignableFrom(component.getClass())) { |
158 | 0 | typeComponents.add((T) component); |
159 | |
} |
160 | |
} |
161 | |
|
162 | 0 | return typeComponents; |
163 | |
} |
164 | |
|
165 | |
public static <T extends Component> List<T> getComponentsOfTypeDeep(List<? extends Component> items, |
166 | |
Class<T> componentType) { |
167 | 0 | List<T> typeComponents = new ArrayList<T>(); |
168 | |
|
169 | 0 | for (Component component : items) { |
170 | 0 | typeComponents.addAll(getComponentsOfTypeDeep(component, componentType)); |
171 | |
} |
172 | |
|
173 | 0 | return typeComponents; |
174 | |
} |
175 | |
|
176 | |
@SuppressWarnings("unchecked") |
177 | |
public static <T extends Component> List<T> getComponentsOfTypeDeep(Component component, Class<T> componentType) { |
178 | 0 | List<T> typeComponents = new ArrayList<T>(); |
179 | |
|
180 | 0 | if (component == null) { |
181 | 0 | return typeComponents; |
182 | |
} |
183 | |
|
184 | 0 | if (componentType.isAssignableFrom(component.getClass())) { |
185 | 0 | typeComponents.add((T) component); |
186 | |
} |
187 | |
|
188 | 0 | for (Component nested : component.getNestedComponents()) { |
189 | 0 | typeComponents.addAll(getComponentsOfTypeDeep(nested, componentType)); |
190 | |
} |
191 | |
|
192 | 0 | return typeComponents; |
193 | |
} |
194 | |
|
195 | |
public static void prefixBindingPath(List<? extends Field> fields, String addBindingPrefix) { |
196 | 0 | for (Field field : fields) { |
197 | 0 | if (field instanceof DataBinding) { |
198 | 0 | prefixBindingPath((DataBinding) field, addBindingPrefix); |
199 | |
} |
200 | 0 | else if (field instanceof GroupField) { |
201 | 0 | List<Field> groupFields = getComponentsOfType(((GroupField) field).getItems(), Field.class); |
202 | 0 | prefixBindingPath(groupFields, addBindingPrefix); |
203 | 0 | } |
204 | |
} |
205 | 0 | } |
206 | |
|
207 | |
public static void prefixBindingPathNested(Component component, String addBindingPrefix) { |
208 | 0 | if (component instanceof DataBinding) { |
209 | 0 | prefixBindingPath((DataBinding) component, addBindingPrefix); |
210 | |
} |
211 | |
|
212 | 0 | for (Component nested : component.getNestedComponents()) { |
213 | 0 | if (nested != null) { |
214 | 0 | prefixBindingPathNested(nested, addBindingPrefix); |
215 | |
} |
216 | |
} |
217 | 0 | } |
218 | |
|
219 | |
public static void prefixBindingPath(DataBinding field, String addBindingPrefix) { |
220 | 0 | String bindingPrefix = addBindingPrefix; |
221 | 0 | if (StringUtils.isNotBlank(field.getBindingInfo().getBindByNamePrefix())) { |
222 | 0 | bindingPrefix += "." + field.getBindingInfo().getBindByNamePrefix(); |
223 | |
} |
224 | 0 | field.getBindingInfo().setBindByNamePrefix(bindingPrefix); |
225 | |
|
226 | 0 | if (field instanceof AttributeField){ |
227 | 0 | AttributeField attrfield = (AttributeField)field; |
228 | 0 | if (StringUtils.isNotBlank(attrfield.getAdditionalDisplayPropertyName())){ |
229 | 0 | attrfield.getAdditionalDisplayPropertyBindingInfo().setBindByNamePrefix(bindingPrefix); |
230 | |
} |
231 | 0 | if (StringUtils.isNotBlank(attrfield.getAlternateDisplayPropertyName())){ |
232 | 0 | attrfield.getAlternateDisplayPropertyBindingInfo().setBindByNamePrefix(bindingPrefix); |
233 | |
} |
234 | |
} |
235 | 0 | } |
236 | |
|
237 | |
public static void updateIdsWithSuffixNested(List<? extends Component> components, String idSuffix) { |
238 | 0 | for (Component component : components) { |
239 | 0 | updateIdsWithSuffixNested(component, idSuffix); |
240 | |
} |
241 | 0 | } |
242 | |
|
243 | |
public static void updateIdsWithSuffixNested(Component component, String idSuffix) { |
244 | 0 | updateIdsWithSuffix(component, idSuffix); |
245 | |
|
246 | 0 | if (Container.class.isAssignableFrom(component.getClass())) { |
247 | 0 | LayoutManager layoutManager = ((Container) component).getLayoutManager(); |
248 | 0 | layoutManager.setId(layoutManager.getId() + idSuffix); |
249 | |
} |
250 | |
|
251 | 0 | for (Component nested : component.getNestedComponents()) { |
252 | 0 | if (nested != null) { |
253 | 0 | updateIdsWithSuffixNested(nested, idSuffix); |
254 | |
} |
255 | |
} |
256 | 0 | } |
257 | |
|
258 | |
public static void updateIdsWithSuffix(Component component, String idSuffix) { |
259 | |
|
260 | 0 | component.setId(component.getId() + idSuffix); |
261 | 0 | } |
262 | |
|
263 | |
public static void setComponentsPropertyDeep(List<? extends Component> components, String propertyPath, |
264 | |
Object propertyValue) { |
265 | 0 | for (Component component : components) { |
266 | 0 | setComponentPropertyDeep(component, propertyPath, propertyValue); |
267 | |
} |
268 | 0 | } |
269 | |
|
270 | |
public static void setComponentPropertyDeep(Component component, String propertyPath, Object propertyValue) { |
271 | 0 | ObjectPropertyUtils.setPropertyValue(component, propertyPath, propertyValue, true); |
272 | |
|
273 | 0 | for (Component nested : component.getNestedComponents()) { |
274 | 0 | if (nested != null) { |
275 | 0 | setComponentPropertyDeep(nested, propertyPath, propertyValue); |
276 | |
} |
277 | |
} |
278 | 0 | } |
279 | |
|
280 | |
public static List<String> getComponentPropertyNames(Class<? extends Component> componentClass) { |
281 | 0 | List<String> componentProperties = new ArrayList<String>(); |
282 | |
|
283 | 0 | PropertyDescriptor[] properties = BeanUtils.getPropertyDescriptors(componentClass); |
284 | 0 | for (int i = 0; i < properties.length; i++) { |
285 | 0 | PropertyDescriptor descriptor = properties[i]; |
286 | 0 | if (descriptor.getReadMethod() != null) { |
287 | 0 | componentProperties.add(descriptor.getName()); |
288 | |
} |
289 | |
} |
290 | |
|
291 | 0 | return componentProperties; |
292 | |
} |
293 | |
|
294 | |
public static void pushObjectToContext(List<? extends Component> components, String contextName, Object contextValue) { |
295 | 0 | for (Component component : components) { |
296 | 0 | pushObjectToContext(component, contextName, contextValue); |
297 | |
} |
298 | 0 | } |
299 | |
|
300 | |
public static void pushObjectToContext(Component component, String contextName, Object contextValue) { |
301 | 0 | if (component == null) { |
302 | 0 | return; |
303 | |
} |
304 | |
|
305 | 0 | component.pushObjectToContext(contextName, contextValue); |
306 | |
|
307 | |
|
308 | 0 | if (Container.class.isAssignableFrom(component.getClass())) { |
309 | 0 | LayoutManager layoutManager = ((Container) component).getLayoutManager(); |
310 | 0 | if (layoutManager != null) { |
311 | 0 | layoutManager.pushObjectToContext(contextName, contextValue); |
312 | |
|
313 | 0 | for (Component nestedComponent : layoutManager.getNestedComponents()) { |
314 | 0 | pushObjectToContext(nestedComponent, contextName, contextValue); |
315 | |
} |
316 | |
} |
317 | |
} |
318 | |
|
319 | 0 | for (Component nestedComponent : component.getNestedComponents()) { |
320 | 0 | pushObjectToContext(nestedComponent, contextName, contextValue); |
321 | |
} |
322 | 0 | } |
323 | |
|
324 | |
public static void updateContextsForLine(List<? extends Component> components, Object collectionLine, |
325 | |
int lineIndex) { |
326 | 0 | for (Component component : components) { |
327 | 0 | updateContextForLine(component, collectionLine, lineIndex); |
328 | |
} |
329 | 0 | } |
330 | |
|
331 | |
public static void updateContextForLine(Component component, Object collectionLine, int lineIndex) { |
332 | 0 | pushObjectToContext(component, UifConstants.ContextVariableNames.LINE, collectionLine); |
333 | 0 | pushObjectToContext(component, UifConstants.ContextVariableNames.INDEX, new Integer(lineIndex)); |
334 | |
|
335 | 0 | boolean isAddLine = (lineIndex == -1); |
336 | 0 | pushObjectToContext(component, UifConstants.ContextVariableNames.IS_ADD_LINE, isAddLine); |
337 | 0 | } |
338 | |
|
339 | |
public static void processIds(Component component, Map<String, Integer> seenIds) { |
340 | 0 | String componentId = component.getId(); |
341 | 0 | Integer seenCount = new Integer(0); |
342 | 0 | if (StringUtils.isNotBlank(componentId)) { |
343 | 0 | if (seenIds.containsKey(componentId)) { |
344 | 0 | seenCount = seenIds.get(componentId); |
345 | 0 | seenCount += 1; |
346 | |
|
347 | 0 | component.setId(componentId + "_" + seenCount); |
348 | |
} |
349 | |
|
350 | 0 | seenIds.put(componentId, seenCount); |
351 | |
} |
352 | |
|
353 | 0 | if (Container.class.isAssignableFrom(component.getClass())) { |
354 | 0 | LayoutManager layoutManager = ((Container) component).getLayoutManager(); |
355 | 0 | if ((layoutManager != null) && StringUtils.isNotBlank(layoutManager.getId())) { |
356 | 0 | seenCount = new Integer(0); |
357 | 0 | if (seenIds.containsKey(layoutManager.getId())) { |
358 | 0 | seenCount = seenIds.get(layoutManager.getId()); |
359 | 0 | seenCount += 1; |
360 | |
|
361 | 0 | layoutManager.setId(layoutManager.getId() + "_" + seenCount); |
362 | |
} |
363 | |
|
364 | 0 | seenIds.put(layoutManager.getId(), seenCount); |
365 | |
} |
366 | |
} |
367 | |
|
368 | 0 | for (Component nested : component.getNestedComponents()) { |
369 | 0 | if (nested != null) { |
370 | 0 | processIds(nested, seenIds); |
371 | |
} |
372 | |
} |
373 | 0 | } |
374 | |
|
375 | |
|
376 | |
|
377 | |
|
378 | |
|
379 | |
|
380 | |
|
381 | |
|
382 | |
|
383 | |
|
384 | |
|
385 | |
|
386 | |
|
387 | |
|
388 | |
|
389 | |
|
390 | |
|
391 | |
|
392 | |
|
393 | |
public static List<? extends Ordered> sort(List<? extends Ordered> items, int defaultOrderSequence) { |
394 | 0 | List<Ordered> orderedItems = new ArrayList<Ordered>(); |
395 | |
|
396 | |
|
397 | 0 | Set<Integer> foundOrders = new HashSet<Integer>(); |
398 | |
|
399 | |
|
400 | 0 | Collections.reverse(items); |
401 | 0 | for (Ordered component : items) { |
402 | 0 | int order = component.getOrder(); |
403 | |
|
404 | |
|
405 | 0 | if (order == 0) { |
406 | 0 | orderedItems.add(component); |
407 | |
} |
408 | |
|
409 | 0 | else if (!foundOrders.contains(new Integer(order))) { |
410 | 0 | orderedItems.add(component); |
411 | 0 | foundOrders.add(new Integer(order)); |
412 | |
} |
413 | 0 | } |
414 | |
|
415 | |
|
416 | |
|
417 | 0 | Collections.reverse(items); |
418 | 0 | for (Ordered component : items) { |
419 | 0 | int order = component.getOrder(); |
420 | |
|
421 | |
|
422 | 0 | if (order == 0) { |
423 | 0 | defaultOrderSequence++; |
424 | 0 | while (foundOrders.contains(new Integer(defaultOrderSequence))) { |
425 | 0 | defaultOrderSequence++; |
426 | |
} |
427 | 0 | component.setOrder(defaultOrderSequence); |
428 | |
} |
429 | 0 | } |
430 | |
|
431 | |
|
432 | 0 | Collections.sort(orderedItems, new OrderComparator()); |
433 | |
|
434 | 0 | return orderedItems; |
435 | |
} |
436 | |
|
437 | |
|
438 | |
|
439 | |
|
440 | |
|
441 | |
|
442 | |
|
443 | |
|
444 | |
|
445 | |
|
446 | |
|
447 | |
|
448 | |
|
449 | |
|
450 | |
|
451 | |
|
452 | |
|
453 | |
|
454 | |
|
455 | |
public static String parseExpression(String exp, List<String> controlNames){ |
456 | |
|
457 | 0 | exp = StringUtils.replace(exp, "!=", " != "); |
458 | 0 | exp = StringUtils.replace(exp, "==", " == "); |
459 | 0 | exp = StringUtils.replace(exp, ">", " > "); |
460 | 0 | exp = StringUtils.replace(exp, "<", " < "); |
461 | 0 | exp = StringUtils.replace(exp, "<=", " <= "); |
462 | 0 | exp = StringUtils.replace(exp, ">=", " >= "); |
463 | 0 | exp = exp.trim(); |
464 | |
|
465 | 0 | String conditionJs = exp; |
466 | 0 | String stack = ""; |
467 | 0 | boolean expectingSingleQuote = false; |
468 | 0 | boolean ignoreNext = false; |
469 | 0 | for(int i = 0; i < exp.length(); i++) { |
470 | 0 | char c = exp.charAt(i); |
471 | 0 | if(!expectingSingleQuote && !ignoreNext && (c == '(' || c == ' ' || c == ')')){ |
472 | 0 | evaluateCurrentStack(stack.trim(), controlNames); |
473 | |
|
474 | 0 | stack = ""; |
475 | 0 | continue; |
476 | |
} |
477 | 0 | else if(!ignoreNext && c == '\''){ |
478 | 0 | stack = stack + c; |
479 | 0 | expectingSingleQuote = !expectingSingleQuote; |
480 | |
} |
481 | 0 | else if(c == '\\'){ |
482 | 0 | stack = stack + c; |
483 | 0 | ignoreNext = !ignoreNext; |
484 | |
} |
485 | |
else{ |
486 | 0 | stack = stack + c; |
487 | 0 | ignoreNext = false; |
488 | |
} |
489 | |
} |
490 | |
|
491 | 0 | conditionJs = conditionJs |
492 | |
.replaceAll("\\s(?i:ne)\\s", " != ") |
493 | |
.replaceAll("\\s(?i:eq)\\s", " == ") |
494 | |
.replaceAll("\\s(?i:gt)\\s", " > ") |
495 | |
.replaceAll("\\s(?i:lt)\\s", " < ") |
496 | |
.replaceAll("\\s(?i:lte)\\s", " <= ") |
497 | |
.replaceAll("\\s(?i:gte)\\s", " >= ") |
498 | |
.replaceAll("\\s(?i:and)\\s", " && ") |
499 | |
.replaceAll("\\s(?i:or)\\s", " || ") |
500 | |
.replaceAll("\\s+(?i:matches)\\s+'.*'", ".match(/" |
501 | |
+ "$0".replaceAll("\\s+(?i:matches)\\s+", "") + "/).length > 0 "); |
502 | |
|
503 | 0 | for(String propertyName: controlNames){ |
504 | 0 | conditionJs = conditionJs.replace(propertyName, |
505 | |
"coerceValue(\""+ propertyName +"\")"); |
506 | |
} |
507 | 0 | return conditionJs; |
508 | |
} |
509 | |
|
510 | |
|
511 | |
|
512 | |
|
513 | |
|
514 | |
|
515 | |
|
516 | |
|
517 | |
private static void evaluateCurrentStack(String stack, List<String> controlNames){ |
518 | 0 | if(StringUtils.isNotBlank(stack)){ |
519 | 0 | if(!(stack.equals("==") |
520 | |
|| stack.equals("!=") |
521 | |
|| stack.equals(">") |
522 | |
|| stack.equals("<") |
523 | |
|| stack.equals(">=") |
524 | |
|| stack.equals("<=") |
525 | |
|| stack.equalsIgnoreCase("ne") |
526 | |
|| stack.equalsIgnoreCase("eq") |
527 | |
|| stack.equalsIgnoreCase("gt") |
528 | |
|| stack.equalsIgnoreCase("lt") |
529 | |
|| stack.equalsIgnoreCase("lte") |
530 | |
|| stack.equalsIgnoreCase("gte") |
531 | |
|| stack.equalsIgnoreCase("matches"))){ |
532 | |
|
533 | 0 | boolean isNumber = false; |
534 | 0 | if((StringUtils.isNumeric(stack.substring(0,1)) |
535 | |
|| stack.substring(0,1).equals("-"))){ |
536 | |
try{ |
537 | 0 | Double.parseDouble(stack); |
538 | 0 | isNumber = true; |
539 | |
} |
540 | 0 | catch(NumberFormatException e){ |
541 | 0 | isNumber = false; |
542 | 0 | } |
543 | |
} |
544 | |
|
545 | 0 | if(!(stack.equalsIgnoreCase("false") || stack.equalsIgnoreCase("true") || isNumber |
546 | |
|| stack.startsWith("'") || stack.endsWith("'"))){ |
547 | 0 | if(!controlNames.contains(stack)){ |
548 | 0 | controlNames.add(stack); |
549 | |
} |
550 | |
} |
551 | |
} |
552 | |
} |
553 | 0 | } |
554 | |
|
555 | |
} |