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.service.DataDictionaryService;
 15  
 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
 16  
 import org.kuali.rice.krad.uif.container.CollectionGroup;
 17  
 import org.kuali.rice.krad.uif.container.Group;
 18  
 import org.kuali.rice.krad.uif.control.RadioGroupControl;
 19  
 import org.kuali.rice.krad.uif.control.TextControl;
 20  
 import org.kuali.rice.krad.uif.core.Component;
 21  
 import org.kuali.rice.krad.uif.field.AttributeField;
 22  
 import org.kuali.rice.krad.uif.field.Field;
 23  
 import org.kuali.rice.krad.uif.field.GroupField;
 24  
 import org.kuali.rice.krad.uif.field.LabelField;
 25  
 import org.kuali.rice.krad.uif.field.MessageField;
 26  
 import org.kuali.rice.krad.web.form.UifFormBase;
 27  
 
 28  
 import java.util.HashMap;
 29  
 import java.util.List;
 30  
 import java.util.Map;
 31  
 
 32  
 /**
 33  
  * Factory class for creating new UIF components from their base definitions
 34  
  * in the dictionary
 35  
  *
 36  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 37  
  */
 38  0
 public class ComponentFactory {
 39  
 
 40  0
     private static final Map<String, Component> componentDefinitions = new HashMap<String, Component>();
 41  
 
 42  
     protected static final String LABEL_FIELD = "LabelField";
 43  
     protected static final String MESSAGE_FIELD = "MessageField";
 44  
     protected static final String TEXT_CONTROL = "TextControl";
 45  
     protected static final String RADIO_GROUP_CONTROL = "RadioGroupControl";
 46  
 
 47  
     /**
 48  
      * Adds a component instance to the factories map of components indexed by the given dictionary id
 49  
      *
 50  
      * @param componentDictionaryId - id to index component under
 51  
      * @param component - component instance to add
 52  
      */
 53  
     public static void addComponentDefinition(String componentDictionaryId, Component component) {
 54  0
         componentDefinitions.put(componentDictionaryId, ComponentUtils.copyObject(component));
 55  0
     }
 56  
 
 57  
     /**
 58  
      * Returns a new <code>Component</code> instance from the given component id initialized by the
 59  
      * corresponding dictionary configuration
 60  
      *
 61  
      * @param componentDictionaryId - id for the component to retrieve, note this is the id initially
 62  
      * assigned and could be the spring generated id (if a static id was not set)
 63  
      * @return new component instance or null if no such component definition was found
 64  
      */
 65  
     public static Component getNewComponentInstance(String componentDictionaryId) {
 66  0
         if (componentDefinitions.containsKey(componentDictionaryId)) {
 67  0
             Component component = componentDefinitions.get(componentDictionaryId);
 68  
 
 69  0
             return ComponentUtils.copyObject(component);
 70  
         }
 71  
 
 72  0
         return null;
 73  
     }
 74  
 
 75  
     public static LabelField getLabelField() {
 76  0
         return (LabelField) getNewComponentInstance(LABEL_FIELD);
 77  
     }
 78  
 
 79  
     public static MessageField getMessageField() {
 80  0
         return (MessageField) getNewComponentInstance(MESSAGE_FIELD);
 81  
     }
 82  
 
 83  
     public static TextControl getTextControl() {
 84  0
         return (TextControl) getNewComponentInstance(TEXT_CONTROL);
 85  
     }
 86  
 
 87  
     public static RadioGroupControl getRadioGroupControl() {
 88  0
         return (RadioGroupControl) getNewComponentInstance(RADIO_GROUP_CONTROL);
 89  
     }
 90  
 
 91  
     /**
 92  
      * Gets a fresh copy of the component by the id passed in which is translated to the
 93  
      * corresponding dictionary id
 94  
      *
 95  
      * @param id - id for the component to retrieve
 96  
      * @return Component new instance initialized from the dictionary
 97  
      */
 98  
     public static Component getComponentById(UifFormBase form, String id) {
 99  0
         Component origComponent = form.getView().getViewIndex().getComponentById(id);
 100  0
         Component component = getNewComponentInstance(origComponent.getBaseId());
 101  
 
 102  0
         return component;
 103  
     }
 104  
 
 105  
     /**
 106  
      * Gets a fresh copy of the component by the id passed in with its lifecycle performed upon it,
 107  
      * using the form data passed in
 108  
      *
 109  
      * @param form - object containing the view data
 110  
      * @param id - id for the component to retrieve
 111  
      * @return Component instance that has been run through the lifecycle
 112  
      */
 113  
     public static Component getComponentByIdWithLifecycle(UifFormBase form, String id) {
 114  0
         Component origComponent = form.getView().getViewIndex().getComponentById(id);
 115  0
         Component component = getComponentById(form, id);
 116  
         
 117  0
         form.getView().getViewHelperService().performComponentLifecycle(form, component, id);
 118  
         
 119  
 
 120  0
         if(component instanceof Field){
 121  0
             ((Field) component).setLabelFieldRendered(((Field)origComponent).isLabelFieldRendered());
 122  
         }
 123  
         
 124  0
         if(component instanceof AttributeField){
 125  0
             ((AttributeField) component).setBindingInfo(((AttributeField)origComponent).getBindingInfo());
 126  
         }
 127  
         
 128  0
         if(component instanceof CollectionGroup){
 129  0
             ((CollectionGroup) component).setBindingInfo(((CollectionGroup)origComponent).getBindingInfo());
 130  
         }
 131  
         
 132  0
         if(component instanceof Group || component instanceof GroupField){
 133  0
             List<AttributeField> fields = ComponentUtils.getComponentsOfTypeDeep(component, AttributeField.class);
 134  0
             String suffix = StringUtils.replaceOnce(component.getId(), component.getBaseId(), "");
 135  0
             for(AttributeField field: fields){
 136  0
                 AttributeField origField = (AttributeField)form.getView().getViewIndex().getComponentById(StringUtils.replaceOnce(field.getId(), field.getBaseId(), field.getBaseId() + suffix));
 137  0
                 if(origField != null){
 138  0
                     field.setBindingInfo(origField.getBindingInfo());
 139  
                 }
 140  0
                 field.setLabelFieldRendered(origField.isLabelFieldRendered());
 141  0
             }
 142  
             
 143  0
             List<CollectionGroup> collections = ComponentUtils.getComponentsOfTypeDeep(component, CollectionGroup.class);
 144  0
             for(CollectionGroup collection: collections){
 145  0
                 CollectionGroup origField = (CollectionGroup)form.getView().getViewIndex().getComponentById(StringUtils.replaceOnce(collection.getId(), collection.getBaseId(), collection.getBaseId() + suffix));
 146  0
                 if(origField != null){
 147  0
                     collection.setBindingInfo(origField.getBindingInfo());
 148  
                 }
 149  0
             }
 150  
         }
 151  
         
 152  0
         form.getView().getViewIndex().indexComponent(component);
 153  
         
 154  0
         return component;
 155  
     }
 156  
     
 157  
 
 158  
 
 159  
     protected static DataDictionaryService getDataDictionaryService() {
 160  0
         return KRADServiceLocatorWeb.getDataDictionaryService();
 161  
     }
 162  
 }