1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.krad.datadictionary.impl; |
17 | |
|
18 | |
import java.util.Comparator; |
19 | |
import java.util.List; |
20 | |
|
21 | |
import org.apache.commons.beanutils.BeanComparator; |
22 | |
import org.apache.commons.lang.StringUtils; |
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
public class FieldOverrideForListElementBase { |
31 | |
|
32 | |
private String propertyName; |
33 | |
private Object element; |
34 | |
protected String propertyNameForElementCompare; |
35 | |
|
36 | |
|
37 | |
public String getPropertyNameForElementCompare() { |
38 | 0 | return propertyNameForElementCompare; |
39 | |
} |
40 | |
|
41 | |
public void setPropertyNameForElementCompare(String propertyNameForElementCompare) { |
42 | 0 | this.propertyNameForElementCompare = propertyNameForElementCompare; |
43 | 0 | } |
44 | |
|
45 | |
protected int getElementPositionInList(Object object, List theList) { |
46 | 0 | Comparator comparator = this.getComparator(); |
47 | 0 | int pos = -1; |
48 | |
|
49 | 0 | if ( object != null && theList != null ) |
50 | |
{ |
51 | 0 | for ( int i = 0; i < theList.size(); ++i ) |
52 | |
{ |
53 | 0 | Object item = theList.get(i); |
54 | 0 | boolean equalFlag = false; |
55 | 0 | if ( comparator != null ) |
56 | |
{ |
57 | 0 | equalFlag = comparator.compare(object, item) == 0; |
58 | |
} |
59 | |
else |
60 | |
{ |
61 | 0 | equalFlag = item.equals(object); |
62 | |
} |
63 | 0 | if ( equalFlag ) |
64 | |
{ |
65 | 0 | pos = i; |
66 | 0 | break; |
67 | |
} |
68 | |
} |
69 | |
} |
70 | 0 | return pos; |
71 | |
} |
72 | |
|
73 | |
public String getPropertyName() { |
74 | 0 | return propertyName; |
75 | |
} |
76 | |
|
77 | |
public void setPropertyName(String propertyName) { |
78 | 0 | this.propertyName = propertyName; |
79 | 0 | } |
80 | |
|
81 | |
public Object getElement() { |
82 | 0 | return element; |
83 | |
} |
84 | |
|
85 | |
public void setElement(Object value) { |
86 | 0 | this.element = value; |
87 | 0 | } |
88 | |
|
89 | |
|
90 | |
public FieldOverrideForListElementBase() { |
91 | 0 | super(); |
92 | 0 | } |
93 | |
|
94 | |
protected Comparator getComparator() { |
95 | 0 | Comparator comparator = null; |
96 | 0 | if ( StringUtils.isNotBlank(propertyNameForElementCompare)) |
97 | |
{ |
98 | 0 | comparator = new BeanComparator(propertyNameForElementCompare); |
99 | |
} |
100 | |
else |
101 | |
{ |
102 | 0 | throw new RuntimeException("Missing required comparator definitions."); |
103 | |
} |
104 | 0 | return comparator; |
105 | |
} |
106 | |
|
107 | |
} |