View Javadoc

1   /**
2    * Copyright 2004-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * This class is for resolving terms for StoredFuncions. It extract values from prerequisites, execute Stored Function 
28   * and resolves the result. (copied from Kuali Coeus)
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 }