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 | |
@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 | |
@Override |
46 | |
public Object get(Object key) { |
47 | 0 | if (cacheValueFinderResults) { |
48 | 0 | if (super.containsKey(key)) { |
49 | |
|
50 | 0 | Object cachedObject = super.get(key); |
51 | 0 | return cachedObject; |
52 | |
} |
53 | |
} |
54 | 0 | String[] methodKey = StringUtils.split((String) key, KRADConstants.ACTION_FORM_UTIL_MAP_METHOD_PARM_DELIMITER); |
55 | |
|
56 | 0 | String methodToCall = methodKey[0]; |
57 | |
|
58 | |
|
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 | |
|
94 | |
|
95 | |
|
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 | |
|
106 | |
|
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 | |
|
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 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
public Object getOptionsMap(Object key, Object boClass, Object keyAttribute, Object labelAttribute, Object includeBlankRow, Object includeKeyInLabel) { |
149 | 0 | List optionsList = new ArrayList(); |
150 | |
|
151 | 0 | if (StringUtils.isBlank((String) key)) { |
152 | 0 | return optionsList; |
153 | |
} |
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | 0 | key = StringUtils.replace((String) key, "|", "."); |
160 | |
|
161 | |
KeyValuesFinder finder; |
162 | |
try { |
163 | 0 | Class finderClass = Class.forName((String) key); |
164 | 0 | finder = (KeyValuesFinder) finderClass.newInstance(); |
165 | 0 | if (finder instanceof PersistableBusinessObjectValuesFinder) { |
166 | 0 | String businessObjectClassName = StringUtils.replace((String) boClass, "|", "."); |
167 | 0 | Class businessObjectClass = Class.forName((String) businessObjectClassName); |
168 | 0 | ((PersistableBusinessObjectValuesFinder) finder).setBusinessObjectClass(businessObjectClass); |
169 | 0 | ((PersistableBusinessObjectValuesFinder) finder).setKeyAttributeName((String)keyAttribute); |
170 | 0 | ((PersistableBusinessObjectValuesFinder) finder).setLabelAttributeName((String)labelAttribute); |
171 | 0 | ((PersistableBusinessObjectValuesFinder) finder).setIncludeBlankRow(Boolean.parseBoolean((String)includeBlankRow)); |
172 | 0 | ((PersistableBusinessObjectValuesFinder) finder).setIncludeKeyInDescription(Boolean.parseBoolean((String)includeKeyInLabel)); |
173 | |
} |
174 | |
|
175 | 0 | optionsList = finder.getKeyValues(); |
176 | |
} |
177 | 0 | catch (ClassNotFoundException e) { |
178 | 0 | throw new RuntimeException(e.getMessage(),e); |
179 | |
} |
180 | 0 | catch (InstantiationException e) { |
181 | 0 | throw new RuntimeException(e.getMessage(),e); |
182 | |
} |
183 | 0 | catch (IllegalAccessException e) { |
184 | 0 | throw new RuntimeException(e.getMessage(),e); |
185 | 0 | } |
186 | |
|
187 | 0 | return optionsList; |
188 | |
} |
189 | |
|
190 | |
|
191 | |
|
192 | |
|
193 | |
|
194 | |
|
195 | |
public String encryptValue(Object value) { |
196 | 0 | String encrypted = ""; |
197 | 0 | if (value != null) { |
198 | 0 | encrypted = value.toString(); |
199 | |
} |
200 | |
|
201 | |
try { |
202 | 0 | encrypted = CoreApiServiceLocator.getEncryptionService().encrypt(value); |
203 | |
} |
204 | 0 | catch (GeneralSecurityException e) { |
205 | 0 | throw new RuntimeException("Unable to encrypt value in action form: " + e.getMessage()); |
206 | 0 | } |
207 | |
|
208 | 0 | return encrypted; |
209 | |
} |
210 | |
|
211 | |
public void setCacheValueFinderResults(boolean cacheValueFinderResults) { |
212 | 0 | this.cacheValueFinderResults = cacheValueFinderResults; |
213 | 0 | } |
214 | |
|
215 | |
|
216 | |
} |