1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.krad.util; |
17 | |
|
18 | |
import org.apache.commons.lang.StringUtils; |
19 | |
import org.kuali.rice.core.api.CoreApiServiceLocator; |
20 | |
import org.kuali.rice.krad.lookup.keyvalues.KeyValuesFinder; |
21 | |
import org.kuali.rice.krad.lookup.keyvalues.KimAttributeValuesFinder; |
22 | |
import org.kuali.rice.krad.lookup.keyvalues.PersistableBusinessObjectValuesFinder; |
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 | |
@SuppressWarnings("unchecked") |
36 | 0 | public class ActionFormUtilMap extends HashMap { |
37 | |
private static final long serialVersionUID = 1L; |
38 | |
private boolean cacheValueFinderResults; |
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
public Object get(Object key) { |
46 | 0 | if (cacheValueFinderResults) { |
47 | 0 | if (super.containsKey(key)) { |
48 | |
|
49 | 0 | Object cachedObject = super.get(key); |
50 | 0 | return cachedObject; |
51 | |
} |
52 | |
} |
53 | 0 | String[] methodKey = StringUtils.split((String) key, KRADConstants.ACTION_FORM_UTIL_MAP_METHOD_PARM_DELIMITER); |
54 | |
|
55 | 0 | String methodToCall = methodKey[0]; |
56 | |
|
57 | |
|
58 | 0 | Object[] methodParms = new Object[methodKey.length - 1]; |
59 | 0 | Class[] methodParmsPrototype = new Class[methodKey.length - 1]; |
60 | 0 | for (int i=1;i<methodKey.length;i++) { |
61 | 0 | methodParms[i-1] = methodKey[i]; |
62 | 0 | methodParmsPrototype[i-1] = Object.class; |
63 | |
} |
64 | |
|
65 | 0 | Method method = null; |
66 | |
try { |
67 | 0 | method = ActionFormUtilMap.class.getMethod(methodToCall, methodParmsPrototype); |
68 | |
} |
69 | 0 | catch (SecurityException e) { |
70 | 0 | throw new RuntimeException("Unable to object handle on method given to ActionFormUtilMap: " + e.getMessage()); |
71 | |
} |
72 | 0 | catch (NoSuchMethodException e1) { |
73 | 0 | throw new RuntimeException("Unable to object handle on method given to ActionFormUtilMap: " + e1.getMessage()); |
74 | 0 | } |
75 | |
|
76 | 0 | Object methodValue = null; |
77 | |
try { |
78 | 0 | methodValue = method.invoke(this, methodParms); |
79 | |
} |
80 | 0 | catch (Exception e) { |
81 | 0 | throw new RuntimeException("Unable to invoke method " + methodToCall,e); |
82 | 0 | } |
83 | |
|
84 | 0 | if (cacheValueFinderResults) { |
85 | 0 | super.put(key, methodValue); |
86 | |
} |
87 | |
|
88 | 0 | return methodValue; |
89 | |
} |
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
public Object getOptionsMap(Object key) { |
97 | 0 | List optionsList = new ArrayList(); |
98 | |
|
99 | 0 | if (StringUtils.isBlank((String) key)) { |
100 | 0 | return optionsList; |
101 | |
} |
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | 0 | key = StringUtils.replace((String) key, "|", "."); |
108 | |
|
109 | |
KeyValuesFinder finder; |
110 | |
try { |
111 | 0 | Class finderClass = Class.forName((String) key); |
112 | 0 | finder = (KeyValuesFinder) finderClass.newInstance(); |
113 | 0 | optionsList = finder.getKeyValues(); |
114 | |
} |
115 | 0 | catch (ClassNotFoundException e) { |
116 | 0 | throw new RuntimeException(e.getMessage()); |
117 | |
} |
118 | 0 | catch (InstantiationException e) { |
119 | 0 | throw new RuntimeException(e.getMessage()); |
120 | |
} |
121 | 0 | catch (IllegalAccessException e) { |
122 | 0 | throw new RuntimeException(e.getMessage()); |
123 | 0 | } |
124 | |
|
125 | 0 | return optionsList; |
126 | |
} |
127 | |
|
128 | |
|
129 | |
public Object getOptionsMap(Object key, Object boClass, Object keyAttribute, Object labelAttribute, Object includeKeyInLabel) { |
130 | 0 | return getOptionsMap(key, boClass, keyAttribute, labelAttribute, null, includeKeyInLabel ); |
131 | |
} |
132 | |
|
133 | |
public Object getOptionsMap(Object key, Object boClass, Object keyAttribute, Object labelAttribute, Object includeBlankRow, Object includeKeyInLabel) { |
134 | 0 | return getOptionsMap(key, boClass, keyAttribute, labelAttribute, includeBlankRow, includeKeyInLabel, null); |
135 | |
} |
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
public Object getOptionsMap(Object key, Object boClass, Object keyAttribute, Object labelAttribute, Object includeBlankRow, Object includeKeyInLabel, Object kimTypeId) { |
156 | 0 | List optionsList = new ArrayList(); |
157 | |
|
158 | 0 | if (StringUtils.isBlank((String) key)) { |
159 | 0 | return optionsList; |
160 | |
} |
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | 0 | key = StringUtils.replace((String) key, "|", "."); |
167 | |
|
168 | |
KeyValuesFinder finder; |
169 | |
try { |
170 | 0 | Class finderClass = Class.forName((String) key); |
171 | 0 | finder = (KeyValuesFinder) finderClass.newInstance(); |
172 | 0 | if (finder instanceof PersistableBusinessObjectValuesFinder) { |
173 | 0 | String businessObjectClassName = StringUtils.replace((String) boClass, "|", "."); |
174 | 0 | Class businessObjectClass = Class.forName((String) businessObjectClassName); |
175 | 0 | ((PersistableBusinessObjectValuesFinder) finder).setBusinessObjectClass(businessObjectClass); |
176 | 0 | ((PersistableBusinessObjectValuesFinder) finder).setKeyAttributeName((String)keyAttribute); |
177 | 0 | ((PersistableBusinessObjectValuesFinder) finder).setLabelAttributeName((String)labelAttribute); |
178 | 0 | ((PersistableBusinessObjectValuesFinder) finder).setIncludeBlankRow(Boolean.parseBoolean((String)includeBlankRow)); |
179 | 0 | ((PersistableBusinessObjectValuesFinder) finder).setIncludeKeyInDescription(Boolean.parseBoolean((String)includeKeyInLabel)); |
180 | 0 | } else if (finder instanceof KimAttributeValuesFinder) { |
181 | 0 | if (kimTypeId == null) { |
182 | 0 | throw new RuntimeException("kimTypeId must be specified for KimAttributeValuesFinder"); |
183 | |
} |
184 | 0 | ((KimAttributeValuesFinder) finder).setKimTypeId(kimTypeId.toString()); |
185 | 0 | ((KimAttributeValuesFinder) finder).setKimAttributeName(keyAttribute.toString()); |
186 | |
} |
187 | |
|
188 | 0 | optionsList = finder.getKeyValues(); |
189 | |
} |
190 | 0 | catch (ClassNotFoundException e) { |
191 | 0 | throw new RuntimeException(e.getMessage(),e); |
192 | |
} |
193 | 0 | catch (InstantiationException e) { |
194 | 0 | throw new RuntimeException(e.getMessage(),e); |
195 | |
} |
196 | 0 | catch (IllegalAccessException e) { |
197 | 0 | throw new RuntimeException(e.getMessage(),e); |
198 | 0 | } |
199 | |
|
200 | 0 | return optionsList; |
201 | |
} |
202 | |
|
203 | |
|
204 | |
|
205 | |
|
206 | |
|
207 | |
|
208 | |
public String encryptValue(Object value) { |
209 | 0 | String encrypted = ""; |
210 | 0 | if (value != null) { |
211 | 0 | encrypted = value.toString(); |
212 | |
} |
213 | |
|
214 | |
try { |
215 | 0 | encrypted = CoreApiServiceLocator.getEncryptionService().encrypt(value); |
216 | |
} |
217 | 0 | catch (GeneralSecurityException e) { |
218 | 0 | throw new RuntimeException("Unable to encrypt value in action form: " + e.getMessage()); |
219 | 0 | } |
220 | |
|
221 | 0 | return encrypted; |
222 | |
} |
223 | |
|
224 | |
public void setCacheValueFinderResults(boolean cacheValueFinderResults) { |
225 | 0 | this.cacheValueFinderResults = cacheValueFinderResults; |
226 | 0 | } |
227 | |
|
228 | |
|
229 | |
} |