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