Coverage Report - org.kuali.rice.krad.uif.util.ComponentFactory
 
Classes in this File Line Coverage Branch Coverage Complexity
ComponentFactory
0%
0/42
0%
0/20
2.222
 
 1  
 /*
 2  
  * Copyright 2011 The Kuali Foundation Licensed under the Educational Community
 3  
  * License, Version 1.0 (the "License"); you may not use this file except in
 4  
  * compliance with the License. You may obtain a copy of the License at
 5  
  * http://www.opensource.org/licenses/ecl1.php Unless required by applicable law
 6  
  * or agreed to in writing, software distributed under the License is
 7  
  * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 8  
  * KIND, either express or implied. See the License for the specific language
 9  
  * governing permissions and limitations under the License.
 10  
  */
 11  
 package org.kuali.rice.krad.uif.util;
 12  
 
 13  
 import org.apache.commons.lang.StringUtils;
 14  
 import org.kuali.rice.krad.uif.container.CollectionGroup;
 15  
 import org.kuali.rice.krad.uif.container.Group;
 16  
 import org.kuali.rice.krad.uif.control.RadioGroupControl;
 17  
 import org.kuali.rice.krad.uif.control.TextControl;
 18  
 import org.kuali.rice.krad.uif.core.Component;
 19  
 import org.kuali.rice.krad.uif.field.AttributeField;
 20  
 import org.kuali.rice.krad.uif.field.Field;
 21  
 import org.kuali.rice.krad.uif.field.GroupField;
 22  
 import org.kuali.rice.krad.uif.field.LabelField;
 23  
 import org.kuali.rice.krad.uif.field.MessageField;
 24  
 import org.kuali.rice.krad.web.form.UifFormBase;
 25  
 
 26  
 import java.util.HashMap;
 27  
 import java.util.List;
 28  
 import java.util.Map;
 29  
 
 30  
 /**
 31  
  * Factory class for creating new UIF components from their base definitions
 32  
  * in the dictionary
 33  
  *
 34  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 35  
  */
 36  0
 public class ComponentFactory {
 37  
 
 38  0
     private static final Map<String, Component> componentDefinitions = new HashMap<String, Component>();
 39  
 
 40  
     protected static final String LABEL_FIELD = "LabelField";
 41  
     protected static final String MESSAGE_FIELD = "MessageField";
 42  
     protected static final String TEXT_CONTROL = "TextControl";
 43  
     protected static final String RADIO_GROUP_CONTROL = "RadioGroupControl";
 44  
     protected static final String RADIO_GROUP_CONTROL_HORIZONTAL = "RadioGroupControlHorizontal";
 45  
 
 46  
     /**
 47  
      * Adds a component instance to the factories map of components indexed by the given dictionary id
 48  
      *
 49  
      * @param componentDictionaryId - id to index component under
 50  
      * @param component - component instance to add
 51  
      */
 52  
     public static void addComponentDefinition(String componentDictionaryId, Component component) {
 53  0
         componentDefinitions.put(componentDictionaryId, ComponentUtils.copyObject(component));
 54  0
     }
 55  
 
 56  
     /**
 57  
      * Returns a new <code>Component</code> instance from the given component id initialized by the
 58  
      * corresponding dictionary configuration
 59  
      *
 60  
      * @param componentDictionaryId - id for the component to retrieve, note this is the id initially
 61  
      * assigned and could be the spring generated id (if a static id was not set)
 62  
      * @return new component instance or null if no such component definition was found
 63  
      */
 64  
     public static Component getNewComponentInstance(String componentDictionaryId) {
 65  0
         if (componentDefinitions.containsKey(componentDictionaryId)) {
 66  0
             Component component = componentDefinitions.get(componentDictionaryId);
 67  
 
 68  0
             return ComponentUtils.copyObject(component);
 69  
         }
 70  
 
 71  0
         return null;
 72  
     }
 73  
 
 74  
     public static LabelField getLabelField() {
 75  0
         return (LabelField) getNewComponentInstance(LABEL_FIELD);
 76  
     }
 77  
 
 78  
     public static MessageField getMessageField() {
 79  0
         return (MessageField) getNewComponentInstance(MESSAGE_FIELD);
 80  
     }
 81  
 
 82  
     public static TextControl getTextControl() {
 83  0
         return (TextControl) getNewComponentInstance(TEXT_CONTROL);
 84  
     }
 85  
 
 86  
     public static RadioGroupControl getRadioGroupControl() {
 87  0
         return (RadioGroupControl) getNewComponentInstance(RADIO_GROUP_CONTROL);
 88  
     }
 89  
 
 90  
     public static RadioGroupControl getRadioGroupControlHorizontal() {
 91  0
         return (RadioGroupControl) getNewComponentInstance(RADIO_GROUP_CONTROL_HORIZONTAL);
 92  
     }
 93  
 
 94  
     /**
 95  
      * Gets a fresh copy of the component by the id passed in which is translated to the
 96  
      * corresponding dictionary id
 97  
      *
 98  
      * @param id - id for the component to retrieve
 99  
      * @return Component new instance initialized from the dictionary
 100  
      */
 101  
     public static Component getComponentById(UifFormBase form, String id) {
 102  0
         Component origComponent = form.getView().getViewIndex().getComponentById(id);
 103  0
         Component component = getNewComponentInstance(origComponent.getBaseId());
 104  
 
 105  0
         return component;
 106  
     }
 107  
 
 108  
     /**
 109  
      * Gets a fresh copy of the component by the id passed in with its lifecycle performed upon it,
 110  
      * using the form data passed in
 111  
      *
 112  
      * @param form - object containing the view data
 113  
      * @param id - id for the component to retrieve
 114  
      * @return Component instance that has been run through the lifecycle
 115  
      */
 116  
     public static Component getComponentByIdWithLifecycle(UifFormBase form, String id) {
 117  0
         Component origComponent = form.getView().getViewIndex().getComponentById(id);
 118  0
         Component component = getComponentById(form, id);
 119  
 
 120  0
         form.getView().getViewHelperService().performComponentLifecycle(form, component, id);
 121  
 
 122  0
         if (component instanceof Field) {
 123  0
             ((Field) component).setLabelFieldRendered(((Field) origComponent).isLabelFieldRendered());
 124  
         }
 125  
 
 126  0
         if (component instanceof AttributeField) {
 127  0
             ((AttributeField) component).setBindingInfo(((AttributeField) origComponent).getBindingInfo());
 128  
         }
 129  
 
 130  0
         if (component instanceof CollectionGroup) {
 131  0
             ((CollectionGroup) component).setBindingInfo(((CollectionGroup) origComponent).getBindingInfo());
 132  
         }
 133  
 
 134  0
         if (component instanceof Group || component instanceof GroupField) {
 135  0
             List<AttributeField> fields = ComponentUtils.getComponentsOfTypeDeep(component, AttributeField.class);
 136  0
             String suffix = StringUtils.replaceOnce(component.getId(), component.getBaseId(), "");
 137  0
             for (AttributeField field : fields) {
 138  0
                 AttributeField origField = (AttributeField) form.getView().getViewIndex().getComponentById(
 139  
                         StringUtils.replaceOnce(field.getId(), field.getBaseId(), field.getBaseId() + suffix));
 140  0
                 if (origField != null) {
 141  0
                     field.setBindingInfo(origField.getBindingInfo());
 142  0
                     field.setLabelFieldRendered(origField.isLabelFieldRendered());
 143  
                 }
 144  0
             }
 145  
 
 146  0
             List<CollectionGroup> collections =
 147  
                     ComponentUtils.getComponentsOfTypeDeep(component, CollectionGroup.class);
 148  0
             for (CollectionGroup collection : collections) {
 149  0
                 CollectionGroup origField = (CollectionGroup) form.getView().getViewIndex().getComponentById(StringUtils
 150  
                         .replaceOnce(collection.getId(), collection.getBaseId(), collection.getBaseId() + suffix));
 151  0
                 if (origField != null) {
 152  0
                     collection.setBindingInfo(origField.getBindingInfo());
 153  
                 }
 154  0
             }
 155  
         }
 156  
 
 157  0
         form.getView().getViewIndex().indexComponent(component);
 158  
 
 159  0
         return component;
 160  
     }
 161  
 
 162  
 }