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.kuali.rice.krms.api.KrmsApiServiceLocator;
19  import org.kuali.rice.krms.api.engine.TermResolver;
20  import org.kuali.rice.krms.api.repository.function.FunctionDefinition;
21  import org.kuali.rice.krms.api.repository.type.KrmsTypeDefinition;
22  
23  import java.util.*;
24  
25  
26  /**
27   * Copied from Kuali Coeus.
28   */
29  public abstract class FunctionTermResolver implements TermResolver<Object> {
30      private List<String> orderedInputParams;
31      private String output;
32      private Set<String> parameterNames;
33      private FunctionDefinition functionTerm;
34      public FunctionTermResolver(List<String> orderedInputParams,Set<String> parameterNames,String output){
35          this.orderedInputParams = orderedInputParams;
36          this.parameterNames = parameterNames;
37          this.output = output;
38      }
39  
40      public List<String> getOrderedInputParams() {
41          return orderedInputParams;
42      }
43      @Override
44      public Set<String> getPrerequisites() {
45          Set<String> prereqs = new HashSet<String>();
46          for (String param : orderedInputParams) {
47              if(!parameterNames.contains(param)){
48                  prereqs.add(param);
49              }
50          }
51          return new HashSet<String>(prereqs);
52      }
53      /**
54       * Returns the name of the function needs to be executed as part of the Term
55       * @see org.kuali.rice.krms.api.engine.TermResolver#getOutput()
56       */
57      @Override
58      public String getOutput() {
59          return this.output;
60      }
61  
62      @Override
63      public Set<String> getParameterNames() {
64          return parameterNames;
65      }
66  
67      @Override
68      public int getCost() {
69          return 1;
70      }
71  
72      /**
73       * This method executes the stored function and returns the result. Parameters to Stored Function are
74       * getting passed through resolvedPrereqs.
75       * @see org.kuali.rice.krms.api.engine.TermResolver#resolve(java.util.Map, java.util.Map)
76       */
77      @Override
78      public Object resolve(Map<String, Object> resolvedPrereqs, Map<String, String> parameters) {
79          String krmsTypeId = getFunctionTerm().getTypeId();
80          String serviceName = null;
81          if(krmsTypeId!=null){
82              KrmsTypeDefinition typeDefinition = KrmsApiServiceLocator.getKrmsTypeRepositoryService().getTypeById(krmsTypeId);
83              serviceName = typeDefinition.getServiceName();
84          }
85          String methodName = getFunctionTerm().getName();
86          Object result = executeFunction(serviceName,methodName,resolvedPrereqs,parameters);
87          return result;
88      }
89      /**
90       *
91       * This method execute the function/method and return 'true' or 'false'
92       * @param resolvedPrereqs
93       * @return
94       */
95      protected abstract Object executeFunction(String serviceName,String methodName,Map<String, Object> resolvedPrereqs,Map<String,String> resolvedParameters);
96  
97      protected List<Object> extractParamValues(Map<String, Object> resolvedPrereqs,Map<String,String> resolvedParameters) {
98          List<String> parameters = getOrderedInputParams();
99          List<Object> extractedParams = new ArrayList<Object>();
100         for (String parameter : parameters) {
101             Object paramValue = resolvedPrereqs.get(parameter);
102             if(paramValue==null)
103                 paramValue = resolvedParameters.get(parameter);
104             extractedParams.add(paramValue);
105         }
106         return extractedParams;
107     }
108 
109     /**
110      * Gets the functionTerm attribute.
111      * @return Returns the functionTerm.
112      */
113     public FunctionDefinition getFunctionTerm() {
114         return functionTerm;
115     }
116 
117     /**
118      * Sets the functionTerm attribute value.
119      * @param functionTerm The functionTerm to set.
120      */
121     public void setFunctionTerm(FunctionDefinition functionTerm) {
122         this.functionTerm = functionTerm;
123     }
124 }