1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package edu.sampleu.krms.impl;
17
18 import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
19 import org.kuali.rice.core.api.uif.RemotableAttributeError;
20 import org.kuali.rice.krms.api.KrmsApiServiceLocator;
21 import org.kuali.rice.krms.api.repository.function.FunctionDefinition;
22 import org.kuali.rice.krms.api.repository.function.FunctionRepositoryService;
23 import org.kuali.rice.krms.api.repository.operator.CustomOperator;
24 import org.kuali.rice.krms.api.repository.type.KrmsTypeDefinition;
25 import org.kuali.rice.krms.api.repository.type.KrmsTypeRepositoryService;
26 import org.kuali.rice.krms.framework.engine.Function;
27 import org.kuali.rice.krms.framework.type.FunctionTypeService;
28 import org.kuali.rice.krms.impl.repository.FunctionBoService;
29
30 import java.text.MessageFormat;
31 import java.util.ArrayList;
32 import java.util.Collection;
33 import java.util.Collections;
34 import java.util.List;
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64 public class ContainsOperator implements CustomOperator, FunctionTypeService {
65
66
67
68
69
70
71
72
73
74
75
76 @Override
77 public FunctionDefinition getOperatorFunctionDefinition() {
78 FunctionBoService functionBoService =
79 (FunctionBoService) GlobalResourceLoader.getService("functionRepositoryService");
80
81 KrmsTypeRepositoryService typeRepository = KrmsApiServiceLocator.getKrmsTypeRepositoryService();
82 KrmsTypeDefinition containsOperatorType = typeRepository.getTypeByName("KR-SAP", "contains operator");
83
84 if (containsOperatorType == null) {
85 throw new IllegalStateException("There must be a persisted KRMS type with namespace 'KR-SAP', "
86 + "name 'contains operator', and serviceName 'sampleAppContainsOperatorService'");
87 }
88
89 FunctionDefinition containsFunction = functionBoService.getFunctionByNameAndNamespace("contains", "KR-SAP");
90
91 if (containsFunction == null) {
92 FunctionDefinition.Builder functionBuilder =
93 FunctionDefinition.Builder.create("KR-SAP", "contains", "java.lang.Boolean", containsOperatorType.getId());
94
95 containsFunction = functionBoService.createFunction(functionBuilder.build());
96 }
97
98 return containsFunction;
99 }
100
101
102
103
104
105
106
107
108
109
110 @Override
111 public List<RemotableAttributeError> validateOperandClasses(String lhsClassName, String rhsClassName) {
112 List<RemotableAttributeError> errors = new ArrayList<RemotableAttributeError>();
113
114 if (String.class.getName().equals(lhsClassName)) {
115 if (!String.class.getName().equals(rhsClassName)) {
116 RemotableAttributeError.Builder errorBuilder =
117 RemotableAttributeError.Builder.create("operator", "right hand operand is not a String");
118 errors.add(errorBuilder.build());
119 }
120 } else {
121 try {
122 Class<?> clazz = getClass().getClassLoader().loadClass(lhsClassName);
123
124 if (!Collection.class.isAssignableFrom(clazz)) {
125 RemotableAttributeError.Builder errorBuilder =
126 RemotableAttributeError.Builder.create("operator", "left hand operand is not a Collection");
127 errors.add(errorBuilder.build());
128 }
129 } catch (ClassNotFoundException e) {
130
131 }
132 }
133
134 return errors;
135 }
136
137
138
139
140
141
142
143 @Override
144 public Function loadFunction(FunctionDefinition functionDefinition) {
145 if (!"contains".equals(functionDefinition.getName()) || "KR-SAP".equals(functionDefinition.getNamespace())) {
146 throw new IllegalArgumentException("oops, you have the wrong type service, I can't load this function");
147 }
148
149
150 return new Function() {
151 @Override
152 public Object invoke(Object... arguments) {
153 if (arguments.length != 2) {
154 throw new IllegalArgumentException("contains operator expects 2 arguments");
155 }
156
157 if (arguments[0] instanceof String) {
158 if (arguments[1] instanceof String) {
159 return Boolean.valueOf(((String)arguments[0]).contains((String)arguments[1]));
160 } else {
161 throw new IllegalArgumentException("if the first argument is a String, the second argument "
162 + "must be as well");
163 }
164 } else if (arguments[0] instanceof Collection) {
165 return Boolean.valueOf(((Collection)arguments[0]).contains(arguments[1]));
166 }
167
168 throw new IllegalArgumentException("argument types must be either (String, String) or (Collection, Object)");
169 }
170 };
171 }
172 }