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 org.kuali.rice.krad.uif.view.View;
19 import org.kuali.rice.krad.uif.component.Component;
20 import org.kuali.rice.krad.uif.util.ComponentUtils;
21 import org.kuali.rice.krad.uif.util.ObjectPropertyUtils;
22
23 import java.util.ArrayList;
24 import java.util.HashSet;
25 import java.util.List;
26 import java.util.Set;
27
28
29
30
31
32
33
34
35
36
37
38
39
40 public class ComponentConvertModifier extends ComponentModifierBase {
41 private static final long serialVersionUID = -7566547737669924605L;
42
43 private Class<? extends Component> componentTypeToReplace;
44
45 private Component componentReplacementPrototype;
46
47 public ComponentConvertModifier() {
48 super();
49 }
50
51
52
53
54
55 @Override
56 public void performModification(View view, Object model, Component component) {
57 if (component == null) {
58 return;
59 }
60
61 int idSuffix = 0;
62 convertToReplacement(component, idSuffix);
63 }
64
65
66
67
68
69
70
71
72
73
74
75
76
77 protected void convertToReplacement(Component component, int idSuffix) {
78 if (component == null) {
79 return;
80 }
81
82
83 List<String> componentProperties = ComponentUtils.getComponentPropertyNames(component.getClass());
84 for (String propertyPath : componentProperties) {
85 Object propValue = ObjectPropertyUtils.getPropertyValue(component, propertyPath);
86
87 if (propValue != null) {
88 if (getComponentTypeToReplace().isAssignableFrom(propValue.getClass())) {
89
90 performConversion(component, propertyPath, idSuffix++);
91 }
92 }
93 }
94
95
96 for (Component nestedComponent : component.getComponentsForLifecycle()) {
97 convertToReplacement(nestedComponent, idSuffix);
98 }
99 }
100
101
102
103
104
105
106
107
108
109
110
111
112 protected void performConversion(Component component, String componentProperty, int idSuffix) {
113
114 Component componentReplacement = ComponentUtils.copy(getComponentReplacementPrototype(), Integer.toString(idSuffix));
115
116 ObjectPropertyUtils.setPropertyValue(component, componentProperty, componentReplacement);
117 }
118
119
120
121
122 @Override
123 public Set<Class<? extends Component>> getSupportedComponents() {
124 Set<Class<? extends Component>> components = new HashSet<Class<? extends Component>>();
125 components.add(Component.class);
126
127 return components;
128 }
129
130
131
132
133 public List<Component> getComponentPrototypes() {
134 List<Component> components = new ArrayList<Component>();
135
136 components.add(componentReplacementPrototype);
137
138 return components;
139 }
140
141
142
143
144
145
146
147 public Class<? extends Component> getComponentTypeToReplace() {
148 return this.componentTypeToReplace;
149 }
150
151
152
153
154
155
156 public void setComponentTypeToReplace(Class<? extends Component> componentTypeToReplace) {
157 this.componentTypeToReplace = componentTypeToReplace;
158 }
159
160
161
162
163
164
165
166
167
168
169
170 public Component getComponentReplacementPrototype() {
171 return this.componentReplacementPrototype;
172 }
173
174
175
176
177
178
179 public void setComponentReplacementPrototype(Component componentReplacementPrototype) {
180 this.componentReplacementPrototype = componentReplacementPrototype;
181 }
182
183 }