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