Coverage Report - org.kuali.rice.krad.service.impl.DataDictionaryRemoteFieldServiceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
DataDictionaryRemoteFieldServiceImpl
0%
0/100
0%
0/50
6.857
 
 1  
 /**
 2  
  * Copyright 2005-2011 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.service.impl;
 17  
 
 18  
 import org.apache.commons.lang.StringUtils;
 19  
 import org.kuali.rice.core.api.config.property.ConfigurationService;
 20  
 import org.kuali.rice.core.api.exception.RiceRuntimeException;
 21  
 import org.kuali.rice.core.api.uif.RemotableAbstractControl;
 22  
 import org.kuali.rice.core.api.uif.RemotableAbstractWidget;
 23  
 import org.kuali.rice.core.api.uif.RemotableAttributeField;
 24  
 import org.kuali.rice.core.api.uif.RemotableCheckboxGroup;
 25  
 import org.kuali.rice.core.api.uif.RemotableHiddenInput;
 26  
 import org.kuali.rice.core.api.uif.RemotableQuickFinder;
 27  
 import org.kuali.rice.core.api.uif.RemotableRadioButtonGroup;
 28  
 import org.kuali.rice.core.api.uif.RemotableSelect;
 29  
 import org.kuali.rice.core.api.uif.RemotableTextInput;
 30  
 import org.kuali.rice.core.api.uif.RemotableTextarea;
 31  
 import org.kuali.rice.core.api.util.KeyValue;
 32  
 import org.kuali.rice.kim.api.KimConstants;
 33  
 import org.kuali.rice.kns.kim.type.DataDictionaryTypeServiceHelper;
 34  
 import org.kuali.rice.kns.lookup.LookupUtils;
 35  
 import org.kuali.rice.kns.util.FieldUtils;
 36  
 import org.kuali.rice.kns.web.ui.Field;
 37  
 import org.kuali.rice.krad.bo.BusinessObject;
 38  
 import org.kuali.rice.krad.bo.DataObjectRelationship;
 39  
 import org.kuali.rice.krad.datadictionary.AttributeDefinition;
 40  
 import org.kuali.rice.krad.service.DataDictionaryRemoteFieldService;
 41  
 import org.kuali.rice.krad.service.DataDictionaryService;
 42  
 import org.kuali.rice.krad.service.DataObjectMetaDataService;
 43  
 import org.kuali.rice.krad.service.KRADServiceLocator;
 44  
 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
 45  
 import org.kuali.rice.krad.uif.control.CheckboxGroupControl;
 46  
 import org.kuali.rice.krad.uif.control.Control;
 47  
 import org.kuali.rice.krad.uif.control.GroupControl;
 48  
 import org.kuali.rice.krad.uif.control.HiddenControl;
 49  
 import org.kuali.rice.krad.uif.control.MultiValueControl;
 50  
 import org.kuali.rice.krad.uif.control.RadioGroupControl;
 51  
 import org.kuali.rice.krad.uif.control.SelectControl;
 52  
 import org.kuali.rice.krad.uif.control.TextAreaControl;
 53  
 import org.kuali.rice.krad.uif.control.TextControl;
 54  
 import org.kuali.rice.krad.uif.control.UserControl;
 55  
 import org.kuali.rice.krad.util.KRADConstants;
 56  
 
 57  
 import java.util.Collections;
 58  
 import java.util.HashMap;
 59  
 import java.util.List;
 60  
 import java.util.Map;
 61  
 
 62  
 /**
 63  
  * Implementation of the {@link DataDictionaryRemoteFieldService} service
 64  
  *
 65  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 66  
  */
 67  0
 public class DataDictionaryRemoteFieldServiceImpl implements DataDictionaryRemoteFieldService {
 68  
 
 69  
     /**
 70  
      * @see org.kuali.rice.krad.service.DataDictionaryRemoteFieldService#buildRemotableFieldFromAttributeDefinition(java.lang.String,
 71  
      *      java.lang.String)
 72  
      */
 73  
     public RemotableAttributeField buildRemotableFieldFromAttributeDefinition(String componentClassName,
 74  
             String attributeName) {
 75  
         AttributeDefinition baseDefinition;
 76  
         Class<?> componentClass;
 77  
         // try to resolve the component name - if not possible - try to pull the definition from the app mediation service
 78  
         try {
 79  0
             componentClass = (Class<? extends BusinessObject>) Class.forName(componentClassName);
 80  0
             baseDefinition = getDataDictionaryService().getDataDictionary().getDataObjectEntry(componentClassName)
 81  
                     .getAttributeDefinition(attributeName);
 82  0
         } catch (ClassNotFoundException ex) {
 83  0
             throw new RiceRuntimeException("Unable to find attribute definition for attribute : " + attributeName);
 84  0
         }
 85  
 
 86  0
         RemotableAttributeField.Builder definition = RemotableAttributeField.Builder.create(baseDefinition.getName());
 87  
 
 88  0
         definition.setLongLabel(baseDefinition.getLabel());
 89  0
         definition.setShortLabel(baseDefinition.getShortLabel());
 90  0
         definition.setMaxLength(baseDefinition.getMaxLength());
 91  0
         definition.setRequired(baseDefinition.isRequired());
 92  0
         definition.setForceUpperCase(baseDefinition.getForceUppercase());
 93  
 
 94  0
         RemotableAbstractControl.Builder control = createControl(baseDefinition);
 95  0
         if (control != null) {
 96  0
             definition.setControl(control);
 97  
         }
 98  
 
 99  0
         RemotableQuickFinder.Builder qf = createQuickFinder(componentClass, attributeName);
 100  0
         if (qf != null) {
 101  0
             definition.setWidgets(Collections.<RemotableAbstractWidget.Builder>singletonList(qf));
 102  
         }
 103  
 
 104  0
         return definition.build();
 105  
     }
 106  
 
 107  
     /**
 108  
      * Creates a {@link RemotableAbstractControl} instance based on the control definition within the given
 109  
      * attribute definition
 110  
      *
 111  
      * @param attr - attribute definition instance to pull control from
 112  
      * @return RemotableAbstractControl instance or null if one could not be built
 113  
      */
 114  
     protected RemotableAbstractControl.Builder createControl(AttributeDefinition attr) {
 115  0
         Control control = attr.getControlField();
 116  
 
 117  0
         if (control != null) {
 118  0
             if (control instanceof CheckboxGroupControl) {
 119  0
                 return RemotableCheckboxGroup.Builder.create(getValues(attr));
 120  0
             } else if (control instanceof HiddenControl) {
 121  0
                 return RemotableHiddenInput.Builder.create();
 122  0
             } else if (control instanceof SelectControl) {
 123  0
                 RemotableSelect.Builder b = RemotableSelect.Builder.create(getValues(attr));
 124  0
                 b.setMultiple(((SelectControl) control).isMultiple());
 125  0
                 b.setSize(((SelectControl) control).getSize());
 126  0
             } else if (control instanceof RadioGroupControl) {
 127  0
                 return RemotableRadioButtonGroup.Builder.create(getValues(attr));
 128  0
             } else if (control instanceof TextControl) {
 129  0
                 final RemotableTextInput.Builder b = RemotableTextInput.Builder.create();
 130  0
                 b.setSize(((TextControl) control).getSize());
 131  0
                 return b;
 132  0
             } else if (control instanceof UserControl) {
 133  0
                 final RemotableTextInput.Builder b = RemotableTextInput.Builder.create();
 134  0
                 b.setSize(((UserControl) control).getSize());
 135  0
                 return b;
 136  0
             } else if (control instanceof GroupControl) {
 137  0
                 final RemotableTextInput.Builder b = RemotableTextInput.Builder.create();
 138  0
                 b.setSize(((GroupControl) control).getSize());
 139  0
                 return b;
 140  0
             } else if (control instanceof TextAreaControl) {
 141  0
                 final RemotableTextarea.Builder b = RemotableTextarea.Builder.create();
 142  0
                 b.setCols(((TextAreaControl) control).getCols());
 143  0
                 b.setRows(((TextAreaControl) control).getRows());
 144  0
                 return b;
 145  
             }
 146  
         }
 147  
 
 148  0
         return null;
 149  
     }
 150  
 
 151  
     /**
 152  
      * Will first try to retrieve options configured on the control.  If that doesn't return any values then will
 153  
      * try to use the optionfinder on the AttributeDefinition.
 154  
      *
 155  
      * @param attr - AttributeDefinition
 156  
      * @return Map of key value pairs
 157  
      */
 158  
     protected Map<String, String> getValues(AttributeDefinition attr) {
 159  0
         Control control = attr.getControlField();
 160  
 
 161  0
         if ((control instanceof MultiValueControl)
 162  
                 && (((MultiValueControl) control).getOptions() != null)
 163  
                 && !((MultiValueControl) control).getOptions().isEmpty()) {
 164  0
             List<KeyValue> keyValues = ((MultiValueControl) control).getOptions();
 165  0
             Map<String, String> options = new HashMap<String, String>();
 166  0
             for (KeyValue keyValue : keyValues) {
 167  0
                 options.put(keyValue.getKey(), keyValue.getValue());
 168  
             }
 169  0
             return options;
 170  0
         } else if (attr.getOptionsFinder() != null) {
 171  0
             return attr.getOptionsFinder().getKeyLabelMap();
 172  
         }
 173  
 
 174  0
         return Collections.emptyMap();
 175  
     }
 176  
 
 177  
     /**
 178  
      * Builds a {@link RemotableQuickFinder} instance for the given attribute based on determined relationships
 179  
      *
 180  
      * <p>
 181  
      * Uses the {@link DataObjectMetaDataService} to find relationships the given attribute participates in within the
 182  
      * given class. If a relationship is not found, the title attribute is also checked to determine if a lookup should
 183  
      * be rendered back to the component class itself. If a relationship suitable for lookup is found, the associated
 184  
      * field conversions and lookup parameters are built
 185  
      * </p>
 186  
      *
 187  
      * @param componentClass - class that attribute belongs to and should be checked for relationships
 188  
      * @param attributeName - name of the attribute to determine quickfinder for
 189  
      * @return RemotableQuickFinder.Builder instance for the configured lookup, or null if one could not be found
 190  
      */
 191  
     protected RemotableQuickFinder.Builder createQuickFinder(Class<?> componentClass, String attributeName) {
 192  
         Object sampleComponent;
 193  
         try {
 194  0
             sampleComponent = componentClass.newInstance();
 195  0
         } catch (InstantiationException e) {
 196  0
             throw new RiceRuntimeException(e);
 197  0
         } catch (IllegalAccessException e) {
 198  0
             throw new RiceRuntimeException(e);
 199  0
         }
 200  
 
 201  0
         String lookupClassName = null;
 202  0
         Map<String, String> fieldConversions = new HashMap<String, String>();
 203  0
         Map<String, String> lookupParameters = new HashMap<String, String>();
 204  
 
 205  0
         DataObjectRelationship relationship = getDataObjectMetaDataService().getDataObjectRelationship(sampleComponent,
 206  
                 componentClass, attributeName, "", true, true, false);
 207  0
         if (relationship != null) {
 208  0
             lookupClassName = relationship.getRelatedClass().getName();
 209  
 
 210  0
             for (Map.Entry<String, String> entry : relationship.getParentToChildReferences().entrySet()) {
 211  0
                 String fromField = entry.getValue();
 212  0
                 String toField = entry.getKey();
 213  0
                 fieldConversions.put(fromField, toField);
 214  0
             }
 215  
 
 216  0
             for (Map.Entry<String, String> entry : relationship.getParentToChildReferences().entrySet()) {
 217  0
                 String fromField = entry.getKey();
 218  0
                 String toField = entry.getValue();
 219  
 
 220  0
                 if (relationship.getUserVisibleIdentifierKey() == null || relationship.getUserVisibleIdentifierKey()
 221  
                         .equals(fromField)) {
 222  0
                     lookupParameters.put(fromField, toField);
 223  
                 }
 224  0
             }
 225  
         } else {
 226  
             // check for title attribute and if match build lookup to component class using pk fields
 227  0
             String titleAttribute = getDataObjectMetaDataService().getTitleAttribute(componentClass);
 228  0
             if (StringUtils.equals(titleAttribute, attributeName)) {
 229  0
                 lookupClassName = componentClass.getName();
 230  
 
 231  0
                 List<String> pkAttributes = getDataObjectMetaDataService().listPrimaryKeyFieldNames(componentClass);
 232  0
                 for (String pkAttribute : pkAttributes) {
 233  0
                     fieldConversions.put(pkAttribute, pkAttribute);
 234  0
                     if (!StringUtils.equals(pkAttribute, attributeName)) {
 235  0
                         lookupParameters.put(pkAttribute, pkAttribute);
 236  
                     }
 237  
                 }
 238  
             }
 239  
         }
 240  
 
 241  0
         if (StringUtils.isNotBlank(lookupClassName)) {
 242  0
             String baseUrl = getKualiConfigurationService().getPropertyValueAsString(KRADConstants.KRAD_LOOKUP_URL_KEY);
 243  0
             RemotableQuickFinder.Builder builder = RemotableQuickFinder.Builder.create(baseUrl, lookupClassName);
 244  0
             builder.setLookupParameters(lookupParameters);
 245  0
             builder.setFieldConversions(fieldConversions);
 246  
 
 247  0
             return builder;
 248  
         }
 249  
 
 250  0
         return null;
 251  
     }
 252  
 
 253  
     protected DataDictionaryService getDataDictionaryService() {
 254  0
         return KRADServiceLocatorWeb.getDataDictionaryService();
 255  
     }
 256  
 
 257  
     protected DataObjectMetaDataService getDataObjectMetaDataService() {
 258  0
         return KRADServiceLocatorWeb.getDataObjectMetaDataService();
 259  
     }
 260  
 
 261  
     protected ConfigurationService getKualiConfigurationService() {
 262  0
         return KRADServiceLocator.getKualiConfigurationService();
 263  
     }
 264  
 }