1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.kns.service.impl; |
17 | |
|
18 | |
import java.util.Arrays; |
19 | |
import java.util.Collections; |
20 | |
import java.util.List; |
21 | |
|
22 | |
import org.apache.commons.lang.StringUtils; |
23 | |
import org.apache.log4j.Logger; |
24 | |
import org.kuali.rice.core.util.ClassLoaderUtils; |
25 | |
import org.kuali.rice.kew.service.KEWServiceLocator; |
26 | |
import org.kuali.rice.kns.bo.BusinessObject; |
27 | |
import org.kuali.rice.kns.bo.Parameter; |
28 | |
import org.kuali.rice.kns.bo.ParameterDetailType; |
29 | |
import org.kuali.rice.kns.datadictionary.BusinessObjectEntry; |
30 | |
import org.kuali.rice.kns.document.TransactionalDocument; |
31 | |
import org.kuali.rice.kns.service.DataDictionaryService; |
32 | |
import org.kuali.rice.kns.service.KualiModuleService; |
33 | |
import org.kuali.rice.kns.service.ModuleService; |
34 | |
import org.kuali.rice.kns.service.ParameterConstants; |
35 | |
import org.kuali.rice.kns.service.ParameterEvaluator; |
36 | |
import org.kuali.rice.kns.service.ParameterService; |
37 | |
import org.kuali.rice.kns.service.ParameterConstants.COMPONENT; |
38 | |
import org.kuali.rice.kns.service.ParameterConstants.NAMESPACE; |
39 | |
import org.kuali.rice.kns.util.KNSConstants; |
40 | |
import org.kuali.rice.kns.util.KNSUtils; |
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
public abstract class ParameterServiceBase implements ParameterService { |
49 | |
protected DataDictionaryService dataDictionaryService; |
50 | |
protected KualiModuleService kualiModuleService; |
51 | |
protected static final String PARAMETER_CACHE_PREFIX = "Parameter:"; |
52 | |
protected static final String PARAMETER_CACHE_GROUP_NAME = "SystemParameter"; |
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
public ParameterServiceBase() { |
59 | 0 | super(); |
60 | 0 | } |
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
@SuppressWarnings("unchecked") |
66 | |
public boolean parameterExists(Class componentClass, String parameterName) { |
67 | 0 | return retrieveParameter(getNamespace(componentClass), getDetailType(componentClass), parameterName) != null; |
68 | |
} |
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
@SuppressWarnings("unchecked") |
79 | |
public boolean getIndicatorParameter(Class componentClass, String parameterName) { |
80 | 0 | return "Y".equals(getParameter(componentClass, parameterName).getParameterValue()); |
81 | |
} |
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
@SuppressWarnings("unchecked") |
93 | |
public boolean getIndicatorParameter(String namespaceCode, String detailTypeCode, String parameterName) { |
94 | 0 | return "Y".equals(getParameter(namespaceCode, detailTypeCode, parameterName).getParameterValue()); |
95 | |
} |
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
@SuppressWarnings("unchecked") |
101 | |
public String getParameterValue(Class componentClass, String parameterName) { |
102 | 0 | return getParameter(componentClass, parameterName).getParameterValue(); |
103 | |
} |
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
@SuppressWarnings("unchecked") |
109 | |
public String getParameterValue(String namespaceCode, String detailTypeCode, String parameterName) { |
110 | 0 | return getParameter(namespaceCode, detailTypeCode, parameterName).getParameterValue(); |
111 | |
} |
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
@SuppressWarnings("unchecked") |
125 | |
public String getParameterValue(Class componentClass, String parameterName, String constrainingValue) { |
126 | 0 | List<String> parameterValues = getParameterValues(componentClass, parameterName, constrainingValue); |
127 | 0 | if (parameterValues.size() == 1) { |
128 | 0 | return parameterValues.get(0); |
129 | |
} |
130 | 0 | return null; |
131 | |
} |
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
@SuppressWarnings("unchecked") |
141 | |
public List<String> getParameterValues(Class componentClass, String parameterName) { |
142 | 0 | return Collections.unmodifiableList( getParameterValues(getParameter(componentClass, parameterName)) ); |
143 | |
} |
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | |
@SuppressWarnings("unchecked") |
154 | |
public List<String> getParameterValues(String namespaceCode, String detailTypeCode, String parameterName) { |
155 | 0 | return Collections.unmodifiableList( getParameterValues(getParameter(namespaceCode, detailTypeCode, parameterName)) ); |
156 | |
} |
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
|
167 | |
@SuppressWarnings("unchecked") |
168 | |
public List<String> getParameterValues(Class componentClass, String parameterName, String constrainingValue) { |
169 | 0 | return Collections.unmodifiableList( getParameterValues(getParameter(componentClass, parameterName), constrainingValue) ); |
170 | |
} |
171 | |
|
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
|
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | |
@SuppressWarnings("unchecked") |
182 | |
public ParameterEvaluator getParameterEvaluator(Class componentClass, String parameterName) { |
183 | 0 | return getParameterEvaluator(getParameter(componentClass, parameterName)); |
184 | |
} |
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | |
|
191 | |
|
192 | |
|
193 | |
|
194 | |
|
195 | |
|
196 | |
@SuppressWarnings("unchecked") |
197 | |
public ParameterEvaluator getParameterEvaluator(String namespaceCode, String detailTypeCode, String parameterName) { |
198 | 0 | return getParameterEvaluator(getParameter(namespaceCode, detailTypeCode, parameterName)); |
199 | |
} |
200 | |
|
201 | |
|
202 | |
|
203 | |
|
204 | |
|
205 | |
|
206 | |
|
207 | |
|
208 | |
|
209 | |
|
210 | |
|
211 | |
|
212 | |
@SuppressWarnings("unchecked") |
213 | |
public ParameterEvaluator getParameterEvaluator(Class componentClass, String parameterName, String constrainedValue) { |
214 | 0 | return getParameterEvaluator(getParameter(componentClass, parameterName), constrainedValue); |
215 | |
} |
216 | |
|
217 | |
|
218 | |
|
219 | |
|
220 | |
|
221 | |
|
222 | |
|
223 | |
|
224 | |
|
225 | |
|
226 | |
|
227 | |
@SuppressWarnings("unchecked") |
228 | |
public ParameterEvaluator getParameterEvaluator(String namespaceCode, String detailTypeCode, String parameterName, String constrainedValue) { |
229 | 0 | return getParameterEvaluator(getParameter(namespaceCode, detailTypeCode, parameterName), constrainedValue); |
230 | |
} |
231 | |
|
232 | |
|
233 | |
|
234 | |
|
235 | |
|
236 | |
|
237 | |
|
238 | |
|
239 | |
|
240 | |
|
241 | |
|
242 | |
|
243 | |
@SuppressWarnings("unchecked") |
244 | |
public ParameterEvaluator getParameterEvaluator(Class componentClass, String parameterName, String constrainingValue, |
245 | |
String constrainedValue) { |
246 | 0 | return getParameterEvaluator(getParameter(componentClass, parameterName), constrainingValue, constrainedValue); |
247 | |
} |
248 | |
|
249 | |
|
250 | |
|
251 | |
|
252 | |
|
253 | |
|
254 | |
|
255 | |
|
256 | |
|
257 | |
|
258 | |
|
259 | |
|
260 | |
|
261 | |
|
262 | |
|
263 | |
|
264 | |
|
265 | |
@SuppressWarnings("unchecked") |
266 | |
public ParameterEvaluator getParameterEvaluator(Class componentClass, String allowParameterName, String denyParameterName, |
267 | |
String constrainingValue, String constrainedValue) { |
268 | 0 | Parameter allowParameter = getParameter(componentClass, allowParameterName); |
269 | 0 | Parameter denyParameter = getParameter(componentClass, denyParameterName); |
270 | 0 | if (!getParameterValues(allowParameter, constrainingValue).isEmpty() && !getParameterValues(denyParameter, constrainingValue).isEmpty()) { |
271 | 0 | throw new IllegalArgumentException("The getParameterEvaluator(Class componentClass, String allowParameterName, String denyParameterName, String constrainingValue, String constrainedValue) method of ParameterServiceImpl does not facilitate evaluation of combination allow and deny parameters that both have values for the constraining value: " + allowParameterName + " / " + denyParameterName + " / " + constrainingValue); |
272 | |
} |
273 | 0 | if (getParameterValues(allowParameter, constrainingValue).isEmpty() && getParameterValues(denyParameter, constrainingValue).isEmpty()) { |
274 | 0 | return AlwaysSucceedParameterEvaluatorImpl.getInstance(); |
275 | |
} |
276 | 0 | return getParameterEvaluator(getParameterValues(denyParameter, constrainingValue).isEmpty() ? allowParameter : denyParameter, constrainingValue, constrainedValue); |
277 | |
} |
278 | |
|
279 | |
|
280 | |
@SuppressWarnings("unchecked") |
281 | |
public String getNamespace(Class documentOrStepClass) { |
282 | 0 | if (documentOrStepClass == null) { |
283 | 0 | throw new IllegalArgumentException("The getNamespace method of ParameterServiceImpl requires non-null documentOrStepClass"); |
284 | |
} |
285 | 0 | if (documentOrStepClass.isAnnotationPresent(NAMESPACE.class)) { |
286 | 0 | return ((NAMESPACE) documentOrStepClass.getAnnotation(NAMESPACE.class)).namespace(); |
287 | |
} |
288 | 0 | ModuleService moduleService = kualiModuleService.getResponsibleModuleService(documentOrStepClass); |
289 | 0 | if (moduleService != null) { |
290 | 0 | return moduleService.getModuleConfiguration().getNamespaceCode(); |
291 | |
} |
292 | 0 | if (documentOrStepClass.getName().startsWith("org.kuali.rice.kns")) { |
293 | 0 | return ParameterConstants.NERVOUS_SYSTEM_NAMESPACE; |
294 | |
} |
295 | 0 | if (documentOrStepClass.getName().startsWith("org.kuali.rice.kew")) { |
296 | 0 | return "KR-WKFLW"; |
297 | |
} |
298 | 0 | if (documentOrStepClass.getName().startsWith("org.kuali.rice.kim")) { |
299 | 0 | return "KR-IDM"; |
300 | |
} |
301 | 0 | throw new IllegalArgumentException("Unable to determine the namespace for documentOrStepClass " + documentOrStepClass.getName() ); |
302 | |
} |
303 | |
|
304 | |
@SuppressWarnings("unchecked") |
305 | |
public String getDetailType(Class documentOrStepClass) { |
306 | 0 | if (documentOrStepClass == null) { |
307 | 0 | throw new IllegalArgumentException("The getDetailType method of ParameterServiceImpl requires non-null documentOrStepClass"); |
308 | |
} |
309 | 0 | else if (documentOrStepClass.isAnnotationPresent(COMPONENT.class)) { |
310 | 0 | return ((COMPONENT) documentOrStepClass.getAnnotation(COMPONENT.class)).component(); |
311 | |
} |
312 | 0 | else if (TransactionalDocument.class.isAssignableFrom(documentOrStepClass)) { |
313 | 0 | return documentOrStepClass.getSimpleName().replace("Document", ""); |
314 | |
} |
315 | 0 | else if (BusinessObject.class.isAssignableFrom(documentOrStepClass) ) { |
316 | 0 | return documentOrStepClass.getSimpleName(); |
317 | |
} |
318 | |
else { |
319 | |
|
320 | 0 | Class c = getStepClass(); |
321 | 0 | if ( c != null && c.isAssignableFrom(documentOrStepClass) ) { |
322 | 0 | return documentOrStepClass.getSimpleName(); |
323 | |
} |
324 | |
} |
325 | 0 | throw new IllegalArgumentException("The getDetailType method of ParameterServiceImpl requires a TransactionalDocument or BusinessObject class. Was:" + documentOrStepClass.getName()); |
326 | |
} |
327 | |
|
328 | |
@SuppressWarnings("unchecked") |
329 | |
private Class getStepClass() { |
330 | |
try { |
331 | 0 | ClassLoader cl = ClassLoaderUtils.getDefaultClassLoader(); |
332 | 0 | return Class.forName("org.kuali.kfs.sys.batch.Step", true, cl); |
333 | |
} |
334 | 0 | catch ( Exception e ) { |
335 | 0 | e.printStackTrace(); |
336 | 0 | return null; |
337 | |
} |
338 | |
} |
339 | |
|
340 | |
@SuppressWarnings("unchecked") |
341 | |
protected String getDetailTypeName(Class documentOrStepClass) { |
342 | 0 | if (documentOrStepClass == null) { |
343 | 0 | throw new IllegalArgumentException("The getDetailTypeName method of ParameterServiceImpl requires non-null documentOrStepClass"); |
344 | |
} |
345 | 0 | if (documentOrStepClass.isAnnotationPresent(COMPONENT.class)) { |
346 | 0 | BusinessObjectEntry boe = dataDictionaryService.getDataDictionary().getBusinessObjectEntry(documentOrStepClass.getName()); |
347 | 0 | if (boe != null) { |
348 | 0 | return boe.getObjectLabel(); |
349 | |
} |
350 | |
else { |
351 | 0 | return ((COMPONENT) documentOrStepClass.getAnnotation(COMPONENT.class)).component(); |
352 | |
} |
353 | |
} |
354 | 0 | if (TransactionalDocument.class.isAssignableFrom(documentOrStepClass)) { |
355 | 0 | return dataDictionaryService.getDocumentLabelByClass(documentOrStepClass); |
356 | |
} |
357 | 0 | else if (BusinessObject.class.isAssignableFrom(documentOrStepClass) ) { |
358 | 0 | BusinessObjectEntry boe = dataDictionaryService.getDataDictionary().getBusinessObjectEntry(documentOrStepClass.getName()); |
359 | 0 | if (boe != null) { |
360 | 0 | return boe.getObjectLabel(); |
361 | |
} |
362 | |
else { |
363 | 0 | return KNSUtils.getBusinessTitleForClass(documentOrStepClass); |
364 | |
} |
365 | |
} |
366 | 0 | throw new IllegalArgumentException("The getDetailTypeName method of ParameterServiceImpl requires TransactionalDocument, BusinessObject, or Step class. Was: " + documentOrStepClass.getName() ); |
367 | |
} |
368 | |
|
369 | |
protected ParameterEvaluatorImpl getParameterEvaluator(Parameter parameter) { |
370 | 0 | ParameterEvaluatorImpl parameterEvaluator = new ParameterEvaluatorImpl(); |
371 | 0 | parameterEvaluator.setParameter(parameter); |
372 | 0 | parameterEvaluator.setConstraintIsAllow(constraintIsAllow(parameter)); |
373 | 0 | parameterEvaluator.setValues(getParameterValues(parameter)); |
374 | 0 | return parameterEvaluator; |
375 | |
} |
376 | |
|
377 | |
protected ParameterEvaluatorImpl getParameterEvaluator(Parameter parameter, String constrainedValue) { |
378 | 0 | ParameterEvaluatorImpl parameterEvaluator = getParameterEvaluator(parameter); |
379 | 0 | parameterEvaluator.setConstrainedValue(constrainedValue); |
380 | 0 | return parameterEvaluator; |
381 | |
} |
382 | |
|
383 | |
protected ParameterEvaluatorImpl getParameterEvaluator(Parameter parameter, String constrainingValue, |
384 | |
String constrainedValue) { |
385 | 0 | ParameterEvaluatorImpl parameterEvaluator = getParameterEvaluator(parameter, constrainedValue); |
386 | 0 | parameterEvaluator.setValues(getParameterValues(parameter, constrainingValue)); |
387 | 0 | return parameterEvaluator; |
388 | |
} |
389 | |
|
390 | |
@SuppressWarnings("unchecked") |
391 | |
protected ParameterDetailType getParameterDetailType(Class documentOrStepClass) { |
392 | 0 | String detailTypeString = getDetailType(documentOrStepClass); |
393 | 0 | String detailTypeName = getDetailTypeName(documentOrStepClass); |
394 | 0 | ParameterDetailType detailType = new ParameterDetailType(getNamespace(documentOrStepClass), detailTypeString, (detailTypeName == null) ? detailTypeString : detailTypeName); |
395 | 0 | detailType.refreshNonUpdateableReferences(); |
396 | 0 | return detailType; |
397 | |
} |
398 | |
|
399 | |
protected List<String> getParameterValues(Parameter parameter, String constrainingValue) { |
400 | 0 | List<String> constraintValuePairs = getParameterValues(parameter); |
401 | 0 | for (String pair : constraintValuePairs) { |
402 | 0 | if (StringUtils.equals(constrainingValue, StringUtils.substringBefore(pair, "="))) { |
403 | 0 | return Arrays.asList(StringUtils.substringAfter(pair, "=").split(",")); |
404 | |
} |
405 | |
} |
406 | 0 | return Collections.emptyList(); |
407 | |
} |
408 | |
|
409 | |
private List<String> getParameterValues(Parameter parameter) { |
410 | 0 | if (parameter == null || StringUtils.isBlank(parameter.getParameterValue())) { |
411 | 0 | return Collections.emptyList(); |
412 | |
} |
413 | 0 | return Arrays.asList(parameter.getParameterValue().split(";")); |
414 | |
} |
415 | |
|
416 | |
|
417 | |
|
418 | |
|
419 | |
public void clearCache() { |
420 | |
|
421 | 0 | KEWServiceLocator.getCacheAdministrator().flushGroup(PARAMETER_CACHE_GROUP_NAME); |
422 | 0 | } |
423 | |
|
424 | |
@SuppressWarnings("unchecked") |
425 | |
protected Parameter getParameter(Class componentClass, String parameterName) { |
426 | 0 | String key = componentClass.toString() + ":" + parameterName; |
427 | 0 | Parameter parameter = null; |
428 | |
|
429 | |
|
430 | |
|
431 | |
|
432 | |
|
433 | |
|
434 | 0 | parameter = getParameter(getNamespace(componentClass), getDetailType(componentClass), parameterName); |
435 | 0 | if (parameter == null) { |
436 | 0 | throw new IllegalArgumentException("The getParameter method of ParameterServiceImpl requires a componentClass and parameterName that correspond to an existing parameter. Was passed: " + componentClass.getName() + "/" + parameterName); |
437 | |
} |
438 | |
|
439 | 0 | return parameter; |
440 | |
} |
441 | |
|
442 | |
protected Parameter getParameter(String namespaceCode, String detailTypeCode, String parameterName) { |
443 | 0 | if (StringUtils.isBlank(namespaceCode) || StringUtils.isBlank(detailTypeCode) || StringUtils.isBlank(parameterName)) { |
444 | 0 | throw new IllegalArgumentException("The getParameter method of KualiConfigurationServiceImpl requires a non-blank namespaceCode, parameterDetailTypeCode, and parameterName: " + namespaceCode + " / " + detailTypeCode + " / " + parameterName); |
445 | |
} |
446 | 0 | Parameter param = retrieveParameter(namespaceCode, detailTypeCode, parameterName); |
447 | 0 | if (param == null) { |
448 | 0 | throw new IllegalArgumentException("The getParameter method of KualiConfigurationServiceImpl was unable to find parameter: " + namespaceCode + " / " + detailTypeCode + " / " + parameterName); |
449 | |
} |
450 | 0 | return param; |
451 | |
} |
452 | |
|
453 | |
|
454 | |
|
455 | |
|
456 | |
|
457 | |
protected Parameter fetchFromCache(String namespaceCode, String detailTypeCode, String name) { |
458 | 0 | return (Parameter)KEWServiceLocator.getCacheAdministrator().getFromCache(getParameterCacheKey(namespaceCode, detailTypeCode, name)); |
459 | |
} |
460 | |
|
461 | |
|
462 | |
|
463 | |
|
464 | |
|
465 | |
protected void insertIntoCache(Parameter parameter) { |
466 | 0 | if (parameter == null) { |
467 | 0 | return; |
468 | |
} |
469 | 0 | KEWServiceLocator.getCacheAdministrator().putInCache(getParameterCacheKey(parameter.getParameterNamespaceCode(), parameter.getParameterDetailTypeCode(), parameter.getParameterName()), parameter, PARAMETER_CACHE_GROUP_NAME); |
470 | 0 | } |
471 | |
|
472 | |
|
473 | |
|
474 | |
|
475 | |
|
476 | |
|
477 | |
|
478 | |
|
479 | |
|
480 | |
|
481 | |
|
482 | |
|
483 | |
protected void flushParameterFromCache(String namespaceCode, String detailTypeCode, String name) { |
484 | 0 | KEWServiceLocator.getCacheAdministrator().flushEntry(getParameterCacheKey(namespaceCode, detailTypeCode, name)); |
485 | 0 | } |
486 | |
|
487 | |
|
488 | |
|
489 | |
protected String getParameterCacheKey(String namespaceCode, String detailTypeCode, String name) { |
490 | 0 | return PARAMETER_CACHE_PREFIX + namespaceCode + "-" + detailTypeCode + "-" + name; |
491 | |
} |
492 | |
|
493 | |
private boolean constraintIsAllow(Parameter parameter) { |
494 | 0 | return KNSConstants.APC_ALLOWED_OPERATOR.equals(parameter.getParameterConstraintCode()); |
495 | |
} |
496 | |
|
497 | |
public void setDataDictionaryService(DataDictionaryService dataDictionaryService) { |
498 | 0 | this.dataDictionaryService = dataDictionaryService; |
499 | 0 | } |
500 | |
|
501 | |
public void setKualiModuleService(KualiModuleService kualiModuleService) { |
502 | 0 | this.kualiModuleService = kualiModuleService; |
503 | 0 | } |
504 | |
} |