View Javadoc
1   /**
2    * Copyright 2005-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.rice.krms.impl.repository;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.apache.commons.lang.builder.EqualsBuilder;
20  import org.kuali.rice.core.api.CoreConstants;
21  import org.kuali.rice.core.api.criteria.Predicate;
22  import org.kuali.rice.core.api.criteria.QueryByCriteria;
23  import org.kuali.rice.core.api.criteria.QueryResults;
24  import org.kuali.rice.core.api.exception.RiceIllegalArgumentException;
25  import org.kuali.rice.krad.data.DataObjectService;
26  import org.kuali.rice.krad.data.platform.MaxValueIncrementerFactory;
27  import org.kuali.rice.krms.api.repository.NaturalLanguageTree;
28  import org.kuali.rice.krms.api.repository.RuleManagementService;
29  import org.kuali.rice.krms.api.repository.TranslateBusinessMethods;
30  import org.kuali.rice.krms.api.repository.action.ActionDefinition;
31  import org.kuali.rice.krms.api.repository.agenda.AgendaDefinition;
32  import org.kuali.rice.krms.api.repository.agenda.AgendaItemDefinition;
33  import org.kuali.rice.krms.api.repository.context.ContextDefinition;
34  import org.kuali.rice.krms.api.repository.language.NaturalLanguageTemplate;
35  import org.kuali.rice.krms.api.repository.language.NaturalLanguageTemplaterContract;
36  import org.kuali.rice.krms.api.repository.language.NaturalLanguageUsage;
37  import org.kuali.rice.krms.api.repository.proposition.PropositionDefinition;
38  import org.kuali.rice.krms.api.repository.proposition.PropositionParameter;
39  import org.kuali.rice.krms.api.repository.proposition.PropositionParameterType;
40  import org.kuali.rice.krms.api.repository.proposition.PropositionType;
41  import org.kuali.rice.krms.api.repository.reference.ReferenceObjectBinding;
42  import org.kuali.rice.krms.api.repository.rule.RuleDefinition;
43  import org.kuali.rice.krms.api.repository.term.TermDefinition;
44  import org.kuali.rice.krms.api.repository.term.TermRepositoryService;
45  import org.kuali.rice.krms.impl.repository.language.SimpleNaturalLanguageTemplater;
46  import org.springframework.jdbc.support.incrementer.DataFieldMaxValueIncrementer;
47  
48  import javax.sql.DataSource;
49  import java.util.ArrayList;
50  import java.util.Arrays;
51  import java.util.Collections;
52  import java.util.LinkedHashSet;
53  import java.util.LinkedList;
54  import java.util.List;
55  import java.util.Set;
56  
57  import static org.kuali.rice.core.api.criteria.PredicateFactory.in;
58  
59  /**
60   * The implementation of {@link RuleManagementService} operations facilitate management of rules and
61   * associated information.
62   *
63   * @author Kuali Rice Team (rice.collab@kuali.org)
64   */
65  public class RuleManagementServiceImpl extends RuleRepositoryServiceImpl implements RuleManagementService {
66  
67      private ReferenceObjectBindingBoService referenceObjectBindingBoService = new ReferenceObjectBindingBoServiceImpl();
68      private AgendaBoService agendaBoService = new AgendaBoServiceImpl();
69      private RuleBoService ruleBoService = new RuleBoServiceImpl();
70      private ActionBoService actionBoService = new ActionBoServiceImpl();
71      private PropositionBoService propositionBoService = new PropositionBoServiceImpl();
72      private NaturalLanguageUsageBoService naturalLanguageUsageBoService = new NaturalLanguageUsageBoServiceImpl();
73      private NaturalLanguageTemplateBoService naturalLanguageTemplateBoService = new NaturalLanguageTemplateBoServiceImpl();
74      private ContextBoService contextBoService = new ContextBoServiceImpl();
75      private NaturalLanguageTemplaterContract templater = new SimpleNaturalLanguageTemplater();
76      private TermRepositoryService termRepositoryService = new TermBoServiceImpl();
77      private TranslateBusinessMethods translationBusinessMethods = null;
78  
79      // For MaxValueIncrementerFactory use.
80      private DataSource dataSource;
81      private DataFieldMaxValueIncrementer ruleSequenceIncrementer;
82      private DataFieldMaxValueIncrementer referenceObjectBindingSequenceIncrementer;
83      private static final String RULE_SEQ_NAME = "KRMS_RULE_S";
84      private static final String REF_OBJ_SEQ_NAME= "KRMS_REF_OBJ_KRMS_OBJ_S";
85  
86      /**
87       * Fields to ignore when calling reflection-based equals in
88       * {@link #isSame(org.kuali.rice.krms.api.repository.agenda.AgendaDefinition, org.kuali.rice.krms.api.repository.agenda.AgendaDefinition)}
89       */
90      private static final List<String> isSameIgnoreFields = Arrays.asList(
91              AgendaDefinition.Elements.AGENDA_ID,
92              CoreConstants.CommonElements.VERSION_NUMBER,
93              CoreConstants.CommonElements.FUTURE_ELEMENTS
94      );
95  
96      /**
97       * get the {@link ReferenceObjectBindingBoService}
98       * @return the {@link ReferenceObjectBindingBoService}
99       */
100     public ReferenceObjectBindingBoService getReferenceObjectBindingBoService() {
101         return referenceObjectBindingBoService;
102     }
103 
104     /**
105      * set the {@link ReferenceObjectBindingBoService}
106      * @param referenceObjectBindingBoService the {@link ReferenceObjectBindingBoService} to set
107      */
108     public void setReferenceObjectBindingBoService(ReferenceObjectBindingBoService referenceObjectBindingBoService) {
109         this.referenceObjectBindingBoService = referenceObjectBindingBoService;
110     }
111 
112     /**
113      * get the {@link AgendaBoService}
114      * @return the {@link AgendaBoService}
115      */
116     public AgendaBoService getAgendaBoService() {
117         return agendaBoService;
118     }
119 
120     /**
121      * set the {@link AgendaBoService}
122      * @param agendaBoService the {@link AgendaBoService} to set
123      */
124     public void setAgendaBoService(AgendaBoService agendaBoService) {
125         this.agendaBoService = agendaBoService;
126     }
127 
128     /**
129      * get the {@link RuleBoService}
130      * @return the {@link RuleBoService}
131      */
132     public RuleBoService getRuleBoService() {
133         return ruleBoService;
134     }
135 
136     /**
137      * set the {@link RuleBoService}
138      * @param ruleBoService the {@link RuleBoService} to set
139      */
140     public void setRuleBoService(RuleBoService ruleBoService) {
141         this.ruleBoService = ruleBoService;
142     }
143 
144     /**
145      * get the {@link PropositionBoService}
146      * @return the {@link PropositionBoService}
147      */
148     public PropositionBoService getPropositionBoService() {
149         return propositionBoService;
150     }
151 
152     /**
153      * set the {@link PropositionBoService}
154      * @param propositionBoService the {@link PropositionBoService} to set
155      */
156     public void setPropositionBoService(PropositionBoService propositionBoService) {
157         this.propositionBoService = propositionBoService;
158     }
159 
160     /**
161      * set the {@link NaturalLanguageUsageBoService}
162      * @return the {@link NaturalLanguageUsageBoService}
163      */
164     public NaturalLanguageUsageBoService getNaturalLanguageUsageBoService() {
165         return naturalLanguageUsageBoService;
166     }
167 
168     /**
169      * set the {@link NaturalLanguageUsageBoService}
170      * @param naturalLanguageUsageBoService the {@link NaturalLanguageUsageBoService} to set
171      */
172     public void setNaturalLanguageUsageBoService(NaturalLanguageUsageBoService naturalLanguageUsageBoService) {
173         this.naturalLanguageUsageBoService = naturalLanguageUsageBoService;
174     }
175 
176     /**
177      * get the {@link NaturalLanguageTemplateBoService}
178      * @return the {@link NaturalLanguageTemplateBoService}
179      */
180     public NaturalLanguageTemplateBoService getNaturalLanguageTemplateBoService() {
181         return naturalLanguageTemplateBoService;
182     }
183 
184     /**
185      * set the {@link NaturalLanguageTemplateBoService}
186      * @param naturalLanguageTemplateBoService the {@link NaturalLanguageTemplateBoService} to set
187      */
188     public void setNaturalLanguageTemplateBoService(NaturalLanguageTemplateBoService naturalLanguageTemplateBoService) {
189         this.naturalLanguageTemplateBoService = naturalLanguageTemplateBoService;
190     }
191 
192     /**
193      * get the {@link ContextBoService}
194      * @return the {@link ContextBoService}
195      */
196     public ContextBoService getContextBoService() {
197         return contextBoService;
198     }
199 
200     /**
201      * set the {@link ContextBoService}
202      * @param contextBoService the {@link ContextBoService} to set
203      */
204     public void setContextBoService(ContextBoService contextBoService) {
205         this.contextBoService = contextBoService;
206     }
207 
208     /**
209      * get the {@link ActionBoService}
210      * @return the {@link ActionBoService}
211      */
212     public ActionBoService getActionBoService() {
213         return actionBoService;
214     }
215 
216     /**
217      * Set the {@link ActionBoService}
218      * @param actionBoService the {@link ActionBoService} to set
219      */
220     public void setActionBoService(ActionBoService actionBoService) {
221         this.actionBoService = actionBoService;
222     }
223 
224     /**
225      * get the {@link NaturalLanguageTemplaterContract}
226      * @return the {@link NaturalLanguageTemplaterContract}
227      */
228     public NaturalLanguageTemplaterContract getTemplater() {
229         return templater;
230     }
231 
232     /**
233      * set the {@link NaturalLanguageTemplaterContract}
234      * @param templater the {@link NaturalLanguageTemplaterContract} to set
235      */
236     public void setTemplater(NaturalLanguageTemplaterContract templater) {
237         this.templater = templater;
238     }
239 
240     /**
241      * get the {@link TermRepositoryService}
242      * @return the {@link TermRepositoryService}
243      */
244     public TermRepositoryService getTermRepositoryService() {
245         return termRepositoryService;
246     }
247 
248     /**
249      * Set the {@link TermRepositoryService}
250      * @param termRepositoryService the {@link TermRepositoryService} to set
251      */
252     public void setTermRepositoryService(TermRepositoryService termRepositoryService) {
253         this.termRepositoryService = termRepositoryService;
254     }
255 
256     public DataSource getDataSource() {
257         return dataSource;
258     }
259 
260     public void setDataSource(DataSource dataSource) {
261         this.dataSource = dataSource;
262     }
263 
264     private DataFieldMaxValueIncrementer getRuleSequenceIncrementer() {
265         if (ruleSequenceIncrementer == null) {
266             ruleSequenceIncrementer = MaxValueIncrementerFactory.getIncrementer(dataSource, RULE_SEQ_NAME);
267         }
268 
269         return ruleSequenceIncrementer;
270     }
271 
272     private DataFieldMaxValueIncrementer getReferenceObjectBindingSequenceIncrementer() {
273         if (referenceObjectBindingSequenceIncrementer == null) {
274             referenceObjectBindingSequenceIncrementer = MaxValueIncrementerFactory.getIncrementer(dataSource, REF_OBJ_SEQ_NAME);
275         }
276 
277         return referenceObjectBindingSequenceIncrementer;
278     }
279 
280     /**
281      * get the {@link TranslateBusinessMethods}
282      * @return the current {@link TranslateBusinessMethods}
283      */
284     public TranslateBusinessMethods getTranslateBusinessMethods() {
285         if(translationBusinessMethods  == null) {
286             this.translationBusinessMethods  = new TranslationUtility(this, getTermRepositoryService(), this.templater);
287         }
288         return this.translationBusinessMethods ;
289     }
290 
291     /**
292      * set the {@link TranslateBusinessMethods}
293      * @param translationBusinessMethods the {@link TranslateBusinessMethods} to set
294      */
295     public void setTranslationBusinessMethods (TranslateBusinessMethods  translationBusinessMethods) {
296         this.translationBusinessMethods  = translationBusinessMethods;
297     }
298 
299     ////
300     //// reference object binding methods
301     ////
302 
303     @Override
304     public ReferenceObjectBinding createReferenceObjectBinding(ReferenceObjectBinding referenceObjectDefinition)
305             throws RiceIllegalArgumentException {
306         //Set the id if it doesn't exist.
307         if (referenceObjectDefinition.getId() == null) {
308             String referenceObjectBindingId = getReferenceObjectBindingSequenceIncrementer().nextStringValue();
309             ReferenceObjectBinding.Builder refBldr = ReferenceObjectBinding.Builder.create(referenceObjectDefinition);
310             refBldr.setId(referenceObjectBindingId);
311             referenceObjectDefinition = refBldr.build();
312         }
313 
314         return referenceObjectBindingBoService.createReferenceObjectBinding(referenceObjectDefinition);
315     }
316 
317     @Override
318     public ReferenceObjectBinding getReferenceObjectBinding(String id) throws RiceIllegalArgumentException {
319         return referenceObjectBindingBoService.getReferenceObjectBinding(id);
320     }
321 
322     @Override
323     public List<ReferenceObjectBinding> getReferenceObjectBindings(List<String> ids) throws RiceIllegalArgumentException {
324         if (ids == null) {
325             throw new IllegalArgumentException("reference binding object ids must not be null");
326         }
327 
328         // Fetch BOs
329         List<ReferenceObjectBindingBo> bos = null;
330 
331         if (ids.size() == 0) {
332             bos = Collections.emptyList();
333         } else {
334             QueryByCriteria.Builder qBuilder = QueryByCriteria.Builder.create();
335             List<Predicate> pList = new ArrayList<Predicate>();
336             qBuilder.setPredicates(in("id", ids.toArray()));
337             QueryResults<ReferenceObjectBindingBo> results = getDataObjectService().findMatching(
338                     ReferenceObjectBindingBo.class, qBuilder.build());
339 
340             bos = results.getResults();
341         }
342 
343         // Translate BOs
344         List<ReferenceObjectBinding> bindings = new LinkedList<ReferenceObjectBinding>();
345         for (ReferenceObjectBindingBo bo : bos) {
346             ReferenceObjectBinding binding = ReferenceObjectBindingBo.to(bo);
347             bindings.add(binding);
348         }
349 
350         return Collections.unmodifiableList(bindings);
351     }
352 
353     @Override
354     public List<ReferenceObjectBinding> findReferenceObjectBindingsByReferenceObject(String referenceObjectReferenceDiscriminatorType,
355             String referenceObjectId)
356             throws RiceIllegalArgumentException {
357         if (referenceObjectReferenceDiscriminatorType == null) {
358             throw new RiceIllegalArgumentException("reference binding object discriminator type must not be null");
359         }
360 
361         if (referenceObjectId == null) {
362             throw new RiceIllegalArgumentException("reference object id must not be null");
363         }
364 
365         List<ReferenceObjectBinding> list = new ArrayList<ReferenceObjectBinding>();
366         for (ReferenceObjectBinding binding : this.referenceObjectBindingBoService.findReferenceObjectBindingsByReferenceObject(referenceObjectId)) {
367             if (binding.getReferenceDiscriminatorType().equals(referenceObjectReferenceDiscriminatorType)) {
368                 list.add(binding);
369             }
370         }
371 
372         return list;
373     }
374     
375     @Override
376     public List<ReferenceObjectBinding> findReferenceObjectBindingsByReferenceDiscriminatorType(String referenceObjectReferenceDiscriminatorType) throws RiceIllegalArgumentException {
377         return referenceObjectBindingBoService.findReferenceObjectBindingsByReferenceDiscriminatorType(referenceObjectReferenceDiscriminatorType);
378     }
379 
380     @Override
381     public List<ReferenceObjectBinding> findReferenceObjectBindingsByKrmsDiscriminatorType(String referenceObjectKrmsDiscriminatorType) throws RiceIllegalArgumentException {
382         return referenceObjectBindingBoService.findReferenceObjectBindingsByKrmsDiscriminatorType(referenceObjectKrmsDiscriminatorType);
383     }
384 
385     @Override
386     public List<ReferenceObjectBinding> findReferenceObjectBindingsByKrmsObject(String krmsObjectId) throws RiceIllegalArgumentException {
387         return referenceObjectBindingBoService.findReferenceObjectBindingsByKrmsObject(krmsObjectId);
388     }
389 
390     @Override
391     public void updateReferenceObjectBinding(ReferenceObjectBinding referenceObjectBindingDefinition)
392             throws RiceIllegalArgumentException {
393         referenceObjectBindingBoService.updateReferenceObjectBinding(referenceObjectBindingDefinition);
394     }
395 
396     @Override
397     public void deleteReferenceObjectBinding(String id) throws RiceIllegalArgumentException {
398         referenceObjectBindingBoService.deleteReferenceObjectBinding(id);
399     }
400 
401     @Override
402     public List<String> findReferenceObjectBindingIds(QueryByCriteria queryByCriteria) throws RiceIllegalArgumentException {
403         return referenceObjectBindingBoService.findReferenceObjectBindingIds(queryByCriteria);
404     }
405 
406     ////
407     //// agenda
408     //// 
409     @Override
410     public AgendaDefinition createAgenda(AgendaDefinition agendaDefinition) throws RiceIllegalArgumentException {
411         return agendaBoService.createAgenda(agendaDefinition);
412     }
413 
414     @Override
415     public AgendaDefinition getAgendaByNameAndContextId(String name, String contextId) {
416         return this.agendaBoService.getAgendaByNameAndContextId(name, contextId);
417     }
418 
419     @Override
420     public AgendaDefinition findCreateAgenda(AgendaDefinition agendaDefinition) throws RiceIllegalArgumentException {
421         AgendaDefinition existing = this.getAgendaByNameAndContextId(agendaDefinition.getName(), agendaDefinition.getContextId());
422 
423         if (existing != null) {
424             existing = this.updateAgendaIfNeeded(agendaDefinition, existing);
425             return existing;
426         }
427 
428         return this.createAgenda(agendaDefinition);
429     }
430 
431     private AgendaDefinition updateAgendaIfNeeded(AgendaDefinition agenda, AgendaDefinition existing) {
432         if (this.isSame(agenda, existing)) {
433             return existing;
434         }
435 
436         AgendaDefinition.Builder bldr = AgendaDefinition.Builder.create(existing);
437         bldr.setActive(agenda.isActive());
438         bldr.setAttributes(agenda.getAttributes());
439         bldr.setContextId(agenda.getContextId());
440 
441         if (agenda.getFirstItemId() != null) {
442             bldr.setFirstItemId(agenda.getFirstItemId());
443         }
444 
445         bldr.setTypeId(agenda.getTypeId());
446         this.updateAgenda(bldr.build());
447 
448         return this.getAgenda(existing.getId());
449     }
450 
451     /**
452      * Compares two {@link AgendaDefinition}s for equality, ignoring certain fields defined by
453      * {@link #isSameIgnoreFields}.
454      *
455      * @param agenda one agenda
456      * @param existing another agenda
457      * @return true if the two agendas are considered to be the same
458      */
459     private boolean isSame(AgendaDefinition agenda, AgendaDefinition existing) {
460         return EqualsBuilder.reflectionEquals(agenda, existing, isSameIgnoreFields);
461     }
462 
463     @Override
464     public AgendaDefinition getAgenda(String id) throws RiceIllegalArgumentException {
465         return agendaBoService.getAgendaByAgendaId(id);
466     }
467 
468     @Override
469     public List<AgendaDefinition> getAgendasByContext(String contextId) throws RiceIllegalArgumentException {
470         return agendaBoService.getAgendasByContextId(contextId);
471     }
472 
473     @Override
474     public void updateAgenda(AgendaDefinition agendaDefinition) throws RiceIllegalArgumentException {
475         agendaBoService.updateAgenda(agendaDefinition);
476     }
477 
478     @Override
479     public void deleteAgenda(String id) throws RiceIllegalArgumentException {
480         agendaBoService.deleteAgenda(id);
481     }
482 
483     @Override
484     public List<AgendaDefinition> getAgendasByType(String typeId) throws RiceIllegalArgumentException {
485         return agendaBoService.getAgendasByType(typeId);
486     }
487 
488     @Override
489     public List<AgendaDefinition> getAgendasByTypeAndContext(String typeId, String contextId)
490             throws RiceIllegalArgumentException {
491         return agendaBoService.getAgendasByTypeAndContext(typeId, contextId);
492     }
493 
494     ////
495     //// agenda item methods
496     ////
497     @Override
498     public AgendaItemDefinition createAgendaItem(AgendaItemDefinition agendaItemDefinition) throws RiceIllegalArgumentException {
499         agendaItemDefinition = createUpdateAgendaItemIfNeeded(agendaItemDefinition);
500         return agendaBoService.createAgendaItem(agendaItemDefinition);
501     }
502 
503     private AgendaItemDefinition createUpdateAgendaItemIfNeeded(AgendaItemDefinition agendaItemDefinition) {
504         this.crossCheckRuleId(agendaItemDefinition);
505         this.crossCheckWhenTrueId(agendaItemDefinition);
506         this.crossCheckWhenFalseId(agendaItemDefinition);
507         this.crossCheckAlwaysId(agendaItemDefinition);
508         this.crossCheckSubAgendaId(agendaItemDefinition);
509         agendaItemDefinition = createUpdateRuleIfNeeded(agendaItemDefinition);
510         agendaItemDefinition = createWhenTrueAgendaItemIfNeeded(agendaItemDefinition);
511         agendaItemDefinition = createWhenFalseAgendaItemIfNeeded(agendaItemDefinition);
512         agendaItemDefinition = createAlwaysAgendaItemIfNeeded(agendaItemDefinition);
513         return createSubAgendaIfNeeded(agendaItemDefinition);
514     }
515 
516     private void crossCheckRuleId(AgendaItemDefinition agendItemDefinition)
517             throws RiceIllegalArgumentException {
518         // if both are set they better match
519         if (agendItemDefinition.getRuleId() != null && agendItemDefinition.getRule() != null) {
520             if (!agendItemDefinition.getRuleId().equals(agendItemDefinition.getRule().getId())) {
521                 throw new RiceIllegalArgumentException("ruleId does not rule.getId" + agendItemDefinition.getRuleId() + " " + agendItemDefinition.getRule().getId());
522             }
523         }
524     }
525 
526     private void crossCheckWhenTrueId(AgendaItemDefinition agendaItemDefinition)
527             throws RiceIllegalArgumentException {
528         // if both are set they better match
529         if (agendaItemDefinition.getWhenTrueId()!= null && agendaItemDefinition.getWhenTrue() != null) {
530             if (!agendaItemDefinition.getWhenTrueId().equals(agendaItemDefinition.getWhenTrue().getId())) {
531                 throw new RiceIllegalArgumentException("when true id does not match " + agendaItemDefinition.getWhenTrueId() + " " + agendaItemDefinition.getWhenTrue().getId());
532             }
533         }
534     }
535 
536     private void crossCheckWhenFalseId(AgendaItemDefinition agendItemDefinition)
537             throws RiceIllegalArgumentException {
538         // if both are set they better match
539         if (agendItemDefinition.getWhenFalseId()!= null && agendItemDefinition.getWhenFalse() != null) {
540             if (!agendItemDefinition.getWhenFalseId().equals(agendItemDefinition.getWhenFalse().getId())) {
541                 throw new RiceIllegalArgumentException("when false id does not match " + agendItemDefinition.getWhenFalseId() + " " + agendItemDefinition.getWhenFalse().getId());
542             }
543         }
544     }
545 
546     private void crossCheckAlwaysId(AgendaItemDefinition agendItemDefinition)
547             throws RiceIllegalArgumentException {
548         // if both are set they better match
549         if (agendItemDefinition.getAlwaysId()!= null && agendItemDefinition.getAlways() != null) {
550             if (!agendItemDefinition.getAlwaysId().equals(agendItemDefinition.getAlways().getId())) {
551                 throw new RiceIllegalArgumentException("Always id does not match " + agendItemDefinition.getAlwaysId() + " " + agendItemDefinition.getAlways().getId());
552             }
553         }
554     }
555 
556     private void crossCheckSubAgendaId(AgendaItemDefinition agendItemDefinition)
557             throws RiceIllegalArgumentException {
558         // if both are set they better match
559         if (agendItemDefinition.getSubAgendaId()!= null && agendItemDefinition.getSubAgenda() != null) {
560             if (!agendItemDefinition.getSubAgendaId().equals(agendItemDefinition.getSubAgenda().getId())) {
561                 throw new RiceIllegalArgumentException("SubAgenda id does not match " + agendItemDefinition.getSubAgendaId() + " " + agendItemDefinition.getSubAgenda().getId());
562             }
563         }
564     }
565 
566     private AgendaItemDefinition createUpdateRuleIfNeeded(AgendaItemDefinition agendaItemDefinition)
567             throws RiceIllegalArgumentException {
568         // no rule to create
569         if (agendaItemDefinition.getRule() == null) {
570             return agendaItemDefinition;
571         }
572 
573         // update
574         if (agendaItemDefinition.getRule().getId() != null) {
575             this.updateRule(agendaItemDefinition.getRule());
576             RuleDefinition rule = this.getRule(agendaItemDefinition.getRule ().getId());
577             AgendaItemDefinition.Builder agendaItemBuilder = AgendaItemDefinition.Builder.create(agendaItemDefinition);
578             agendaItemBuilder.setRule(RuleDefinition.Builder.create(rule));
579             agendaItemBuilder.setRuleId(rule.getId());
580             
581             return agendaItemBuilder.build();
582         }
583 
584         AgendaItemDefinition.Builder agendaItemBuilder = AgendaItemDefinition.Builder.create(agendaItemDefinition);
585         RuleDefinition ruleDefinition = this.createRule(agendaItemDefinition.getRule());
586         RuleDefinition.Builder ruleBuilder = RuleDefinition.Builder.create(ruleDefinition);
587         agendaItemBuilder.setRule(ruleBuilder);
588         agendaItemBuilder.setRuleId(ruleBuilder.getId());
589 
590         return agendaItemBuilder.build();
591     }
592 
593     private AgendaItemDefinition createWhenTrueAgendaItemIfNeeded(AgendaItemDefinition agendaItemDefinition) {
594         // nothing to create
595         if (agendaItemDefinition.getWhenTrue() == null) {
596             return agendaItemDefinition;
597         }
598 
599         // ojb will take care of it if it has already been created
600         if (agendaItemDefinition.getWhenTrue().getId() != null) {
601             return agendaItemDefinition;
602         }
603 
604         // ojb does not take care of terms and termparameters, recursively loop thru agendaitems to update terms
605         AgendaItemDefinition.Builder agendaItemBuilder = AgendaItemDefinition.Builder.create(agendaItemDefinition);
606         AgendaItemDefinition subAgendaItem = this.createUpdateAgendaItemIfNeeded(agendaItemDefinition.getWhenTrue());
607         agendaItemBuilder.setWhenTrue(AgendaItemDefinition.Builder.create(subAgendaItem));
608         agendaItemBuilder.setWhenTrueId(subAgendaItem.getId());
609 
610         return agendaItemBuilder.build();
611     }
612 
613 
614     private AgendaItemDefinition createWhenFalseAgendaItemIfNeeded(AgendaItemDefinition agendaItemDefinition) {
615         // nothing to create
616         if (agendaItemDefinition.getWhenFalse() == null) {
617             return agendaItemDefinition;
618         }
619 
620         // ojb will take care of it if it has already been created
621         if (agendaItemDefinition.getWhenFalse().getId() != null) {
622             return agendaItemDefinition;
623         }
624 
625         // ojb does not take care of terms and termparameters, recursively loop thru agendaitems to update terms
626         AgendaItemDefinition.Builder agendaItemBuilder = AgendaItemDefinition.Builder.create(agendaItemDefinition);
627         AgendaItemDefinition subAgendaItem = this.createUpdateAgendaItemIfNeeded(agendaItemDefinition.getWhenFalse());
628         agendaItemBuilder.setWhenFalse(AgendaItemDefinition.Builder.create(subAgendaItem));
629         agendaItemBuilder.setWhenFalseId(subAgendaItem.getId());
630         return agendaItemBuilder.build();
631     }
632     
633 
634     private AgendaItemDefinition createAlwaysAgendaItemIfNeeded(AgendaItemDefinition agendaItemDefinition) {
635         // nothing to create
636         if (agendaItemDefinition.getAlways()== null) {
637             return agendaItemDefinition;
638         } 
639         // ojb will take care of it if it has already been created
640         if (agendaItemDefinition.getAlways().getId() != null) {
641             return agendaItemDefinition;
642         }
643         
644         AgendaItemDefinition.Builder agendaItemBuilder = AgendaItemDefinition.Builder.create(agendaItemDefinition);
645         AgendaItemDefinition subAgendaItem = this.createAgendaItem(agendaItemDefinition.getAlways());
646         agendaItemBuilder.setAlways(AgendaItemDefinition.Builder.create(subAgendaItem));
647         agendaItemBuilder.setAlwaysId(subAgendaItem.getId());
648 
649         return agendaItemBuilder.build();
650     }
651 
652     private AgendaItemDefinition createSubAgendaIfNeeded(AgendaItemDefinition agendaItemDefinition) {
653         // nothing to create
654         if (agendaItemDefinition.getSubAgenda() == null) {
655             return agendaItemDefinition;
656         }
657 
658         // ojb will take care of it if it has already been created
659         if (agendaItemDefinition.getSubAgenda().getId() != null) {
660             return agendaItemDefinition;
661         }
662 
663         AgendaItemDefinition.Builder agendaItemBuilder = AgendaItemDefinition.Builder.create(agendaItemDefinition);
664         AgendaDefinition subAgenda = this.createAgenda(agendaItemDefinition.getSubAgenda());
665         agendaItemBuilder.setSubAgenda(AgendaDefinition.Builder.create(subAgenda));
666         agendaItemBuilder.setSubAgendaId(subAgenda.getId());
667 
668         return agendaItemBuilder.build();
669     }
670 
671     @Override
672     public AgendaItemDefinition getAgendaItem(String id) throws RiceIllegalArgumentException {
673         AgendaItemDefinition agendaItem = agendaBoService.getAgendaItemById(id);
674 
675         if (agendaItem != null) {
676             return setTermValuesForAgendaItem(agendaItem).build();
677         }
678 
679         return agendaItem;
680     }
681 
682     private AgendaItemDefinition.Builder setTermValuesForAgendaItem(AgendaItemDefinition agendaItem) {
683         if (agendaItem == null){
684             return null;
685         }
686 
687         AgendaItemDefinition.Builder itemBuiler = AgendaItemDefinition.Builder.create(agendaItem);
688         if (itemBuiler.getRule() != null) {
689             // Set the termValues on the agenda item.
690             PropositionDefinition proposition = agendaItem.getRule().getProposition();
691             if (proposition != null) {
692                 proposition = this.orderCompoundPropositionsIfNeeded(proposition);
693                 itemBuiler.getRule().setProposition(this.replaceTermValues(proposition));
694             }
695         }
696 
697         // Recursively set the term values on child agenda items.
698         itemBuiler.setWhenTrue(setTermValuesForAgendaItem(agendaItem.getWhenTrue()));
699         itemBuiler.setWhenFalse(setTermValuesForAgendaItem(agendaItem.getWhenFalse()));
700         itemBuiler.setAlways(setTermValuesForAgendaItem(agendaItem.getAlways()));
701 
702         return itemBuiler;
703     }
704 
705     @Override
706     public List<AgendaItemDefinition> getAgendaItemsByType(String typeId)
707             throws RiceIllegalArgumentException {
708         return agendaBoService.getAgendaItemsByType(typeId);
709     }
710 
711     @Override
712     public List<AgendaItemDefinition> getAgendaItemsByContext(String contextId) throws RiceIllegalArgumentException {
713         return agendaBoService.getAgendaItemsByContext(contextId);
714     }
715 
716     @Override
717     public List<AgendaItemDefinition> getAgendaItemsByTypeAndContext(String typeId, String contextId)
718             throws RiceIllegalArgumentException {
719         return agendaBoService.getAgendaItemsByTypeAndContext(typeId, contextId);
720     }
721 
722     @Override
723     public void deleteAgendaItem(String id) throws RiceIllegalArgumentException {
724         agendaBoService.deleteAgendaItem(id);
725     }
726 
727     @Override
728     public void updateAgendaItem(AgendaItemDefinition agendaItemDefinition) throws RiceIllegalArgumentException {
729         agendaItemDefinition = createUpdateAgendaItemIfNeeded(agendaItemDefinition);
730         agendaBoService.updateAgendaItem(agendaItemDefinition);
731     }
732 
733     private void crossCheckPropId(RuleDefinition ruleDefinition)
734             throws RiceIllegalArgumentException {
735         // if both are set they better match
736         if (ruleDefinition.getPropId() != null && ruleDefinition.getProposition() != null) {
737             if (!ruleDefinition.getPropId().equals(ruleDefinition.getProposition().getId())) {
738                 throw new RiceIllegalArgumentException("propId does not match proposition.getId" + ruleDefinition.getPropId() + " " + ruleDefinition.getProposition().getId());
739             }
740         }
741     }
742 
743     ////
744     //// rule methods
745     ////
746     @Override
747     public RuleDefinition getRuleByNameAndNamespace(String name, String namespace) {
748         return this.ruleBoService.getRuleByNameAndNamespace(name, namespace);
749     }
750 
751 
752     @Override
753     public RuleDefinition createRule(RuleDefinition ruleDefinition) throws RiceIllegalArgumentException {
754         if (ruleDefinition.getId() != null) {
755             RuleDefinition orig = this.getRule(ruleDefinition.getId());
756 
757             if (orig != null) {
758                 throw new RiceIllegalArgumentException(ruleDefinition.getId());
759             }
760         } else {
761             // if no id then set it because it is needed to store propositions connected to this rule
762             String ruleId = getRuleSequenceIncrementer().nextStringValue();
763             RuleDefinition.Builder ruleBldr = RuleDefinition.Builder.create(ruleDefinition);
764             ruleBldr.setId(ruleId);
765             ruleDefinition = ruleBldr.build();
766         }
767 
768         // if both are set they better match
769         crossCheckPropId(ruleDefinition);
770         ruleDefinition = this.createUpdatePropositionIfNeeded(ruleDefinition);
771         ruleDefinition = ruleBoService.createRule(ruleDefinition);
772 
773         return ruleDefinition;
774     }
775 
776     private RuleDefinition createRuleIfNeeded(RuleDefinition ruleDefinition) throws RiceIllegalArgumentException {
777 
778         // no rule to create
779         if (ruleDefinition == null) {
780             return null;
781         }
782 
783         // if no id then set it because it is needed to store propositions connected to this rule
784         if (ruleDefinition.getId() == null) {
785             String ruleId = getRuleSequenceIncrementer().nextStringValue();
786             RuleDefinition.Builder ruleBldr = RuleDefinition.Builder.create(ruleDefinition);
787             ruleBldr.setId(ruleId);
788             ruleDefinition = ruleBldr.build();
789         }
790 
791         // if both are set they better match
792         crossCheckPropId(ruleDefinition);
793         ruleDefinition = this.createUpdatePropositionIfNeeded(ruleDefinition);
794 
795         return ruleDefinition;
796     }
797 
798     private RuleDefinition createUpdatePropositionIfNeeded(RuleDefinition rule) {
799         // no prop to create
800         if (rule.getProposition() == null) {
801             return rule;
802         }
803 
804         // update 
805         if (rule.getProposition().getId() != null) {
806             this.updateProposition(rule.getProposition());
807             PropositionDefinition prop = this.getProposition(rule.getProposition().getId());
808             RuleDefinition.Builder ruleBldr = RuleDefinition.Builder.create(rule);
809             ruleBldr.setProposition(PropositionDefinition.Builder.create(prop));
810             ruleBldr.setPropId(prop.getId());
811             return ruleBldr.build();
812         }
813 
814         // create the proposition
815         RuleDefinition.Builder ruleBldr = RuleDefinition.Builder.create(rule);
816         PropositionDefinition propositionDefinition = null;
817         // ojb will take care of props that have already been created, but we still need to take care of the terms.
818         PropositionDefinition.Builder propBldr = ruleBldr.getProposition();
819 
820         if (rule.getProposition().getId() != null) {
821             this.crossCheckPropositionParameters(rule.getProposition());
822             propBldr = setSequenceOnCompoundPropositionsIfNeeded(propBldr);
823             propBldr = maintainTermValuesAndChildPropositions(propBldr);
824         } else {
825             // create the proposition
826             propBldr.setRule(ruleBldr);
827             propositionDefinition = this.createProposition(propBldr.build());
828             propBldr = PropositionDefinition.Builder.create(propositionDefinition);
829         }
830 
831         // now update the rule so it holds the proposition id
832         ruleBldr.setProposition(propBldr);
833 
834         return ruleBldr.build();
835     }
836 
837     @Override
838     public void updateRule(RuleDefinition ruleDefinition) throws RiceIllegalArgumentException {
839         crossCheckPropId(ruleDefinition);
840         ruleDefinition = this.createUpdatePropositionIfNeeded(ruleDefinition);
841 
842         ruleBoService.updateRule(ruleDefinition);
843     }
844 
845     @Override
846     public void deleteRule(String id) throws RiceIllegalArgumentException {
847         ruleBoService.deleteRule(id);
848     }
849 
850     ////
851     //// action methods
852     ////
853     @Override
854     public ActionDefinition createAction(ActionDefinition actionDefinition) throws RiceIllegalArgumentException {
855         return actionBoService.createAction(actionDefinition);
856     }
857 
858     @Override
859     public void updateAction(ActionDefinition actionDefinition) throws RiceIllegalArgumentException {
860         actionBoService.updateAction(actionDefinition);
861     }
862 
863     @Override
864     public void deleteAction(String id) throws RiceIllegalArgumentException {
865         throw new RiceIllegalArgumentException("not implemented yet because not supported by the bo service");
866 //        actionBoService.deleteAction(id);
867     }
868 
869     @Override
870     public ActionDefinition getAction(String actionId) {
871         return actionBoService.getActionByActionId(actionId);
872     }
873 
874     @Override
875     public List<ActionDefinition> getActions(List<String> actionIds) {
876         // TODO: implement this more efficiently by adding the bulk op to the bo service and calling it
877         List<ActionDefinition> list = new ArrayList<ActionDefinition>();
878 
879         for (String id : actionIds) {
880             list.add(this.getAction(id));
881         }
882 
883         return list;
884     }
885 
886     private void crossCheckPropositionParameters(PropositionDefinition propositionDefinition) {
887         // check that if the value and termValue are both supplied that they match
888         for (PropositionParameter param : propositionDefinition.getParameters()) {
889             if (param.getValue() != null && param.getTermValue() != null) {
890                 if (!param.getValue().equals(param.getTermValue().getId())) {
891                     throw new RiceIllegalArgumentException("value does not match termValue.id on param "
892                             + param.getSequenceNumber() + " " + param.getValue() + " " + param.getTermValue().getId());
893                 }
894             }
895         }
896     }
897 
898 
899     ////
900     //// proposition methods
901     ////
902     @Override
903     public PropositionDefinition createProposition(PropositionDefinition propositionDefinition) throws RiceIllegalArgumentException {
904         // CREATE
905         if (propositionDefinition.getId() != null) {
906             PropositionDefinition orig = this.getProposition(propositionDefinition.getId());
907             if (orig != null) {
908                 throw new RiceIllegalArgumentException(propositionDefinition.getId());
909             }
910         }
911 
912         crossCheckPropositionParameters(propositionDefinition);
913         PropositionDefinition.Builder propBldr = PropositionDefinition.Builder.create(propositionDefinition);
914         propBldr = setSequenceOnCompoundPropositionsIfNeeded(propBldr);
915         propBldr = maintainTermValuesAndChildPropositions(propBldr);
916         PropositionDefinition prop = propositionBoService.createProposition(propBldr.build());
917 
918         return prop;
919     }
920 
921     private PropositionDefinition.Builder maintainTermValuesAndChildPropositions(PropositionDefinition.Builder propBldr) {
922         if (PropositionType.SIMPLE.getCode().equalsIgnoreCase(propBldr.getPropositionTypeCode())) {
923             return maintainTermValues(propBldr);
924         } else {
925             return createChildPropsIfNeeded(propBldr);
926         }
927     }
928 
929     private PropositionDefinition.Builder maintainTermValues(PropositionDefinition.Builder propBldr) {
930         if (propBldr.getParameters() == null) {
931             return propBldr;
932         }
933 
934         if (propBldr.getParameters().isEmpty()) {
935             return propBldr;
936         }
937 
938         boolean updated = false;
939         List<PropositionParameter.Builder> paramBldrs = new ArrayList<PropositionParameter.Builder> ();
940 
941         for (PropositionParameter.Builder paramBldr : propBldr.getParameters()) {
942             paramBldrs.add(paramBldr);
943 
944             // link the param the proposition's id
945             // not sure we need to do this but...
946             if (paramBldr.getPropId() == null) {
947                 paramBldr.setPropId(propBldr.getId());
948                 updated = true;
949             }
950 
951             // create the termvalue if it was specified
952             if (paramBldr.getTermValue() != null) {
953                 TermDefinition termValue = paramBldr.getTermValue();
954 
955                 // no id means it does not exist yet
956                 if (termValue.getId() == null) {
957                     termValue = this.termRepositoryService.createTerm(termValue);
958                     paramBldr.setTermValue(termValue);
959                     updated = true;
960                 } else {
961                     this.termRepositoryService.updateTerm(termValue);
962                 }
963                 
964                 if ((paramBldr.getValue() == null) || (!paramBldr.getValue().equals(termValue.getId()))) {
965                     paramBldr.setValue(termValue.getId());
966                     updated = true;
967                 }
968             }
969         }
970 
971         if (!updated) {
972             return propBldr;
973         }
974 
975         propBldr.setParameters(paramBldrs);
976         return propBldr;
977     }
978 
979     private PropositionDefinition.Builder createChildPropsIfNeeded(PropositionDefinition.Builder propBldr) {
980         if (propBldr.getCompoundComponents() == null) {
981             return propBldr;
982         }
983 
984         if (propBldr.getCompoundComponents().isEmpty()) {
985             return propBldr;
986         }
987 
988         List<PropositionDefinition.Builder> childPropBldrs = new ArrayList<PropositionDefinition.Builder>();
989 
990         for (PropositionDefinition.Builder compPropBldr : propBldr.getCompoundComponents()) {
991             compPropBldr.setRuleId(propBldr.getRuleId());
992             propBldr = setSequenceOnCompoundPropositionsIfNeeded(propBldr);
993             compPropBldr = maintainTermValuesAndChildPropositions(compPropBldr);
994             childPropBldrs.add(compPropBldr);
995         }
996 
997         propBldr.setCompoundComponents(childPropBldrs);
998 
999         return propBldr;
1000     }
1001 
1002     @Override
1003     public PropositionDefinition getProposition(String id) throws RiceIllegalArgumentException {
1004         if (StringUtils.isBlank(id)) {
1005             throw new RiceIllegalArgumentException("Proposition id must not be null or blank");
1006         }
1007 
1008         PropositionDefinition proposition = propositionBoService.getPropositionById(id);
1009 
1010         if (proposition == null) {
1011             return null;
1012         }
1013 
1014         proposition = this.replaceTermValues(proposition).build();
1015         proposition = this.orderCompoundPropositionsIfNeeded(proposition);
1016 
1017         return proposition;
1018     }
1019 
1020     private PropositionDefinition orderCompoundPropositionsIfNeeded(PropositionDefinition prop) {
1021         if (!prop.getPropositionTypeCode().equals(PropositionType.COMPOUND.getCode())) {
1022             return prop;
1023         }
1024 
1025         if (prop.getCompoundComponents() == null) {
1026             return prop;
1027         }
1028 
1029         if (prop.getCompoundComponents().size() <= 1) {
1030             return prop;
1031         }
1032 
1033         PropositionDefinition.Builder propBldr = PropositionDefinition.Builder.create(prop);        
1034         List<PropositionDefinition> childProps = new ArrayList<PropositionDefinition> (prop.getCompoundComponents());
1035         Collections.sort(childProps, new CompoundPropositionComparator());
1036         List<PropositionDefinition.Builder> childPropBldrs = new ArrayList<PropositionDefinition.Builder> (childProps.size());
1037 
1038         for (PropositionDefinition chidProp : childProps) {
1039             PropositionDefinition.Builder childPropBlder = PropositionDefinition.Builder.create(chidProp);
1040             childPropBldrs.add(childPropBlder);
1041         }
1042 
1043         propBldr.setCompoundComponents(childPropBldrs);
1044 
1045         return propBldr.build();
1046     }
1047 
1048     private PropositionDefinition.Builder replaceTermValues(PropositionDefinition proposition) {
1049 
1050         PropositionDefinition.Builder bldr = PropositionDefinition.Builder.create(proposition);
1051 
1052         // recursively add termValues to child propositions.
1053         if (!PropositionType.SIMPLE.getCode().equalsIgnoreCase(proposition.getPropositionTypeCode())) {
1054             List<PropositionDefinition.Builder> cmpdProps = new ArrayList<PropositionDefinition.Builder>();
1055             for (PropositionDefinition cmpdProp : proposition.getCompoundComponents()) {
1056                 cmpdProps.add(replaceTermValues(cmpdProp));
1057             }
1058             bldr.setCompoundComponents(cmpdProps);
1059             return bldr;
1060         }
1061 
1062         // that have parameters
1063         if (proposition.getParameters() == null) {
1064             return bldr;
1065         }
1066 
1067         if (proposition.getParameters().isEmpty()) {
1068             return bldr;
1069         }
1070 
1071         boolean found = false;
1072         List<PropositionParameter.Builder> params = new ArrayList<PropositionParameter.Builder>(proposition.getParameters().size());
1073 
1074         for (PropositionParameter param : proposition.getParameters()) {
1075             if (!PropositionParameterType.TERM.getCode().equalsIgnoreCase(param.getParameterType())) {
1076                 params.add(PropositionParameter.Builder.create(param));
1077                 continue;
1078             }
1079 
1080             // inflate the termValue
1081             found = true;
1082             TermDefinition termValue = this.termRepositoryService.getTerm(param.getValue());
1083             PropositionParameter.Builder parmbldr = PropositionParameter.Builder.create(param);
1084             parmbldr.setTermValue(termValue);
1085             params.add(parmbldr);
1086         }
1087 
1088         if (!found) {
1089             return bldr;
1090         }
1091 
1092         bldr.setParameters(params);
1093 
1094         return bldr;
1095     }
1096 
1097 
1098     private Set<PropositionDefinition> replaceTermValuesInSet(Set<PropositionDefinition> propositions) {
1099         if (propositions == null) {
1100             return null;
1101         }
1102 
1103         if (propositions.isEmpty()) {
1104             return propositions;
1105         }
1106 
1107         Set<PropositionDefinition> set = new LinkedHashSet<PropositionDefinition>(propositions.size());
1108         for (PropositionDefinition proposition : propositions) {
1109             proposition = this.replaceTermValues(proposition).build();
1110             set.add(proposition);
1111         }
1112 
1113         return set;
1114     }
1115 
1116     @Override
1117     public Set<PropositionDefinition> getPropositionsByType(String typeId) throws RiceIllegalArgumentException {
1118         return replaceTermValuesInSet(propositionBoService.getPropositionsByType(typeId));
1119     }
1120 
1121     @Override
1122     public Set<PropositionDefinition> getPropositionsByRule(String ruleId) throws RiceIllegalArgumentException {
1123         return replaceTermValuesInSet(propositionBoService.getPropositionsByRule(ruleId));
1124     }
1125 
1126     @Override
1127     public void updateProposition(PropositionDefinition propositionDefinition) throws RiceIllegalArgumentException {
1128         this.crossCheckPropositionParameters(propositionDefinition);
1129         PropositionDefinition.Builder propBldr = PropositionDefinition.Builder.create(propositionDefinition);
1130         propBldr = setSequenceOnCompoundPropositionsIfNeeded(propBldr);
1131         propBldr = maintainTermValuesAndChildPropositions(propBldr);
1132 
1133         propositionBoService.updateProposition(propBldr.build());
1134     }
1135 
1136     private PropositionDefinition.Builder setSequenceOnCompoundPropositionsIfNeeded(PropositionDefinition.Builder propBldr) {
1137         if (propBldr.getCompoundComponents() == null) {
1138             return propBldr;
1139         }
1140 
1141         if (propBldr.getCompoundComponents().size() <= 1) {
1142             return propBldr;
1143         }
1144 
1145         List<PropositionDefinition.Builder> childList = new ArrayList<PropositionDefinition.Builder>(propBldr.getCompoundComponents().size());
1146         int i = 1; // start at 1 because rice does that
1147 
1148         for (PropositionDefinition.Builder childPropBldr : propBldr.getCompoundComponents()) {
1149             childPropBldr.setCompoundSequenceNumber(i);
1150             i++;
1151             childList.add(childPropBldr);
1152         }
1153         propBldr.setCompoundComponents(childList);
1154         return propBldr;
1155     }
1156 
1157     @Override
1158     public void deleteProposition(String id) throws RiceIllegalArgumentException {
1159         propositionBoService.deleteProposition(id);
1160     }
1161 
1162     ////
1163     //// natural language usage methods
1164     ////
1165     @Override
1166     public NaturalLanguageUsage createNaturalLanguageUsage(NaturalLanguageUsage naturalLanguageUsage) throws RiceIllegalArgumentException {
1167         return naturalLanguageUsageBoService.createNaturalLanguageUsage(naturalLanguageUsage);
1168     }
1169 
1170     @Override
1171     public NaturalLanguageUsage getNaturalLanguageUsage(String id) throws RiceIllegalArgumentException {
1172         return naturalLanguageUsageBoService.getNaturalLanguageUsage(id);
1173     }
1174 
1175     @Override
1176     public void updateNaturalLanguageUsage(NaturalLanguageUsage naturalLanguageUsage) throws RiceIllegalArgumentException {
1177         naturalLanguageUsageBoService.updateNaturalLanguageUsage(naturalLanguageUsage);
1178     }
1179 
1180     @Override
1181     public void deleteNaturalLanguageUsage(String naturalLanguageUsageId) throws RiceIllegalArgumentException {
1182         naturalLanguageUsageBoService.deleteNaturalLanguageUsage(naturalLanguageUsageId);
1183     }
1184 
1185     @Override
1186     public List<NaturalLanguageUsage> getNaturalLanguageUsagesByNamespace(String namespace)
1187             throws RiceIllegalArgumentException {
1188         return this.naturalLanguageUsageBoService.findNaturalLanguageUsagesByNamespace(namespace);
1189     }
1190 
1191     @Override
1192     public NaturalLanguageUsage getNaturalLanguageUsageByNameAndNamespace(String name, String namespace)
1193             throws RiceIllegalArgumentException {
1194         return this.naturalLanguageUsageBoService.getNaturalLanguageUsageByName(namespace, name);
1195     }
1196 
1197 
1198     ////
1199     //// natural language translations
1200     ////
1201     @Override
1202     public String translateNaturalLanguageForObject(String naturalLanguageUsageId,
1203                                                     String typeId,
1204                                                     String krmsObjectId,
1205                                                     String languageCode)
1206             throws RiceIllegalArgumentException {
1207 
1208         return this.getTranslateBusinessMethods().translateNaturalLanguageForObject(naturalLanguageUsageId, typeId, krmsObjectId, languageCode);
1209     }
1210 
1211     @Override
1212     public String translateNaturalLanguageForProposition(String naturalLanguageUsageId,
1213                                                          PropositionDefinition proposition, String languageCode)
1214             throws RiceIllegalArgumentException {
1215 
1216         return this.getTranslateBusinessMethods().translateNaturalLanguageForProposition(naturalLanguageUsageId, proposition, languageCode);
1217     }
1218 
1219     @Override
1220     public NaturalLanguageTree translateNaturalLanguageTreeForProposition(String naturalLanguageUsageId, 
1221             PropositionDefinition propositionDefinintion, String languageCode) throws RiceIllegalArgumentException {
1222 
1223         return getTranslateBusinessMethods().translateNaturalLanguageTreeForProposition(naturalLanguageUsageId,
1224                 propositionDefinintion, languageCode);
1225     }
1226 
1227     ////
1228     //// context methods
1229     ////
1230     @Override
1231     public ContextDefinition createContext(ContextDefinition contextDefinition) throws RiceIllegalArgumentException {
1232         return this.contextBoService.createContext(contextDefinition);
1233     }
1234 
1235 
1236     @Override
1237     public ContextDefinition findCreateContext(ContextDefinition contextDefinition) throws RiceIllegalArgumentException {
1238         ContextDefinition orig = this.contextBoService.getContextByNameAndNamespace(contextDefinition.getName(), contextDefinition.getNamespace());
1239 
1240         if (orig != null) {
1241             return orig;
1242         }
1243 
1244         return this.contextBoService.createContext(contextDefinition);
1245     }
1246 
1247     @Override
1248     public void updateContext(ContextDefinition contextDefinition) throws RiceIllegalArgumentException {
1249         this.contextBoService.updateContext(contextDefinition);
1250     }
1251 
1252     @Override
1253     public void deleteContext(String id) throws RiceIllegalArgumentException {
1254         throw new RiceIllegalArgumentException("not implemented yet");
1255     }
1256 
1257     @Override
1258     public ContextDefinition getContext(String id) throws RiceIllegalArgumentException {
1259         return this.contextBoService.getContextByContextId(id);
1260     }
1261 
1262     @Override
1263     public ContextDefinition getContextByNameAndNamespace(String name, String namespace) throws RiceIllegalArgumentException {
1264         return this.contextBoService.getContextByNameAndNamespace(name, namespace);
1265     }
1266 
1267     ////
1268     //// natural language templates
1269     ////
1270     @Override
1271     public NaturalLanguageTemplate createNaturalLanguageTemplate(NaturalLanguageTemplate naturalLanguageTemplate) throws RiceIllegalArgumentException {
1272         return this.naturalLanguageTemplateBoService.createNaturalLanguageTemplate(naturalLanguageTemplate);
1273     }
1274 
1275     @Override
1276     public NaturalLanguageTemplate getNaturalLanguageTemplate(String naturalLanguageTemplateId) throws RiceIllegalArgumentException {
1277         return this.naturalLanguageTemplateBoService.getNaturalLanguageTemplate(naturalLanguageTemplateId);
1278     }
1279 
1280     @Override
1281     public void updateNaturalLanguageTemplate(NaturalLanguageTemplate naturalLanguageTemplate) throws RiceIllegalArgumentException {
1282         this.naturalLanguageTemplateBoService.updateNaturalLanguageTemplate(naturalLanguageTemplate);
1283     }
1284 
1285     @Override
1286     public void deleteNaturalLanguageTemplate(String naturalLanguageTemplateId) throws RiceIllegalArgumentException {
1287         this.naturalLanguageTemplateBoService.deleteNaturalLanguageTemplate(naturalLanguageTemplateId);
1288     }
1289 
1290     @Override
1291     public List<NaturalLanguageTemplate> findNaturalLanguageTemplatesByLanguageCode(String languageCode) throws RiceIllegalArgumentException {
1292         return this.naturalLanguageTemplateBoService.findNaturalLanguageTemplatesByLanguageCode(languageCode);
1293     }
1294 
1295     @Override
1296     public NaturalLanguageTemplate findNaturalLanguageTemplateByLanguageCodeTypeIdAndNluId(String languageCode, String typeId, String naturalLanguageUsageId) throws RiceIllegalArgumentException {
1297         return this.naturalLanguageTemplateBoService.findNaturalLanguageTemplateByLanguageCodeTypeIdAndNluId(languageCode, typeId, naturalLanguageUsageId);
1298     }
1299 
1300     @Override
1301     public List<NaturalLanguageTemplate> findNaturalLanguageTemplatesByNaturalLanguageUsage(String naturalLanguageUsageId) throws RiceIllegalArgumentException {
1302         return this.naturalLanguageTemplateBoService.findNaturalLanguageTemplatesByNaturalLanguageUsage(naturalLanguageUsageId);
1303     }
1304 
1305     @Override
1306     public List<NaturalLanguageTemplate> findNaturalLanguageTemplatesByType(String typeId) throws RiceIllegalArgumentException {
1307         return this.naturalLanguageTemplateBoService.findNaturalLanguageTemplatesByType(typeId);
1308     }
1309 
1310     @Override
1311     public List<NaturalLanguageTemplate> findNaturalLanguageTemplatesByTemplate(String template) throws RiceIllegalArgumentException {
1312         return this.naturalLanguageTemplateBoService.findNaturalLanguageTemplatesByTemplate(template);
1313     }
1314 
1315     @Override
1316     public List<String> findContextIds(QueryByCriteria queryByCriteria) throws RiceIllegalArgumentException {
1317         if (queryByCriteria == null) {
1318             throw new RiceIllegalArgumentException("queryByCriteria must not be null");
1319         }
1320 
1321         QueryResults<ContextBo> results = getDataObjectService().findMatching(ContextBo.class, queryByCriteria);
1322 
1323         List<String> list = new ArrayList<String> ();
1324         for (ContextBo bo : results.getResults()) {
1325             list.add(bo.getId());
1326         }
1327 
1328         return list;
1329     }
1330 
1331     @Override
1332     public List<String> findAgendaIds(QueryByCriteria queryByCriteria) throws RiceIllegalArgumentException {
1333         QueryResults<AgendaBo> results = getDataObjectService().findMatching(AgendaBo.class, queryByCriteria);
1334         List<String> list = new ArrayList<String> ();
1335 
1336         for (AgendaBo bo : results.getResults()) {
1337             list.add(bo.getId());
1338         }
1339 
1340         return list;
1341     }
1342 
1343     @Override
1344     public List<String> findRuleIds(QueryByCriteria queryByCriteria) throws RiceIllegalArgumentException {
1345         QueryResults<RuleBo> results = getDataObjectService().findMatching(RuleBo.class, queryByCriteria);
1346         List<String> list = new ArrayList<String> ();
1347 
1348         for (RuleBo bo : results.getResults()) {
1349             list.add(bo.getId());
1350         }
1351 
1352         return list;
1353     }
1354 
1355     @Override
1356     public List<String> findPropositionIds(QueryByCriteria queryByCriteria) throws RiceIllegalArgumentException {
1357         QueryResults<PropositionBo> results = getDataObjectService().findMatching(PropositionBo.class, queryByCriteria);
1358 
1359         List<String> list = new ArrayList<String> ();
1360         for (PropositionBo bo : results.getResults()) {
1361             list.add(bo.getId());
1362         }
1363 
1364         return list;
1365     }
1366 
1367     @Override
1368     public List<String> findActionIds(QueryByCriteria queryByCriteria) throws RiceIllegalArgumentException {
1369         QueryResults<ActionBo> results = getDataObjectService().findMatching(ActionBo.class, queryByCriteria);
1370 
1371         List<String> list = new ArrayList<String> ();
1372         for (ActionBo bo : results.getResults()) {
1373             list.add(bo.getId());
1374         }
1375 
1376         return list;
1377     }
1378 
1379     /**
1380      * Sets the dataObjectService property.
1381      *
1382      * @param dataObjectService The businessObjectService to set.
1383      */
1384     @Override
1385     public void setDataObjectService(final DataObjectService dataObjectService) {
1386 
1387         super.setDataObjectService(dataObjectService);
1388 
1389         if (agendaBoService instanceof AgendaBoServiceImpl) {
1390             ((AgendaBoServiceImpl) agendaBoService).setDataObjectService(dataObjectService);
1391         }
1392 
1393         if (ruleBoService instanceof RuleBoServiceImpl) {
1394             ((RuleBoServiceImpl) ruleBoService).setDataObjectService(dataObjectService);
1395         }
1396 
1397         if (actionBoService instanceof ActionBoServiceImpl) {
1398             ((ActionBoServiceImpl) actionBoService).setDataObjectService(dataObjectService);
1399         }
1400 
1401         if (propositionBoService instanceof PropositionBoServiceImpl) {
1402             ((PropositionBoServiceImpl) propositionBoService).setDataObjectService(dataObjectService);
1403         }
1404 
1405         if (contextBoService instanceof ContextBoServiceImpl) {
1406             ((ContextBoServiceImpl) contextBoService).setDataObjectService(dataObjectService);
1407         }
1408 
1409         if (referenceObjectBindingBoService instanceof ReferenceObjectBindingBoServiceImpl) {
1410             ((ReferenceObjectBindingBoServiceImpl) referenceObjectBindingBoService).setDataObjectService(
1411                     dataObjectService);
1412         }
1413 
1414         if (naturalLanguageUsageBoService instanceof NaturalLanguageUsageBoServiceImpl) {
1415             ((NaturalLanguageUsageBoServiceImpl) naturalLanguageUsageBoService).setDataObjectService(dataObjectService);
1416         }
1417 
1418         if (naturalLanguageTemplateBoService instanceof NaturalLanguageTemplateBoServiceImpl) {
1419             ((NaturalLanguageTemplateBoServiceImpl) naturalLanguageTemplateBoService).setDataObjectService(
1420                     dataObjectService);
1421         }
1422 
1423         if (termRepositoryService instanceof TermBoServiceImpl) {
1424             ((TermBoServiceImpl) termRepositoryService).setDataObjectService(dataObjectService);
1425         }
1426     }
1427 }