1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.kpme.core.krms.function;
17
18 import org.apache.log4j.Logger;
19 import org.kuali.kpme.core.service.HrServiceLocator;
20 import org.kuali.rice.krms.api.repository.function.FunctionDefinition;
21 import org.kuali.rice.krms.api.repository.function.FunctionParameterDefinition;
22
23 import java.lang.reflect.Method;
24 import java.util.*;
25
26
27
28
29
30 public class JavaFunctionResolver extends FunctionTermResolver{
31 private static final Logger LOG = Logger.getLogger(JavaFunctionResolver.class);
32
33 public JavaFunctionResolver(List<String> orderedInputParams, Set<String> parameterNames, String output, FunctionDefinition functionTerm) {
34 super(orderedInputParams, parameterNames, output);
35 setFunctionTerm(functionTerm);
36 }
37
38 protected Object executeFunction(String serviceName,String methodName,Map<String, Object> resolvedPrereqs,Map<String,String> resolvedParameters) {
39 List<Object> orderedParamValues = extractParamValues(resolvedPrereqs,resolvedParameters);
40 if(serviceName==null)
41 throw new RuntimeException("Service name is not defined for the term:"+getOutput());
42 return callFunction(serviceName,methodName,orderedParamValues);
43 }
44
45 private static HashSet<Class<?>> getWrapperTypes()
46 {
47 HashSet<Class<?>> ret = new HashSet<Class<?>>();
48 ret.add(Boolean.class);
49 ret.add(Integer.class);
50 ret.add(Long.class);
51 ret.add(Float.class);
52 ret.add(Double.class);
53 return ret;
54 }
55 private static final HashSet<Class<?>> WRAPPER_TYPES = getWrapperTypes();
56 @SuppressWarnings("rawtypes")
57 private Object callFunction(String serviceName, String methodName,List<Object> orderedParamValues) {
58 try {
59 List<Object> functionParamObjects = new ArrayList<Object>();
60 List<FunctionParameterDefinition> functionParams = getFunctionTerm().getParameters();
61 List<FunctionParameterDefinition> modifiableParams = new ArrayList<FunctionParameterDefinition>(functionParams);
62 Collections.sort(modifiableParams, new FunctionParamComparator());
63 Class[] classtypes = new Class[orderedParamValues.size()];
64 for (int i = 0; i < orderedParamValues.size(); i++) {
65 Object objValue = orderedParamValues.get(i);
66 String paramClassType = modifiableParams.get(i).getParameterType();
67 Class paramClass = Class.forName(paramClassType);
68 if(WRAPPER_TYPES.contains(paramClass)){
69 Object convertedObject = wrapValue(objValue,paramClassType);
70 classtypes[i] = paramClass;
71 functionParamObjects.add(convertedObject);
72 }else{
73 classtypes[i] = paramClass;
74 functionParamObjects.add(objValue);
75 }
76
77 }
78 Object javaFunctionService = HrServiceLocator.getService(serviceName);
79 Class javaFunctionServiceClass = javaFunctionService.getClass();
80 Method method = javaFunctionServiceClass.getMethod(methodName, classtypes);
81 return method.invoke(javaFunctionService,functionParamObjects.toArray());
82 }catch (Exception e) {
83 LOG.error(e.getMessage(), e);
84 throw new RuntimeException(e);
85 }
86 }
87
88 private Object wrapValue(Object objValue, String paramClassType) {
89 Object retObj = objValue;
90 if(objValue==null){
91 return null;
92 }
93 if(paramClassType.equals("java.lang.Integer")){
94 retObj = new Integer(objValue.toString());
95 }else if(paramClassType.equals("java.lang.Long")){
96 retObj = new Long(objValue.toString());
97 }else if(paramClassType.equals("java.lang.Boolean")){
98 retObj = new Boolean(objValue.toString());
99 }else if(paramClassType.equals("java.lang.Float")){
100 retObj = new Float(objValue.toString());
101 }else if(paramClassType.equals("java.lang.Double")){
102 retObj = new Double(objValue.toString());
103 }
104 return retObj;
105 }
106 }