001: /*
002: * Copyright 2006-2007 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.opensource.org/licenses/ecl1.php
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.kuali.core.util;
017:
018: import java.lang.reflect.Method;
019: import java.security.GeneralSecurityException;
020: import java.util.ArrayList;
021: import java.util.HashMap;
022: import java.util.List;
023:
024: import org.apache.commons.lang.StringUtils;
025: import org.kuali.RiceConstants;
026: import org.kuali.core.lookup.keyvalues.ApcValuesFinder;
027: import org.kuali.core.lookup.keyvalues.KeyValuesFinder;
028: import org.kuali.rice.KNSServiceLocator;
029:
030: /**
031: * Utility map for the action form to provide a way for calling functions through jstl.
032: *
033: *
034: */
035: public class ActionFormUtilMap extends HashMap {
036: private boolean cacheValueFinderResults;
037:
038: /**
039: * This method parses from the key the actual method to run.
040: *
041: * @see java.util.Map#get(java.lang.Object)
042: */
043: public Object get(Object key) {
044: if (cacheValueFinderResults) {
045: if (super .containsKey(key)) {
046: // doing a 2 step retrieval allows us to also cache the null key correctly
047: Object cachedObject = super .get(key);
048: return cachedObject;
049: }
050: }
051: String[] methodKey = StringUtils
052: .split(
053: (String) key,
054: RiceConstants.ACTION_FORM_UTIL_MAP_METHOD_PARM_DELIMITER);
055:
056: String methodToCall = methodKey[0];
057: String methodParm = methodKey[1];
058:
059: Method method = null;
060: try {
061: method = ActionFormUtilMap.class.getMethod(methodToCall,
062: new Class[] { Object.class });
063: } catch (SecurityException e) {
064: throw new RuntimeException(
065: "Unable to object handle on method given to ActionFormUtilMap: "
066: + e.getMessage());
067: } catch (NoSuchMethodException e1) {
068: throw new RuntimeException(
069: "Unable to object handle on method given to ActionFormUtilMap: "
070: + e1.getMessage());
071: }
072:
073: Object methodValue = null;
074: try {
075: methodValue = method.invoke(this ,
076: new Object[] { methodParm });
077: } catch (Exception e) {
078: throw new RuntimeException("Unable to invoke method "
079: + e.getMessage());
080: }
081:
082: if (cacheValueFinderResults) {
083: super .put(key, methodValue);
084: }
085:
086: return methodValue;
087: }
088:
089: /*
090: * Will take in a class name parameter and attempt to create a KeyValueFinder instance, then call the finder to return a list of
091: * KeyValue pairs. This is used by the htmlControlAttribute.tag to render select options from a given finder class specified in
092: * the data dictionary.
093: */
094: public Object getOptionsMap(Object key) {
095: List optionsList = new ArrayList();
096:
097: if (StringUtils.isBlank((String) key)) {
098: return optionsList;
099: }
100:
101: /*
102: * the class name has . replaced with | in the jsp to prevent struts from treating each part of the class name as a property
103: * substitute back here to get the correct name
104: */
105: key = StringUtils.replace((String) key, "|", ".");
106:
107: KeyValuesFinder finder;
108: try {
109: Class finderClass = Class.forName((String) key);
110: finder = (KeyValuesFinder) finderClass.newInstance();
111: if (finder instanceof ApcValuesFinder) {
112: throw new IllegalArgumentException(
113: "Cannot currently use <apcSelect> in this form");
114: }
115: optionsList = finder.getKeyValues();
116: } catch (ClassNotFoundException e) {
117: throw new RuntimeException(e.getMessage());
118: } catch (InstantiationException e) {
119: throw new RuntimeException(e.getMessage());
120: } catch (IllegalAccessException e) {
121: throw new RuntimeException(e.getMessage());
122: }
123:
124: return optionsList;
125: }
126:
127: /**
128: * Encrypts a value passed from the ui.
129: * @param value - clear text
130: * @return String - encrypted text
131: */
132: public String encryptValue(Object value) {
133: String encrypted = "";
134: if (value != null) {
135: encrypted = value.toString();
136: }
137:
138: try {
139: encrypted = KNSServiceLocator.getEncryptionService()
140: .encrypt(value);
141: } catch (GeneralSecurityException e) {
142: throw new RuntimeException(
143: "Unable to encrypt value in action form: "
144: + e.getMessage());
145: }
146:
147: return encrypted;
148: }
149:
150: public void setCacheValueFinderResults(
151: boolean cacheValueFinderResults) {
152: this.cacheValueFinderResults = cacheValueFinderResults;
153: }
154:
155: }
|