1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krms.impl.ui;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
20 import org.kuali.rice.core.api.util.ConcreteKeyValue;
21 import org.kuali.rice.core.api.util.KeyValue;
22 import org.kuali.rice.krad.uif.control.UifKeyValuesFinderBase;
23 import org.kuali.rice.krad.uif.view.ViewModel;
24 import org.kuali.rice.krad.web.form.MaintenanceDocumentForm;
25 import org.kuali.rice.krms.api.KrmsApiServiceLocator;
26 import org.kuali.rice.krms.api.repository.RuleManagementService;
27 import org.kuali.rice.krms.api.repository.context.ContextDefinition;
28 import org.kuali.rice.krms.api.repository.function.FunctionDefinition;
29 import org.kuali.rice.krms.api.repository.operator.CustomOperator;
30 import org.kuali.rice.krms.api.repository.type.KrmsTypeDefinition;
31 import org.kuali.rice.krms.api.repository.type.KrmsTypeRepositoryService;
32 import org.kuali.rice.krms.api.repository.typerelation.RelationshipType;
33 import org.kuali.rice.krms.api.repository.typerelation.TypeTypeRelation;
34 import org.kuali.rice.krms.framework.engine.expression.ComparisonOperator;
35 import org.kuali.rice.krms.impl.util.KrmsImplConstants;
36 import org.kuali.rice.krms.impl.util.KrmsServiceLocatorInternal;
37
38 import javax.xml.namespace.QName;
39 import java.util.ArrayList;
40 import java.util.Collections;
41 import java.util.List;
42
43
44
45
46
47
48
49
50 public class PropositionOpCodeValuesFinder extends UifKeyValuesFinderBase {
51
52 private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(PropositionOpCodeValuesFinder.class);
53
54 private static final List<KeyValue> LABELS;
55
56 static {
57 final List<KeyValue> labels = new ArrayList<KeyValue>( ComparisonOperator.values().length );
58 for (ComparisonOperator operator : ComparisonOperator.values()) {
59 labels.add(new ConcreteKeyValue(operator.getCode(), operator.getCode()));
60 }
61
62 LABELS = Collections.unmodifiableList(labels);
63 }
64
65
66
67
68 @Override
69 public List<KeyValue> getKeyValues(ViewModel model) {
70
71 List<KeyValue> keyValues = new ArrayList<KeyValue>(LABELS);
72
73 MaintenanceDocumentForm maintenanceForm = (MaintenanceDocumentForm) model;
74 AgendaEditor agendaEditor =
75 (AgendaEditor) maintenanceForm.getDocument().getNewMaintainableObject().getDataObject();
76
77
78 ContextDefinition context = getRuleManagementService().getContext(agendaEditor.getAgenda().getContextId());
79
80 keyValues.addAll(getCustomOperatorsKeyValuesForContextType(context.getTypeId()));
81
82 return keyValues;
83 }
84
85
86
87
88
89
90
91 private List<KeyValue> getCustomOperatorsKeyValuesForContextType(String contextTypeId) {
92 List<KeyValue> keyValues = new ArrayList<KeyValue>();
93
94 if (contextTypeId == null) {
95 return keyValues;
96 }
97
98
99 List<TypeTypeRelation> typeRelations =
100 getTypeRepositoryService().findTypeTypeRelationsByFromType(contextTypeId);
101
102 for (TypeTypeRelation typeRelation : typeRelations) {
103 if (typeRelation.getRelationshipType().equals(RelationshipType.USAGE_ALLOWED)) {
104 KrmsTypeDefinition krmsType = getTypeRepositoryService().getTypeById(typeRelation.getToTypeId());
105
106 Object service = getTypeServiceImplementation(krmsType);
107
108 if (service != null && service instanceof CustomOperator) {
109
110 keyValues.add(getKeyValueForCustomOperator(krmsType, (CustomOperator)service));
111 }
112 }
113 }
114
115 return keyValues;
116 }
117
118
119
120
121
122
123
124
125
126
127
128 private ConcreteKeyValue getKeyValueForCustomOperator(KrmsTypeDefinition krmsType, CustomOperator customOperator) {
129 FunctionDefinition operatorFunctionDefinition =
130 customOperator.getOperatorFunctionDefinition();
131 String key = KrmsImplConstants.CUSTOM_OPERATOR_PREFIX
132 + krmsType.getNamespace() +
133 ":" + krmsType.getServiceName();
134
135 return new ConcreteKeyValue(key, operatorFunctionDefinition.getName());
136 }
137
138
139
140
141
142
143
144 private Object getTypeServiceImplementation(KrmsTypeDefinition krmsType) {
145 Object service = null;
146
147 if (krmsType != null && !StringUtils.isEmpty(krmsType.getServiceName())) {
148 QName serviceQName = new QName(krmsType.getNamespace(), krmsType.getServiceName());
149 service = GlobalResourceLoader.getService(serviceQName);
150 }
151
152 return service;
153 }
154
155
156
157
158 private static class TypeRepositoryServiceHolder {
159 static final KrmsTypeRepositoryService typeRepositoryService =
160 KrmsApiServiceLocator.getKrmsTypeRepositoryService();
161 }
162
163
164 private KrmsTypeRepositoryService getTypeRepositoryService() {
165 return TypeRepositoryServiceHolder.typeRepositoryService;
166 }
167
168
169
170
171 private static class RuleManagementServiceHolder {
172 static final RuleManagementService ruleManagementService = KrmsServiceLocatorInternal.getService(
173 "ruleManagementService");
174 }
175
176
177 private RuleManagementService getRuleManagementService() {
178 return RuleManagementServiceHolder.ruleManagementService;
179 }
180
181 }