Coverage Report - org.kuali.rice.kns.uif.util.LookupInquiryUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
LookupInquiryUtils
0%
0/65
0%
0/34
5.167
 
 1  
 /*
 2  
  * Copyright 2011 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 1.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/ecl1.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.kns.uif.util;
 17  
 
 18  
 import java.security.GeneralSecurityException;
 19  
 import java.util.ArrayList;
 20  
 import java.util.HashMap;
 21  
 import java.util.List;
 22  
 import java.util.Map;
 23  
 
 24  
 import javax.servlet.http.HttpServletRequest;
 25  
 
 26  
 import org.apache.commons.lang.StringUtils;
 27  
 import org.hibernate.mapping.Component;
 28  
 import org.kuali.rice.core.api.encryption.EncryptionService;
 29  
 import org.kuali.rice.core.api.services.CoreApiServiceLocator;
 30  
 import org.kuali.rice.core.util.ConcreteKeyValue;
 31  
 import org.kuali.rice.core.util.KeyValue;
 32  
 import org.kuali.rice.core.web.format.Formatter;
 33  
 import org.kuali.rice.kns.datadictionary.AttributeDefinition;
 34  
 import org.kuali.rice.kns.service.KNSServiceLocatorWeb;
 35  
 import org.kuali.rice.kns.uif.control.CheckboxControl;
 36  
 import org.kuali.rice.kns.uif.control.Control;
 37  
 import org.kuali.rice.kns.uif.control.RadioGroupControl;
 38  
 import org.kuali.rice.kns.uif.control.TextAreaControl;
 39  
 import org.kuali.rice.kns.uif.control.TextControl;
 40  
 import org.kuali.rice.kns.uif.field.AttributeField;
 41  
 import org.kuali.rice.kns.util.KNSConstants;
 42  
 import org.kuali.rice.kns.web.spring.form.UifFormBase;
 43  
 
 44  
 /**
 45  
  * Class for utility methods that pertain to UIF Lookup processing
 46  
  * 
 47  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 48  
  */
 49  0
 public class LookupInquiryUtils {
 50  0
         private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(LookupInquiryUtils.class);
 51  
 
 52  
         public static void initializeAttributeFieldFromAttributeDefinition(AttributeField attributeField,
 53  
                         AttributeDefinition attributeDefinition) {
 54  
                 // label
 55  0
                 if (StringUtils.isEmpty(attributeField.getLabel())) {
 56  0
                         attributeField.setLabel(attributeDefinition.getLabel());
 57  
                 }
 58  
 
 59  
                 // short label
 60  0
                 if (StringUtils.isEmpty(attributeField.getShortLabel())) {
 61  0
                         attributeField.setShortLabel(attributeDefinition.getShortLabel());
 62  
                 }
 63  
 
 64  
                 // control
 65  0
                 setupControlFromAttributeDefinition(attributeField, attributeDefinition);
 66  
 
 67  
                 // do not inherit requiredness
 68  
                 // if (attributeField.getRequired() == null) {
 69  
                 // attributeField.setRequired(attributeDefinition.isRequired());
 70  
                 // }
 71  
 
 72  
                 // do not inherit summary
 73  
                 // if (StringUtils.isEmpty(attributeField.getSummary())) {
 74  
                 // attributeField.setSummary(attributeDefinition.getSummary());
 75  
                 // }
 76  
 
 77  
                 // do not inherit description
 78  
                 // if (StringUtils.isEmpty(attributeField.getDescription())) {
 79  
                 // attributeField.setDescription(attributeDefinition.getDescription());
 80  
                 // }
 81  
 
 82  
                 // do not inherit constraint
 83  
                 // if (StringUtils.isEmpty(attributeField.getConstraint())) {
 84  
                 // attributeField.setConstraint(attributeDefinition.getConstraint());
 85  
                 // }
 86  
 
 87  
                 // max length
 88  
                 // if (attributeField.getMaxLength() == null) {
 89  
                 // attributeField.setMaxLength(attributeDefinition.getMaxLength());
 90  
                 // }
 91  
 
 92  
                 // min length
 93  
                 // if (attributeField.getMinLength() == null) {
 94  
                 // attributeField.setMinLength(attributeDefinition.getMinLength());
 95  
                 // }
 96  
 
 97  0
         }
 98  
 
 99  
         protected static void setupControlFromAttributeDefinition(AttributeField attributeField,
 100  
                         AttributeDefinition attributeDefinition) {
 101  0
                 if (attributeField.getControl() == null) {
 102  0
                         attributeField.setControl(convertControlToLookupControl(attributeDefinition));
 103  
                 }
 104  0
         }
 105  
 
 106  
         protected static Control convertControlToLookupControl(AttributeDefinition attributeDefinition) {
 107  0
                 Control newControl = null;
 108  0
                 if (attributeDefinition.getControlField() == null) {
 109  0
                     return null;
 110  
                 }
 111  
                 
 112  0
                 if (CheckboxControl.class.isAssignableFrom(attributeDefinition.getControlField().getClass())) {
 113  0
                         newControl = ComponentFactory.getRadioGroupControl();
 114  0
             List<KeyValue> options = new ArrayList<KeyValue>();
 115  0
             options.add(new ConcreteKeyValue("Y", "Yes"));
 116  0
             options.add(new ConcreteKeyValue("N", "No"));
 117  0
             options.add(new ConcreteKeyValue("", "Both"));
 118  
 
 119  0
             ((RadioGroupControl) newControl).setOptions(options);
 120  0
                 }
 121  0
                 else if (TextAreaControl.class.isAssignableFrom(attributeDefinition.getControlField().getClass())) {
 122  0
                         newControl = ComponentFactory.getTextControl();
 123  
                 }
 124  
                 else {
 125  0
                         newControl = ComponentUtils.copy(attributeDefinition.getControlField(), "");
 126  
                 }
 127  
 
 128  0
                 return newControl;
 129  
         }
 130  
 
 131  
         public static Control generateCustomLookupControlFromExisting(Class<? extends Control> controlClass,
 132  
                         Control existingControl) {
 133  
                 try {
 134  0
                         Control newControl = controlClass.newInstance();
 135  0
                         for (String propertyPath : ComponentUtils.getComponentPropertyNames(existingControl.getClass())) {
 136  0
                                 Object propValue = ObjectPropertyUtils.getPropertyValue(existingControl, propertyPath);
 137  0
                                 if (propValue != null) {
 138  0
                                         ObjectPropertyUtils.setPropertyValue(newControl, propertyPath, propValue, true);
 139  
                                 }
 140  0
                         }
 141  0
                         return newControl;
 142  
                 }
 143  0
                 catch (InstantiationException e) {
 144  0
                         LOG.error("Exception instantiating new instance of class '" + controlClass.getClass() + "'", e);
 145  0
                         throw new RuntimeException(e);
 146  
                 }
 147  0
                 catch (IllegalAccessException e) {
 148  0
                         LOG.error("Exception instantiating new instance of class '" + controlClass.getClass() + "'", e);
 149  0
                         throw new RuntimeException(e);
 150  
                 }
 151  
         }
 152  
 
 153  
         public static String retrieveLookupParameterValue(UifFormBase form, HttpServletRequest request,
 154  
                         Class<?> lookupObjectClass, String propertyName, String propertyValueName) {
 155  0
                 String parameterValue = "";
 156  
 
 157  
                 // get literal parameter values first
 158  0
                 if (StringUtils.contains(propertyValueName, "'")) {
 159  0
                         parameterValue = StringUtils.replace(propertyValueName, "'", "");
 160  
                 }
 161  0
                 else if (parameterValue.startsWith(KNSConstants.LOOKUP_PARAMETER_LITERAL_PREFIX
 162  
                                 + KNSConstants.LOOKUP_PARAMETER_LITERAL_DELIMITER)) {
 163  0
                         parameterValue = StringUtils.removeStart(parameterValue, KNSConstants.LOOKUP_PARAMETER_LITERAL_PREFIX
 164  
                                         + KNSConstants.LOOKUP_PARAMETER_LITERAL_DELIMITER);
 165  
                 }
 166  
                 // check if parameter is in request
 167  0
                 else if (request.getParameterMap().containsKey(propertyValueName)) {
 168  0
                         parameterValue = request.getParameter(propertyValueName);
 169  
                 }
 170  
                 // get parameter value from form object
 171  
                 else {
 172  0
                         Object value = ObjectPropertyUtils.getPropertyValue(form, propertyValueName);
 173  0
                         if (value != null) {
 174  0
                                 if (value instanceof String) {
 175  0
                                         parameterValue = (String) value;
 176  
                                 }
 177  
 
 178  0
                                 Formatter formatter = Formatter.getFormatter(value.getClass());
 179  0
                                 parameterValue = (String) formatter.format(value);
 180  
                         }
 181  
                 }
 182  
 
 183  0
                 if (parameterValue != null
 184  
                                 && lookupObjectClass != null
 185  
                                 && KNSServiceLocatorWeb.getBusinessObjectAuthorizationService()
 186  
                                                 .attributeValueNeedsToBeEncryptedOnFormsAndLinks(lookupObjectClass, propertyName)) {
 187  
                         try {
 188  0
                                 parameterValue = CoreApiServiceLocator.getEncryptionService().encrypt(parameterValue)
 189  
                                                 + EncryptionService.ENCRYPTION_POST_PREFIX;
 190  
                         }
 191  0
                         catch (GeneralSecurityException e) {
 192  0
                                 LOG.error("Unable to encrypt value for property name: " + propertyName);
 193  0
                                 throw new RuntimeException(e);
 194  0
                         }
 195  
                 }
 196  
 
 197  0
                 return parameterValue;
 198  
         }
 199  
         
 200  
         /**
 201  
          * Helper method for building the title text for an element and a map of
 202  
          * key/value pairs
 203  
          * 
 204  
          * @param prependText
 205  
          *            - text to prepend to the title
 206  
          * @param element
 207  
          *            - element class the title is being generated for, used to as
 208  
          *            the parent for getting the key labels
 209  
          * @param keyValueMap
 210  
          *            - map of key value pairs to add to the title text
 211  
          * @return String title text
 212  
          */
 213  
         public static String getTitleText(String prependText, Class<?> element, Map<String, String> keyValueMap) {
 214  0
                 StringBuffer titleText = new StringBuffer(prependText);
 215  0
                 for (String key : keyValueMap.keySet()) {
 216  0
                         String fieldVal = keyValueMap.get(key).toString();
 217  
 
 218  0
                         titleText.append(KNSServiceLocatorWeb.getDataDictionaryService().getAttributeLabel(element, key) + "="
 219  
                                         + fieldVal.toString() + " ");
 220  0
                 }
 221  
 
 222  0
                 return titleText.toString();
 223  
         }
 224  
 
 225  
 }