View Javadoc
1   package org.kuali.ole.ingest;
2   
3   import org.kuali.ole.OLEConstants;
4   import org.kuali.ole.ingest.pojo.*;
5   import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
6   import org.kuali.rice.kew.impl.peopleflow.PeopleFlowBo;
7   import org.kuali.rice.krad.service.BusinessObjectService;
8   import org.kuali.rice.krad.service.KRADServiceLocator;
9   import org.kuali.rice.krms.impl.repository.*;
10  
11  import java.io.IOException;
12  import java.net.URISyntaxException;
13  import java.util.*;
14  
15  /**
16   * KrmsBuilder will create context,category and rules for creating agenda and finally persists into database.
17   */
18  public class KrmsBuilder {
19  
20      private BusinessObjectService businessObjectService;
21      private TermBoService termBoService;
22      private KrmsObjectGeneratorFromXML krmsObjectGeneratorFromXML;
23      private ProfileBuilder profileBuilder = new ProfileBuilder();
24      private CategoryBo category;
25      private ContextBo context;
26      private AgendaBo agendaBo;
27      private RuleBo ruleBo;
28  
29      /**
30       * This method returns the contextBo object based on the namespace and contextName parameters.
31       * Creates a new object, stores it in database and returns if none exists, otherwise returns the existing object.
32       * @param namespace
33       * @param contextName
34       * @return  contextBo
35       */
36      public ContextBo createContext(String namespace, String contextName) {
37  
38          return profileBuilder.createContext(namespace,contextName);
39      }
40  
41      /**
42       * This method returns the Object value if it exists or else null value based on objectClass, key and value parameters.
43       * @param objectClass
44       * @param key
45       * @param value
46       * @return object
47       */
48      public Object objectExists(Class objectClass, String key, String value) {
49  
50          return profileBuilder.objectExists(objectClass,key,value);
51      }
52  
53      /**
54       * Returns the businessObjectService value
55       * @return businessObjectService
56       */
57      private BusinessObjectService getBusinessObjectService() {
58          if (null == businessObjectService) {
59              businessObjectService = KRADServiceLocator.getBusinessObjectService();
60          }
61          return businessObjectService;
62      }
63  
64      /**
65       * This method returns the categoryBo object based on the namespace and the categoryName parameters.
66       * Creates a new object, stores it in database and returns if none exists, otherwise returns the existing object.
67       * @param namespace
68       * @param categoryName
69       * @return categoryBo
70       */
71      public CategoryBo createCategory(String namespace, String categoryName) {
72  
73          return profileBuilder.createCategory(namespace,categoryName);
74      }
75  
76      /**
77       * This method save the DefaultFunctionLoader into the database.
78       */
79      public void registerDefaultFunctionLoader() {
80          profileBuilder.registerDefaultFunctionLoader();
81      }
82  
83      /**
84       * This method returns the agendaBo object based on the agendaName and contextBo parameters.
85       * Creates a new object, stores it in database and returns if none exists, otherwise returns the existing object.
86       * @param agendaName
87       * @param contextBo
88       * @return agendaBo
89       */
90      public AgendaBo createAgenda(String agendaName, ContextBo contextBo) {
91  
92          return profileBuilder.createAgenda(agendaName, contextBo);
93      }
94  
95      /**
96       * This method returns the termSpecificationBo object based on the namespace, termSpecificationName, type, categoryBoList and contextBoList parameters.
97       * Creates a new object, stores it in database and returns if none exists, otherwise returns the existing object.
98       * @param namespace
99       * @param termSpecificationName
100      * @param type
101      * @param categoryBoList
102      * @param contextBoList
103      * @return termSpecificationBo
104      */
105     public TermSpecificationBo createTermSpecification(String namespace, String termSpecificationName, String type, List<CategoryBo> categoryBoList, List<ContextBo> contextBoList) {
106 
107         return profileBuilder.createTermSpecification(namespace,termSpecificationName,type,categoryBoList,contextBoList);
108     }
109 
110     /**
111      * This method  returns the termBoService object through the GlobalResourceLoader class.
112      * @return termBoService
113      */
114     private TermBoService getTermBoService() {
115         if (null == termBoService) {
116             termBoService = GlobalResourceLoader.getService("termBoService");
117         }
118         return termBoService;
119     }
120 
121     /**
122      * This method returns the termBo object based on the name and termSpecificationBo parameters
123      * Creates a new object, stores it in database and returns if none exists, otherwise returns the existing object.
124      * @param name
125      * @param termSpecificationBo
126      * @return termBo
127      */
128     public TermBo createTerm(String name, TermSpecificationBo termSpecificationBo) {
129 
130         return profileBuilder.createTerm(name,termSpecificationBo);
131     }
132 
133     /**
134      * This method returns the ruleBo object based on the name and termSpecificationBo parameters
135      * Creates a new object, stores it in database and returns if none exists, otherwise returns the existing object.
136      * @param namespace
137      * @param ruleName
138      * @return ruleBo
139      */
140     public RuleBo createRule(String namespace, String ruleName) {
141 
142         return profileBuilder.createRule(namespace, ruleName);
143     }
144 
145     /**
146      * This method returns the agendaBo object based on the agendaBo, ruleBo and count parameters
147      * Creates a new object, stores it in database and returns if none exists, otherwise returns the existing object.
148      * @param agendaBo
149      * @param ruleBo
150      * @param count
151      * @return agendaBo
152      */
153     public AgendaBo addRuleToAgenda(AgendaBo agendaBo, RuleBo ruleBo,int count) {
154         AgendaItemBo agendaItemBo = new AgendaItemBo();
155         agendaItemBo.setAgendaId(agendaBo.getId());
156         agendaItemBo.setRule(ruleBo);
157         getBusinessObjectService().save(agendaItemBo);
158         List<AgendaItemBo> agendaBoItems = agendaBo.getItems();
159         if (null == agendaBoItems) {
160             agendaBoItems = new ArrayList<AgendaItemBo>();
161         }
162         agendaBoItems.add(agendaItemBo);
163         agendaBo.setItems(agendaBoItems);
164         agendaBo.setFirstItemId(agendaBoItems.get(0).getId());
165         if(count>-1){
166             AgendaItemBo updateAgendaItem = agendaBoItems.get(count);
167             updateAgendaItem.setAlways(agendaItemBo);
168             getBusinessObjectService().save(updateAgendaItem);
169         }
170         getBusinessObjectService().save(agendaBo);
171         return agendaBo;
172     }
173 
174     /**
175      * This method returns the propositionBo object based on the categoryId, propositionDescription, propositionType, rule and seqNum parameters
176      * Creates a new object, stores it in database and returns the object.
177      * @param categoryId
178      * @param propositionDescription
179      * @param propositionType
180      * @param rule
181      * @param seqNum
182      * @return propositionBo
183      */
184     public PropositionBo createProposition(String categoryId, String propositionDescription, String propositionType, RuleBo rule,Integer seqNum) {
185         PropositionBo propositionBo = new PropositionBo();
186         propositionBo.setCategoryId(categoryId);
187         propositionBo.setDescription(propositionDescription);
188         propositionBo.setRuleId(rule.getId());
189         propositionBo.setPropositionTypeCode(propositionType);
190         propositionBo.setCompoundSequenceNumber(seqNum);
191         getBusinessObjectService().save(propositionBo);
192         return propositionBo;
193     }
194 
195     /**
196      * This method returns the propositionBo object based on the categoryId, propositionDescription, propositionType, rule and operatorCode parameters
197      * Creates a new object, stores it in database and returns the object.
198      * @param categoryId
199      * @param propositionDescription
200      * @param propositionType
201      * @param rule
202      * @param operatorCode
203      * @return propositionBo
204      */
205     public PropositionBo createCompoundProposition(String categoryId, String propositionDescription, String propositionType, RuleBo rule,String operatorCode) {
206         PropositionBo propositionBo = new PropositionBo();
207         propositionBo.setCategoryId(categoryId);
208         propositionBo.setDescription(propositionDescription);
209         propositionBo.setRuleId(rule.getId());
210         propositionBo.setPropositionTypeCode(propositionType);
211         propositionBo.setCompoundOpCode(operatorCode);
212         getBusinessObjectService().save(propositionBo);
213         return propositionBo;
214     }
215 
216     /**
217      * This method returns the functionBo object based on the namespace, termName, returnType, categories and parameterType parameters
218      * Creates a new object, stores it in database and returns if none exists, otherwise returns the existing object.
219      * @param namespace
220      * @param termName
221      * @param returnType
222      * @param categories
223      * @param parameterType
224      * @return functionBo
225      */
226     public FunctionBo createFunction(String namespace, String termName, String returnType, List categories,String parameterType) {
227         Object existing = objectExists(FunctionBo.class, "nm", termName + "Function");
228         FunctionBo functionBo;
229         if (existing == null) {
230             HashMap<String, String> map = new HashMap<String, String>();
231             map.put("nm", "FunctionLoader");
232             List<KrmsTypeBo> matching =
233                     (List<KrmsTypeBo>) getBusinessObjectService().findMatching(KrmsTypeBo.class, map);
234             KrmsTypeBo krmsTypeBo = matching.get(0);
235             functionBo = new FunctionBo();
236             functionBo.setActive(true);
237             functionBo.setName(termName + "Function");
238             functionBo.setNamespace(namespace);
239             functionBo.setReturnType(returnType);
240             functionBo.setCategories(categories);
241             functionBo.setTypeId(krmsTypeBo.getId());
242             getBusinessObjectService().save(functionBo);
243 
244             FunctionParameterBo functionParameterBo = new FunctionParameterBo();
245             functionParameterBo.setFunctionId(functionBo.getId());
246             functionParameterBo.setParameterType(parameterType);
247             functionParameterBo.setName(termName);
248             functionParameterBo.setSequenceNumber(0);
249 
250             getBusinessObjectService().save(functionParameterBo);
251 
252             List<FunctionParameterBo> parameters = functionBo.getParameters();
253             if (null == parameters) {
254                 parameters = new ArrayList<FunctionParameterBo>();
255             }
256             parameters.add(functionParameterBo);
257             functionBo.setParameters(parameters);
258 
259             getBusinessObjectService().save(functionBo);
260         } else {
261             functionBo = (FunctionBo) existing;
262         }
263 
264         return functionBo;
265     }
266 
267     /**
268      * This method returns the PropositionBo object based on the termId and proposition parameters
269      * Creates a new object, stores it in database and returns the object.
270      * @param termId
271      * @param proposition
272      * @return  PropositionBo
273      */
274     public PropositionBo addTermToPropositionParameter(String termId, PropositionBo proposition) {
275 
276         return profileBuilder.addTermToPropositionParameter(termId,proposition);
277     }
278 
279     /**
280      * This method returns the PropositionBo object based on the operator and proposition parameters
281      * Creates a new object, stores it in database and returns the object.
282      * @param operator
283      * @param proposition
284      * @return  proposition
285      */
286     public PropositionBo addOperatorToPropositionParameter(String operator,PropositionBo proposition){
287         PropositionParameterBo propositionParameterBo = new PropositionParameterBo();
288         propositionParameterBo.setPropId(proposition.getId());
289         propositionParameterBo.setValue(operator);
290         propositionParameterBo.setSequenceNumber(2);
291         propositionParameterBo.setParameterType("O");
292         getBusinessObjectService().save(propositionParameterBo);
293         List<PropositionParameterBo> parameters = proposition.getParameters();
294         if (null == parameters) {
295             parameters = new ArrayList<PropositionParameterBo>();
296         }
297         parameters.add(propositionParameterBo);
298         proposition.setParameters(parameters);
299         getBusinessObjectService().save(proposition);
300         return proposition;
301     }
302 
303     /**
304      * This method returns the PropositionBo object based on the constant and proposition parameters
305      * Creates a new object, stores it in database and returns the object.
306      * @param constant
307      * @param proposition
308      * @return  proposition
309      */
310     public PropositionBo addConstantToPropositionParameter(String constant,PropositionBo proposition){
311         PropositionParameterBo propositionParameterBo = new PropositionParameterBo();
312         propositionParameterBo.setPropId(proposition.getId());
313         propositionParameterBo.setValue(constant);
314         propositionParameterBo.setSequenceNumber(1);
315         propositionParameterBo.setParameterType("C");
316         getBusinessObjectService().save(propositionParameterBo);
317         List<PropositionParameterBo> parameters = proposition.getParameters();
318         if (null == parameters) {
319             parameters = new ArrayList<PropositionParameterBo>();
320         }
321         parameters.add(propositionParameterBo);
322         proposition.setParameters(parameters);
323         getBusinessObjectService().save(proposition);
324         return proposition;
325     }
326 
327     /**
328      * This method returns the PropositionBo object based on the functionId and proposition parameters
329      * Creates a new object, stores it in database and returns the object.
330      * @param functionId
331      * @param proposition
332      * @return PropositionBo
333      */
334     public PropositionBo addCustomFunctionToPropositionParameter(String functionId, PropositionBo proposition) {
335 
336         return profileBuilder.addCustomFunctionToPropositionParameter(functionId,proposition);
337     }
338 
339     /**
340      * This method will create context,category and rules for creating agenda and finally persists into database.
341      * @param oleAgenda
342      * @return  agendaName
343      */
344     public String persist(OleAgenda oleAgenda) {
345         boolean agendaExists = doesKrmsExist(oleAgenda.getName());
346         if(agendaExists){
347             deleteAgenda(oleAgenda);
348         }
349         context = createContext(OLEConstants.OLE_NAMESPACE, oleAgenda.getContextName());
350         category = createCategory(OLEConstants.OLE_NAMESPACE, oleAgenda.getCategoryName());
351 
352         registerDefaultFunctionLoader();
353 
354         ContextValidActionBo contextValidActionBo = registerDefaultServiceTypes(context);
355 
356         agendaBo = createAgenda(oleAgenda.getName(), context);
357 
358         List<KrmsRule> rules = oleAgenda.getRules();
359         int count = -1;
360         for (Iterator<KrmsRule> iterator = rules.iterator(); iterator.hasNext(); ) {
361             KrmsRule rule = iterator.next();
362             ruleBo = createRule(OLEConstants.OLE_NAMESPACE, rule.getName());
363 
364             addRuleToAgenda(agendaBo, ruleBo,count);
365 
366             count++;
367             PropositionBo propositionBo =  null;
368 
369             if(rule.getProposition()!=null){
370                 propositionBo = createSimpleProposition(rule.getProposition(),1);
371             }
372             if(rule.getCompoundProposition()!=null){
373                 OleProposition oleProposition = rule.getCompoundProposition();
374                 List<PropositionBo> propositionBos = new ArrayList<PropositionBo>();
375                 propositionBos.addAll(createCompoundProposition(oleProposition, propositionBos));
376                 propositionBo = propositionBos.get(0);
377             }
378             ruleBo.setProposition(propositionBo);
379             getBusinessObjectService().save(ruleBo);
380             List<KrmsAction> trueActions =rule.getTrueActions()!=null && rule.getTrueActions().size()>0 ? rule.getTrueActions():new ArrayList<KrmsAction>();
381             for (Iterator<KrmsAction> oleActionIterator = trueActions.iterator(); oleActionIterator.hasNext(); ) {
382                 KrmsAction trueAction = oleActionIterator.next();
383                 contextValidActionBo = getContextValidActionBo(context,trueAction);
384                 ActionBo actionBo = addActionToRule(OLEConstants.OLE_NAMESPACE, trueAction.getName(), trueAction.getDescription(), contextValidActionBo.getActionTypeId(), ruleBo, trueAction.getSequenceNumber());
385                 addActionAttributes(actionBo,trueAction.getName());
386             }
387 
388             List<KrmsAction> falseActions = rule.getFalseActions()!=null && rule.getFalseActions().size()>0 ? rule.getFalseActions():new ArrayList<KrmsAction>();
389             for (Iterator<KrmsAction> oleActionIterator = falseActions.iterator(); oleActionIterator.hasNext(); ) {
390                 KrmsAction falseAction = oleActionIterator.next();
391                 RuleBo falseActionRuleBo = createRule(OLEConstants.OLE_NAMESPACE, rule.getName() + "_falseActionRule");
392                 addActionToRule(OLEConstants.OLE_NAMESPACE, falseAction.getName(), falseAction.getDescription(), contextValidActionBo.getActionTypeId(), falseActionRuleBo, falseAction.getSequenceNumber());
393                 addDummyRuleForFalseAction(ruleBo, falseActionRuleBo);
394             }
395 
396         }
397         persistProfileAttributes(oleAgenda);
398         return agendaBo.getName();
399     }
400 
401     /**
402      * This method creates compound proposition from the oleProposition and propositionBos parameters
403      * @param oleProposition
404      * @param propositionBos
405      */
406     private List<PropositionBo> createCompoundProposition(OleProposition oleProposition, List<PropositionBo> propositionBos){
407 
408         if(oleProposition.getPropositions()!=null){
409             List<PropositionBo> propositionBoList = new ArrayList<PropositionBo>();
410             propositionBoList.add(createCompoundProposition(oleProposition.getPropositions(), oleProposition.getPropositionType()));
411             return propositionBoList;
412         } else if(oleProposition.getOlePropositions()!=null){
413             PropositionBo propositionBo =  null;
414             List<PropositionBo> propositionBoList = new ArrayList<PropositionBo>();
415             for(OleProposition oleProposition1 : oleProposition.getOlePropositions()){
416                 propositionBoList.addAll(createCompoundProposition(oleProposition1, propositionBoList));
417             }
418             propositionBo =  createCompoundProposition(category.getId(),"compound","C",ruleBo,oleProposition.getPropositionType());
419             propositionBo.setCompoundComponents(propositionBoList);
420             getBusinessObjectService().save(propositionBo);
421             propositionBoList = new ArrayList<PropositionBo>();
422             propositionBoList.add(propositionBo);
423             return propositionBoList;
424         }
425         return null;
426     }
427 
428     /**
429      * This method adds the action attributes to create the agenda.
430      * @param action
431      * @param peopleFlowName
432      */
433     private void addActionAttributes(ActionBo action,String peopleFlowName){
434         Map<String, String> peopleFlowPk = new HashMap<String, String>();
435         peopleFlowPk.put("nm", peopleFlowName);
436         List<PeopleFlowBo> peopleFlowBos =(List<PeopleFlowBo>) getBusinessObjectService().findMatching(PeopleFlowBo.class, peopleFlowPk);
437         if(peopleFlowBos!=null && peopleFlowBos.size()>0){
438             Map<String, String> attrDefPk = new HashMap<String, String>();
439             attrDefPk.put("attr_defn_id", "1000");
440             KrmsAttributeDefinitionBo peopleFlowIdDef = getBusinessObjectService().findByPrimaryKey(KrmsAttributeDefinitionBo.class,attrDefPk);
441 
442             attrDefPk = new HashMap<String, String>();
443             attrDefPk.put("attr_defn_id", "1006");
444             KrmsAttributeDefinitionBo peopleFlowNameDef = getBusinessObjectService().findByPrimaryKey(KrmsAttributeDefinitionBo.class,attrDefPk);
445 
446             Set<ActionAttributeBo> actionAttributeBos = new HashSet<ActionAttributeBo>();
447             String peopleFlowId =  peopleFlowBos.get(0).getId();
448             ActionAttributeBo actionAttributeBo = new ActionAttributeBo();
449             actionAttributeBo.setActionId(action.getId());
450             actionAttributeBo.setAttributeDefinitionId(peopleFlowIdDef.getId());
451             actionAttributeBo.setAttributeDefinition(peopleFlowIdDef);
452             actionAttributeBo.setValue(peopleFlowId);
453             getBusinessObjectService().save(actionAttributeBo);
454             actionAttributeBos.add(actionAttributeBo);
455 
456             actionAttributeBo = new ActionAttributeBo();
457             actionAttributeBo.setActionId(action.getId());
458             actionAttributeBo.setAttributeDefinitionId(peopleFlowNameDef.getId());
459             actionAttributeBo.setAttributeDefinition(peopleFlowNameDef);
460             actionAttributeBo.setValue(peopleFlowName);
461             getBusinessObjectService().save(actionAttributeBo);
462             actionAttributeBos.add(actionAttributeBo);
463 
464             action.setAttributeBos(actionAttributeBos);
465             getBusinessObjectService().save(action);
466 
467         }
468 
469     }
470 
471     /**
472      * This method returns the contextValidActionBo object based on the contextBo and action parameters
473      * Creates a new object, stores it in database and returns if none exists, otherwise returns the existing object.
474      * @param contextBo
475      * @param action
476      * @return  contextValidActionBo
477      */
478     private ContextValidActionBo getContextValidActionBo(ContextBo contextBo,KrmsAction action){
479         KrmsTypeBo actionTypeService = getActiontypeService(action);
480         ContextValidActionBo contextValidActionBo =null;
481         if(actionTypeService!=null){
482             Object existing = objectExists(ContextValidActionBo.class, "actn_typ_id", actionTypeService.getId());
483             if (existing == null) {
484                 contextValidActionBo = new ContextValidActionBo();
485                 contextValidActionBo.setActionType(actionTypeService);
486                 contextValidActionBo.setActionTypeId(actionTypeService.getId());
487                 contextValidActionBo.setContextId(contextBo.getId());
488                 getBusinessObjectService().save(contextValidActionBo);
489             } else {
490                 contextValidActionBo = (ContextValidActionBo) existing;
491             }
492         }
493         if(contextValidActionBo==null){
494             contextValidActionBo = registerDefaultServiceTypes(contextBo);
495         }
496         return contextValidActionBo;
497     }
498 
499     /**
500      * Lookups the database for the particular Servicename and the returns the krmsTypeBo object.
501      * @param action
502      * @return  krmsTypeBo
503      */
504     private KrmsTypeBo  getActiontypeService(KrmsAction action){
505         Map<String, String> krmsTypePk = new HashMap<String, String>();
506         krmsTypePk.put("srvc_nm", action.getServiceName());
507         KrmsTypeBo krmsTypeBo = getBusinessObjectService().findByPrimaryKey(KrmsTypeBo.class, krmsTypePk);
508         return  krmsTypeBo;
509     }
510 
511     /**
512      * This method persists the matchBo object based on the agendaBo and proposition parameters..
513      * @param agendaBo
514      * @param proposition
515      */
516     private void persistMatchBo(AgendaBo agendaBo, KrmsProposition proposition) {
517         MatchBo matchBo = new MatchBo();
518         matchBo.setAgendaName(agendaBo.getName());
519         matchBo.setTermName(proposition.getTerm());
520         getBusinessObjectService().save(matchBo);
521     }
522 
523     /**
524      * This method builds the Krms object from the fileContent parameter and the returns the list of agendaNames from the Krms object.
525      * @param fileContent
526      * @return  agendaNames
527      * @throws java.io.IOException
528      * @throws java.net.URISyntaxException
529      */
530     public List<String> persistKrmsFromFileContent(String fileContent) throws IOException, URISyntaxException {
531         Krms krms = getKrmsObjectGeneratorFromXML().buildKrmsFromFileContent(fileContent);
532         List<String> agendaNames = new ArrayList<String>();
533         for(OleAgenda oleAgenda :krms.getOleAgendaList()){
534             agendaNames.add(persist(oleAgenda));
535         }
536         return agendaNames;
537 
538     }
539 
540     /**
541      * Gets the krmsObjectGeneratorFromXML object if exists, otherwise it creates a new object.
542      * @return  krmsObjectGeneratorFromXML
543      */
544     public KrmsObjectGeneratorFromXML getKrmsObjectGeneratorFromXML() {
545         if(null==krmsObjectGeneratorFromXML){
546             krmsObjectGeneratorFromXML = new KrmsObjectGeneratorFromXML();            krmsObjectGeneratorFromXML = new KrmsObjectGeneratorFromXML();
547         }
548         return krmsObjectGeneratorFromXML;
549     }
550 
551     /**
552      * Sets the krmsObjectGeneratorFromXML value.
553      * @param krmsObjectGeneratorFromXML
554      */
555     public void setKrmsObjectGeneratorFromXML(KrmsObjectGeneratorFromXML krmsObjectGeneratorFromXML) {
556         this.krmsObjectGeneratorFromXML = krmsObjectGeneratorFromXML;
557     }
558 
559     /**
560      * Lookups the database based on the agendaName and returns a boolean value.
561      * @param agendaName
562      * @return  Boolean
563      */
564     public Boolean doesKrmsExist(String agendaName) {
565         HashMap<String, String> map = new HashMap<String, String>();
566         map.put("nm", agendaName);
567         List<AgendaBo> matching = (List<AgendaBo>) getBusinessObjectService().findMatching(AgendaBo.class, map);
568         return !matching.isEmpty();
569     }
570 
571     /**
572      * This method deletes the term record from database.
573      * @param oleAgenda
574      */
575     public void deleteAgenda(OleAgenda oleAgenda) {
576 
577         Map<String, String> contextPks = new HashMap<String, String>();
578         contextPks.put("nm", oleAgenda.getContextName());
579         contextPks.put("nmspc_cd", OLEConstants.OLE_NAMESPACE);
580         ContextBo context = getBusinessObjectService().findByPrimaryKey(ContextBo.class, contextPks);
581         String contextId = context.getId();
582 
583         Map<String, String> agendaPks = new HashMap<String, String>();
584         agendaPks.put("nm", oleAgenda.getName());
585         agendaPks.put("cntxt_id", contextId);
586         AgendaBo oldAgenda = getBusinessObjectService().findByPrimaryKey(AgendaBo.class, agendaPks);
587 
588         Map<String, String> itemPks = new HashMap<String, String>();
589         itemPks.put("agenda_id", oldAgenda.getId());
590         List<AgendaItemBo> items = (List<AgendaItemBo>) getBusinessObjectService().findMatching(AgendaItemBo.class, itemPks);
591         for(AgendaItemBo agendaItemBo : items){
592             getBusinessObjectService().delete(agendaItemBo);
593         }
594         getBusinessObjectService().delete(oldAgenda);
595 
596         List<AgendaBo> agendas = context.getAgendas();
597         AgendaBo removeAgenda = null;
598         for (AgendaBo agenda : agendas) {
599             if (agenda.getName().equalsIgnoreCase(oldAgenda.getName())) {
600                 removeAgenda = agenda;
601             }
602         }
603         agendas.remove(removeAgenda);
604         context.setAgendas(agendas);
605         getBusinessObjectService().save(context);
606 
607         deleteRegisteredRules(oleAgenda);
608 
609         HashMap matchingProfileAttr = new HashMap();
610         matchingProfileAttr.put("agenda_name", oldAgenda.getName());
611         List<ProfileAttributeBo> profileAttributeBos =
612                 (List<ProfileAttributeBo>) getBusinessObjectService().findMatching(ProfileAttributeBo.class, matchingProfileAttr);
613         getBusinessObjectService().delete(profileAttributeBos);
614 
615         HashMap matchingProfileFacts = new HashMap();
616         matchingProfileFacts.put("agenda_name", oldAgenda.getName());
617         List<MatchBo> matchBos =
618                 (List<MatchBo>) getBusinessObjectService().findMatching(MatchBo.class, matchingProfileFacts);
619         getBusinessObjectService().delete(matchBos);
620     }
621 
622     /**
623      *  This method deletes the term related rules from database.
624      * @param oleAgenda
625      */
626     private void deleteRegisteredRules(OleAgenda oleAgenda) {
627         Map map = new HashMap();
628         List<KrmsRule> rules = oleAgenda.getRules();
629         for (Iterator<KrmsRule> iterator = rules.iterator(); iterator.hasNext(); ) {
630             KrmsRule krmsRule = iterator.next();
631             if(krmsRule.getProposition()!=null){
632                 String term = krmsRule.getProposition().getTerm();
633                 deleteTerm(term) ;
634             }
635             if(krmsRule.getCompoundProposition()!=null){
636                 deleteCompoundProposition(krmsRule.getCompoundProposition());
637             }
638             map.put("nm", krmsRule.getName());
639             List<RuleBo> matching = (List<RuleBo>) getBusinessObjectService().findMatching(RuleBo.class, map);
640             getBusinessObjectService().delete(matching);
641         }
642     }
643 
644     /**
645      *  This method deletes the compound proposition based on the oleProposition from database.
646      * @param oleProposition
647      */
648     private void deleteCompoundProposition(OleProposition oleProposition){
649         for(OleProposition oleProposition1 : oleProposition.getOlePropositions()!=null?oleProposition.getOlePropositions():new ArrayList<OleProposition>()){
650             deleteCompoundProposition(oleProposition1);
651         }
652         for(KrmsProposition krmsProposition : oleProposition.getPropositions()!=null?oleProposition.getPropositions():new ArrayList<KrmsProposition>()){
653             String term = krmsProposition.getTerm();
654             deleteTerm(term) ;
655         }
656     }
657 
658     /**
659      *  This method deletes the term from database.
660      * @param term
661      */
662     private void deleteTerm(String term) {
663         Map map = new HashMap();
664         map.put("desc_txt", term);
665         List<TermBo> termsToBeDeleted = (List<TermBo>) getBusinessObjectService().findMatching(TermBo.class, map);
666         if (!termsToBeDeleted.isEmpty()) {
667             getBusinessObjectService().delete(termsToBeDeleted);
668         }
669     }
670 
671     /**
672      * This method registers the default service types in the database for the context.
673      * @param contextBo
674      * @return  ContextValidActionBo
675      */
676     public ContextValidActionBo registerDefaultServiceTypes(ContextBo contextBo) {
677         return profileBuilder.registerDefaultServiceTypes(contextBo);
678     }
679 
680     /**
681      * This method adds the action into rule to create the agenda.
682      * @param namespace
683      * @param actionName
684      * @param description
685      * @param actionTypeId
686      * @param ruleBo
687      * @param sequenceNumber
688      * @return  ActionBo
689      */
690     public ActionBo addActionToRule(String namespace, String actionName, String description, String actionTypeId, RuleBo ruleBo, Integer sequenceNumber) {
691         return profileBuilder.addActionToRule(namespace,actionName,description,actionTypeId,ruleBo,sequenceNumber);
692     }
693 
694     /**
695      * This method adds the dummy rule into database for falseAction.
696      * @param ruleBo
697      * @param falseActionRuleBo
698      * @return  AgendaItemBo
699      */
700     public AgendaItemBo addDummyRuleForFalseAction(RuleBo ruleBo, RuleBo falseActionRuleBo) {
701         return profileBuilder.addDummyRuleForFalseAction(ruleBo,falseActionRuleBo);
702     }
703 
704     /**
705      * This method persists the profile attributes in to database.
706      * @param oleAgenda
707      */
708     private void persistProfileAttributes(OleAgenda oleAgenda) {
709         List<ProfileAttributeBo> profileAttributes = oleAgenda.getProfileAttributes();
710         for (Iterator<ProfileAttributeBo> iterator = profileAttributes.iterator(); iterator.hasNext(); ) {
711             ProfileAttributeBo profileAttributeBo = iterator.next();
712             profileAttributeBo.setAgendaName(oleAgenda.getName());
713         }
714         getBusinessObjectService().save(profileAttributes);
715     }
716 
717     /**
718      *  This method creates a compound proposition based on the propositions list and opCode parameters and returns the propositionBo.
719      * @param propositions
720      * @param opCode
721      * @return  PropositionBo
722      */
723     private PropositionBo createCompoundProposition(List<KrmsProposition> propositions,String opCode){
724         List<PropositionBo> propositionBos = new ArrayList<PropositionBo>();
725         int seqNum = 0;
726         for(KrmsProposition proposition : propositions){
727             PropositionBo propositionBo = createSimpleProposition(proposition,seqNum);
728             if(opCode==null){
729                 return propositionBo;
730             }
731             seqNum++;
732             propositionBos.add(propositionBo);
733         }
734         PropositionBo propositionBo =  createCompoundProposition(category.getId(),"compound","C",ruleBo,opCode);
735         propositionBo.setCompoundComponents(propositionBos);
736         return propositionBo;
737     }
738 
739     /**
740      * This method creates a simple proposition based on the proposition and seqNum parameters and returns the propositionBo.
741      * @param proposition
742      * @param seqNum
743      * @return PropositionBo
744      */
745     private PropositionBo createSimpleProposition(KrmsProposition proposition,int seqNum){
746         TermSpecificationBo termSpecification =
747                 createTermSpecification(OLEConstants.OLE_NAMESPACE, proposition.getTerm(), proposition.getType(), Arrays.asList(category), Arrays.asList(context));
748 
749         TermBo termBo = createTerm(proposition.getTerm(), termSpecification);
750 
751 
752         PropositionBo propositionBo = createProposition(category.getId(), proposition.getTerm() + " check", "S", ruleBo,seqNum);
753 
754         if(proposition.getFunction()!=null && proposition.getFunction().equalsIgnoreCase("yes")){
755 
756             FunctionBo function = createFunction(OLEConstants.OLE_NAMESPACE, proposition.getTerm(), "java.lang.Boolean", Arrays.asList(category),proposition.getType());
757 
758             addTermToPropositionParameter(termBo.getId(), propositionBo);
759 
760             addCustomFunctionToPropositionParameter(function.getId(), propositionBo);
761         }   else{
762 
763             addTermToPropositionParameter(termBo.getId(), propositionBo);
764 
765             addConstantToPropositionParameter(proposition.getConstant(),propositionBo);
766 
767             addOperatorToPropositionParameter(proposition.getOperator(),propositionBo);
768         }
769         persistMatchBo(agendaBo, proposition);
770         return propositionBo;
771     }
772 
773     /**
774      *  Gets the category attribute.
775      * @return Returns the category
776      */
777     public CategoryBo getCategory() {
778         return category;
779     }
780 
781     /**
782      *  Sets the category attribute value.
783      * @param category .The category to set.
784      */
785     public void setCategory(CategoryBo category) {
786         this.category = category;
787     }
788 
789     /**
790      * Gets the context attribute.
791      * @return  Returns the context
792      */
793     public ContextBo getContext() {
794         return context;
795     }
796 
797     /**
798      * Sets the Context attribute value.
799      * @param context
800      */
801     public void setContext(ContextBo context) {
802         this.context = context;
803     }
804 
805     /**
806      *  Gets the agendaBo attribute.
807      * @return Returns agendaBo.
808      */
809     public AgendaBo getAgendaBo() {
810         return agendaBo;
811     }
812 
813     /**
814      * Sets the agendaBo attribute value.
815      * @param agendaBo
816      */
817     public void setAgendaBo(AgendaBo agendaBo) {
818         this.agendaBo = agendaBo;
819     }
820 
821     /**
822      *  Gets the ruleBo attribute.
823      * @return Returns ruleBo.
824      */
825     public RuleBo getRuleBo() {
826         return ruleBo;
827     }
828 
829     /**
830      * Sets the ruleBo attribute value.
831      * @param ruleBo
832      */
833     public void setRuleBo(RuleBo ruleBo) {
834         this.ruleBo = ruleBo;
835     }
836 }