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