View Javadoc

1   package org.kuali.ole.ingest;
2   
3   import org.kuali.ole.OLEConstants;
4   import org.kuali.ole.ingest.ProfileXMLSchemaValidator;
5   import org.kuali.ole.ingest.pojo.*;
6   import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
7   import org.kuali.rice.krad.bo.PersistableBusinessObject;
8   import org.kuali.rice.krad.service.BusinessObjectService;
9   import org.kuali.rice.krad.service.KRADServiceLocator;
10  import org.kuali.rice.krms.api.repository.term.TermSpecificationDefinition;
11  import org.kuali.rice.krms.impl.repository.*;
12  import org.xml.sax.SAXException;
13  
14  import java.io.File;
15  import java.io.IOException;
16  import java.net.MalformedURLException;
17  import java.net.URISyntaxException;
18  import java.net.URL;
19  import java.util.*;
20  
21  /**
22   * Created by IntelliJ IDEA.
23   * User: pvsubrah
24   * Date: 3/28/12
25   * Time: 2:27 PM
26   * To change this template use File | Settings | File Templates.
27   */
28  public class ProfileBuilder {
29      private TermBoService termBoService;
30      private BusinessObjectService businessObjectService;
31      private ProfileObjectGeneratorFromXML profileObjectGeneratorFromXML;
32  
33      public ContextBo createContext(String namespace, String contextName) {
34          ContextBo contextBo;
35          Object existing = objectExists(ContextBo.class, "nm", contextName);
36          if (null == existing) {
37              contextBo = new ContextBo();
38              contextBo.setNamespace(namespace);
39              contextBo.setName(contextName);
40              getBusinessObjectService().save(contextBo);
41          } else {
42              contextBo = (ContextBo) existing;
43          }
44          return contextBo;
45      }
46  
47      public ContextValidActionBo registerDefaultServiceTypes(ContextBo contextBo) {
48          KrmsTypeBo actionTypeService = createActionTypeService();
49          ContextValidActionBo contextValidActionBo;
50          Object existing = objectExists(ContextValidActionBo.class, "actn_typ_id", actionTypeService.getId());
51          if (existing == null) {
52              contextValidActionBo = new ContextValidActionBo();
53              contextValidActionBo.setActionType(actionTypeService);
54              contextValidActionBo.setActionTypeId(actionTypeService.getId());
55              contextValidActionBo.setContextId(contextBo.getId());
56              getBusinessObjectService().save(contextValidActionBo);
57          } else {
58              contextValidActionBo = (ContextValidActionBo) existing;
59          }
60          return contextValidActionBo;
61      }
62  
63      public KrmsTypeBo createActionTypeService() {
64          KrmsTypeBo actionTypeService;
65          String typeName = "Create Bibliographic Record";
66          Object existing = objectExists(KrmsTypeBo.class, "nm", typeName);
67          if (existing == null) {
68              actionTypeService = new KrmsTypeBo();
69              actionTypeService.setActive(true);
70              actionTypeService.setName("Create Bibliographic Record");
71              actionTypeService.setNamespace(OLEConstants.OLE_NAMESPACE);
72              actionTypeService.setServiceName("createBibActionTypeService");
73              getBusinessObjectService().save(actionTypeService);
74          } else {
75              actionTypeService = (KrmsTypeBo) existing;
76          }
77          return actionTypeService;
78      }
79  
80      public KrmsTypeBo createTermResolverTypeService() {
81          KrmsTypeBo profileTermResolverTypeService;
82          String serviceName = "profileTermResolverTypeService";
83          Object existing = objectExists(KrmsTypeBo.class, "srvc_nm", serviceName);
84          if (existing == null) {
85              profileTermResolverTypeService = new KrmsTypeBo();
86              profileTermResolverTypeService.setActive(true);
87              profileTermResolverTypeService.setName("ProfileTermResolver");
88              profileTermResolverTypeService.setNamespace(OLEConstants.OLE_NAMESPACE);
89              profileTermResolverTypeService.setServiceName("profileTermResolverTypeService");
90              getBusinessObjectService().save(profileTermResolverTypeService);
91          } else {
92              profileTermResolverTypeService = (KrmsTypeBo) existing;
93          }
94          return profileTermResolverTypeService;
95      }
96  
97      public CategoryBo createCategory(String namespace, String categoryName) {
98          CategoryBo categoryBo;
99          Object existing = objectExists(CategoryBo.class, "nm", categoryName);
100         if (existing == null) {
101             categoryBo = new CategoryBo();
102             categoryBo.setName(categoryName);
103             categoryBo.setNamespace(namespace);
104             getBusinessObjectService().save(categoryBo);
105         } else {
106             categoryBo = (CategoryBo) existing;
107         }
108         return categoryBo;
109     }
110 
111     //TODO: Wasn't able to use business object service to save the term spec as the pk generated was empty string.
112     //TOOD: When using the termboservice (which in turn uses the bo service) its generating the pk. Bug need to be
113     //TODO: fixed with Rice Team.
114     public TermSpecificationBo createTermSpecification(String namespace, String termSpecificationName, String type, List<CategoryBo> categoryBoList, List<ContextBo> contextBoList) {
115         TermSpecificationBo termSpecificationBo;
116         Object existing = objectExists(TermSpecificationBo.class, "nm", termSpecificationName);
117         if (existing == null) {
118             termSpecificationBo = new TermSpecificationBo();
119             termSpecificationBo.setActive(true);
120             termSpecificationBo.setName(termSpecificationName);
121             termSpecificationBo.setNamespace(namespace);
122             termSpecificationBo.setCategories(categoryBoList);
123             termSpecificationBo.setDescription(termSpecificationName);
124             termSpecificationBo.setContexts(contextBoList);
125             termSpecificationBo.setType(type);
126             TermSpecificationDefinition termSpecification = getTermBoService().createTermSpecification(TermSpecificationDefinition.Builder.create(termSpecificationBo).build());
127             termSpecificationBo = TermSpecificationBo.from(termSpecification);
128         } else {
129             termSpecificationBo = (TermSpecificationBo) existing;
130         }
131         return termSpecificationBo;
132     }
133 
134     public TermBo createTerm(String name, TermSpecificationBo termSpecificationBo) {
135         TermBo termBo;
136         Object existing = objectExists(TermBo.class, "term_spec_id", termSpecificationBo.getId());
137         if (existing == null) {
138             termBo = new TermBo();
139             termBo.setSpecification(termSpecificationBo);
140             termBo.setSpecificationId(termSpecificationBo.getId());
141             termBo.setDescription(name);
142             getBusinessObjectService().save(termBo);
143         } else {
144             termBo = (TermBo) existing;
145         }
146         return termBo;
147     }
148 
149 
150     public FunctionBo createFunction(String namespace, String termName, String returnType, List categories) {
151         Object existing = objectExists(FunctionBo.class, "nm", termName + "Function");
152         FunctionBo functionBo;
153         if (existing == null) {
154             HashMap<String, String> map = new HashMap<String, String>();
155             map.put("nm", "FunctionLoader");
156             List<KrmsTypeBo> matching =
157                     (List<KrmsTypeBo>) getBusinessObjectService().findMatching(KrmsTypeBo.class, map);
158             KrmsTypeBo krmsTypeBo = matching.get(0);
159             functionBo = new FunctionBo();
160             functionBo.setActive(true);
161             functionBo.setName(termName + "Function");
162             functionBo.setNamespace(namespace);
163             functionBo.setReturnType(returnType);
164             functionBo.setCategories(categories);
165             functionBo.setTypeId(krmsTypeBo.getId());
166             getBusinessObjectService().save(functionBo);
167 
168             FunctionParameterBo functionParameterBo = new FunctionParameterBo();
169             functionParameterBo.setFunctionId(functionBo.getId());
170             functionParameterBo.setParameterType("org.kuali.ole.Ingest.ProfileTerm");
171             functionParameterBo.setName(termName);
172             functionParameterBo.setSequenceNumber(0);
173 
174             getBusinessObjectService().save(functionParameterBo);
175 
176             List<FunctionParameterBo> parameters = functionBo.getParameters();
177             if (null == parameters) {
178                 parameters = new ArrayList<FunctionParameterBo>();
179             }
180             parameters.add(functionParameterBo);
181             functionBo.setParameters(parameters);
182 
183             getBusinessObjectService().save(functionBo);
184         } else {
185             functionBo = (FunctionBo) existing;
186         }
187 
188         return functionBo;
189     }
190 
191     public PropositionBo createProposition(String categoryId, String propositionDescription, String propositionType, RuleBo rule) {
192         PropositionBo propositionBo = new PropositionBo();
193         propositionBo.setCategoryId(categoryId);
194         propositionBo.setDescription(propositionDescription);
195         propositionBo.setRuleId(rule.getId());
196         propositionBo.setPropositionTypeCode(propositionType);
197         getBusinessObjectService().save(propositionBo);
198         rule.setProposition(propositionBo);
199         getBusinessObjectService().save(rule);
200         return propositionBo;
201     }
202 
203     public RuleBo createRule(String namespace, String ruleName) {
204         RuleBo ruleBo = new RuleBo();
205         Object existing = objectExists(RuleBo.class, "nm", ruleName);
206         if (existing == null) {
207             ruleBo.setName(ruleName);
208             ruleBo.setNamespace(namespace);
209             getBusinessObjectService().save(ruleBo);
210         } else {
211             ruleBo = (RuleBo) existing;
212         }
213         return ruleBo;
214     }
215 
216     public ActionBo addActionToRule(String namespace, String actionName, String description, String actionTypeId, RuleBo ruleBo, Integer sequenceNumber) {
217         ActionBo actionBo = new ActionBo();
218         Object existing = objectExists(ActionBo.class, "nm", actionName);
219         if (existing == null) {
220             actionBo.setName(actionName + "Action");
221             actionBo.setRuleId(ruleBo.getId());
222             actionBo.setNamespace(namespace);
223             actionBo.setSequenceNumber(sequenceNumber);
224             actionBo.setDescription(description);
225             actionBo.setTypeId(actionTypeId);
226             getBusinessObjectService().save(actionBo);
227             List<ActionBo> actionBos = ruleBo.getActions();
228             if (null == actionBos) {
229                 actionBos = new ArrayList<ActionBo>();
230             }
231             actionBos.add(actionBo);
232             ruleBo.setActions(actionBos);
233             getBusinessObjectService().save(ruleBo);
234         } else {
235             actionBo = (ActionBo) existing;
236         }
237         return actionBo;
238     }
239 
240     public PropositionBo addCustomFunctionToPropositionParameter(String functionId, PropositionBo proposition) {
241         PropositionParameterBo propositionParameterBo = new PropositionParameterBo();
242         propositionParameterBo.setPropId(proposition.getId());
243         propositionParameterBo.setValue(functionId);
244         propositionParameterBo.setSequenceNumber(1);
245         propositionParameterBo.setParameterType("F");
246         getBusinessObjectService().save(propositionParameterBo);
247         List<PropositionParameterBo> parameters = proposition.getParameters();
248         if (null == parameters) {
249             parameters = new ArrayList<PropositionParameterBo>();
250         }
251         parameters.add(propositionParameterBo);
252         proposition.setParameters(parameters);
253         getBusinessObjectService().save(proposition);
254         return proposition;
255     }
256 
257     public PropositionBo addTermToPropositionParameter(String termId, PropositionBo proposition) {
258         PropositionParameterBo propositionParameterBo = new PropositionParameterBo();
259         propositionParameterBo.setPropId(proposition.getId());
260         propositionParameterBo.setValue(termId);
261         propositionParameterBo.setSequenceNumber(0);
262         propositionParameterBo.setParameterType("T");
263         getBusinessObjectService().save(propositionParameterBo);
264         List<PropositionParameterBo> parameters = proposition.getParameters();
265         if (null == parameters) {
266             parameters = new ArrayList<PropositionParameterBo>();
267         }
268         parameters.add(propositionParameterBo);
269         proposition.setParameters(parameters);
270         getBusinessObjectService().save(proposition);
271         return proposition;
272     }
273 
274     public AgendaBo createAgenda(String agendaName, ContextBo contextBo) {
275         AgendaBo agendaBo;
276         Object existing = objectExists(AgendaBo.class, "nm", agendaName);
277         if (existing == null) {
278             agendaBo = new AgendaBo();
279             agendaBo.setActive(true);
280             agendaBo.setName(agendaName);
281             agendaBo.setContext(contextBo);
282             agendaBo.setContextId(contextBo.getId());
283             getBusinessObjectService().save(agendaBo);
284             List<AgendaBo> agendas = contextBo.getAgendas();
285             if (null == agendas) {
286                 agendas = new ArrayList<AgendaBo>();
287             }
288             agendas.add(agendaBo);
289             contextBo.setAgendas(agendas);
290             getBusinessObjectService().save(contextBo);
291         } else {
292             agendaBo = (AgendaBo) existing;
293         }
294         return agendaBo;
295     }
296 
297     public AgendaBo addRuleToAgenda(AgendaBo agendaBo, RuleBo ruleBo) {
298         AgendaItemBo agendaItemBo = new AgendaItemBo();
299         agendaItemBo.setAgendaId(agendaBo.getId());
300         agendaItemBo.setRule(ruleBo);
301         getBusinessObjectService().save(agendaItemBo);
302         List<AgendaItemBo> agendaBoItems = agendaBo.getItems();
303         if (null == agendaBoItems) {
304             agendaBoItems = new ArrayList<AgendaItemBo>();
305         }
306         agendaBoItems.add(agendaItemBo);
307         agendaBo.setItems(agendaBoItems);
308         agendaBo.setFirstItemId(agendaBoItems.get(0).getId());
309         getBusinessObjectService().save(agendaBo);
310         return agendaBo;
311     }
312 
313     public AgendaItemBo addDummyRuleForFalseAction(RuleBo ruleBo, RuleBo falseActionRuleBo) {
314         HashMap<String, String> map = new HashMap<String, String>();
315         map.put("rule_id", ruleBo.getId());
316         List<AgendaItemBo> matching =
317                 (List<AgendaItemBo>) getBusinessObjectService().findMatching(AgendaItemBo.class, map);
318         AgendaItemBo existingAgendaItemBo = matching.get(0);
319 
320         AgendaItemBo falseActionAgendaItemBo = new AgendaItemBo();
321         falseActionAgendaItemBo.setAgendaId(existingAgendaItemBo.getAgendaId());
322         falseActionAgendaItemBo.setRule(falseActionRuleBo);
323         falseActionAgendaItemBo.setRuleId(falseActionRuleBo.getId());
324         getBusinessObjectService().save(falseActionAgendaItemBo);
325         existingAgendaItemBo.setWhenFalse(falseActionAgendaItemBo);
326         getBusinessObjectService().save(existingAgendaItemBo);
327         return existingAgendaItemBo;
328 
329     }
330 
331 
332     public MatchBo addMatchBo(String agendaName, String termName, String docType, String incomingField, String existingField) {
333         MatchBo matchBo = new MatchBo();
334         matchBo.setAgendaName(agendaName);
335         matchBo.setDocType(docType);
336         matchBo.setTermName(termName);
337         matchBo.setIncomingField(incomingField);
338         matchBo.setExistingField(existingField);
339         getBusinessObjectService().save(matchBo);
340         return matchBo;
341 
342     }
343 
344     public Profile buildProfileFromFileContent(String fileContent) throws URISyntaxException, IOException {
345         return getProfileObjectGeneratorFromXML().buildProfileFromFileContent(fileContent);
346     }
347 
348     public Profile buildProfile(String fileName) throws URISyntaxException, IOException, SAXException {
349         URL resource = getClass().getResource(fileName);
350         File file = new File(resource.toURI());
351         String fileContent = new FileUtil().readFile(file);
352         if (validProfileXML(fileContent)) {
353             return getProfileObjectGeneratorFromXML().buildProfileFromFileContent(fileContent);
354         }
355         return null;
356     }
357 
358     private boolean validProfileXML(String fileContent) throws IOException, SAXException {
359         return new ProfileXMLSchemaValidator().validateContentsAgainstSchema(null);
360     }
361 
362     public String persistKRMSProfileFromFileContent(String fileContent) throws IOException, URISyntaxException {
363         Profile profile = getProfileObjectGeneratorFromXML().buildProfileFromFileContent(fileContent);
364         return persist(profile);
365 
366     }
367 
368     public String persistKRMSProfile(String profileFileName) throws IOException, URISyntaxException, SAXException {
369         Profile profile = buildProfile(profileFileName);
370         return persist(profile);
371     }
372 
373     public String persist(Profile profile) {
374         Boolean profileExists = doesProfileExist(profile.getName());
375         if (profileExists) {
376             deleteAgenda(profile);
377         }
378         ContextBo context = createContext(OLEConstants.OLE_NAMESPACE, profile.getContextName());
379         CategoryBo category = createCategory(OLEConstants.OLE_NAMESPACE, profile.getCategoryName());
380 
381         registerDefaultTermResolvers(Arrays.asList(context), Arrays.asList(category));
382         registerDefaultFunctionLoader();
383 
384         ContextValidActionBo contextValidActionBo = registerDefaultServiceTypes(context);
385 
386 
387         AgendaBo agendaBo = createAgenda(profile.getName(), context);
388 
389         List<OleRule> rules = profile.getRules();
390         for (Iterator<OleRule> iterator = rules.iterator(); iterator.hasNext(); ) {
391             OleRule rule = iterator.next();
392 
393             TermSpecificationBo termSpecification =
394                     createTermSpecification(OLEConstants.OLE_NAMESPACE, rule.getTerm(), "org.kuali.ole.ProfileTerm", Arrays.asList(category), Arrays.asList(context));
395 
396             TermBo termBo = createTerm(rule.getTerm(), termSpecification);
397 
398             RuleBo ruleBo = createRule(OLEConstants.OLE_NAMESPACE, rule.getName());
399 
400             addRuleToAgenda(agendaBo, ruleBo);
401 
402             PropositionBo propositionBo = createProposition(category.getId(), rule.getTerm() + " check", "S", ruleBo);
403 
404             FunctionBo function = createFunction(OLEConstants.OLE_NAMESPACE, rule.getTerm(), "java.lang.Boolean", Arrays.asList(category));
405 
406             addTermToPropositionParameter(termBo.getId(), propositionBo);
407 
408             addCustomFunctionToPropositionParameter(function.getId(), propositionBo);
409 
410             List<OleAction> trueActions = rule.getTrueActions();
411             for (Iterator<OleAction> oleActionIterator = trueActions.iterator(); oleActionIterator.hasNext(); ) {
412                 OleAction trueAction = oleActionIterator.next();
413                 addActionToRule(OLEConstants.OLE_NAMESPACE, trueAction.getName(), trueAction.getDescription(), contextValidActionBo.getActionTypeId(), ruleBo, trueAction.getSequenceNumber());
414             }
415 
416             List<OleAction> falseActions = rule.getFalseActions();
417             for (Iterator<OleAction> oleActionIterator = falseActions.iterator(); oleActionIterator.hasNext(); ) {
418                 OleAction falseAction = oleActionIterator.next();
419                 RuleBo falseActionRuleBo = createRule(OLEConstants.OLE_NAMESPACE, rule.getName() + "_falseActionRule");
420                 addActionToRule(OLEConstants.OLE_NAMESPACE, falseAction.getName(), falseAction.getDescription(), contextValidActionBo.getActionTypeId(), falseActionRuleBo, falseAction.getSequenceNumber());
421                 addDummyRuleForFalseAction(ruleBo, falseActionRuleBo);
422             }
423             persistMatchBo(agendaBo, rule);
424         }
425         persistProfileAttributes(profile);
426         return agendaBo.getName();
427     }
428 
429     public void registerDefaultFunctionLoader() {
430         if (null == objectExists(KrmsTypeBo.class, "nm", "FunctionLoader")) {
431             KrmsTypeBo krmsTypeBo = new KrmsTypeBo();
432             krmsTypeBo.setNamespace(OLEConstants.OLE_NAMESPACE);
433             krmsTypeBo.setName("FunctionLoader");
434             krmsTypeBo.setServiceName("functionLoader");
435             getBusinessObjectService().save(krmsTypeBo);
436         }
437     }
438 
439     public void registerDefaultTermResolvers(List<ContextBo> contextBos, List<CategoryBo> categoryBos) {
440         List list = new ArrayList();
441         KrmsTypeBo termResolverTypeService = createTermResolverTypeService();
442 
443         TermSpecificationBo termSpecificationBo = createTermSpecification(OLEConstants.OLE_NAMESPACE, OLEConstants.EXISTING_FIELD, "java.lang.String", categoryBos, contextBos);
444         createTerm(OLEConstants.EXISTING_FIELD, termSpecificationBo);
445 
446         TermResolverBo termResolverBoForExistinField = null;
447         if (null == objectExists(TermResolverBo.class, "nm", OLEConstants.EXISTING_FIELD)) {
448             termResolverBoForExistinField = new TermResolverBo();
449             termResolverBoForExistinField.setNamespace(OLEConstants.OLE_NAMESPACE);
450             termResolverBoForExistinField.setName(OLEConstants.EXISTING_FIELD);
451             termResolverBoForExistinField.setOutputId(termSpecificationBo.getId());
452             termResolverBoForExistinField.setTypeId(termResolverTypeService.getId());
453             list.add(termResolverBoForExistinField);
454         }
455 
456         TermSpecificationBo termSpecificationBo1 = createTermSpecification(OLEConstants.OLE_NAMESPACE, OLEConstants.INCOMING_FIELD, "java.lang.String", categoryBos, contextBos);
457         createTerm(OLEConstants.INCOMING_FIELD, termSpecificationBo1);
458 
459         TermResolverBo termResolverBoForIncomingField = null;
460         if (null == objectExists(TermResolverBo.class, "nm", OLEConstants.INCOMING_FIELD)) {
461             termResolverBoForIncomingField = new TermResolverBo();
462             termResolverBoForIncomingField.setNamespace(OLEConstants.OLE_NAMESPACE);
463             termResolverBoForIncomingField.setName(OLEConstants.INCOMING_FIELD);
464             termResolverBoForIncomingField.setTypeId(termResolverTypeService.getId());
465             termResolverBoForIncomingField.setOutputId(termSpecificationBo1.getId());
466             getBusinessObjectService().save(termResolverBoForIncomingField);
467             list.add(termResolverBoForIncomingField);
468         }
469 
470         if (!list.isEmpty()) {
471             getBusinessObjectService().save(list);
472         }
473     }
474 
475     private void persistProfileAttributes(Profile profile) {
476         List<ProfileAttributeBo> profileAttributes = profile.getProfileAttributes();
477         for (Iterator<ProfileAttributeBo> iterator = profileAttributes.iterator(); iterator.hasNext(); ) {
478             ProfileAttributeBo profileAttributeBo = iterator.next();
479             profileAttributeBo.setAgendaName(profile.getName());
480         }
481         getBusinessObjectService().save(profileAttributes);
482     }
483 
484     private void persistMatchBo(AgendaBo agendaBo, OleRule rule) {
485         MatchBo matchBo = new MatchBo();
486         matchBo.setAgendaName(agendaBo.getName());
487         matchBo.setDocType(rule.getDocType());
488         String incomingField = rule.getIncomingField().getField() + rule.getIncomingField().getSubfield();
489         String existingField = rule.getExistingField().getField() + rule.getExistingField().getSubfield();
490         matchBo.setIncomingField(incomingField);
491         matchBo.setExistingField(existingField);
492         matchBo.setTermName(rule.getTerm());
493         getBusinessObjectService().save(matchBo);
494     }
495 
496 
497     public Object objectExists(Class objectClass, String key, String value) {
498         HashMap<String, String> map = new HashMap<String, String>();
499         map.put(key, value);
500         List<Object> matching = (List<Object>) getBusinessObjectService().findMatching(objectClass, map);
501         return matching.isEmpty() ? null : matching.get(0);
502     }
503 
504     /**
505      * To clear Profile Data if Profile already Exists
506      *
507      * @param profile
508      */
509     public void deleteAgenda(Profile profile) {
510 
511         Map<String, String> contextPks = new HashMap<String, String>();
512         contextPks.put("nm", profile.getContextName());
513         contextPks.put("nmspc_cd", OLEConstants.OLE_NAMESPACE);
514         ContextBo context = getBusinessObjectService().findByPrimaryKey(ContextBo.class, contextPks);
515         String contextId = context.getId();
516 
517         Map<String, String> agendaPks = new HashMap<String, String>();
518         agendaPks.put("nm", profile.getName());
519         agendaPks.put("cntxt_id", contextId);
520         AgendaBo oldAgenda = getBusinessObjectService().findByPrimaryKey(AgendaBo.class, agendaPks);
521 
522         Map<String, String> itemPks = new HashMap<String, String>();
523         itemPks.put("agenda_id", oldAgenda.getId());
524         List<AgendaItemBo> items = (List<AgendaItemBo>) getBusinessObjectService().findMatching(AgendaItemBo.class, itemPks);
525 
526         getBusinessObjectService().delete(items);
527 
528         getBusinessObjectService().delete(oldAgenda);
529 
530         List<AgendaBo> agendas = context.getAgendas();
531         AgendaBo removeAgenda = null;
532         for (AgendaBo agenda : agendas) {
533             if (agenda.getName().equalsIgnoreCase(oldAgenda.getName())) {
534                 removeAgenda = agenda;
535             }
536         }
537         agendas.remove(removeAgenda);
538         context.setAgendas(agendas);
539         getBusinessObjectService().save(context);
540 
541         deleteRegisteredRules(profile);
542         unregisterDefaultTermResolvers();
543         unregisterFunctionLoader();
544 
545         HashMap matchingProfileAttr = new HashMap();
546         matchingProfileAttr.put("agenda_name", oldAgenda.getName());
547         List<ProfileAttributeBo> profileAttributeBos =
548                 (List<ProfileAttributeBo>) getBusinessObjectService().findMatching(ProfileAttributeBo.class, matchingProfileAttr);
549         getBusinessObjectService().delete(profileAttributeBos);
550 
551         HashMap matchingProfileFacts = new HashMap();
552         matchingProfileFacts.put("agenda_name", oldAgenda.getName());
553         List<MatchBo> matchBos =
554                 (List<MatchBo>) getBusinessObjectService().findMatching(MatchBo.class, matchingProfileFacts);
555         getBusinessObjectService().delete(matchBos);
556     }
557 
558     private void deleteRegisteredRules(Profile profile) {
559         Map map = new HashMap();
560         List<OleRule> rules = profile.getRules();
561         for (Iterator<OleRule> iterator = rules.iterator(); iterator.hasNext(); ) {
562             OleRule oleRule = iterator.next();
563             String term = oleRule.getTerm();
564             deleteTerm(term);
565 
566             map.put("nm", oleRule.getName());
567             List<RuleBo> matching = (List<RuleBo>) getBusinessObjectService().findMatching(RuleBo.class, map);
568             getBusinessObjectService().delete(matching);
569         }
570     }
571 
572     private void deleteTerm(String term) {
573         Map map = new HashMap();
574         map.put("desc_txt", term);
575         List<TermBo> termsToBeDeleted = (List<TermBo>) getBusinessObjectService().findMatching(TermBo.class, map);
576         if (!termsToBeDeleted.isEmpty()) {
577             getBusinessObjectService().delete(termsToBeDeleted);
578         }
579 //        List<TermSpecificationBo> termSpecificationBosToBeDeleted =
580 //                (List<TermSpecificationBo>) getBusinessObjectService().findMatching(TermSpecificationBo.class, map);
581 //        for (Iterator<TermSpecificationBo> iterator = termSpecificationBosToBeDeleted.iterator(); iterator.hasNext(); ) {
582 //            TermSpecificationBo termSpecificationBo = iterator.next();
583 //            Map categoryBoMap = new HashMap();
584 //            categoryBoMap.put("term_spec_id", termSpecificationBo.getId());
585 //            List<CategoryBo> categoryBosToBeDeleted =
586 //                    (List<CategoryBo>) getBusinessObjectService().findMatching(CategoryBo.class, categoryBoMap);
587 //            getBusinessObjectService().delete(categoryBosToBeDeleted);
588 //        }
589 //        getBusinessObjectService().delete(termSpecificationBosToBeDeleted);
590     }
591 
592     private void unregisterFunctionLoader() {
593         HashMap map = new HashMap();
594         map.put("nm", "functionLoader");
595         List<KrmsTypeBo> matching = (List<KrmsTypeBo>) getBusinessObjectService().findMatching(KrmsTypeBo.class, map);
596         if (!matching.isEmpty()) {
597             getBusinessObjectService().delete(matching);
598         }
599     }
600 
601     private void unregisterDefaultTermResolvers() {
602         Map incomingTermMap = new HashMap();
603         incomingTermMap.put("desc_txt", OLEConstants.INCOMING_FIELD);
604         getBusinessObjectService().
605                 delete((List<? extends PersistableBusinessObject>) getBusinessObjectService().
606                         findMatching(TermBo.class, incomingTermMap));
607 
608         Map existingTermMap = new HashMap();
609         existingTermMap.put("desc_txt", OLEConstants.EXISTING_FIELD);
610         getBusinessObjectService().
611                 delete((List<? extends PersistableBusinessObject>) getBusinessObjectService().
612                         findMatching(TermBo.class, existingTermMap));
613 
614 
615         HashMap map = new HashMap();
616         map.put("nm", "Create Bibliographic Record");
617         List<KrmsTypeBo> matching = (List<KrmsTypeBo>) getBusinessObjectService().findMatching(KrmsTypeBo.class, map);
618         if (!matching.isEmpty()) {
619             getBusinessObjectService().delete(matching);
620         }
621 //
622 //        HashMap map1 = new HashMap();
623 //        map1.put("nm", "ProfileTermResolver");
624 //        List<KrmsTypeBo> matching1 = (List<KrmsTypeBo>) getBusinessObjectService().findMatching(KrmsTypeBo.class, map1);
625 //        if (!matching1.isEmpty()) {
626 //            getBusinessObjectService().delete(matching1);
627 //        }
628     }
629 
630     public Boolean doesProfileExist(String profileName) {
631         HashMap<String, String> map = new HashMap<String, String>();
632         map.put("nm", profileName);
633         List<AgendaBo> matching = (List<AgendaBo>) getBusinessObjectService().findMatching(AgendaBo.class, map);
634         return !matching.isEmpty();
635     }
636 
637     private BusinessObjectService getBusinessObjectService() {
638         if (null == businessObjectService) {
639             businessObjectService = KRADServiceLocator.getBusinessObjectService();
640         }
641         return businessObjectService;
642     }
643 
644     private TermBoService getTermBoService() {
645         if (null == termBoService) {
646             termBoService = GlobalResourceLoader.getService("termBoService");
647         }
648         return termBoService;
649     }
650 
651     public ProfileObjectGeneratorFromXML getProfileObjectGeneratorFromXML() {
652         if (null == profileObjectGeneratorFromXML) {
653             profileObjectGeneratorFromXML = new ProfileObjectGeneratorFromXML();
654         }
655         return profileObjectGeneratorFromXML;
656     }
657 
658     public void setProfileObjectGeneratorFromXML(ProfileObjectGeneratorFromXML profileObjectGeneratorFromXML) {
659         this.profileObjectGeneratorFromXML = profileObjectGeneratorFromXML;
660     }
661 }