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