001    /*
002     * Copyright 2011 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.osedu.org/licenses/ECL-2.0
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     */
016    package org.kuali.rice.krms.impl.repository.mock;
017    
018    import java.util.ArrayList;
019    import java.util.Collection;
020    import java.util.LinkedHashMap;
021    import java.util.LinkedHashSet;
022    import java.util.List;
023    import java.util.Map;
024    import java.util.Set;
025    import java.util.UUID;
026    import org.kuali.rice.core.api.criteria.QueryByCriteria;
027    import org.kuali.rice.core.api.exception.RiceIllegalArgumentException;
028    import org.kuali.rice.core.api.exception.RiceIllegalStateException;
029    import org.kuali.rice.krms.api.repository.NaturalLanguageTree;
030    import org.kuali.rice.krms.api.repository.RuleManagementService;
031    import org.kuali.rice.krms.api.repository.action.ActionDefinition;
032    import org.kuali.rice.krms.api.repository.agenda.AgendaDefinition;
033    import org.kuali.rice.krms.api.repository.agenda.AgendaItemDefinition;
034    import org.kuali.rice.krms.api.repository.agenda.AgendaTreeDefinition;
035    import org.kuali.rice.krms.api.repository.context.ContextDefinition;
036    import org.kuali.rice.krms.api.repository.context.ContextSelectionCriteria;
037    import org.kuali.rice.krms.api.repository.language.NaturalLanguageTemplate;
038    import org.kuali.rice.krms.api.repository.language.NaturalLanguageTemplaterContract;
039    import org.kuali.rice.krms.api.repository.language.NaturalLanguageUsage;
040    import org.kuali.rice.krms.api.repository.proposition.PropositionDefinition;
041    import org.kuali.rice.krms.api.repository.proposition.PropositionParameter;
042    import org.kuali.rice.krms.api.repository.reference.ReferenceObjectBinding;
043    import org.kuali.rice.krms.api.repository.rule.RuleDefinition;
044    import org.kuali.rice.krms.api.repository.term.TermDefinition;
045    import org.kuali.rice.krms.api.repository.term.TermRepositoryService;
046    import org.kuali.rice.krms.impl.repository.TranslationUtility;
047    import org.kuali.rice.krms.impl.repository.language.SimpleNaturalLanguageTemplater;
048    
049    public class RuleManagementServiceMockImpl implements RuleManagementService {
050        
051        
052        // cache variable 
053        // The LinkedHashMap is just so the values come back in a predictable order
054    
055        private Map<String, ReferenceObjectBinding> referenceObjectBindingMap = new LinkedHashMap<String, ReferenceObjectBinding>();
056        private Map<String, ContextDefinition> contextMap = new LinkedHashMap<String, ContextDefinition>();
057        private Map<String, AgendaDefinition> agendaMap = new LinkedHashMap<String, AgendaDefinition>();
058        private Map<String, AgendaItemDefinition> agendaItemMap = new LinkedHashMap<String, AgendaItemDefinition>();
059        private Map<String, RuleDefinition> ruleMap = new LinkedHashMap<String, RuleDefinition>();
060        private Map<String, ActionDefinition> actionMap = new LinkedHashMap<String, ActionDefinition>();
061        private Map<String, PropositionDefinition> propositionMap = new LinkedHashMap<String, PropositionDefinition>();
062        private Map<String, NaturalLanguageUsage> naturalLanguageUsageMap = new LinkedHashMap<String, NaturalLanguageUsage>();
063        private Map<String, NaturalLanguageTemplate> naturalLanguageTemplateMap = new LinkedHashMap<String, NaturalLanguageTemplate>();
064        
065        // supporting services used in this service impl
066        private NaturalLanguageTemplaterContract templater = new SimpleNaturalLanguageTemplater();
067        private TermRepositoryService termRepositoryService;
068        
069        public NaturalLanguageTemplaterContract getTemplater() {
070            return templater;
071        }
072    
073        public void setTemplater(NaturalLanguageTemplaterContract templater) {
074            this.templater = templater;
075        }
076    
077        public TermRepositoryService getTermRepositoryService() {
078            return termRepositoryService;
079        }
080    
081        public void setTermRepositoryService(TermRepositoryService termRepositoryService) {
082            this.termRepositoryService = termRepositoryService;
083        }
084    
085        
086        
087        public void clear() {
088            this.referenceObjectBindingMap.clear();
089            this.contextMap.clear();
090            this.agendaMap.clear();
091            this.agendaItemMap.clear();
092            this.ruleMap.clear();
093            this.actionMap.clear();
094            this.propositionMap.clear();
095            this.naturalLanguageUsageMap.clear();
096            this.naturalLanguageTemplateMap.clear();
097        }
098    
099        @Override
100        public ReferenceObjectBinding createReferenceObjectBinding(ReferenceObjectBinding referenceObjectDefinition)
101                throws RiceIllegalArgumentException {
102            // CREATE
103            ReferenceObjectBinding orig = this.getReferenceObjectBinding(referenceObjectDefinition.getId());
104            if (orig != null) {
105                throw new RiceIllegalArgumentException(referenceObjectDefinition.getId());
106            }
107            ReferenceObjectBinding.Builder copy = ReferenceObjectBinding.Builder.create(referenceObjectDefinition);
108            if (copy.getId() == null) {
109                copy.setId(UUID.randomUUID().toString());
110            }
111            referenceObjectDefinition = copy.build();
112            referenceObjectBindingMap.put(referenceObjectDefinition.getId(), referenceObjectDefinition);
113            return referenceObjectDefinition;
114        }
115    
116        @Override
117        public ReferenceObjectBinding getReferenceObjectBinding(String id)
118                throws RiceIllegalArgumentException {
119            // GET_BY_ID
120            if (!this.referenceObjectBindingMap.containsKey(id)) {
121                throw new RiceIllegalArgumentException(id);
122            }
123            return this.referenceObjectBindingMap.get(id);
124        }
125    
126        @Override
127        public List<ReferenceObjectBinding> getReferenceObjectBindings(List<String> ids)
128                throws RiceIllegalArgumentException {
129            // GET_BY_IDS
130            List<ReferenceObjectBinding> list = new ArrayList<ReferenceObjectBinding>();
131            for (String id : ids) {
132                list.add(this.getReferenceObjectBinding(id));
133            }
134            return list;
135        }
136    
137        @Override
138        public List<ReferenceObjectBinding> findReferenceObjectBindingsByReferenceDiscriminatorType(String referenceObjectReferenceDiscriminatorType)
139                throws RiceIllegalArgumentException {
140            List<ReferenceObjectBinding> list = new ArrayList<ReferenceObjectBinding>();
141            for (ReferenceObjectBinding info : this.referenceObjectBindingMap.values()) {
142                if (info.getReferenceDiscriminatorType().equals(referenceObjectReferenceDiscriminatorType)) {
143                    list.add(info);
144                }
145            }
146            return list;
147        }
148    
149        @Override
150        public List<ReferenceObjectBinding> findReferenceObjectBindingsByReferenceObject(String referenceObjectReferenceDiscriminatorType, String referenceObjectId) throws RiceIllegalArgumentException {
151    
152            List<ReferenceObjectBinding> list = new ArrayList<ReferenceObjectBinding>();
153            for (ReferenceObjectBinding info : this.referenceObjectBindingMap.values()) {
154                if (info.getReferenceDiscriminatorType().equals(referenceObjectReferenceDiscriminatorType)) {
155                    list.add(info);
156                }
157            }
158            return list;
159        }
160    
161        @Override
162        public List<ReferenceObjectBinding> findReferenceObjectBindingsByKrmsDiscriminatorType(String referenceObjectKrmsDiscriminatorType)
163                throws RiceIllegalArgumentException {
164            List<ReferenceObjectBinding> list = new ArrayList<ReferenceObjectBinding>();
165            for (ReferenceObjectBinding info : this.referenceObjectBindingMap.values()) {
166                if (info.getKrmsDiscriminatorType().equals(referenceObjectKrmsDiscriminatorType)) {
167                    list.add(info);
168                }
169            }
170            return list;
171        }
172    
173        @Override
174        public List<ReferenceObjectBinding> findReferenceObjectBindingsByKrmsObject(String krmsObjectId)
175                throws RiceIllegalArgumentException {
176            List<ReferenceObjectBinding> list = new ArrayList<ReferenceObjectBinding>();
177            for (ReferenceObjectBinding info : this.referenceObjectBindingMap.values()) {
178                if (info.getKrmsObjectId().equals(krmsObjectId)) {
179                    list.add(info);
180                }
181            }
182            return list;
183        }
184    
185        @Override
186        public void updateReferenceObjectBinding(ReferenceObjectBinding referenceObjectBindingDefinition)
187                throws RiceIllegalArgumentException {
188            // UPDATE
189            ReferenceObjectBinding.Builder copy = ReferenceObjectBinding.Builder.create(referenceObjectBindingDefinition);
190            ReferenceObjectBinding old = this.getReferenceObjectBinding(referenceObjectBindingDefinition.getId());
191            if (!old.getVersionNumber().equals(copy.getVersionNumber())) {
192                throw new RiceIllegalStateException("" + old.getVersionNumber());
193            }
194            copy.setVersionNumber(copy.getVersionNumber() + 1);
195            referenceObjectBindingDefinition = copy.build();
196            this.referenceObjectBindingMap.put(referenceObjectBindingDefinition.getId(), referenceObjectBindingDefinition);
197            return;
198        }
199    
200        @Override
201        public void deleteReferenceObjectBinding(String id)
202                throws RiceIllegalArgumentException {
203            // DELETE
204            if (this.referenceObjectBindingMap.remove(id) == null) {
205                throw new RiceIllegalArgumentException(id);
206            }
207            return;
208        }
209    
210        @Override
211        public List<String> findReferenceObjectBindingIds(QueryByCriteria queryByCriteria)
212                throws RiceIllegalArgumentException {
213            CriteriaMatcherInMemory<ReferenceObjectBinding> instance = new CriteriaMatcherInMemory<ReferenceObjectBinding>();
214            instance.setCriteria(queryByCriteria);
215            Collection<ReferenceObjectBinding> selected = instance.findMatching(this.referenceObjectBindingMap.values());
216            List<String> list = new ArrayList<String>();
217            for (ReferenceObjectBinding sel : selected) {
218                list.add(sel.getId());
219            }
220            return list;
221        }
222    
223        @Override
224        public AgendaDefinition createAgenda(AgendaDefinition agendaDefinition)
225                throws RiceIllegalArgumentException {
226            // CREATE
227            if (agendaDefinition.getId() != null) {
228                AgendaDefinition orig = this.getAgenda(agendaDefinition.getId());
229                if (orig != null) {
230                    throw new RiceIllegalArgumentException(agendaDefinition.getId() + "." + agendaDefinition.getName());
231                }
232            }
233            AgendaDefinition.Builder copy = AgendaDefinition.Builder.create(agendaDefinition);
234            if (copy.getId() == null) {
235                copy.setId(UUID.randomUUID().toString());
236            }
237            copy.setVersionNumber(0l);
238            agendaDefinition = copy.build();
239            agendaMap.put(agendaDefinition.getId(), agendaDefinition);
240            return agendaDefinition;
241        }
242    
243        @Override
244        public AgendaDefinition getAgenda(String id)
245                throws RiceIllegalArgumentException {
246            // GET_BY_ID
247            if (!this.agendaMap.containsKey(id)) {
248                throw new RiceIllegalArgumentException(id);
249            }
250            return this.agendaMap.get(id);
251        }
252    
253        @Override
254        public List<AgendaDefinition> getAgendasByType(String typeId)
255                throws RiceIllegalArgumentException {
256            // GET_IDS_BY_TYPE
257            List<AgendaDefinition> list = new ArrayList<AgendaDefinition>();
258            for (AgendaDefinition info : agendaMap.values()) {
259                if (typeId.equals(info.getTypeId())) {
260                    list.add(info);
261                }
262            }
263            return list;
264        }
265    
266        @Override
267        public List<AgendaDefinition> getAgendasByContext(String contextId)
268                throws RiceIllegalArgumentException {
269            List<AgendaDefinition> list = new ArrayList<AgendaDefinition>();
270            for (AgendaDefinition info : this.agendaMap.values()) {
271                if (info.getContextId().equals(contextId)) {
272                    list.add(info);
273                }
274            }
275            return list;
276        }
277    
278        @Override
279        public List<AgendaDefinition> getAgendasByTypeAndContext(String typeId, String contextId)
280                throws RiceIllegalArgumentException {
281            List<AgendaDefinition> list = new ArrayList<AgendaDefinition>();
282            for (AgendaDefinition info : this.agendaMap.values()) {
283                if (info.getContextId().equals(contextId)) {
284                    if (info.getTypeId().equals(typeId)) {
285                        list.add(info);
286                    }
287                }
288            }
289            return list;
290        }
291    
292        @Override
293        public void updateAgenda(AgendaDefinition agendaDefinition)
294                throws RiceIllegalArgumentException {
295            // UPDATE
296            AgendaDefinition.Builder copy = AgendaDefinition.Builder.create(agendaDefinition);
297            AgendaDefinition old = this.getAgenda(agendaDefinition.getId());
298            if (!old.getVersionNumber().equals(copy.getVersionNumber())) {
299                throw new RiceIllegalStateException("" + old.getVersionNumber());
300            }
301            copy.setVersionNumber(copy.getVersionNumber() + 1);
302            agendaDefinition = copy.build();
303            this.agendaMap.put(agendaDefinition.getId(), agendaDefinition);
304            return;
305        }
306    
307        @Override
308        public void deleteAgenda(String id)
309                throws RiceIllegalArgumentException {
310            // DELETE
311            if (this.agendaMap.remove(id) == null) {
312                throw new RiceIllegalArgumentException(id);
313            }
314            return;
315        }
316    
317        @Override
318        public AgendaItemDefinition createAgendaItem(AgendaItemDefinition agendaItemDefinition)
319                throws RiceIllegalArgumentException {
320            // CREATE
321            AgendaItemDefinition.Builder copy = AgendaItemDefinition.Builder.create(agendaItemDefinition);
322            if (copy.getId() == null) {
323                copy.setId(UUID.randomUUID().toString());
324            }
325            agendaItemDefinition = copy.build();
326            agendaItemMap.put(agendaItemDefinition.getId(), agendaItemDefinition);
327            return agendaItemDefinition;
328        }
329    
330        @Override
331        public AgendaItemDefinition getAgendaItem(String id)
332                throws RiceIllegalArgumentException {
333            // GET_BY_ID
334            if (!this.agendaItemMap.containsKey(id)) {
335                throw new RiceIllegalArgumentException(id);
336            }
337            return this.agendaItemMap.get(id);
338        }
339    
340        @Override
341        public List<AgendaItemDefinition> getAgendaItemsByType(String typeId)
342                throws RiceIllegalArgumentException {
343            // GET_IDS_BY_TYPE
344            List<AgendaDefinition> agendas = this.getAgendasByType(typeId);
345            List<AgendaItemDefinition> list = new ArrayList<AgendaItemDefinition>();
346            for (AgendaDefinition agenda : agendas) {
347                for (AgendaItemDefinition info : agendaItemMap.values()) {
348                    if (agenda.getId().equals(info.getAgendaId())) {
349                        list.add(info);
350                    }
351                }
352            }
353            return list;
354        }
355    
356        @Override
357        public List<AgendaItemDefinition> getAgendaItemsByContext(String contextId)
358                throws RiceIllegalArgumentException {
359            List<AgendaDefinition> agendas = this.getAgendasByContext(contextId);
360            List<AgendaItemDefinition> list = new ArrayList<AgendaItemDefinition>();
361            for (AgendaDefinition agenda : agendas) {
362                for (AgendaItemDefinition info : agendaItemMap.values()) {
363                    if (agenda.getId().equals(info.getAgendaId())) {
364                        list.add(info);
365                    }
366                }
367            }
368            return list;
369        }
370    
371        @Override
372        public List<AgendaItemDefinition> getAgendaItemsByTypeAndContext(String typeId, String contextId)
373                throws RiceIllegalArgumentException {
374            List<AgendaDefinition> agendas = this.getAgendasByContext(contextId);
375            List<AgendaItemDefinition> list = new ArrayList<AgendaItemDefinition>();
376            for (AgendaDefinition agenda : agendas) {
377                if (agenda.getTypeId().equals(typeId)) {
378                    for (AgendaItemDefinition info : agendaItemMap.values()) {
379                        if (agenda.getId().equals(info.getAgendaId())) {
380                            list.add(info);
381                        }
382                    }
383                }
384            }
385            return list;
386        }
387    
388        @Override
389        public void updateAgendaItem(AgendaItemDefinition agendaItemDefinition)
390                throws RiceIllegalArgumentException {
391            // UPDATE
392            AgendaItemDefinition.Builder copy = AgendaItemDefinition.Builder.create(agendaItemDefinition);
393            AgendaItemDefinition old = this.getAgendaItem(agendaItemDefinition.getId());
394            if (!old.getVersionNumber().equals(copy.getVersionNumber())) {
395                throw new RiceIllegalStateException("" + old.getVersionNumber());
396            }
397            copy.setVersionNumber(copy.getVersionNumber() + 1);
398            agendaItemDefinition = copy.build();
399            this.agendaItemMap.put(agendaItemDefinition.getId(), agendaItemDefinition);
400            return;
401        }
402    
403        @Override
404        public void deleteAgendaItem(String id)
405                throws RiceIllegalArgumentException {
406            // DELETE
407            if (this.agendaItemMap.remove(id) == null) {
408                throw new RiceIllegalArgumentException(id);
409            }
410            return;
411        }
412    
413        @Override
414        public RuleDefinition createRule(RuleDefinition ruleDefinition)
415                throws RiceIllegalArgumentException {
416            // CREATE
417            if (ruleDefinition.getId() != null) {
418                RuleDefinition orig = this.getRule(ruleDefinition.getId());
419                if (orig != null) {
420                    throw new RiceIllegalArgumentException(ruleDefinition.getId());
421                }
422            }
423            RuleDefinition.Builder copy = RuleDefinition.Builder.create(ruleDefinition);
424            if (copy.getId() == null) {
425                copy.setId(UUID.randomUUID().toString());
426            }
427            ruleDefinition = copy.build();
428            ruleMap.put(ruleDefinition.getId(), ruleDefinition);
429            return ruleDefinition;
430        }
431    
432        @Override
433        public RuleDefinition getRule(String ruleId) {
434            // GET_BY_ID
435            if (!this.ruleMap.containsKey(ruleId)) {
436                throw new RiceIllegalArgumentException(ruleId);
437            }
438            return this.ruleMap.get(ruleId);
439        }
440    
441        @Override
442        public List<RuleDefinition> getRules(List<String> ruleIds) {
443            List<RuleDefinition> list = new ArrayList<RuleDefinition>();
444            for (String id : ruleIds) {
445                list.add(this.getRule(id));
446            }
447            return list;
448        }
449    
450        @Override
451        public void updateRule(RuleDefinition ruleDefinition)
452                throws RiceIllegalArgumentException {
453            // UPDATE
454            RuleDefinition.Builder copy = RuleDefinition.Builder.create(ruleDefinition);
455            RuleDefinition old = this.getRule(ruleDefinition.getId());
456            if (!old.getVersionNumber().equals(copy.getVersionNumber())) {
457                throw new RiceIllegalStateException("" + old.getVersionNumber());
458            }
459            copy.setVersionNumber(copy.getVersionNumber() + 1);
460            ruleDefinition = copy.build();
461            this.ruleMap.put(ruleDefinition.getId(), ruleDefinition);
462            return;
463        }
464    
465        @Override
466        public void deleteRule(String id)
467                throws RiceIllegalArgumentException {
468            // DELETE
469            if (this.ruleMap.remove(id) == null) {
470                throw new RiceIllegalArgumentException(id);
471            }
472            return;
473        }
474    
475        @Override
476        public ActionDefinition createAction(ActionDefinition actionDefinition)
477                throws RiceIllegalArgumentException {
478            // CREATE
479            ActionDefinition orig = this.getAction(actionDefinition.getId());
480            if (orig != null) {
481                throw new RiceIllegalArgumentException(actionDefinition.getId());
482            }
483            ActionDefinition.Builder copy = ActionDefinition.Builder.create(actionDefinition);
484            if (copy.getId() == null) {
485                copy.setId(UUID.randomUUID().toString());
486            }
487            actionDefinition = copy.build();
488            actionMap.put(actionDefinition.getId(), actionDefinition);
489            return actionDefinition;
490        }
491    
492        @Override
493        public ActionDefinition getAction(String actionId) {
494            // GET_BY_ID
495            if (!this.actionMap.containsKey(actionId)) {
496                throw new RiceIllegalArgumentException(actionId);
497            }
498            return this.actionMap.get(actionId);
499        }
500    
501        @Override
502        public List<ActionDefinition> getActions(List<String> actionIds) {
503            List<ActionDefinition> list = new ArrayList<ActionDefinition>();
504            for (String id : actionIds) {
505                list.add(this.getAction(id));
506            }
507            return list;
508        }
509    
510        @Override
511        public void updateAction(ActionDefinition actionDefinition)
512                throws RiceIllegalArgumentException {
513            // UPDATE
514            ActionDefinition.Builder copy = ActionDefinition.Builder.create(actionDefinition);
515            ActionDefinition old = this.getAction(actionDefinition.getId());
516            if (!old.getVersionNumber().equals(copy.getVersionNumber())) {
517                throw new RiceIllegalStateException("" + old.getVersionNumber());
518            }
519            copy.setVersionNumber(copy.getVersionNumber() + 1);
520            actionDefinition = copy.build();
521            this.actionMap.put(actionDefinition.getId(), actionDefinition);
522            return;
523        }
524    
525        @Override
526        public void deleteAction(String id)
527                throws RiceIllegalArgumentException {
528            // DELETE
529            if (this.actionMap.remove(id) == null) {
530                throw new RiceIllegalArgumentException(id);
531            }
532            return;
533        }
534    
535        @Override
536        public PropositionDefinition createProposition(PropositionDefinition propositionDefinition)
537                throws RiceIllegalArgumentException {
538            // CREATE
539            if (propositionDefinition.getId() != null) {
540                PropositionDefinition orig = this.getProposition(propositionDefinition.getId());
541                if (orig != null) {
542                    throw new RiceIllegalArgumentException(propositionDefinition.getId());
543                }
544            }
545            PropositionDefinition.Builder copy = PropositionDefinition.Builder.create(propositionDefinition);
546            if (copy.getId() == null) {
547                copy.setId(UUID.randomUUID().toString());
548            }
549            for (PropositionParameter.Builder paramBldr : copy.getParameters()) {
550                if (paramBldr.getId() == null) {
551                    paramBldr.setId(UUID.randomUUID().toString());
552                }
553                if (paramBldr.getPropId() == null) {
554                    paramBldr.setPropId(copy.getId());
555                }
556                if (paramBldr.getTermValue() != null) {
557                    TermDefinition termValue = paramBldr.getTermValue();
558                    // no id means it does not exist yet
559                    if (termValue.getId() == null) {
560                        termValue = this.termRepositoryService.createTerm(termValue);
561                        paramBldr.setTermValue(termValue);
562                    }
563                    if (paramBldr.getValue() == null) {
564                        paramBldr.setValue(termValue.getId());
565                    }
566                }
567            }
568            propositionDefinition = copy.build();
569            propositionMap.put(propositionDefinition.getId(), propositionDefinition);
570            return propositionDefinition;
571        }
572    
573        @Override
574        public PropositionDefinition getProposition(String id)
575                throws RiceIllegalArgumentException {
576            // GET_BY_ID
577            if (!this.propositionMap.containsKey(id)) {
578                throw new RiceIllegalArgumentException(id);
579            }
580            return this.propositionMap.get(id);
581        }
582    
583        @Override
584        public Set<PropositionDefinition> getPropositionsByType(String typeId)
585                throws RiceIllegalArgumentException {
586            // GET_IDS_BY_TYPE
587            Set<PropositionDefinition> set = new LinkedHashSet<PropositionDefinition>();
588            for (PropositionDefinition info : propositionMap.values()) {
589                if (typeId.equals(info.getTypeId())) {
590                    set.add(info);
591                }
592            }
593            return set;
594        }
595    
596        @Override
597        public Set<PropositionDefinition> getPropositionsByRule(String ruleId)
598                throws RiceIllegalArgumentException {
599            Set<PropositionDefinition> set = new LinkedHashSet<PropositionDefinition>();
600            for (PropositionDefinition info : this.propositionMap.values()) {
601                if (info.getRuleId().equals(ruleId)) {
602                    set.add(info);
603                }
604            }
605            return set;
606        }
607    
608        @Override
609        public void updateProposition(PropositionDefinition propositionDefinition)
610                throws RiceIllegalArgumentException {
611            if (this.propositionMap.containsKey(propositionDefinition.getId())) {
612                throw new RiceIllegalArgumentException (propositionDefinition.getId() + "not found");
613            }
614            // UPDATE
615            PropositionDefinition.Builder copy = PropositionDefinition.Builder.create(propositionDefinition);
616            PropositionDefinition old = this.getProposition(propositionDefinition.getId());
617            if (!old.getVersionNumber().equals(copy.getVersionNumber())) {
618                throw new RiceIllegalStateException("" + old.getVersionNumber());
619            }
620            copy.setVersionNumber(copy.getVersionNumber() + 1);
621            for (PropositionParameter.Builder paramBldr : copy.getParameters()) {
622                if (paramBldr.getId() == null) {
623                    paramBldr.setId(UUID.randomUUID().toString());
624                }
625                if (paramBldr.getPropId() == null) {
626                    paramBldr.setPropId(copy.getId());
627                }
628                if (paramBldr.getTermValue() != null) {
629                    TermDefinition termValue = paramBldr.getTermValue();
630                    // no id means it does not exist yet
631                    if (termValue.getId() == null) {
632                        termValue = this.termRepositoryService.createTerm(termValue);
633                        paramBldr.setTermValue(termValue);
634                    }
635                    if (paramBldr.getValue() == null) {
636                        paramBldr.setValue(termValue.getId());
637                    }
638                }
639            }
640            propositionDefinition = copy.build();
641            this.propositionMap.put(propositionDefinition.getId(), propositionDefinition);
642            return;
643        }
644    
645        @Override
646        public void deleteProposition(String id)
647                throws RiceIllegalArgumentException {
648            // DELETE
649            if (this.propositionMap.remove(id) == null) {
650                throw new RiceIllegalArgumentException(id);
651            }
652            return;
653        }
654    
655        @Override
656        public NaturalLanguageUsage createNaturalLanguageUsage(NaturalLanguageUsage naturalLanguageUsage)
657                throws RiceIllegalArgumentException {
658            // CREATE
659            try {
660                NaturalLanguageUsage orig = this.getNaturalLanguageUsage(naturalLanguageUsage.getId());
661                if (orig != null) {
662                    throw new RiceIllegalArgumentException(naturalLanguageUsage.getId());
663                }
664            } catch (RiceIllegalArgumentException ex) {
665    //            same as returning null
666            }
667            NaturalLanguageUsage.Builder copy = NaturalLanguageUsage.Builder.create(naturalLanguageUsage);
668            if (copy.getId() == null) {
669                copy.setId(UUID.randomUUID().toString());
670            }
671            naturalLanguageUsage = copy.build();
672            naturalLanguageUsageMap.put(naturalLanguageUsage.getId(), naturalLanguageUsage);
673            return naturalLanguageUsage;
674        }
675    
676        @Override
677        public NaturalLanguageUsage getNaturalLanguageUsage(String id)
678                throws RiceIllegalArgumentException {
679            // GET_BY_ID
680            if (!this.naturalLanguageUsageMap.containsKey(id)) {
681                throw new RiceIllegalArgumentException(id);
682            }
683            return this.naturalLanguageUsageMap.get(id);
684        }
685    
686        @Override
687        public void updateNaturalLanguageUsage(NaturalLanguageUsage naturalLanguageUsage)
688                throws RiceIllegalArgumentException {
689            // UPDATE
690            NaturalLanguageUsage.Builder copy = NaturalLanguageUsage.Builder.create(naturalLanguageUsage);
691            NaturalLanguageUsage old = this.getNaturalLanguageUsage(naturalLanguageUsage.getId());
692            if (!old.getVersionNumber().equals(copy.getVersionNumber())) {
693                throw new RiceIllegalStateException("" + old.getVersionNumber());
694            }
695            copy.setVersionNumber(copy.getVersionNumber() + 1);
696            naturalLanguageUsage = copy.build();
697            this.naturalLanguageUsageMap.put(naturalLanguageUsage.getId(), naturalLanguageUsage);
698            return;
699        }
700    
701        @Override
702        public void deleteNaturalLanguageUsage(String naturalLanguageUsageId)
703                throws RiceIllegalArgumentException {
704            // DELETE
705            if (this.naturalLanguageUsageMap.remove(naturalLanguageUsageId) == null) {
706                throw new RiceIllegalArgumentException(naturalLanguageUsageId);
707            }
708            return;
709        }
710    
711        ////
712        //// natural language translations
713        ////
714        @Override
715        public String translateNaturalLanguageForObject(String naturalLanguageUsageId,
716                String typeId,
717                String krmsObjectId,
718                String languageCode)
719                throws RiceIllegalArgumentException {
720            TranslationUtility util = new TranslationUtility(this, this.templater);
721            return util.translateNaturalLanguageForObject(naturalLanguageUsageId, typeId, krmsObjectId, languageCode);
722        }
723    
724        @Override
725        public String translateNaturalLanguageForProposition(String naturalLanguageUsageId,
726                PropositionDefinition proposition, String languageCode)
727                throws RiceIllegalArgumentException {
728            TranslationUtility util = new TranslationUtility(this, this.templater);
729            return util.translateNaturalLanguageForProposition(naturalLanguageUsageId, proposition, languageCode);
730        }
731    
732        @Override
733        public NaturalLanguageTree translateNaturalLanguageTreeForProposition(String naturalLanguageUsageId,
734                PropositionDefinition propositionDefinintion,
735                String languageCode) throws RiceIllegalArgumentException {
736            TranslationUtility util = new TranslationUtility(this, this.templater);
737            return util.translateNaturalLanguageTreeForProposition(naturalLanguageUsageId, propositionDefinintion, languageCode);
738        }
739    
740        @Override
741        public List<NaturalLanguageUsage> getNaturalLanguageUsagesByNamespace(String namespace) throws RiceIllegalArgumentException {
742            List<NaturalLanguageUsage> list = new ArrayList<NaturalLanguageUsage>();
743            for (NaturalLanguageUsage info : naturalLanguageUsageMap.values()) {
744                if (namespace.equals(info.getNamespace())) {
745                    list.add(info);
746                }
747            }
748            return list;
749        }
750    
751        @Override
752        public NaturalLanguageUsage getNaturalLanguageUsageByNameAndNamespace(String name, String namespace) throws RiceIllegalArgumentException {
753            for (NaturalLanguageUsage info : naturalLanguageUsageMap.values()) {
754                if (namespace.equals(info.getNamespace())) {
755                    if (name.equals(info.getName())) {
756                        return info;
757                    }
758                }
759            }
760            return null;
761        }
762    
763        @Override
764        public ContextDefinition selectContext(ContextSelectionCriteria contextSelectionCriteria) throws RiceIllegalArgumentException {
765            throw new UnsupportedOperationException("Not supported yet.");
766        }
767    
768        @Override
769        public AgendaTreeDefinition getAgendaTree(String agendaId) throws RiceIllegalArgumentException {
770            throw new UnsupportedOperationException("Not supported yet.");
771        }
772    
773        @Override
774        public List<AgendaTreeDefinition> getAgendaTrees(List<String> agendaIds) throws RiceIllegalArgumentException {
775            throw new UnsupportedOperationException("Not supported yet.");
776        }
777    
778        @Override
779        public ContextDefinition createContext(ContextDefinition contextDefinition)
780                throws RiceIllegalArgumentException {
781            // CREATE
782            ContextDefinition orig = this.getContextByNameAndNamespace(contextDefinition.getName(), contextDefinition.getNamespace());
783            if (orig != null) {
784                throw new RiceIllegalArgumentException(contextDefinition.getNamespace() + "." + contextDefinition.getName());
785            }
786            ContextDefinition.Builder copy = ContextDefinition.Builder.create(contextDefinition);
787            if (copy.getId() == null) {
788                copy.setId(UUID.randomUUID().toString());
789            }
790            contextDefinition = copy.build();
791            contextMap.put(contextDefinition.getId(), contextDefinition);
792            return contextDefinition;
793        }
794    
795        @Override
796        public ContextDefinition findCreateContext(ContextDefinition contextDefinition) throws RiceIllegalArgumentException {
797            ContextDefinition orig = this.getContextByNameAndNamespace(contextDefinition.getName(), contextDefinition.getNamespace());
798            if (orig != null) {
799                return orig;
800            }
801            return this.createContext(contextDefinition);
802        }
803    
804        @Override
805        public void updateContext(ContextDefinition contextDefinition)
806                throws RiceIllegalArgumentException {
807            // UPDATE
808            ContextDefinition.Builder copy = ContextDefinition.Builder.create(contextDefinition);
809            ContextDefinition old = this.getContext(contextDefinition.getId());
810            if (!old.getVersionNumber().equals(copy.getVersionNumber())) {
811                throw new RiceIllegalStateException("" + old.getVersionNumber());
812            }
813            copy.setVersionNumber(copy.getVersionNumber() + 1);
814            contextDefinition = copy.build();
815            this.contextMap.put(contextDefinition.getId(), contextDefinition);
816            return;
817        }
818    
819        @Override
820        public void deleteContext(String id)
821                throws RiceIllegalArgumentException {
822            // DELETE
823            if (this.contextMap.remove(id) == null) {
824                throw new RiceIllegalArgumentException(id);
825            }
826            return;
827        }
828    
829        @Override
830        public ContextDefinition getContext(String id)
831                throws RiceIllegalArgumentException {
832            // GET_BY_ID
833            if (!this.contextMap.containsKey(id)) {
834                throw new RiceIllegalArgumentException(id);
835            }
836            return this.contextMap.get(id);
837        }
838    
839        @Override
840        public ContextDefinition getContextByNameAndNamespace(String name, String namespace)
841                throws RiceIllegalArgumentException {
842            if (name == null || name.trim().isEmpty()) {
843                throw new RiceIllegalArgumentException("name is null or empty");
844            }
845            if (namespace == null || namespace.trim().isEmpty()) {
846                throw new RiceIllegalArgumentException("name is null or empty");
847            }
848            // RICE_GET_BY_NAMESPACE_AND_NAME
849            for (ContextDefinition info : contextMap.values()) {
850                if (name.equals(info.getName())) {
851                    if (namespace.equals(info.getNamespace())) {
852                        return ContextDefinition.Builder.create(info).build();
853                    }
854                }
855            }
856            return null;
857        }
858    
859        @Override
860        public NaturalLanguageTemplate createNaturalLanguageTemplate(NaturalLanguageTemplate naturalLanguageTemplate)
861                throws RiceIllegalArgumentException {
862            // CREATE
863            try {
864                NaturalLanguageTemplate orig = this.getNaturalLanguageTemplate(naturalLanguageTemplate.getId());
865                if (orig != null) {
866                    throw new RiceIllegalArgumentException(naturalLanguageTemplate.getId());
867                }
868            } catch (RiceIllegalArgumentException ex) {
869                // same as getting a null
870            }
871            NaturalLanguageTemplate.Builder copy = NaturalLanguageTemplate.Builder.create(naturalLanguageTemplate);
872            if (copy.getId() == null) {
873                copy.setId(UUID.randomUUID().toString());
874            }
875            naturalLanguageTemplate = copy.build();
876            naturalLanguageTemplateMap.put(naturalLanguageTemplate.getId(), naturalLanguageTemplate);
877            return naturalLanguageTemplate;
878        }
879    
880        @Override
881        public NaturalLanguageTemplate getNaturalLanguageTemplate(String naturalLanguageTemplateId)
882                throws RiceIllegalArgumentException {
883            // GET_BY_ID
884            if (!this.naturalLanguageTemplateMap.containsKey(naturalLanguageTemplateId)) {
885                throw new RiceIllegalArgumentException(naturalLanguageTemplateId);
886            }
887            return this.naturalLanguageTemplateMap.get(naturalLanguageTemplateId);
888        }
889    
890        @Override
891        public void updateNaturalLanguageTemplate(NaturalLanguageTemplate naturalLanguageTemplate)
892                throws RiceIllegalArgumentException {
893            // UPDATE
894            NaturalLanguageTemplate.Builder copy = NaturalLanguageTemplate.Builder.create(naturalLanguageTemplate);
895            NaturalLanguageTemplate old = this.getNaturalLanguageTemplate(naturalLanguageTemplate.getId());
896            if (!old.getVersionNumber().equals(copy.getVersionNumber())) {
897                throw new RiceIllegalStateException("" + old.getVersionNumber());
898            }
899            copy.setVersionNumber(copy.getVersionNumber() + 1);
900            naturalLanguageTemplate = copy.build();
901            this.naturalLanguageTemplateMap.put(naturalLanguageTemplate.getId(), naturalLanguageTemplate);
902            return;
903        }
904    
905        @Override
906        public void deleteNaturalLanguageTemplate(String naturalLanguageTemplateId)
907                throws RiceIllegalArgumentException {
908            // DELETE
909            if (this.naturalLanguageTemplateMap.remove(naturalLanguageTemplateId) == null) {
910                throw new RiceIllegalArgumentException(naturalLanguageTemplateId);
911            }
912            return;
913        }
914    
915        @Override
916        public List<NaturalLanguageTemplate> findNaturalLanguageTemplatesByLanguageCode(String languageCode)
917                throws RiceIllegalArgumentException {
918            List<NaturalLanguageTemplate> list = new ArrayList<NaturalLanguageTemplate>();
919            for (NaturalLanguageTemplate nlt : this.naturalLanguageTemplateMap.values()) {
920                if (nlt.getLanguageCode().equals(languageCode)) {
921                    list.add(nlt);
922                }
923            }
924            return list;
925        }
926    
927        @Override
928        public NaturalLanguageTemplate findNaturalLanguageTemplateByLanguageCodeTypeIdAndNluId(String languageCode, String typeId, String naturalLanguageUsageId)
929                throws RiceIllegalArgumentException {
930            for (NaturalLanguageTemplate nlt : this.naturalLanguageTemplateMap.values()) {
931                if (nlt.getLanguageCode().equals(languageCode)) {
932                    if (nlt.getTypeId().equals(typeId)) {
933                        if (nlt.getNaturalLanguageUsageId().equals(naturalLanguageUsageId)) {
934                            return nlt;
935                        }
936                    }
937                }
938            }
939            return null;
940        }
941    
942        @Override
943        public List<NaturalLanguageTemplate> findNaturalLanguageTemplatesByNaturalLanguageUsage(String naturalLanguageUsageId)
944                throws RiceIllegalArgumentException {
945            List<NaturalLanguageTemplate> list = new ArrayList<NaturalLanguageTemplate>();
946            for (NaturalLanguageTemplate nlt : this.naturalLanguageTemplateMap.values()) {
947                if (nlt.getNaturalLanguageUsageId().equals(naturalLanguageUsageId)) {
948                    list.add(nlt);
949                }
950            }
951            return list;
952        }
953    
954        @Override
955        public List<NaturalLanguageTemplate> findNaturalLanguageTemplatesByType(String typeId)
956                throws RiceIllegalArgumentException {
957            List<NaturalLanguageTemplate> list = new ArrayList<NaturalLanguageTemplate>();
958            for (NaturalLanguageTemplate nlt : this.naturalLanguageTemplateMap.values()) {
959                if (nlt.getTypeId().equals(typeId)) {
960                    list.add(nlt);
961                }
962            }
963            return list;
964        }
965    
966        @Override
967        public List<NaturalLanguageTemplate> findNaturalLanguageTemplatesByTemplate(String template)
968                throws RiceIllegalArgumentException {
969            List<NaturalLanguageTemplate> list = new ArrayList<NaturalLanguageTemplate>();
970            for (NaturalLanguageTemplate nlt : this.naturalLanguageTemplateMap.values()) {
971                if (nlt.getTemplate().equals(template)) {
972                    list.add(nlt);
973                }
974            }
975            return list;
976        }
977    
978        @Override
979        public List<String> findContextIds(QueryByCriteria queryByCriteria) throws RiceIllegalArgumentException {
980            CriteriaMatcherInMemory<ContextDefinition> instance = new CriteriaMatcherInMemory<ContextDefinition>();
981            instance.setCriteria(queryByCriteria);
982            Collection<ContextDefinition> selected = instance.findMatching(this.contextMap.values());
983            List<String> list = new ArrayList<String>();
984            for (ContextDefinition sel : selected) {
985                list.add(sel.getId());
986            }
987            return list;
988        }
989    
990        @Override
991        public List<String> findAgendaIds(QueryByCriteria queryByCriteria) throws RiceIllegalArgumentException {
992            CriteriaMatcherInMemory<AgendaDefinition> instance = new CriteriaMatcherInMemory<AgendaDefinition>();
993            instance.setCriteria(queryByCriteria);
994            Collection<AgendaDefinition> selected = instance.findMatching(this.agendaMap.values());
995            List<String> list = new ArrayList<String>();
996            for (AgendaDefinition sel : selected) {
997                list.add(sel.getId());
998            }
999            return list;
1000        }
1001    
1002        @Override
1003        public List<String> findRuleIds(QueryByCriteria queryByCriteria) throws RiceIllegalArgumentException {
1004            CriteriaMatcherInMemory<RuleDefinition> instance = new CriteriaMatcherInMemory<RuleDefinition>();
1005            instance.setCriteria(queryByCriteria);
1006            Collection<RuleDefinition> selected = instance.findMatching(this.ruleMap.values());
1007            List<String> list = new ArrayList<String>();
1008            for (RuleDefinition sel : selected) {
1009                list.add(sel.getId());
1010            }
1011            return list;
1012        }
1013    
1014        @Override
1015        public List<String> findActionIds(QueryByCriteria queryByCriteria) throws RiceIllegalArgumentException {
1016            CriteriaMatcherInMemory<ActionDefinition> instance = new CriteriaMatcherInMemory<ActionDefinition>();
1017            instance.setCriteria(queryByCriteria);
1018            Collection<ActionDefinition> selected = instance.findMatching(this.actionMap.values());
1019            List<String> list = new ArrayList<String>();
1020            for (ActionDefinition sel : selected) {
1021                list.add(sel.getId());
1022            }
1023            return list;
1024        }
1025    
1026        @Override
1027        public List<String> findPropositionIds(QueryByCriteria queryByCriteria) throws RiceIllegalArgumentException {
1028            CriteriaMatcherInMemory<PropositionDefinition> instance = new CriteriaMatcherInMemory<PropositionDefinition>();
1029            instance.setCriteria(queryByCriteria);
1030            Collection<PropositionDefinition> selected = instance.findMatching(this.propositionMap.values());
1031            List<String> list = new ArrayList<String>();
1032            for (PropositionDefinition sel : selected) {
1033                list.add(sel.getId());
1034            }
1035            return list;
1036        }
1037    }