1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
32
33
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
43
44
45
46 @Override
47 public Object get(Object key) {
48 if (cacheValueFinderResults) {
49 if (super.containsKey(key)) {
50
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
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
95
96
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
107
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
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
137
138
139
140
141
142
143
144
145
146
147
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
158
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
193
194
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 }