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