Coverage Report - org.kuali.rice.kns.uif.util.ComponentFactory
 
Classes in this File Line Coverage Branch Coverage Complexity
ComponentFactory
0%
0/19
0%
0/4
1.429
 
 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.kns.uif.util;
 12  
 
 13  
 import org.apache.commons.lang.StringUtils;
 14  
 import org.kuali.rice.kns.service.DataDictionaryService;
 15  
 import org.kuali.rice.kns.service.KNSServiceLocatorWeb;
 16  
 import org.kuali.rice.kns.uif.control.RadioGroupControl;
 17  
 import org.kuali.rice.kns.uif.control.TextControl;
 18  
 import org.kuali.rice.kns.uif.core.Component;
 19  
 import org.kuali.rice.kns.uif.field.MessageField;
 20  
 import org.kuali.rice.kns.web.spring.form.UifFormBase;
 21  
 
 22  
 import java.util.HashMap;
 23  
 import java.util.Map;
 24  
 
 25  
 /**
 26  
  * Factory class for creating new UIF components from their base definitions
 27  
  * in the dictionary
 28  
  *
 29  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 30  
  */
 31  0
 public class ComponentFactory {
 32  
 
 33  0
     private static final Map<String, Component> componentDefinitions = new HashMap<String, Component>();
 34  
 
 35  
     protected static final String MESSAGE_FIELD = "MessageField";
 36  
     protected static final String TEXT_CONTROL = "TextControl";
 37  
     protected static final String RADIO_GROUP_CONTROL = "RadioGroupControl";
 38  
 
 39  
     /**
 40  
      * Adds a component instance to the factories map of components indexed by the given dictionary id
 41  
      *
 42  
      * @param componentDictionaryId - id to index component under
 43  
      * @param component - component instance to add
 44  
      */
 45  
     public static void addComponentDefinition(String componentDictionaryId, Component component) {
 46  0
         componentDefinitions.put(componentDictionaryId, ComponentUtils.copyObject(component));
 47  0
     }
 48  
 
 49  
     /**
 50  
      * Returns a new <code>Component</code> instance from the given component id initialized by the
 51  
      * corresponding dictionary configuration
 52  
      *
 53  
      * @param componentDictionaryId - id for the component to retrieve, note this is the id initially
 54  
      * assigned and could be the spring generated id (if a static id was not set)
 55  
      * @return new component instance or null if no such component definition was found
 56  
      */
 57  
     public static Component getNewComponentInstance(String componentDictionaryId) {
 58  0
         if (componentDefinitions.containsKey(componentDictionaryId)) {
 59  0
             Component component = componentDefinitions.get(componentDictionaryId);
 60  
 
 61  0
             return ComponentUtils.copyObject(component);
 62  
         }
 63  
 
 64  0
         return null;
 65  
     }
 66  
 
 67  
     public static MessageField getMessageField() {
 68  0
         return (MessageField) getNewComponentInstance(MESSAGE_FIELD);
 69  
     }
 70  
 
 71  
     public static TextControl getTextControl() {
 72  0
         return (TextControl) getNewComponentInstance(TEXT_CONTROL);
 73  
     }
 74  
 
 75  
     public static RadioGroupControl getRadioGroupControl() {
 76  0
         return (RadioGroupControl) getNewComponentInstance(RADIO_GROUP_CONTROL);
 77  
     }
 78  
 
 79  
     /**
 80  
      * Gets a fresh copy of the component by the id passed in with its lifecycle performed upon it,
 81  
      * using the form data passed in.
 82  
      *
 83  
      * @param form
 84  
      * @param id
 85  
      * @return
 86  
      */
 87  
     public static Component getComponentById(UifFormBase form, String id) {
 88  0
         String origId = id;
 89  
 
 90  0
         if (id.contains("_")) {
 91  0
             id = StringUtils.substringBefore(id, "_");
 92  
         }
 93  0
         Component component = getNewComponentInstance(id);
 94  
 
 95  0
         form.getView().getViewHelperService().performComponentLifecycle(form, component, origId);
 96  0
         form.getView().getViewIndex().indexComponent(component);
 97  0
         return component;
 98  
     }
 99  
 
 100  
     protected static DataDictionaryService getDataDictionaryService() {
 101  0
         return KNSServiceLocatorWeb.getDataDictionaryService();
 102  
     }
 103  
 }