Coverage Report - org.kuali.rice.kns.util.ActionFormUtilMap
 
Classes in this File Line Coverage Branch Coverage Complexity
ActionFormUtilMap
0%
0/82
0%
0/20
5.857
 
 1  
 /*
 2  
  * Copyright 2006-2007 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.kns.util;
 17  
 
 18  
 import org.apache.commons.lang.StringUtils;
 19  
 import org.kuali.rice.core.api.services.CoreApiServiceLocator;
 20  
 import org.kuali.rice.kns.lookup.keyvalues.KeyValuesFinder;
 21  
 import org.kuali.rice.kns.lookup.keyvalues.KimAttributeValuesFinder;
 22  
 import org.kuali.rice.kns.lookup.keyvalues.PersistableBusinessObjectValuesFinder;
 23  
 import org.kuali.rice.kns.service.KNSServiceLocator;
 24  
 
 25  
 import java.lang.reflect.Method;
 26  
 import java.security.GeneralSecurityException;
 27  
 import java.util.ArrayList;
 28  
 import java.util.HashMap;
 29  
 import java.util.List;
 30  
 
 31  
 /**
 32  
  * Utility map for the action form to provide a way for calling functions through jstl.
 33  
  *
 34  
  *
 35  
  */
 36  
 @SuppressWarnings("unchecked")
 37  0
 public class ActionFormUtilMap extends HashMap {
 38  
     private static final long serialVersionUID = 1L;
 39  
         private boolean cacheValueFinderResults;
 40  
 
 41  
     /**
 42  
      * This method parses from the key the actual method to run.
 43  
      *
 44  
      * @see java.util.Map#get(java.lang.Object)
 45  
      */
 46  
         public Object get(Object key) {
 47  0
             if (cacheValueFinderResults) {
 48  0
                 if (super.containsKey(key)) {
 49  
                     // doing a 2 step retrieval allows us to also cache the null key correctly
 50  0
                     Object cachedObject = super.get(key);
 51  0
                         return cachedObject;
 52  
                 }
 53  
             }
 54  0
         String[] methodKey = StringUtils.split((String) key, KNSConstants.ACTION_FORM_UTIL_MAP_METHOD_PARM_DELIMITER);
 55  
 
 56  0
         String methodToCall = methodKey[0];
 57  
 
 58  
         // handle method calls with more than one parameter
 59  0
         Object[] methodParms = new Object[methodKey.length - 1];
 60  0
         Class[] methodParmsPrototype = new Class[methodKey.length - 1];
 61  0
         for (int i=1;i<methodKey.length;i++) {
 62  0
             methodParms[i-1] = methodKey[i];
 63  0
             methodParmsPrototype[i-1] = Object.class;
 64  
         }
 65  
 
 66  0
         Method method = null;
 67  
         try {
 68  0
             method = ActionFormUtilMap.class.getMethod(methodToCall, methodParmsPrototype);
 69  
         }
 70  0
         catch (SecurityException e) {
 71  0
             throw new RuntimeException("Unable to object handle on method given to ActionFormUtilMap: " + e.getMessage());
 72  
         }
 73  0
         catch (NoSuchMethodException e1) {
 74  0
             throw new RuntimeException("Unable to object handle on method given to ActionFormUtilMap: " + e1.getMessage());
 75  0
         }
 76  
 
 77  0
         Object methodValue = null;
 78  
         try {
 79  0
             methodValue = method.invoke(this, methodParms);
 80  
         }
 81  0
         catch (Exception e) {
 82  0
             throw new RuntimeException("Unable to invoke method " + methodToCall,e);
 83  0
         }
 84  
 
 85  0
         if (cacheValueFinderResults) {
 86  0
             super.put(key, methodValue);
 87  
         }
 88  
 
 89  0
         return methodValue;
 90  
     }
 91  
 
 92  
     /*
 93  
      * Will take in a class name parameter and attempt to create a KeyValueFinder instance, then call the finder to return a list of
 94  
      * KeyValue pairs. This is used by the htmlControlAttribute.tag to render select options from a given finder class specified in
 95  
      * the data dictionary.
 96  
      */
 97  
         public Object getOptionsMap(Object key) {
 98  0
         List optionsList = new ArrayList();
 99  
 
 100  0
         if (StringUtils.isBlank((String) key)) {
 101  0
             return optionsList;
 102  
         }
 103  
 
 104  
         /*
 105  
          * the class name has . replaced with | in the jsp to prevent struts from treating each part of the class name as a property
 106  
          * substitute back here to get the correct name
 107  
          */
 108  0
         key = StringUtils.replace((String) key, "|", ".");
 109  
 
 110  
         KeyValuesFinder finder;
 111  
         try {
 112  0
             Class finderClass = Class.forName((String) key);
 113  0
             finder = (KeyValuesFinder) finderClass.newInstance();
 114  0
             optionsList = finder.getKeyValues();
 115  
         }
 116  0
         catch (ClassNotFoundException e) {
 117  0
             throw new RuntimeException(e.getMessage());
 118  
         }
 119  0
         catch (InstantiationException e) {
 120  0
             throw new RuntimeException(e.getMessage());
 121  
         }
 122  0
         catch (IllegalAccessException e) {
 123  0
             throw new RuntimeException(e.getMessage());
 124  0
         }
 125  
 
 126  0
         return optionsList;
 127  
     }
 128  
 
 129  
     // Method added to keep backward compatibility for non-kimTypeId cases
 130  
     public Object getOptionsMap(Object key, Object boClass, Object keyAttribute, Object labelAttribute, Object includeKeyInLabel) {
 131  0
             return getOptionsMap(key, boClass, keyAttribute, labelAttribute, null, includeKeyInLabel );
 132  
     }
 133  
     
 134  
     public Object getOptionsMap(Object key, Object boClass, Object keyAttribute, Object labelAttribute, Object includeBlankRow, Object includeKeyInLabel) {
 135  0
             return getOptionsMap(key, boClass, keyAttribute, labelAttribute, includeBlankRow, includeKeyInLabel, null);
 136  
     }
 137  
     
 138  
     /*
 139  
      *
 140  
     */
 141  
     /**
 142  
      * This method will take in a key parameter (values finder class name - in this case the generic
 143  
      * PersistableObjectValuesFinder) along with the related parameters required by this ValuesFinder,
 144  
      * and attempt to create a KeyValueFinder instance, then call the finder to return a list of
 145  
      * KeyValue pairs. This is used by the htmlControlAttribute.tag to render select options from
 146  
      * a given finder class specified in the data dictionary.
 147  
      *
 148  
      * @param key values finder class name
 149  
      * @param boClass BO class name
 150  
      * @param keyAttribute name of BO attribute for key
 151  
      * @param labelAttribute name of BO attribute for label
 152  
      * @param includeKeyInLabel whether to include the key in the label or not
 153  
      * @param kimTypeId the KIM Type to use in case of KimAttributeValuesFinder 
 154  
      * @return list of KeyValue pairs
 155  
      */
 156  
         public Object getOptionsMap(Object key, Object boClass, Object keyAttribute, Object labelAttribute, Object includeBlankRow, Object includeKeyInLabel, Object kimTypeId) {
 157  0
         List optionsList = new ArrayList();
 158  
 
 159  0
         if (StringUtils.isBlank((String) key)) {
 160  0
             return optionsList;
 161  
         }
 162  
 
 163  
         /*
 164  
          * the class name has . replaced with | in the jsp to prevent struts from treating each part of the class name as a property
 165  
          * substitute back here to get the correct name
 166  
          */
 167  0
         key = StringUtils.replace((String) key, "|", ".");
 168  
 
 169  
         KeyValuesFinder finder;
 170  
         try {
 171  0
                     Class finderClass = Class.forName((String) key);
 172  0
             finder = (KeyValuesFinder) finderClass.newInstance();
 173  0
             if (finder instanceof PersistableBusinessObjectValuesFinder) {
 174  0
                 String businessObjectClassName = StringUtils.replace((String) boClass, "|", ".");
 175  0
                 Class businessObjectClass = Class.forName((String) businessObjectClassName);
 176  0
                 ((PersistableBusinessObjectValuesFinder) finder).setBusinessObjectClass(businessObjectClass);
 177  0
                 ((PersistableBusinessObjectValuesFinder) finder).setKeyAttributeName((String)keyAttribute);
 178  0
                 ((PersistableBusinessObjectValuesFinder) finder).setLabelAttributeName((String)labelAttribute);
 179  0
                 ((PersistableBusinessObjectValuesFinder) finder).setIncludeBlankRow(Boolean.parseBoolean((String)includeBlankRow)); 
 180  0
                 ((PersistableBusinessObjectValuesFinder) finder).setIncludeKeyInDescription(Boolean.parseBoolean((String)includeKeyInLabel));
 181  0
             } else if (finder instanceof KimAttributeValuesFinder) {
 182  0
                     if (kimTypeId == null) {
 183  0
                             throw new RuntimeException("kimTypeId must be specified for KimAttributeValuesFinder");
 184  
                     }
 185  0
                 ((KimAttributeValuesFinder) finder).setKimTypeId(kimTypeId.toString());
 186  0
                 ((KimAttributeValuesFinder) finder).setKimAttributeName(keyAttribute.toString());
 187  
             }
 188  
             
 189  0
             optionsList = finder.getKeyValues();
 190  
         }
 191  0
         catch (ClassNotFoundException e) {
 192  0
             throw new RuntimeException(e.getMessage(),e);
 193  
         }
 194  0
         catch (InstantiationException e) {
 195  0
             throw new RuntimeException(e.getMessage(),e);
 196  
         }
 197  0
         catch (IllegalAccessException e) {
 198  0
             throw new RuntimeException(e.getMessage(),e);
 199  0
         }
 200  
 
 201  0
         return optionsList;
 202  
     }
 203  
 
 204  
     /**
 205  
      * Encrypts a value passed from the ui.
 206  
      * @param value - clear text
 207  
      * @return String - encrypted text
 208  
      */
 209  
     public String encryptValue(Object value) {
 210  0
         String encrypted = "";
 211  0
         if (value != null) {
 212  0
             encrypted = value.toString();
 213  
         }
 214  
 
 215  
         try {
 216  0
             encrypted = CoreApiServiceLocator.getEncryptionService().encrypt(value);
 217  
         }
 218  0
         catch (GeneralSecurityException e) {
 219  0
             throw new RuntimeException("Unable to encrypt value in action form: " + e.getMessage());
 220  0
         }
 221  
 
 222  0
         return encrypted;
 223  
     }
 224  
 
 225  
     public void setCacheValueFinderResults(boolean cacheValueFinderResults) {
 226  0
         this.cacheValueFinderResults = cacheValueFinderResults;
 227  0
     }
 228  
 
 229  
 
 230  
 }