View Javadoc
1   /**
2    * Copyright 2005-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * For a given <code>Component</code> instance converts all component properties
37   * of a certain type to instances of another configured <code>Component</code>.
38   * The conversion is performed recursively down all the component children
39   *
40   * <p>
41   * Some example uses of this are converting all checkbox controls to radio group
42   * controls within a group and replacement of a widget with another
43   * </p>
44   *
45   * @author Kuali Rice Team (rice.collab@kuali.org)
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       * {@inheritDoc}
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       * Reads the component properties and looks for types that match the
75       * configured type to replace. If a match is found, a new instance of the
76       * replacement component prototype is created and set as the property value.
77       * The method is then called for each of the component's children
78       *
79       * @param component component instance to inspect properties for
80       * @param idSuffix suffix string to use for any generated component
81       * replacements
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             // check all component properties for the type to replace
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                         // types match, convert the component
110                         performConversion(component, propertyPath, idSuffix++);
111                     }
112                 }
113             }
114         }
115         
116         elementQueue.clear();
117         RecycleUtils.recycle(elementQueue);
118     }
119 
120     /**
121      * Creates a new instance of the replacement component prototype and sets a
122      * the property value for the given property name and component instance
123      *
124      * @param component component instance to set property on
125      * @param componentProperty property name to set
126      * @param idSuffix suffix string to use for the generated component
127      */
128     protected void performConversion(Component component, String componentProperty, int idSuffix) {
129         // create new instance of replacement component
130         Component componentReplacement = ComponentUtils.copy(getComponentReplacementPrototype(), Integer.toString(
131                 idSuffix));
132 
133         ObjectPropertyUtils.setPropertyValue(component, componentProperty, componentReplacement);
134     }
135 
136     /**
137      * {@inheritDoc}
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      * @see org.kuali.rice.krad.uif.modifier.ComponentModifierBase#getComponentPrototypes()
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      * Type of component that should be replaced with an instance of the
160      * component prototype
161      *
162      * @return component type to replace
163      */
164     @BeanTagAttribute(name = "componentTypeToReplace")
165     public Class<? extends Component> getComponentTypeToReplace() {
166         return this.componentTypeToReplace;
167     }
168 
169     /**
170      * Setter for the component type to replace
171      *
172      * @param componentTypeToReplace
173      */
174     public void setComponentTypeToReplace(Class<? extends Component> componentTypeToReplace) {
175         this.componentTypeToReplace = componentTypeToReplace;
176     }
177 
178     /**
179      * Prototype for the component replacement
180      *
181      * <p>
182      * Each time the type to replace if found a new instance of the component
183      * prototype will be created and set as the new property value
184      * </p>
185      *
186      * @return Component
187      */
188     @BeanTagAttribute(name = "componentReplacementPrototype", type = BeanTagAttribute.AttributeType.SINGLEBEAN)
189     public Component getComponentReplacementPrototype() {
190         return this.componentReplacementPrototype;
191     }
192 
193     /**
194      * Setter for the replacement component prototype
195      *
196      * @param componentReplacementPrototype
197      */
198     public void setComponentReplacementPrototype(Component componentReplacementPrototype) {
199         this.componentReplacementPrototype = componentReplacementPrototype;
200     }
201 
202 }