1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.uif.modifier;
17
18 import java.util.ArrayList;
19 import java.util.HashSet;
20 import java.util.LinkedList;
21 import java.util.List;
22 import java.util.Queue;
23 import java.util.Set;
24
25 import org.kuali.rice.krad.datadictionary.parse.BeanTag;
26 import org.kuali.rice.krad.datadictionary.parse.BeanTagAttribute;
27 import org.kuali.rice.krad.datadictionary.parse.BeanTags;
28 import org.kuali.rice.krad.uif.component.Component;
29 import org.kuali.rice.krad.uif.lifecycle.ViewLifecycleUtils;
30 import org.kuali.rice.krad.uif.util.ComponentUtils;
31 import org.kuali.rice.krad.uif.util.LifecycleElement;
32 import org.kuali.rice.krad.uif.util.ObjectPropertyUtils;
33 import org.kuali.rice.krad.uif.util.RecycleUtils;
34
35
36
37
38
39
40
41
42
43
44
45
46
47 @BeanTags({@BeanTag(name = "componentConverter-modifier-bean", parent = "Uif-ComponentConverter-Modifier"),
48 @BeanTag(name = "checkboxToRadioConverter-modifier-bean", parent = "Uif-CheckboxToRadioConverter-Modifier")})
49 public class ComponentConvertModifier extends ComponentModifierBase {
50 private static final long serialVersionUID = -7566547737669924605L;
51
52 private Class<? extends Component> componentTypeToReplace;
53
54 private Component componentReplacementPrototype;
55
56 public ComponentConvertModifier() {
57 super();
58 }
59
60
61
62
63 @Override
64 public void performModification(Object model, Component component) {
65 if (component == null) {
66 return;
67 }
68
69 int idSuffix = 0;
70 convertToReplacement(component, idSuffix);
71 }
72
73
74
75
76
77
78
79
80
81
82
83 protected void convertToReplacement(Component component, int idSuffix) {
84 if (component == null) {
85 return;
86 }
87
88 @SuppressWarnings("unchecked")
89 Queue<LifecycleElement> elementQueue = RecycleUtils.getInstance(LinkedList.class);
90 elementQueue.offer(component);
91
92 while (elementQueue.isEmpty()) {
93 LifecycleElement element = elementQueue.poll();
94
95 elementQueue.addAll(ViewLifecycleUtils.getElementsForLifecycle(element).values());
96
97 if (!(element instanceof Component)) {
98 continue;
99 }
100
101
102 Set<String> componentProperties =
103 ObjectPropertyUtils.getReadablePropertyNames(component.getClass());
104 for (String propertyPath : componentProperties) {
105 Object propValue = ObjectPropertyUtils.getPropertyValue(component, propertyPath);
106
107 if (propValue != null) {
108 if (getComponentTypeToReplace().isAssignableFrom(propValue.getClass())) {
109
110 performConversion(component, propertyPath, idSuffix++);
111 }
112 }
113 }
114 }
115
116 elementQueue.clear();
117 RecycleUtils.recycle(elementQueue);
118 }
119
120
121
122
123
124
125
126
127
128 protected void performConversion(Component component, String componentProperty, int idSuffix) {
129
130 Component componentReplacement = ComponentUtils.copy(getComponentReplacementPrototype(), Integer.toString(
131 idSuffix));
132
133 ObjectPropertyUtils.setPropertyValue(component, componentProperty, componentReplacement);
134 }
135
136
137
138
139 @Override
140 public Set<Class<? extends Component>> getSupportedComponents() {
141 Set<Class<? extends Component>> components = new HashSet<Class<? extends Component>>();
142 components.add(Component.class);
143
144 return components;
145 }
146
147
148
149
150 public List<Component> getComponentPrototypes() {
151 List<Component> components = new ArrayList<Component>();
152
153 components.add(componentReplacementPrototype);
154
155 return components;
156 }
157
158
159
160
161
162
163
164 @BeanTagAttribute(name = "componentTypeToReplace")
165 public Class<? extends Component> getComponentTypeToReplace() {
166 return this.componentTypeToReplace;
167 }
168
169
170
171
172
173
174 public void setComponentTypeToReplace(Class<? extends Component> componentTypeToReplace) {
175 this.componentTypeToReplace = componentTypeToReplace;
176 }
177
178
179
180
181
182
183
184
185
186
187
188 @BeanTagAttribute(name = "componentReplacementPrototype", type = BeanTagAttribute.AttributeType.SINGLEBEAN)
189 public Component getComponentReplacementPrototype() {
190 return this.componentReplacementPrototype;
191 }
192
193
194
195
196
197
198 public void setComponentReplacementPrototype(Component componentReplacementPrototype) {
199 this.componentReplacementPrototype = componentReplacementPrototype;
200 }
201
202 }