1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krms.impl.repository;
17
18 import org.kuali.rice.core.api.exception.RiceIllegalArgumentException;
19 import org.kuali.rice.krms.api.repository.RuleManagementService;
20 import org.kuali.rice.krms.api.repository.NaturalLanguage;
21 import org.kuali.rice.krms.api.repository.TranslateBusinessMethods;
22 import org.kuali.rice.krms.api.repository.agenda.AgendaDefinition;
23 import org.kuali.rice.krms.api.repository.agenda.AgendaItemDefinition;
24 import org.kuali.rice.krms.api.repository.language.NaturalLanguageTemplaterContract;
25 import org.kuali.rice.krms.api.repository.reference.ReferenceObjectBinding;
26 import org.kuali.rice.krms.api.repository.rule.RuleDefinition;
27 import org.kuali.rice.krms.impl.repository.language.SimpleNaturalLanguageTemplater;
28
29 import java.util.ArrayList;
30 import java.util.List;
31 import java.util.Map;
32
33 import static org.kuali.rice.core.api.criteria.PredicateFactory.in;
34
35
36
37
38
39
40
41 public class KSRuleManagementServiceImpl extends RuleManagementServiceImpl implements RuleManagementService {
42
43 private TranslateBusinessMethods translationBusinessMethods;
44 private NaturalLanguageTemplaterContract templater = new SimpleNaturalLanguageTemplater();
45 private AgendaBoService agendaBoService = new AgendaBoServiceImpl();
46
47 @Override
48 public List<ReferenceObjectBinding> findReferenceObjectBindingsByReferenceObjectIds(String referenceObjectReferenceDiscriminatorType,
49 List<String> referenceObjectIds)
50 throws RiceIllegalArgumentException {
51 if (referenceObjectReferenceDiscriminatorType == null) {
52 throw new RiceIllegalArgumentException("reference binding object discriminator type must not be null");
53 }
54
55 if (referenceObjectIds == null) {
56 throw new RiceIllegalArgumentException("reference object ids must not be null");
57 }
58
59 List<ReferenceObjectBinding> list = new ArrayList<ReferenceObjectBinding>();
60 for(String referenceObjectId : referenceObjectIds){
61 for (ReferenceObjectBinding binding : this.getReferenceObjectBindingBoService().findReferenceObjectBindingsByReferenceObject(referenceObjectId)) {
62 if (binding.getReferenceDiscriminatorType().equals(referenceObjectReferenceDiscriminatorType)) {
63 list.add(binding);
64 }
65 }
66 }
67
68 return list;
69 }
70
71 @Override
72 public List<NaturalLanguage> translateNaturalLanguageForObjects(String naturalLanguageUsageId, String typeId, List<String> krmsObjectIds,
73 String languageCode)
74 throws RiceIllegalArgumentException {
75
76 List<NaturalLanguage> nlList = new ArrayList<NaturalLanguage>();
77 for(String krmsObjectId : krmsObjectIds){
78 String nl = this.getTranslateBusinessMethods().translateNaturalLanguageForObject(naturalLanguageUsageId, typeId, krmsObjectId, languageCode);
79
80 NaturalLanguage.Builder nlBuilder = NaturalLanguage.Builder.create();
81 nlBuilder.setKrmsObjectId(krmsObjectId);
82 nlBuilder.setNaturalLanguage(nl);
83 nlList.add(nlBuilder.build());
84 }
85
86 return nlList;
87 }
88
89
90
91
92 @Override
93 public AgendaItemDefinition createAgendaItem(AgendaItemDefinition agendaItemDefinition) throws RiceIllegalArgumentException {
94 return createUpdateAgendaItemIfNeeded(agendaItemDefinition);
95 }
96
97 @Override
98 public void updateAgendaItem(AgendaItemDefinition agendaItemDefinition) throws RiceIllegalArgumentException {
99 createUpdateAgendaItemIfNeeded(agendaItemDefinition);
100 }
101
102 private AgendaItemDefinition createUpdateAgendaItemIfNeeded(AgendaItemDefinition agendaItemDefinition) {
103 this.crossCheckRuleId(agendaItemDefinition);
104 this.crossCheckWhenTrueId(agendaItemDefinition);
105 this.crossCheckWhenFalseId(agendaItemDefinition);
106 this.crossCheckAlwaysId(agendaItemDefinition);
107 this.crossCheckSubAgendaId(agendaItemDefinition);
108 agendaItemDefinition = createUpdateRuleIfNeeded(agendaItemDefinition);
109 agendaItemDefinition = createWhenTrueAgendaItemIfNeeded(agendaItemDefinition);
110 agendaItemDefinition = createWhenFalseAgendaItemIfNeeded(agendaItemDefinition);
111 agendaItemDefinition = createAlwaysAgendaItemIfNeeded(agendaItemDefinition);
112 agendaItemDefinition = createSubAgendaIfNeeded(agendaItemDefinition);
113
114
115 if (agendaItemDefinition.getId() != null) {
116 agendaBoService.updateAgendaItem(agendaItemDefinition);
117 } else {
118 agendaItemDefinition = agendaBoService.createAgendaItem(agendaItemDefinition);
119 }
120
121 return agendaItemDefinition;
122 }
123
124 private void crossCheckRuleId(AgendaItemDefinition agendItemDefinition)
125 throws RiceIllegalArgumentException {
126
127 if (agendItemDefinition.getRuleId() != null && agendItemDefinition.getRule() != null) {
128 if (!agendItemDefinition.getRuleId().equals(agendItemDefinition.getRule().getId())) {
129 throw new RiceIllegalArgumentException("ruleId does not rule.getId" + agendItemDefinition.getRuleId() + " " + agendItemDefinition.getRule().getId());
130 }
131 }
132 }
133
134 private void crossCheckWhenTrueId(AgendaItemDefinition agendaItemDefinition)
135 throws RiceIllegalArgumentException {
136
137 if (agendaItemDefinition.getWhenTrueId()!= null && agendaItemDefinition.getWhenTrue() != null) {
138 if (!agendaItemDefinition.getWhenTrueId().equals(agendaItemDefinition.getWhenTrue().getId())) {
139 throw new RiceIllegalArgumentException("when true id does not match " + agendaItemDefinition.getWhenTrueId() + " " + agendaItemDefinition.getWhenTrue().getId());
140 }
141 }
142 }
143
144 private void crossCheckWhenFalseId(AgendaItemDefinition agendItemDefinition)
145 throws RiceIllegalArgumentException {
146
147 if (agendItemDefinition.getWhenFalseId()!= null && agendItemDefinition.getWhenFalse() != null) {
148 if (!agendItemDefinition.getWhenFalseId().equals(agendItemDefinition.getWhenFalse().getId())) {
149 throw new RiceIllegalArgumentException("when false id does not match " + agendItemDefinition.getWhenFalseId() + " " + agendItemDefinition.getWhenFalse().getId());
150 }
151 }
152 }
153
154 private void crossCheckAlwaysId(AgendaItemDefinition agendItemDefinition)
155 throws RiceIllegalArgumentException {
156
157 if (agendItemDefinition.getAlwaysId()!= null && agendItemDefinition.getAlways() != null) {
158 if (!agendItemDefinition.getAlwaysId().equals(agendItemDefinition.getAlways().getId())) {
159 throw new RiceIllegalArgumentException("Always id does not match " + agendItemDefinition.getAlwaysId() + " " + agendItemDefinition.getAlways().getId());
160 }
161 }
162 }
163
164 private void crossCheckSubAgendaId(AgendaItemDefinition agendItemDefinition)
165 throws RiceIllegalArgumentException {
166
167 if (agendItemDefinition.getSubAgendaId()!= null && agendItemDefinition.getSubAgenda() != null) {
168 if (!agendItemDefinition.getSubAgendaId().equals(agendItemDefinition.getSubAgenda().getId())) {
169 throw new RiceIllegalArgumentException("SubAgenda id does not match " + agendItemDefinition.getSubAgendaId() + " " + agendItemDefinition.getSubAgenda().getId());
170 }
171 }
172 }
173
174 private AgendaItemDefinition createUpdateRuleIfNeeded(AgendaItemDefinition agendaItemDefinition)
175 throws RiceIllegalArgumentException {
176
177 if (agendaItemDefinition.getRule() == null) {
178 return agendaItemDefinition;
179 }
180
181
182 RuleDefinition rule = null;
183 if (agendaItemDefinition.getRule().getId() != null) {
184 this.updateRule(agendaItemDefinition.getRule());
185 rule = this.getRule(agendaItemDefinition.getRule().getId());
186 } else {
187 rule = this.createRule(agendaItemDefinition.getRule());
188 }
189
190 AgendaItemDefinition.Builder agendaItemBuilder = AgendaItemDefinition.Builder.create(agendaItemDefinition);
191 RuleDefinition.Builder ruleBuilder = RuleDefinition.Builder.create(rule);
192 agendaItemBuilder.setRule(ruleBuilder);
193 agendaItemBuilder.setRuleId(ruleBuilder.getId());
194 return agendaItemBuilder.build();
195 }
196
197 private AgendaItemDefinition createWhenTrueAgendaItemIfNeeded(AgendaItemDefinition agendaItemDefinition) {
198
199 if (agendaItemDefinition.getWhenTrue() == null) {
200 return agendaItemDefinition;
201 }
202
203
204 AgendaItemDefinition.Builder agendaItemBuilder = AgendaItemDefinition.Builder.create(agendaItemDefinition);
205 AgendaItemDefinition subAgendaItem = this.createUpdateAgendaItemIfNeeded(agendaItemDefinition.getWhenTrue());
206 agendaItemBuilder.setWhenTrue(AgendaItemDefinition.Builder.create(subAgendaItem));
207
208 return agendaItemBuilder.build();
209 }
210
211 private AgendaItemDefinition createWhenFalseAgendaItemIfNeeded(AgendaItemDefinition agendaItemDefinition) {
212
213 if (agendaItemDefinition.getWhenFalse() == null) {
214 return agendaItemDefinition;
215 }
216
217
218 AgendaItemDefinition.Builder agendaItemBuilder = AgendaItemDefinition.Builder.create(agendaItemDefinition);
219 AgendaItemDefinition subAgendaItem = this.createUpdateAgendaItemIfNeeded(agendaItemDefinition.getWhenFalse());
220 agendaItemBuilder.setWhenFalse(AgendaItemDefinition.Builder.create(subAgendaItem));
221 return agendaItemBuilder.build();
222 }
223
224
225 private AgendaItemDefinition createAlwaysAgendaItemIfNeeded(AgendaItemDefinition agendaItemDefinition) {
226
227 if (agendaItemDefinition.getAlways()== null) {
228 return agendaItemDefinition;
229 }
230
231
232 AgendaItemDefinition.Builder agendaItemBuilder = AgendaItemDefinition.Builder.create(agendaItemDefinition);
233 AgendaItemDefinition subAgendaItem = this.createUpdateAgendaItemIfNeeded(agendaItemDefinition.getAlways());
234 agendaItemBuilder.setAlways(AgendaItemDefinition.Builder.create(subAgendaItem));
235
236 return agendaItemBuilder.build();
237 }
238
239 private AgendaItemDefinition createSubAgendaIfNeeded(AgendaItemDefinition agendaItemDefinition) {
240
241 if (agendaItemDefinition.getSubAgenda() == null) {
242 return agendaItemDefinition;
243 }
244
245
246 AgendaItemDefinition.Builder agendaItemBuilder = AgendaItemDefinition.Builder.create(agendaItemDefinition);
247 AgendaDefinition subAgenda = this.createAgenda(agendaItemDefinition.getSubAgenda());
248 agendaItemBuilder.setSubAgenda(AgendaDefinition.Builder.create(subAgenda));
249 agendaItemBuilder.setSubAgendaId(subAgenda.getId());
250
251 return agendaItemBuilder.build();
252 }
253
254
255
256
257
258 public NaturalLanguageTemplaterContract getTemplater() {
259 return templater;
260 }
261
262
263
264
265
266 public void setTemplater(NaturalLanguageTemplaterContract templater) {
267 this.templater = templater;
268 }
269
270
271
272
273
274 public TranslateBusinessMethods getTranslateBusinessMethods() {
275 if(translationBusinessMethods == null) {
276 this.translationBusinessMethods = new KSTranslationUtility(this, getTermRepositoryService(), this.templater);
277 }
278 return this.translationBusinessMethods ;
279 }
280
281
282
283
284
285 public AgendaBoService getAgendaBoService() {
286 return agendaBoService;
287 }
288
289
290
291
292
293 public void setAgendaBoService(AgendaBoService agendaBoService) {
294 this.agendaBoService = agendaBoService;
295 super.setAgendaBoService(agendaBoService);
296 }
297
298 }
299