Coverage Report - org.kuali.rice.kns.uif.util.LookupInquiryUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
LookupInquiryUtils
0%
0/61
0%
0/36
5.333
 
 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.Map;
 20  
 
 21  
 import javax.servlet.http.HttpServletRequest;
 22  
 
 23  
 import org.apache.commons.lang.StringUtils;
 24  
 import org.kuali.rice.core.api.encryption.EncryptionService;
 25  
 import org.kuali.rice.core.api.services.CoreApiServiceLocator;
 26  
 import org.kuali.rice.core.web.format.Formatter;
 27  
 import org.kuali.rice.kns.datadictionary.AttributeDefinition;
 28  
 import org.kuali.rice.kns.service.KNSServiceLocator;
 29  
 import org.kuali.rice.kns.service.KNSServiceLocatorWeb;
 30  
 import org.kuali.rice.kns.uif.control.CheckboxControl;
 31  
 import org.kuali.rice.kns.uif.control.Control;
 32  
 import org.kuali.rice.kns.uif.control.RadioGroupControl;
 33  
 import org.kuali.rice.kns.uif.control.TextAreaControl;
 34  
 import org.kuali.rice.kns.uif.control.TextControl;
 35  
 import org.kuali.rice.kns.uif.field.AttributeField;
 36  
 import org.kuali.rice.kns.util.KNSConstants;
 37  
 import org.kuali.rice.kns.web.spring.form.UifFormBase;
 38  
 
 39  
 /**
 40  
  * Class for utility methods that pertain to UIF Lookup processing
 41  
  * 
 42  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 43  
  */
 44  0
 public class LookupInquiryUtils {
 45  0
         private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(LookupInquiryUtils.class);
 46  
 
 47  
         public static void initializeAttributeFieldFromAttributeDefinition(AttributeField attributeField,
 48  
                         AttributeDefinition attributeDefinition) {
 49  
                 // label
 50  0
                 if (StringUtils.isEmpty(attributeField.getLabel())) {
 51  0
                         attributeField.setLabel(attributeDefinition.getLabel());
 52  
                 }
 53  
 
 54  
                 // short label
 55  0
                 if (StringUtils.isEmpty(attributeField.getShortLabel())) {
 56  0
                         attributeField.setShortLabel(attributeDefinition.getShortLabel());
 57  
                 }
 58  
 
 59  
                 // security
 60  0
                 if (attributeField.getAttributeSecurity() == null) {
 61  0
                         attributeField.setAttributeSecurity(attributeDefinition.getAttributeSecurity());
 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 = generateCustomLookupControlFromExisting(RadioGroupControl.class,
 114  
                                         attributeDefinition.getControlField());
 115  
                 }
 116  0
                 else if (TextAreaControl.class.isAssignableFrom(attributeDefinition.getControlField().getClass())) {
 117  0
                         newControl = generateCustomLookupControlFromExisting(TextControl.class,
 118  
                                         attributeDefinition.getControlField());
 119  
                 }
 120  
                 else {
 121  0
                         newControl = ComponentUtils.copy(attributeDefinition.getControlField());
 122  
                 }
 123  0
                 return newControl;
 124  
         }
 125  
 
 126  
         public static Control generateCustomLookupControlFromExisting(Class<? extends Control> controlClass,
 127  
                         Control existingControl) {
 128  
                 try {
 129  0
                         Control newControl = controlClass.newInstance();
 130  0
                         for (String propertyPath : ComponentUtils.getComponentPropertyNames(existingControl.getClass())) {
 131  0
                                 Object propValue = ObjectPropertyUtils.getPropertyValue(existingControl, propertyPath);
 132  0
                                 if (propValue != null) {
 133  0
                                         ObjectPropertyUtils.setPropertyValue(newControl, propertyPath, propValue, true);
 134  
                                 }
 135  0
                         }
 136  0
                         return newControl;
 137  
                 }
 138  0
                 catch (InstantiationException e) {
 139  0
                         LOG.error("Exception instantiating new instance of class '" + controlClass.getClass() + "'", e);
 140  0
                         throw new RuntimeException(e);
 141  
                 }
 142  0
                 catch (IllegalAccessException e) {
 143  0
                         LOG.error("Exception instantiating new instance of class '" + controlClass.getClass() + "'", e);
 144  0
                         throw new RuntimeException(e);
 145  
                 }
 146  
         }
 147  
 
 148  
         public static String retrieveLookupParameterValue(UifFormBase form, HttpServletRequest request,
 149  
                         Class<?> lookupObjectClass, String propertyName, String propertyValueName) {
 150  0
                 String parameterValue = "";
 151  
 
 152  
                 // get literal parameter values first
 153  0
                 if (StringUtils.contains(propertyValueName, "'")) {
 154  0
                         parameterValue = StringUtils.replace(propertyValueName, "'", "");
 155  
                 }
 156  0
                 else if (parameterValue.startsWith(KNSConstants.LOOKUP_PARAMETER_LITERAL_PREFIX
 157  
                                 + KNSConstants.LOOKUP_PARAMETER_LITERAL_DELIMITER)) {
 158  0
                         parameterValue = StringUtils.removeStart(parameterValue, KNSConstants.LOOKUP_PARAMETER_LITERAL_PREFIX
 159  
                                         + KNSConstants.LOOKUP_PARAMETER_LITERAL_DELIMITER);
 160  
                 }
 161  
                 // check if parameter is in request
 162  0
                 else if (request.getParameterMap().containsKey(propertyValueName)) {
 163  0
                         parameterValue = request.getParameter(propertyValueName);
 164  
                 }
 165  
                 // get parameter value from form object
 166  
                 else {
 167  0
                         Object value = ObjectPropertyUtils.getPropertyValue(form, propertyValueName);
 168  0
                         if (value != null) {
 169  0
                                 if (value instanceof String) {
 170  0
                                         parameterValue = (String) value;
 171  
                                 }
 172  
 
 173  0
                                 Formatter formatter = Formatter.getFormatter(value.getClass());
 174  0
                                 parameterValue = (String) formatter.format(value);
 175  
                         }
 176  
                 }
 177  
 
 178  0
                 if (parameterValue != null
 179  
                                 && lookupObjectClass != null
 180  
                                 && KNSServiceLocatorWeb.getBusinessObjectAuthorizationService()
 181  
                                                 .attributeValueNeedsToBeEncryptedOnFormsAndLinks(lookupObjectClass, propertyName)) {
 182  
                         try {
 183  0
                                 parameterValue = CoreApiServiceLocator.getEncryptionService().encrypt(parameterValue)
 184  
                                                 + EncryptionService.ENCRYPTION_POST_PREFIX;
 185  
                         }
 186  0
                         catch (GeneralSecurityException e) {
 187  0
                                 LOG.error("Unable to encrypt value for property name: " + propertyName);
 188  0
                                 throw new RuntimeException(e);
 189  0
                         }
 190  
                 }
 191  
 
 192  0
                 return parameterValue;
 193  
         }
 194  
         
 195  
         /**
 196  
          * Helper method for building the title text for an element and a map of
 197  
          * key/value pairs
 198  
          * 
 199  
          * @param prependText
 200  
          *            - text to prepend to the title
 201  
          * @param element
 202  
          *            - element class the title is being generated for, used to as
 203  
          *            the parent for getting the key labels
 204  
          * @param keyValueMap
 205  
          *            - map of key value pairs to add to the title text
 206  
          * @return String title text
 207  
          */
 208  
         public static String getTitleText(String prependText, Class<?> element, Map<String, String> keyValueMap) {
 209  0
                 StringBuffer titleText = new StringBuffer(prependText);
 210  0
                 for (String key : keyValueMap.keySet()) {
 211  0
                         String fieldVal = keyValueMap.get(key).toString();
 212  
 
 213  0
                         titleText.append(KNSServiceLocatorWeb.getDataDictionaryService().getAttributeLabel(element, key) + "="
 214  
                                         + fieldVal.toString() + " ");
 215  0
                 }
 216  
 
 217  0
                 return titleText.toString();
 218  
         }
 219  
 
 220  
 }