001package org.kuali.ole.ingest.krms.builder; 002 003import org.apache.log4j.Logger; 004import org.kuali.ole.OLEConstants; 005import org.kuali.ole.docstore.model.xmlpojo.work.bib.marc.DataField; 006import org.kuali.ole.docstore.model.xmlpojo.work.bib.marc.SubField; 007import org.kuali.ole.ingest.krms.pojo.*; 008import org.kuali.ole.ingest.pojo.*; 009import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader; 010import org.kuali.rice.kew.impl.peopleflow.PeopleFlowBo; 011import org.kuali.rice.krad.service.BusinessObjectService; 012import org.kuali.rice.krad.service.KRADServiceLocator; 013import org.kuali.rice.krms.api.repository.term.TermSpecificationDefinition; 014import org.kuali.rice.krms.impl.repository.*; 015 016import java.io.IOException; 017import java.net.URISyntaxException; 018import java.util.*; 019 020/** 021 * Created with IntelliJ IDEA. 022 * User: vivekb 023 * Date: 8/22/12 024 * Time: 3:04 PM 025 * To change this template use File | Settings | File Templates. 026 */ 027public class OleKrmsBuilder { 028 029 private BusinessObjectService businessObjectService; 030 private TermBoService termBoService; 031 private OleKrmsObjectGeneratorFromXML krmsObjectGeneratorFromXML; 032 private CategoryBo category; 033 private ContextBo context; 034 private AgendaBo agendaBo; 035 private RuleBo ruleBo; 036 private HashMap<String,String> operators; 037 private static final Logger LOG = Logger.getLogger(OleKrmsBuilder.class); 038 039 public OleKrmsBuilder() { 040 operators = new HashMap<String, String>(); 041 operators.put("AND","&"); 042 operators.put("OR","|"); 043 operators.put("and","&"); 044 operators.put("or","|"); 045 operators.put("IN","="); 046 operators.put("in","="); 047 operators.put("=","="); 048 operators.put("greaterThan",">"); 049 operators.put("lessThan","<"); 050 operators.put(">",">"); 051 operators.put("<","<"); 052 operators.put("!=","!="); 053 } 054 055 /** 056 * This method returns the contextBo object based on the namespace and contextName parameters. 057 * Creates a new object, stores it in database and returns if none exists, otherwise returns the existing object. 058 * @param namespace 059 * @param contextName 060 * @return contextBo 061 */ 062 063 064 public ContextBo createContext(String namespace, String contextName) { 065 066 ContextBo contextBo; 067 Object existing = objectExists(ContextBo.class, "nm", contextName); 068 if (null == existing) { 069 contextBo = new ContextBo(); 070 contextBo.setNamespace(namespace); 071 contextBo.setName(contextName); 072 getBusinessObjectService().save(contextBo); 073 } else { 074 contextBo = (ContextBo) existing; 075 } 076 return contextBo; 077 } 078 079 /** 080 * This method returns the Object value if it exists or else null value based on objectClass, key and value parameters. 081 * @param objectClass 082 * @param key 083 * @param value 084 * @return object 085 */ 086 public Object objectExists(Class objectClass, String key, String value) { 087 088 HashMap<String, String> map = new HashMap<String, String>(); 089 map.put(key, value); 090 List<Object> matching = (List<Object>) getBusinessObjectService().findMatching(objectClass, map); 091 return matching.isEmpty() ? null : matching.get(0); 092 } 093 094 /** 095 * Returns the businessObjectService value 096 * @return businessObjectService 097 */ 098 private BusinessObjectService getBusinessObjectService() { 099 if (null == businessObjectService) { 100 businessObjectService = KRADServiceLocator.getBusinessObjectService(); 101 } 102 return businessObjectService; 103 } 104 105 /** 106 * This method returns the categoryBo object based on the namespace and the categoryName parameters. 107 * Creates a new object, stores it in database and returns if none exists, otherwise returns the existing object. 108 * @param namespace 109 * @param categoryName 110 * @return categoryBo 111 */ 112 public CategoryBo createCategory(String namespace, String categoryName) { 113 114 CategoryBo categoryBo; 115 Object existing = objectExists(CategoryBo.class, "nm", categoryName); 116 if (existing == null) { 117 categoryBo = new CategoryBo(); 118 categoryBo.setName(categoryName); 119 categoryBo.setNamespace(namespace); 120 getBusinessObjectService().save(categoryBo); 121 } else { 122 categoryBo = (CategoryBo) existing; 123 } 124 return categoryBo; 125 } 126 127 /** 128 * This method save the DefaultFunctionLoader into the database. 129 */ 130 public void registerDefaultFunctionLoader() { 131 if (null == objectExists(KrmsTypeBo.class, "nm", "FunctionLoader")) { 132 KrmsTypeBo krmsTypeBo = new KrmsTypeBo(); 133 krmsTypeBo.setNamespace(OLEConstants.OLE_NAMESPACE); 134 krmsTypeBo.setName("FunctionLoader"); 135 krmsTypeBo.setServiceName("functionLoader"); 136 getBusinessObjectService().save(krmsTypeBo); 137 } 138 } 139 140 /** 141 * This method returns the agendaBo object based on the agendaName and contextBo parameters. 142 * Creates a new object, stores it in database and returns if none exists, otherwise returns the existing object. 143 * @param agendaName 144 * @param contextBo 145 * @return agendaBo 146 */ 147 public AgendaBo createAgenda(String agendaName, ContextBo contextBo) { 148 AgendaBo agendaBo; 149 Object existing = objectExists(AgendaBo.class, "nm", agendaName); 150 if (existing == null) { 151 agendaBo = new AgendaBo(); 152 agendaBo.setActive(true); 153 agendaBo.setName(agendaName); 154 agendaBo.setContext(contextBo); 155 agendaBo.setContextId(contextBo.getId()); 156 getBusinessObjectService().save(agendaBo); 157 List<AgendaBo> agendas = contextBo.getAgendas(); 158 if (null == agendas) { 159 agendas = new ArrayList<AgendaBo>(); 160 } 161 agendas.add(agendaBo); 162 contextBo.setAgendas(agendas); 163 getBusinessObjectService().save(contextBo); 164 } else { 165 agendaBo = (AgendaBo) existing; 166 } 167 return agendaBo; 168 } 169 170 /** 171 * This method returns the termSpecificationBo object based on the namespace, termSpecificationName, type, categoryBoList and contextBoList parameters. 172 * Creates a new object, stores it in database and returns if none exists, otherwise returns the existing object. 173 * @param namespace 174 * @param termSpecificationName 175 * @param type 176 * @param categoryBoList 177 * @param contextBoList 178 * @return termSpecificationBo 179 */ 180 public TermSpecificationBo createTermSpecification(String namespace, String termSpecificationName, String type, List<CategoryBo> categoryBoList, List<ContextBo> contextBoList) { 181 182 TermSpecificationBo termSpecificationBo; 183 Object existing = objectExists(TermSpecificationBo.class, "nm", termSpecificationName); 184 if (existing == null) { 185 termSpecificationBo = new TermSpecificationBo(); 186 termSpecificationBo.setActive(true); 187 termSpecificationBo.setName(termSpecificationName); 188 termSpecificationBo.setNamespace(namespace); 189 termSpecificationBo.setCategories(categoryBoList); 190 termSpecificationBo.setDescription(termSpecificationName); 191 termSpecificationBo.setContexts(contextBoList); 192 termSpecificationBo.setType(type); 193 TermSpecificationDefinition termSpecification = getTermBoService().createTermSpecification(TermSpecificationDefinition.Builder.create(termSpecificationBo).build()); 194 termSpecificationBo = TermSpecificationBo.from(termSpecification); 195 } else { 196 termSpecificationBo = (TermSpecificationBo) existing; 197 } 198 return termSpecificationBo; 199 } 200 201 /** 202 * This method returns the termBoService object through the GlobalResourceLoader class. 203 * @return termBoService 204 */ 205 private TermBoService getTermBoService() { 206 if (null == termBoService) { 207 termBoService = GlobalResourceLoader.getService("termBoService"); 208 } 209 return termBoService; 210 } 211 212 /** 213 * This method returns the termBo object based on the name and termSpecificationBo parameters 214 * Creates a new object, stores it in database and returns if none exists, otherwise returns the existing object. 215 * @param name 216 * @param termSpecificationBo 217 * @return termBo 218 */ 219 public TermBo createTerm(String name, TermSpecificationBo termSpecificationBo) { 220 TermBo termBo; 221 Object existing = objectExists(TermBo.class, "term_spec_id", termSpecificationBo.getId()); 222 if (existing == null) { 223 termBo = new TermBo(); 224 termBo.setSpecification(termSpecificationBo); 225 termBo.setSpecificationId(termSpecificationBo.getId()); 226 termBo.setDescription(name); 227 getBusinessObjectService().save(termBo); 228 } else { 229 termBo = (TermBo) existing; 230 } 231 return termBo; 232 } 233 234 /** 235 * This method returns the ruleBo object based on the name and termSpecificationBo parameters 236 * Creates a new object, stores it in database and returns if none exists, otherwise returns the existing object. 237 * @param namespace 238 * @param ruleName 239 * @return ruleBo 240 */ 241 public RuleBo createRule(String namespace, String ruleName) { 242 RuleBo ruleBo = new RuleBo(); 243 Object existing = objectExists(RuleBo.class, "nm", ruleName); 244 if (existing == null) { 245 ruleBo.setName(ruleName); 246 ruleBo.setNamespace(namespace); 247 getBusinessObjectService().save(ruleBo); 248 } else { 249 ruleBo = (RuleBo) existing; 250 } 251 return ruleBo; 252 } 253 254 /** 255 * This method returns the agendaBo object based on the agendaBo, ruleBo and count parameters 256 * Creates a new object, stores it in database and returns if none exists, otherwise returns the existing object. 257 * @param agendaBo 258 * @param ruleBo 259 * @return agendaBo 260 */ 261 public AgendaBo addRuleToAgenda(AgendaBo agendaBo, RuleBo ruleBo) { 262 AgendaItemBo agendaItemBo = new AgendaItemBo(); 263 agendaItemBo.setAgendaId(agendaBo.getId()); 264 agendaItemBo.setRule(ruleBo); 265 getBusinessObjectService().save(agendaItemBo); 266 List<AgendaItemBo> agendaBoItems = agendaBo.getItems(); 267 if (null == agendaBoItems) { 268 agendaBoItems = new ArrayList<AgendaItemBo>(); 269 } 270 agendaBoItems.add(agendaItemBo); 271 agendaBo.setItems(agendaBoItems); 272 if(agendaBo.getFirstItemId()==null){ 273 agendaBo.setFirstItemId(agendaBoItems.get(0).getId()); 274 getBusinessObjectService().save(agendaBo); 275 } 276 return agendaBo; 277 } 278 279 /** 280 * This method returns the propositionBo object based on the categoryId, propositionDescription, propositionType, rule and seqNum parameters 281 * Creates a new object, stores it in database and returns the object. 282 * @param categoryId 283 * @param propositionDescription 284 * @param propositionType 285 * @param rule 286 * @param seqNum 287 * @return propositionBo 288 */ 289 public PropositionBo createProposition(String categoryId, String propositionDescription, String propositionType, RuleBo rule,Integer seqNum) { 290 PropositionBo propositionBo = new PropositionBo(); 291 propositionBo.setCategoryId(categoryId); 292 propositionBo.setDescription(propositionDescription); 293 propositionBo.setRuleId(rule.getId()); 294 propositionBo.setPropositionTypeCode(propositionType); 295 propositionBo.setCompoundSequenceNumber(seqNum); 296 getBusinessObjectService().save(propositionBo); 297 return propositionBo; 298 } 299 300 /** 301 * This method returns the propositionBo object based on the categoryId, propositionDescription, propositionType, rule and operatorCode parameters 302 * Creates a new object, stores it in database and returns the object. 303 * @param categoryId 304 * @param propositionDescription 305 * @param propositionType 306 * @param rule 307 * @param operatorCode 308 * @return propositionBo 309 */ 310 public PropositionBo createCompoundProposition(String categoryId, String propositionDescription, String propositionType, RuleBo rule,String operatorCode) { 311 PropositionBo propositionBo = new PropositionBo(); 312 propositionBo.setCategoryId(categoryId); 313 propositionBo.setDescription(propositionDescription); 314 propositionBo.setRuleId(rule.getId()); 315 propositionBo.setPropositionTypeCode(propositionType); 316 propositionBo.setCompoundOpCode(operatorCode); 317 getBusinessObjectService().save(propositionBo); 318 return propositionBo; 319 } 320 321 /** 322 * This method returns the functionBo object based on the namespace, termName, returnType, categories and parameterType parameters 323 * Creates a new object, stores it in database and returns if none exists, otherwise returns the existing object. 324 * @param namespace 325 * @param functionName 326 * @param returnType 327 * @param categories 328 * @param parameterTypes 329 * @return functionBo 330 */ 331 public FunctionBo createFunction(String namespace, String functionName, String returnType, List categories,List<String> parameterTypes) { 332 Object existing = objectExists(FunctionBo.class, "nm", functionName); 333 FunctionBo functionBo; 334 if (existing == null) { 335 HashMap<String, String> map = new HashMap<String, String>(); 336 map.put("nm", "FunctionLoader"); 337 List<KrmsTypeBo> matching = 338 (List<KrmsTypeBo>) getBusinessObjectService().findMatching(KrmsTypeBo.class, map); 339 KrmsTypeBo krmsTypeBo = matching.get(0); 340 functionBo = new FunctionBo(); 341 functionBo.setActive(true); 342 functionBo.setName(functionName); 343 functionBo.setNamespace(namespace); 344 functionBo.setReturnType(returnType); 345 functionBo.setCategories(categories); 346 functionBo.setTypeId(krmsTypeBo.getId()); 347 getBusinessObjectService().save(functionBo); 348 List<FunctionParameterBo> parameters = functionBo.getParameters(); 349 if (null == parameters) { 350 parameters = new ArrayList<FunctionParameterBo>(); 351 } 352 FunctionParameterBo functionParameterBo = null; 353 int i =0; 354 for(String parameterType : parameterTypes){ 355 functionParameterBo = new FunctionParameterBo(); 356 functionParameterBo.setFunctionId(functionBo.getId()); 357 functionParameterBo.setParameterType(parameterType); 358 functionParameterBo.setName(functionName); 359 functionParameterBo.setSequenceNumber(i++); 360 361 getBusinessObjectService().save(functionParameterBo); 362 parameters.add(functionParameterBo); 363 } 364 functionBo.setParameters(parameters); 365 366 getBusinessObjectService().save(functionBo); 367 } else { 368 functionBo = (FunctionBo) existing; 369 List<FunctionParameterBo> parameters = functionBo.getParameters(); 370 if (null == parameters) { 371 parameters = new ArrayList<FunctionParameterBo>(); 372 } 373 FunctionParameterBo functionParameterBo = null; 374 int j = 0; 375 for(int i=0 ; i< parameterTypes.size(); i++){ 376 if(parameters.size() > i){ 377 functionParameterBo = parameters.get(i); 378 functionParameterBo.setFunctionId(functionBo.getId()); 379 functionParameterBo.setParameterType(parameterTypes.get(i)); 380 functionParameterBo.setName(functionName); 381 functionParameterBo.setSequenceNumber(j++); 382 383 getBusinessObjectService().save(functionParameterBo); 384 }else{ 385 functionParameterBo = new FunctionParameterBo(); 386 functionParameterBo.setFunctionId(functionBo.getId()); 387 functionParameterBo.setParameterType(parameterTypes.get(i)); 388 functionParameterBo.setName(functionName); 389 functionParameterBo.setSequenceNumber(i++); 390 391 getBusinessObjectService().save(functionParameterBo); 392 parameters.add(functionParameterBo); 393 } 394 } 395 functionBo.setParameters(parameters); 396 397 getBusinessObjectService().save(functionBo); 398 } 399 400 return functionBo; 401 } 402 403 /** 404 * This method returns the PropositionBo object based on the termId and proposition parameters 405 * Creates a new object, stores it in database and returns the object. 406 * @param termId 407 * @param proposition 408 * @return PropositionBo 409 */ 410 public PropositionBo addTermToPropositionParameter(String termId, PropositionBo proposition,Integer seqNum) { 411 PropositionParameterBo propositionParameterBo = new PropositionParameterBo(); 412 propositionParameterBo.setPropId(proposition.getId()); 413 propositionParameterBo.setValue(termId); 414 propositionParameterBo.setSequenceNumber(seqNum); 415 propositionParameterBo.setParameterType("T"); 416 getBusinessObjectService().save(propositionParameterBo); 417 List<PropositionParameterBo> parameters = proposition.getParameters(); 418 if (null == parameters) { 419 parameters = new ArrayList<PropositionParameterBo>(); 420 } 421 parameters.add(propositionParameterBo); 422 proposition.setParameters(parameters); 423 getBusinessObjectService().save(proposition); 424 return proposition; 425 } 426 427 /** 428 * This method returns the PropositionBo object based on the operator and proposition parameters 429 * Creates a new object, stores it in database and returns the object. 430 * @param operator 431 * @param proposition 432 * @return proposition 433 */ 434 public PropositionBo addOperatorToPropositionParameter(String operator,PropositionBo proposition){ 435 PropositionParameterBo propositionParameterBo = new PropositionParameterBo(); 436 propositionParameterBo.setPropId(proposition.getId()); 437 propositionParameterBo.setValue(operator); 438 propositionParameterBo.setSequenceNumber(2); 439 propositionParameterBo.setParameterType("O"); 440 getBusinessObjectService().save(propositionParameterBo); 441 List<PropositionParameterBo> parameters = proposition.getParameters(); 442 if (null == parameters) { 443 parameters = new ArrayList<PropositionParameterBo>(); 444 } 445 parameters.add(propositionParameterBo); 446 proposition.setParameters(parameters); 447 getBusinessObjectService().save(proposition); 448 return proposition; 449 } 450 451 /** 452 * This method returns the PropositionBo object based on the constant and proposition parameters 453 * Creates a new object, stores it in database and returns the object. 454 * @param constant 455 * @param proposition 456 * @return proposition 457 */ 458 public PropositionBo addConstantToPropositionParameter(String constant,PropositionBo proposition){ 459 PropositionParameterBo propositionParameterBo = new PropositionParameterBo(); 460 propositionParameterBo.setPropId(proposition.getId()); 461 propositionParameterBo.setValue(constant!=null && !constant.isEmpty() ? constant : null); 462 propositionParameterBo.setSequenceNumber(1); 463 propositionParameterBo.setParameterType("C"); 464 getBusinessObjectService().save(propositionParameterBo); 465 List<PropositionParameterBo> parameters = proposition.getParameters(); 466 if (null == parameters) { 467 parameters = new ArrayList<PropositionParameterBo>(); 468 } 469 parameters.add(propositionParameterBo); 470 proposition.setParameters(parameters); 471 getBusinessObjectService().save(proposition); 472 return proposition; 473 } 474 475 /** 476 * This method returns the PropositionBo object based on the functionId and proposition parameters 477 * Creates a new object, stores it in database and returns the object. 478 * @param functionId 479 * @param proposition 480 * @return PropositionBo 481 */ 482 public PropositionBo addCustomFunctionToPropositionParameter(String functionId, PropositionBo proposition,Integer seqNum) { 483 PropositionParameterBo propositionParameterBo = new PropositionParameterBo(); 484 propositionParameterBo.setPropId(proposition.getId()); 485 propositionParameterBo.setValue(functionId); 486 propositionParameterBo.setSequenceNumber(seqNum); 487 propositionParameterBo.setParameterType("F"); 488 getBusinessObjectService().save(propositionParameterBo); 489 List<PropositionParameterBo> parameters = proposition.getParameters(); 490 if (null == parameters) { 491 parameters = new ArrayList<PropositionParameterBo>(); 492 } 493 parameters.add(propositionParameterBo); 494 proposition.setParameters(parameters); 495 getBusinessObjectService().save(proposition); 496 return proposition; 497 } 498 499 /** 500 * This method will create context,category and rules for creating agenda and finally persists into database. 501 * @param oleAgenda 502 * @return agendaName 503 */ 504 public String persist(OleKrmsAgenda oleAgenda) { 505 boolean agendaExists = doesKrmsExist(oleAgenda.getName()); 506 if(agendaExists){ 507 deleteAgenda(oleAgenda); 508 } 509 context = createContext(OLEConstants.OLE_NAMESPACE, "OLE-CONTEXT"); 510 category = createCategory(OLEConstants.OLE_NAMESPACE, "OLE_CATEGORY"); 511 512 registerDefaultFunctionLoader(); 513 agendaBo = createAgenda(oleAgenda.getName(), context); 514 createRules(oleAgenda.getRules()); 515 updateAgendaItems(oleAgenda.getRules(),null,null,agendaBo.getId()); 516 persistProfileAttributes(oleAgenda); 517 persistOverlayOptions(oleAgenda); 518 // persistOverlayLookupActions(oleAgenda); 519 return agendaBo.getName(); 520 } 521 522 /** 523 * This method persists the profile attributes in to database. 524 * @param oleAgenda 525 */ 526 private void persistProfileAttributes(OleKrmsAgenda oleAgenda) { 527 List<ProfileAttributeBo> profileAttributes = oleAgenda.getProfileAttributes()!=null?oleAgenda.getProfileAttributes():new ArrayList<ProfileAttributeBo>(); 528 for (Iterator<ProfileAttributeBo> iterator = profileAttributes.iterator(); iterator.hasNext(); ) { 529 ProfileAttributeBo profileAttributeBo = iterator.next(); 530 profileAttributeBo.setAgendaName(oleAgenda.getName()); 531 } 532 getBusinessObjectService().save(profileAttributes); 533 } 534 535 /** 536 * This method persists the overlay options in to database. 537 * @param oleAgenda 538 */ 539 private void persistOverlayOptions(OleKrmsAgenda oleAgenda) { 540 List<OverlayOption> overlayOptionList = oleAgenda.getOverlayOptions()!=null?oleAgenda.getOverlayOptions():new ArrayList<OverlayOption>(); 541 for(OverlayOption overlayOption : overlayOptionList){ 542 overlayOption.setAgendaName(oleAgenda.getName()); 543 getBusinessObjectService().save(overlayOption); 544 for(DataField dataField : overlayOption.getDataFields()){ 545 List<OleDataField> oleDataFieldList = new ArrayList<OleDataField>(); 546 setOleDataField(dataField,oleDataFieldList,overlayOption); 547 getBusinessObjectService().save(oleDataFieldList); 548 if(LOG.isInfoEnabled()){ 549 LOG.info("dataField.getTag---------->"+dataField.getTag()); 550 LOG.info("dataField.getInd1--------->"+dataField.getInd1()); 551 LOG.info("dataField.getInd2--------->"+dataField.getInd2()); 552 for(SubField subField : dataField.getSubFields()){ 553 LOG.info("subField.getCode--------->"+subField.getCode()); 554 } 555 } 556 } 557 } 558 } 559 560 private void setOleDataField(DataField dataField,List<OleDataField> oleDataFieldList,OverlayOption overlayOption){ 561 for(SubField subField : dataField.getSubFields()){ 562 OleDataField oleDataField = new OleDataField(); 563 oleDataField.setOverlayOptionId(overlayOption.getId()); 564 oleDataField.setAgendaName(overlayOption.getAgendaName()); 565 oleDataField.setDataFieldTag(dataField.getTag()); 566 oleDataField.setDataFieldInd1(dataField.getInd1()); 567 oleDataField.setDataFieldInd2(dataField.getInd2()); 568 oleDataField.setSubFieldCode(subField.getCode()); 569 oleDataFieldList.add(oleDataField); 570 } 571 } 572 573 574 private void updateAgendaItems(List<OleKrmsRule> rules,AgendaItemBo oldAgendaItemBo,AgendaItemBo agendaItemBo,String agendaId) { 575 for (Iterator<OleKrmsRule> iterator = rules.iterator(); iterator.hasNext(); ) { 576 OleKrmsRule rule = iterator.next(); 577 Object existing = objectExists(RuleBo.class, "nm", rule.getName()); 578 OleKrmsRuleAction oleKrmsRuleFalseAction= rule.getFalseActions(); 579 OleKrmsRuleAction oleKrmsRuleTrueAction = rule.getTrueActions(); 580 RuleBo oleRuleBo = existing!=null? (RuleBo)existing:null; 581 Map<String, String> rulePk = new HashMap<String, String>() ; 582 rulePk.put("ruleId",oleRuleBo.getId()); 583 rulePk.put("agendaId",agendaId); 584 List<AgendaItemBo> agendaItemBos =(List<AgendaItemBo>) getBusinessObjectService().findMatching(AgendaItemBo.class,rulePk); 585 agendaItemBo = agendaItemBos!=null && agendaItemBos.size()>0?agendaItemBos.get(0):null; 586 if(oleKrmsRuleTrueAction!=null && oleKrmsRuleTrueAction.getKrmsRules()!=null && oleKrmsRuleTrueAction.getKrmsRules().size()>0){ 587 agendaItemBo.setWhenTrue(getTrueOrFalseItem( oleKrmsRuleTrueAction.getKrmsRules().get(0),agendaId)); 588 getBusinessObjectService().save(agendaItemBo); 589 } 590 if(oleKrmsRuleFalseAction!=null && oleKrmsRuleFalseAction.getKrmsRules()!=null && oleKrmsRuleFalseAction.getKrmsRules().size()>0){ 591 agendaItemBo.setWhenFalse(getTrueOrFalseItem( oleKrmsRuleFalseAction.getKrmsRules().get(0),agendaId)); 592 getBusinessObjectService().save(agendaItemBo); 593 } 594 if(rules.size()>1 && !rules.get(0).equals(rule) && oldAgendaItemBo!=null) { 595 rulePk = new HashMap<String, String>() ; 596 rulePk.put("ruleId",oldAgendaItemBo.getRuleId()); 597 rulePk.put("agendaId",agendaId); 598 agendaItemBos =(List<AgendaItemBo>) getBusinessObjectService().findMatching(AgendaItemBo.class,rulePk); 599 oldAgendaItemBo = agendaItemBos!=null && agendaItemBos.size()>0?agendaItemBos.get(0):null; 600 if(oldAgendaItemBo!=null){ 601 oldAgendaItemBo.setAlways(agendaItemBo); 602 getBusinessObjectService().save(oldAgendaItemBo); 603 } 604 } 605 oldAgendaItemBo = agendaItemBo; 606 if(oleKrmsRuleTrueAction!=null && oleKrmsRuleTrueAction.getKrmsRules()!=null){ 607 agendaItemBo = new AgendaItemBo(); 608 updateAgendaItems(oleKrmsRuleTrueAction.getKrmsRules(),oldAgendaItemBo,agendaItemBo,agendaId); 609 } 610 if(oleKrmsRuleFalseAction!=null && oleKrmsRuleFalseAction.getKrmsRules()!=null){ 611 agendaItemBo = new AgendaItemBo(); 612 updateAgendaItems(oleKrmsRuleFalseAction.getKrmsRules(),oldAgendaItemBo,agendaItemBo,agendaId); 613 } 614 } 615 } 616 private AgendaItemBo getTrueOrFalseItem(OleKrmsRule rule,String agendaId) { 617 Object existing = objectExists(RuleBo.class, "nm", rule.getName()); 618 RuleBo oleRuleBo = existing!=null? (RuleBo)existing:null; 619 return getAgendaItemUsingRuleId(oleRuleBo.getId(),agendaId); 620 } 621 private AgendaItemBo getAgendaItemUsingRuleId(String ruleId,String agendaId){ 622 Map<String, String> rulePk = new HashMap<String, String>() ; 623 rulePk.put("ruleId",ruleId); 624 rulePk.put("agendaId",agendaId); 625 List<AgendaItemBo> agendaItemBos =(List<AgendaItemBo>) getBusinessObjectService().findMatching(AgendaItemBo.class,rulePk); 626 for(AgendaItemBo agendaItemBo : agendaItemBos){ 627 rulePk = new HashMap<String, String>() ; 628 rulePk.put("whenFalseId",agendaItemBo.getId()); 629 List<AgendaItemBo> whenFalse =(List<AgendaItemBo>) getBusinessObjectService().findMatching(AgendaItemBo.class,rulePk); 630 631 rulePk = new HashMap<String, String>() ; 632 rulePk.put("whenTrueId",agendaItemBo.getId()); 633 List<AgendaItemBo> whenTrue =(List<AgendaItemBo>) getBusinessObjectService().findMatching(AgendaItemBo.class,rulePk); 634 635 rulePk = new HashMap<String, String>() ; 636 rulePk.put("alwaysId",agendaItemBo.getId()); 637 List<AgendaItemBo> always =(List<AgendaItemBo>) getBusinessObjectService().findMatching(AgendaItemBo.class,rulePk); 638 639 if(whenFalse.size()==0 && whenTrue.size()==0 && always.size()==0){ 640 return agendaItemBo; 641 } 642 } 643 return null; 644 } 645 646 private void createRules(List<OleKrmsRule> oleKrmsRules){ 647 ContextValidActionBo contextValidActionBo = registerDefaultServiceTypes(context); 648 List<OleKrmsRule> rules =oleKrmsRules; 649 650 for (Iterator<OleKrmsRule> iterator = rules.iterator(); iterator.hasNext(); ) { 651 OleKrmsRule rule = iterator.next(); 652 ruleBo = createRule(OLEConstants.OLE_NAMESPACE, rule.getName()); 653 654 addRuleToAgenda(agendaBo, ruleBo); 655 PropositionBo propositionBo = null; 656 OleKrmsProposition oleProposition = rule.getOleProposition(); 657 if(oleProposition!=null){ 658 OleSimpleProposition simpleProposition = oleProposition.getSimpleProposition(); 659 OleCompoundProposition compoundProposition = oleProposition.getCompoundProposition(); 660 if(simpleProposition!=null){ 661 propositionBo = createSimpleProposition(simpleProposition); 662 } 663 if(compoundProposition!=null){ 664 List<PropositionBo> propositionBos = createCompoundProposition(compoundProposition.getCompoundPropositions(),compoundProposition.getSimplePropositions(),operators.get(compoundProposition.getOperator())); 665 propositionBo = propositionBos.get(0); 666 667 } 668 } 669 ruleBo.setProposition(propositionBo); 670 getBusinessObjectService().save(ruleBo); 671 OleKrmsRuleAction oleKrmsRuleTrueAction = rule.getTrueActions(); 672 List<OleKrmsAction> trueActions =oleKrmsRuleTrueAction!=null && oleKrmsRuleTrueAction.getKrmsActions()!=null && oleKrmsRuleTrueAction.getKrmsActions().size()>0 ? oleKrmsRuleTrueAction.getKrmsActions():new ArrayList<OleKrmsAction>(); 673 int i = 0; 674 for (Iterator<OleKrmsAction> oleActionIterator = trueActions.iterator(); oleActionIterator.hasNext(); ) { 675 OleKrmsAction trueAction = oleActionIterator.next(); 676 contextValidActionBo = getContextValidActionBo(context,trueAction); 677 String description = trueAction.getParameters()!=null && trueAction.getParameters().size()>0?trueAction.getParameters().get(0).getValue():"null"; 678 ActionBo actionBo = addActionToRule(OLEConstants.OLE_NAMESPACE, trueAction.getName(),description , contextValidActionBo.getActionTypeId(), ruleBo,i++); 679 addActionAttributes(actionBo,trueAction); 680 } 681 OleKrmsRuleAction oleKrmsRuleFalseAction= rule.getFalseActions(); 682 683 List<OleKrmsAction> falseActions =oleKrmsRuleFalseAction!=null && oleKrmsRuleFalseAction.getKrmsActions()!=null && oleKrmsRuleFalseAction.getKrmsActions().size()>0 ? oleKrmsRuleFalseAction.getKrmsActions():new ArrayList<OleKrmsAction>(); 684 int j=0; 685 if(falseActions.size()>0){ 686 RuleBo falseActionRuleBo = createRule(OLEConstants.OLE_NAMESPACE, rule.getName() + "_falseActionRule"); 687 for (Iterator<OleKrmsAction> oleActionIterator = falseActions.iterator(); oleActionIterator.hasNext(); ) { 688 OleKrmsAction falseAction = oleActionIterator.next(); 689 contextValidActionBo = getContextValidActionBo(context,falseAction); 690 String description = falseAction.getParameters()!=null && falseAction.getParameters().size()>0?falseAction.getParameters().get(0).getValue():"null"; 691 ActionBo actionBo = addActionToRule(OLEConstants.OLE_NAMESPACE, falseAction.getName(), description, contextValidActionBo.getActionTypeId(), falseActionRuleBo, j++); 692 addActionAttributes(actionBo,falseAction); 693 } 694 addDummyRuleForFalseAction(ruleBo, falseActionRuleBo); 695 } 696 if(oleKrmsRuleTrueAction!=null && oleKrmsRuleTrueAction.getKrmsRules()!=null){ 697 createRules(oleKrmsRuleTrueAction.getKrmsRules()); 698 } 699 700 if(oleKrmsRuleFalseAction!=null && oleKrmsRuleFalseAction.getKrmsRules()!=null){ 701 createRules(oleKrmsRuleFalseAction.getKrmsRules()); 702 } 703 } 704 } 705 /** 706 * This method creates compound proposition from the oleProposition and propositionBos parameters 707 * @param compoundPropositions 708 */ 709 private List<PropositionBo> createCompoundProposition(List<OleCompoundProposition> compoundPropositions,List<OleSimpleProposition> simplePropositions,String operator){ 710 if(compoundPropositions!=null){ 711 List<PropositionBo> propositionBos = new ArrayList<PropositionBo>(); 712 PropositionBo propositionBo = null; 713 for(OleCompoundProposition compoundProposition :compoundPropositions){ 714 propositionBos.addAll(createCompoundProposition(compoundProposition.getCompoundPropositions(),compoundProposition.getSimplePropositions(),operators.get(compoundProposition.getOperator()))); 715 } 716 if(simplePropositions!=null){ 717 propositionBos.addAll(createCompoundProposition(null,simplePropositions,operator)); 718 } 719 propositionBo = createCompoundProposition(category.getId(), "compound","C",ruleBo,operator); 720 propositionBo.setCompoundComponents(propositionBos); 721 getBusinessObjectService().save(propositionBo); 722 propositionBos = new ArrayList<PropositionBo>(); 723 propositionBos.add(propositionBo); 724 return propositionBos; 725 }else if(simplePropositions!=null){ 726 List<PropositionBo> propositionBos = new ArrayList<PropositionBo>() ; 727 PropositionBo propositionBo = null; 728 for(OleSimpleProposition simpleProposition : simplePropositions){ 729 propositionBos.add(createSimpleProposition(simpleProposition)); 730 } 731 if(simplePropositions.size()>1){ 732 propositionBo = createCompoundProposition(category.getId(), "compound","C",ruleBo,operator); 733 propositionBo.setCompoundComponents(propositionBos); 734 getBusinessObjectService().save(propositionBo); 735 propositionBos = new ArrayList<PropositionBo>(); 736 propositionBos.add(propositionBo); 737 } 738 return propositionBos; 739 } 740 return null; 741 } 742 743 /** 744 * This method adds the action attributes to create the agenda. 745 * @param action 746 * @param oleKrmsAction 747 */ 748 private void addActionAttributes(ActionBo action,OleKrmsAction oleKrmsAction){ 749 Set<ActionAttributeBo> actionAttributeBos = new HashSet<ActionAttributeBo>(); 750 List<OleParameter> oleParameters = oleKrmsAction.getParameters()!=null ? oleKrmsAction.getParameters() :new ArrayList<OleParameter>(); 751 for(OleParameter oleParameter : oleParameters) { 752 Map<String, String> peopleFlowPk = new HashMap<String, String>(); 753 peopleFlowPk.put("nm", oleParameter.getValue()); 754 List<PeopleFlowBo> peopleFlowBos =(List<PeopleFlowBo>) getBusinessObjectService().findMatching(PeopleFlowBo.class, peopleFlowPk); 755 if(peopleFlowBos!=null && peopleFlowBos.size()>0){ 756 Map<String, String> attrDefPk = new HashMap<String, String>(); 757 attrDefPk.put("nm", "peopleFlowId"); 758 attrDefPk.put("nmspc_cd", "KR-RULE"); 759 KrmsAttributeDefinitionBo peopleFlowIdDef = getKrmsAttributeDefinitionBo(attrDefPk); 760 attrDefPk = new HashMap<String, String>(); 761 attrDefPk.put("nm", "peopleFlowName"); 762 attrDefPk.put("nmspc_cd", "KR-RULE"); 763 KrmsAttributeDefinitionBo peopleFlowNameDef = getKrmsAttributeDefinitionBo(attrDefPk); 764 String peopleFlowId = peopleFlowBos.get(0).getId(); 765 ActionAttributeBo actionAttributeBo = new ActionAttributeBo(); 766 actionAttributeBo.setActionId(action.getId()); 767 actionAttributeBo.setAttributeDefinitionId(peopleFlowIdDef.getId()); 768 actionAttributeBo.setAttributeDefinition(peopleFlowIdDef); 769 actionAttributeBo.setValue(peopleFlowId); 770 getBusinessObjectService().save(actionAttributeBo); 771 actionAttributeBos.add(actionAttributeBo); 772 773 actionAttributeBo = new ActionAttributeBo(); 774 actionAttributeBo.setActionId(action.getId()); 775 actionAttributeBo.setAttributeDefinitionId(peopleFlowNameDef.getId()); 776 actionAttributeBo.setAttributeDefinition(peopleFlowNameDef); 777 actionAttributeBo.setValue(oleParameter.getValue()); 778 getBusinessObjectService().save(actionAttributeBo); 779 actionAttributeBos.add(actionAttributeBo); 780 } else{ 781 KrmsAttributeDefinitionBo attributeDefinitionBo = createAttributeDefinition(oleParameter); 782 //createTypeAttribute(attributeDefinitionBo,action); 783 actionAttributeBos.add(createActionAttribute(attributeDefinitionBo,action,oleParameter)); 784 } 785 } 786 if(actionAttributeBos.size()>0){ 787 action.setAttributeBos(actionAttributeBos); 788 getBusinessObjectService().save(action); 789 } 790 791 } 792 793 private KrmsAttributeDefinitionBo getKrmsAttributeDefinitionBo( Map<String, String> attrDefPk){ 794 List<KrmsAttributeDefinitionBo> krmsAttributeDefinitionBos = ( List<KrmsAttributeDefinitionBo>)getBusinessObjectService().findMatching(KrmsAttributeDefinitionBo.class,attrDefPk); 795 return krmsAttributeDefinitionBos!=null && krmsAttributeDefinitionBos.size()>0 ? krmsAttributeDefinitionBos.get(0) : null; 796 } 797 private KrmsAttributeDefinitionBo createAttributeDefinition(OleParameter oleParameter){ 798 Map<String, String> attrDefPk = new HashMap<String, String>(); 799 attrDefPk.put("nm", oleParameter.getName()); 800 attrDefPk.put("nmspc_cd",OLEConstants.OLE_NAMESPACE); 801 KrmsAttributeDefinitionBo attributeDefinitionBo = getBusinessObjectService().findByPrimaryKey(KrmsAttributeDefinitionBo.class,attrDefPk); 802 if(attributeDefinitionBo==null){ 803 attributeDefinitionBo = new KrmsAttributeDefinitionBo(); 804 } 805 attributeDefinitionBo.setNamespace(OLEConstants.OLE_NAMESPACE); 806 attributeDefinitionBo.setName(oleParameter.getName()); 807 getBusinessObjectService().save(attributeDefinitionBo); 808 return attributeDefinitionBo; 809 } 810 private KrmsAttributeDefinitionBo createTypeAttribute(KrmsAttributeDefinitionBo attributeDefinitionBo,ActionBo action){ 811 Map<String, String> typeAttrPk = new HashMap<String, String>(); 812 typeAttrPk.put("typ_id", action.getTypeId()); 813 typeAttrPk.put("attr_defn_id",attributeDefinitionBo.getId()); 814 KrmsTypeAttributeBo typeAttributeBo = getBusinessObjectService().findByPrimaryKey(KrmsTypeAttributeBo.class,typeAttrPk); 815 if(typeAttributeBo==null){ 816 typeAttributeBo = new KrmsTypeAttributeBo(); 817 } 818 typeAttributeBo.setAttributeDefinitionId(attributeDefinitionBo.getId()); 819 typeAttributeBo.setTypeId(action.getTypeId()); 820 typeAttributeBo.setSequenceNumber(0); 821 getBusinessObjectService().save(typeAttributeBo); 822 return attributeDefinitionBo; 823 } 824 private ActionAttributeBo createActionAttribute(KrmsAttributeDefinitionBo attributeDefinitionBo,ActionBo action,OleParameter oleParameter){ 825 /*Map<String, String> actnAttrPk = new HashMap<String, String>(); 826 actnAttrPk.put("attr_defn_id",attributeDefinitionBo.getId()); 827 ActionAttributeBo actionAttributeBo = getBusinessObjectService().findByPrimaryKey(ActionAttributeBo.class,actnAttrPk); 828 if(actionAttributeBo==null){*/ 829 // } 830 ActionAttributeBo actionAttributeBo = new ActionAttributeBo(); 831 actionAttributeBo.setActionId(action.getId()); 832 actionAttributeBo.setAttributeDefinitionId(attributeDefinitionBo.getId()); 833 actionAttributeBo.setAttributeDefinition(attributeDefinitionBo); 834 actionAttributeBo.setValue(oleParameter.getValue()); 835 getBusinessObjectService().save(actionAttributeBo); 836 return actionAttributeBo; 837 } 838 /** 839 * This method returns the contextValidActionBo object based on the contextBo and action parameters 840 * Creates a new object, stores it in database and returns if none exists, otherwise returns the existing object. 841 * @param contextBo 842 * @param action 843 * @return contextValidActionBo 844 */ 845 private ContextValidActionBo getContextValidActionBo(ContextBo contextBo,OleKrmsAction action){ 846 KrmsTypeBo actionTypeService = getActiontypeService(action); 847 ContextValidActionBo contextValidActionBo =null; 848 if(actionTypeService!=null){ 849 Object existing = objectExists(ContextValidActionBo.class, "actn_typ_id", actionTypeService.getId()); 850 if (existing == null) { 851 contextValidActionBo = new ContextValidActionBo(); 852 contextValidActionBo.setActionType(actionTypeService); 853 contextValidActionBo.setActionTypeId(actionTypeService.getId()); 854 contextValidActionBo.setContextId(contextBo.getId()); 855 getBusinessObjectService().save(contextValidActionBo); 856 } else { 857 contextValidActionBo = (ContextValidActionBo) existing; 858 } 859 } 860 if(contextValidActionBo==null){ 861 contextValidActionBo = registerDefaultServiceTypes(contextBo); 862 } 863 return contextValidActionBo; 864 } 865 866 /** 867 * Lookups the database for the particular Servicename and the returns the krmsTypeBo object. 868 * @param action 869 * @return krmsTypeBo 870 */ 871 private KrmsTypeBo getActiontypeService(OleKrmsAction action){ 872 Map<String, String> krmsTypePk = new HashMap<String, String>(); 873 krmsTypePk.put("nm", action.getName()); 874 krmsTypePk.put("nmspc_cd",OLEConstants.OLE_NAMESPACE); 875 KrmsTypeBo krmsTypeBo = getBusinessObjectService().findByPrimaryKey(KrmsTypeBo.class, krmsTypePk); 876 if(krmsTypeBo==null){ 877 krmsTypePk.put("nmspc_cd","KR-RULE"); 878 krmsTypeBo = getBusinessObjectService().findByPrimaryKey(KrmsTypeBo.class, krmsTypePk); 879 } 880 return krmsTypeBo; 881 } 882 883 /** 884 * This method persists the matchBo object based on the agendaBo and proposition parameters.. 885 * @param agendaBo 886 * @param term 887 */ 888 private void persistMatchBo(AgendaBo agendaBo, OleTerm term) { 889 MatchBo matchBo = new MatchBo(); 890 matchBo.setAgendaName(agendaBo.getName()); 891 matchBo.setTermName(term.getValue()); 892 String[] termNames = term.getValue().split("-"); 893 if(termNames[0].equalsIgnoreCase(OLEConstants.INCOMING_FIELD)){ 894 matchBo.setIncomingField(termNames[1]); 895 } 896 else if(termNames[0].equalsIgnoreCase(OLEConstants.EXISTING_FIELD)){ 897 matchBo.setExistingField(termNames[1]); 898 } 899 getBusinessObjectService().save(matchBo); 900 } 901 902 /** 903 * This method builds the Krms object from the fileContent parameter and the returns the list of agendaNames from the Krms object. 904 * @param fileContent 905 * @return agendaNames 906 * @throws java.io.IOException 907 * @throws java.net.URISyntaxException 908 */ 909 public List<String> persistKrmsFromFileContent(String fileContent) throws IOException, URISyntaxException { 910 OleKrms krms = getKrmsObjectGeneratorFromXML().buildKrmsFromFileContent(fileContent); 911 List<String> agendaNames = new ArrayList<String>(); 912 for(OleKrmsAgenda oleAgenda :krms.getAgendas()){ 913 agendaNames.add(persist(oleAgenda)); 914 } 915 updateDefinitions(); 916 return agendaNames; 917 918 } 919 920 private void updateDefinitions(){ 921 Map<String, Object> contextMap = new HashMap<String, Object>(); 922 contextMap.put("name", "OLE-CONTEXT"); 923 contextMap.put("namespace", "OLE"); 924 ContextBo contextBo = getBusinessObjectService().findByPrimaryKey(ContextBo.class, contextMap); 925 KrmsRepositoryServiceLocator.getContextBoService().updateContext(ContextBo.to(contextBo)); 926 } 927 928 /** 929 * Gets the krmsObjectGeneratorFromXML object if exists, otherwise it creates a new object. 930 * @return krmsObjectGeneratorFromXML 931 */ 932 public OleKrmsObjectGeneratorFromXML getKrmsObjectGeneratorFromXML() { 933 if(null==krmsObjectGeneratorFromXML){ 934 krmsObjectGeneratorFromXML = new OleKrmsObjectGeneratorFromXML(); 935 } 936 return krmsObjectGeneratorFromXML; 937 } 938 939 /** 940 * Sets the krmsObjectGeneratorFromXML value. 941 * @param krmsObjectGeneratorFromXML 942 */ 943 public void setKrmsObjectGeneratorFromXML(OleKrmsObjectGeneratorFromXML krmsObjectGeneratorFromXML) { 944 this.krmsObjectGeneratorFromXML = krmsObjectGeneratorFromXML; 945 } 946 947 /** 948 * Lookups the database based on the agendaName and returns a boolean value. 949 * @param agendaName 950 * @return Boolean 951 */ 952 public Boolean doesKrmsExist(String agendaName) { 953 HashMap<String, String> map = new HashMap<String, String>(); 954 map.put("nm", agendaName); 955 List<AgendaBo> matching = (List<AgendaBo>) getBusinessObjectService().findMatching(AgendaBo.class, map); 956 return !matching.isEmpty(); 957 } 958 959 /** 960 * This method deletes the term record from database. 961 * @param oleAgenda 962 */ 963 public void deleteAgenda(OleKrmsAgenda oleAgenda) { 964 965 LOG.info(" inside deleteAgenda method "); 966 Map<String, String> contextPks = new HashMap<String, String>(); 967 contextPks.put("nm", "OLE-CONTEXT"); 968 contextPks.put("nmspc_cd", OLEConstants.OLE_NAMESPACE); 969 ContextBo context = getBusinessObjectService().findByPrimaryKey(ContextBo.class, contextPks); 970 String contextId = context.getId(); 971 972 Map<String, String> agendaPks = new HashMap<String, String>(); 973 agendaPks.put("nm", oleAgenda.getName()); 974 agendaPks.put("cntxt_id", contextId); 975 AgendaBo oldAgenda = getBusinessObjectService().findByPrimaryKey(AgendaBo.class, agendaPks); 976 977 Map<String, String> itemPks = new HashMap<String, String>(); 978 LOG.info(" oldAgenda.getId() --------------> " + oldAgenda.getId()); 979 itemPks.put("agenda_id", oldAgenda.getId()); 980 List<AgendaItemBo> items = (List<AgendaItemBo>) getBusinessObjectService().findMatching(AgendaItemBo.class, itemPks); 981 for(AgendaItemBo agendaItemBo : items){ 982 itemPks = new HashMap<String, String>(); 983 itemPks.put("rule_id", agendaItemBo.getRuleId()); 984 List<AgendaItemBo> agendaItemBos =(List<AgendaItemBo>) getBusinessObjectService().findMatching(AgendaItemBo.class, itemPks); 985 if(agendaItemBos.size()>1){ 986 agendaItemBo.setRule(null); 987 agendaItemBo.setRuleId(null); 988 } 989 if(agendaItemBo.getWhenTrue()!=null){ 990 agendaItemBo.setWhenTrue(null); 991 agendaItemBo.setWhenTrueId(null); 992 } 993 if(agendaItemBo.getWhenFalse()!=null){ 994 agendaItemBo.setWhenFalse(null); 995 agendaItemBo.setWhenFalseId(null); 996 } 997 if(agendaItemBo.getAlways()!=null){ 998 agendaItemBo.setAlways(null); 999 agendaItemBo.setAlwaysId(null); 1000 } 1001 getBusinessObjectService().save(agendaItemBo); 1002 } 1003 1004 itemPks = new HashMap<String, String>(); 1005 itemPks.put("agenda_id", oldAgenda.getId()); 1006 items = (List<AgendaItemBo>) getBusinessObjectService().findMatching(AgendaItemBo.class, itemPks); 1007 for(AgendaItemBo agendaItemBo : items){ 1008 getBusinessObjectService().delete(agendaItemBo); 1009 } 1010 1011 getBusinessObjectService().delete(oldAgenda); 1012 List<AgendaBo> agendas = context.getAgendas(); 1013 AgendaBo removeAgenda = null; 1014 for (AgendaBo agenda : agendas) { 1015 if (agenda.getName().equalsIgnoreCase(oldAgenda.getName())) { 1016 removeAgenda = agenda; 1017 } 1018 } 1019 agendas.remove(removeAgenda); 1020 context.setAgendas(agendas); 1021 getBusinessObjectService().save(context); 1022 1023 // deleteRegisteredRules(oleAgenda); 1024 // unregisterFunctionLoader(); 1025 1026 HashMap matchingProfileAttr = new HashMap(); 1027 matchingProfileAttr.put("agenda_name", oldAgenda.getName()); 1028 List<ProfileAttributeBo> profileAttributeBos = 1029 (List<ProfileAttributeBo>) getBusinessObjectService().findMatching(ProfileAttributeBo.class, matchingProfileAttr); 1030 getBusinessObjectService().delete(profileAttributeBos); 1031 1032 HashMap matchingOleDataField = new HashMap(); 1033 matchingProfileAttr.put("agenda_name", oldAgenda.getName()); 1034 List<OleDataField> matchingOleDataFields = 1035 (List<OleDataField>) getBusinessObjectService().findMatching(OleDataField.class, matchingOleDataField); 1036 getBusinessObjectService().delete(matchingOleDataFields); 1037 1038 HashMap matchingOverlayOption = new HashMap(); 1039 matchingProfileAttr.put("agenda_name", oldAgenda.getName()); 1040 List<OverlayOption> matchingOverlayOptions = 1041 (List<OverlayOption>) getBusinessObjectService().findMatching(OverlayOption.class, matchingOverlayOption); 1042 getBusinessObjectService().delete(matchingOverlayOptions); 1043 1044/* HashMap matchingOverlayLookupAction = new HashMap(); 1045 matchingProfileAttr.put("agenda_name", oldAgenda.getName()); 1046 List<OverlayLookupAction> matchingOverlayLookupActions = 1047 (List<OverlayLookupAction>) getBusinessObjectService().findMatching(OverlayLookupAction.class, matchingOverlayLookupAction); 1048 getBusinessObjectService().delete(matchingOverlayLookupActions); 1049 1050 HashMap matchingOverlayLookupTable = new HashMap(); 1051 matchingProfileAttr.put("agenda_name", oldAgenda.getName()); 1052 List<OverlayLookupTable> matchingOverlayLookupTables = 1053 (List<OverlayLookupTable>) getBusinessObjectService().findMatching(OverlayLookupTable.class, matchingOverlayLookupTable); 1054 getBusinessObjectService().delete(matchingOverlayLookupTables);*/ 1055 1056 HashMap matchingProfileFacts = new HashMap(); 1057 matchingProfileFacts.put("agenda_name", oldAgenda.getName()); 1058 List<MatchBo> matchBos = 1059 (List<MatchBo>) getBusinessObjectService().findMatching(MatchBo.class, matchingProfileFacts); 1060 getBusinessObjectService().delete(matchBos); 1061 } 1062 1063 /** 1064 * This method deletes the functionLoader record from database. 1065 */ 1066 private void unregisterFunctionLoader() { 1067 HashMap map = new HashMap(); 1068 map.put("nm", "functionLoader"); 1069 List<KrmsTypeBo> matching = (List<KrmsTypeBo>) getBusinessObjectService().findMatching(KrmsTypeBo.class, map); 1070 if (!matching.isEmpty()) { 1071 getBusinessObjectService().delete(matching); 1072 } 1073 } 1074 /** 1075 * This method deletes the term related rules from database. 1076 * @param oleAgenda 1077 */ 1078 private void deleteRegisteredRules(OleKrmsAgenda oleAgenda) { 1079 Map map = new HashMap(); 1080 List<OleKrmsRule> rules = oleAgenda.getRules(); 1081 for (Iterator<OleKrmsRule> iterator = rules.iterator(); iterator.hasNext(); ) { 1082 OleKrmsRule krmsRule = iterator.next(); 1083 OleSimpleProposition simpleProposition = krmsRule.getOleProposition().getSimpleProposition(); 1084 OleCompoundProposition compoundProposition = krmsRule.getOleProposition().getCompoundProposition(); 1085 if(simpleProposition!=null){ 1086 List<OleTerm> terms =simpleProposition.getTerms(); 1087 for(OleTerm term : terms){ 1088 deleteTerm(term.getValue()) ; 1089 } 1090 } 1091 if(compoundProposition!=null){ 1092 deleteCompoundProposition(compoundProposition); 1093 } 1094 map.put("nm", krmsRule.getName()+oleAgenda.getName()); 1095 List<RuleBo> matching = (List<RuleBo>) getBusinessObjectService().findMatching(RuleBo.class, map); 1096 getBusinessObjectService().delete(matching); 1097 } 1098 } 1099 1100 /** 1101 * This method deletes the compound proposition based on the oleProposition from database. 1102 * @param compoundProposition 1103 */ 1104 private void deleteCompoundProposition(OleCompoundProposition compoundProposition){ 1105 for(OleCompoundProposition compoundProposition1 : compoundProposition.getCompoundPropositions()!=null?compoundProposition.getCompoundPropositions():new ArrayList<OleCompoundProposition>()){ 1106 deleteCompoundProposition(compoundProposition1); 1107 } 1108 for(OleSimpleProposition simpleProposition : compoundProposition.getSimplePropositions()!=null?compoundProposition.getSimplePropositions():new ArrayList<OleSimpleProposition>()){ 1109 List<OleTerm> terms =simpleProposition.getTerms(); 1110 for(OleTerm term : terms){ 1111 deleteTerm(term.getValue()) ; 1112 } 1113 } 1114 } 1115 1116 /** 1117 * This method deletes the term from database. 1118 * @param term 1119 */ 1120 private void deleteTerm(String term) { 1121 Map map = new HashMap(); 1122 map.put("desc_txt", term); 1123 List<TermBo> termsToBeDeleted = (List<TermBo>) getBusinessObjectService().findMatching(TermBo.class, map); 1124 if (!termsToBeDeleted.isEmpty()) { 1125 getBusinessObjectService().delete(termsToBeDeleted); 1126 } 1127 } 1128 1129 /** 1130 * This method registers the default service types in the database for the context. 1131 * @param contextBo 1132 * @return ContextValidActionBo 1133 */ 1134 public ContextValidActionBo registerDefaultServiceTypes(ContextBo contextBo) { 1135 KrmsTypeBo actionTypeService = createActionTypeService(); 1136 ContextValidActionBo contextValidActionBo; 1137 Object existing = objectExists(ContextValidActionBo.class, "actn_typ_id", actionTypeService.getId()); 1138 if (existing == null) { 1139 contextValidActionBo = new ContextValidActionBo(); 1140 contextValidActionBo.setActionType(actionTypeService); 1141 contextValidActionBo.setActionTypeId(actionTypeService.getId()); 1142 contextValidActionBo.setContextId(contextBo.getId()); 1143 getBusinessObjectService().save(contextValidActionBo); 1144 } else { 1145 contextValidActionBo = (ContextValidActionBo) existing; 1146 } 1147 return contextValidActionBo; 1148 } 1149 /** 1150 * This method create Bibliographic record action as type in the database for creating custom action. 1151 * @return actionTypeService 1152 */ 1153 public KrmsTypeBo createActionTypeService() { 1154 KrmsTypeBo actionTypeService; 1155 String typeName = "KRMS Action"; 1156 Object existing = objectExists(KrmsTypeBo.class, "nm", typeName); 1157 if (existing == null) { 1158 actionTypeService = new KrmsTypeBo(); 1159 actionTypeService.setActive(true); 1160 actionTypeService.setName("KRMS Action"); 1161 actionTypeService.setNamespace(OLEConstants.OLE_NAMESPACE); 1162 actionTypeService.setServiceName("krmsActionTypeService"); 1163 getBusinessObjectService().save(actionTypeService); 1164 } else { 1165 actionTypeService = (KrmsTypeBo) existing; 1166 } 1167 return actionTypeService; 1168 } 1169 /** 1170 * This method adds the action into rule to create the agenda. 1171 * @param namespace 1172 * @param actionName 1173 * @param description 1174 * @param actionTypeId 1175 * @param ruleBo 1176 * @param sequenceNumber 1177 * @return ActionBo 1178 */ 1179 public ActionBo addActionToRule(String namespace, String actionName, String description, String actionTypeId, RuleBo ruleBo, Integer sequenceNumber) { 1180 ActionBo actionBo = new ActionBo(); 1181 HashMap<String, String> map = new HashMap<String, String>(); 1182 map.put("nm", actionName); 1183 map.put("rule_id",ruleBo.getId()); 1184 List<ActionBo> matching = (List<ActionBo>) getBusinessObjectService().findMatching(ActionBo.class, map); 1185 Object existing = matching.isEmpty() ? null : matching.get(0); 1186 if (existing == null) { 1187 actionBo.setName(actionName); 1188 actionBo.setRuleId(ruleBo.getId()); 1189 actionBo.setNamespace(namespace); 1190 actionBo.setSequenceNumber(sequenceNumber); 1191 actionBo.setDescription(description); 1192 actionBo.setTypeId(actionTypeId); 1193 getBusinessObjectService().save(actionBo); 1194 List<ActionBo> actionBos = ruleBo.getActions(); 1195 if (null == actionBos) { 1196 actionBos = new ArrayList<ActionBo>(); 1197 } 1198 actionBos.add(actionBo); 1199 ruleBo.setActions(actionBos); 1200 getBusinessObjectService().save(ruleBo); 1201 } else { 1202 actionBo = (ActionBo) existing; 1203 } 1204 return actionBo; 1205 } 1206 1207 /** 1208 * This method adds the dummy rule into database for falseAction. 1209 * @param ruleBo 1210 * @param falseActionRuleBo 1211 * @return AgendaItemBo 1212 */ 1213 public AgendaItemBo addDummyRuleForFalseAction(RuleBo ruleBo, RuleBo falseActionRuleBo) { 1214 HashMap<String, String> map = new HashMap<String, String>(); 1215 map.put("rule_id", ruleBo.getId()); 1216 List<AgendaItemBo> matching = 1217 (List<AgendaItemBo>) getBusinessObjectService().findMatching(AgendaItemBo.class, map); 1218 AgendaItemBo existingAgendaItemBo = matching.get(0); 1219 1220 AgendaItemBo falseActionAgendaItemBo = new AgendaItemBo(); 1221 falseActionAgendaItemBo.setAgendaId(existingAgendaItemBo.getAgendaId()); 1222 RuleBo falseRuleBo = getBusinessObjectService().findBySinglePrimaryKey(RuleBo.class,falseActionRuleBo.getId()); 1223 if(falseRuleBo!=null){ 1224 falseActionAgendaItemBo.setRule(falseRuleBo); 1225 falseActionAgendaItemBo.setRuleId(falseRuleBo.getId()); 1226 } 1227 getBusinessObjectService().save(falseActionAgendaItemBo); 1228 existingAgendaItemBo.setWhenFalse(falseActionAgendaItemBo); 1229 getBusinessObjectService().save(existingAgendaItemBo); 1230 return existingAgendaItemBo; 1231 } 1232 1233 1234 /** 1235 * This method creates a simple proposition based on the proposition and seqNum parameters and returns the propositionBo. 1236 * @param proposition 1237 * @return PropositionBo 1238 */ 1239 private PropositionBo createSimpleProposition(OleSimpleProposition proposition){ 1240 List<String> parameterTypes = new ArrayList<String>(); 1241 List<OleTerm> terms = proposition.getTerms(); 1242 OleTerm term =terms.get(0); 1243 if(terms.size()>1){ 1244 return createSimplePropositionWithTerms(proposition, terms,parameterTypes) ; 1245 } 1246 List<OleValue> values = proposition.getValues()!=null?proposition.getValues():new ArrayList<OleValue>(); 1247 1248 TermSpecificationBo termSpecification = 1249 createTermSpecification(OLEConstants.OLE_NAMESPACE,term.getValue(), term.getType(), Arrays.asList(category), Arrays.asList(context)); 1250 1251 TermBo termBo = createTerm(term.getValue(), termSpecification); 1252 List<PropositionBo> propositionBos = new ArrayList<PropositionBo>(); 1253 int i =0; 1254 PropositionBo propositionBo = null; 1255 for(OleValue value :values){ 1256 1257 propositionBo = createProposition(category.getId(), term.getValue() + value.getName(), "S", ruleBo,i++); 1258 1259 addTermToPropositionParameter(termBo.getId(), propositionBo,0); 1260 1261 addConstantToPropositionParameter(value.getName(),propositionBo); 1262 1263 addOperatorToPropositionParameter(operators.get(proposition.getOperator()),propositionBo); 1264 1265 propositionBos.add(propositionBo); 1266 1267 } 1268 if(propositionBo==null && proposition.getFunction()!=null){ 1269 1270 propositionBo = createProposition(category.getId(), term.getValue() + proposition.getFunction(), "S", ruleBo,i++); 1271 1272 1273 parameterTypes.add(term.getType()); 1274 1275 FunctionBo function = createFunction(OLEConstants.OLE_NAMESPACE, proposition.getFunction(), "java.lang.Boolean", Arrays.asList(category),parameterTypes); 1276 1277 addTermToPropositionParameter(termBo.getId(), propositionBo,0); 1278 1279 addCustomFunctionToPropositionParameter(function.getId(),propositionBo,1); 1280 } 1281 if(propositionBos.size()>1){ 1282 propositionBo = createCompoundProposition(category.getId(),term.getValue() + "compound","C",ruleBo,"|"); 1283 propositionBo.setCompoundComponents(propositionBos); 1284 getBusinessObjectService().save(propositionBo); 1285 } 1286 persistMatchBo(agendaBo, term); 1287 return propositionBo; 1288 } 1289 1290 private PropositionBo createSimplePropositionWithTerms(OleSimpleProposition proposition, List<OleTerm> terms,List<String> parameterTypes) { 1291 PropositionBo propositionBo = createProposition(category.getId(), "Two Terms", "S", ruleBo,0); 1292 int seqNum=0; 1293 for(OleTerm term :terms){ 1294 TermSpecificationBo termSpecification = 1295 createTermSpecification(OLEConstants.OLE_NAMESPACE,term.getValue(), term.getType(), Arrays.asList(category), Arrays.asList(context)); 1296 TermBo termBo = createTerm(term.getValue(), termSpecification); 1297 addTermToPropositionParameter(termBo.getId(), propositionBo,seqNum); 1298 parameterTypes.add(term.getType()) ; 1299 persistMatchBo(agendaBo, term); 1300 seqNum ++; 1301 } 1302 if(proposition.getFunction()!=null){ 1303 FunctionBo function = createFunction(OLEConstants.OLE_NAMESPACE, proposition.getFunction(), "java.lang.Boolean", Arrays.asList(category),parameterTypes); 1304 addCustomFunctionToPropositionParameter(function.getId(),propositionBo,seqNum); 1305 } 1306 if(proposition.getOperator()!=null){ 1307 addOperatorToPropositionParameter(operators.get(proposition.getOperator()),propositionBo); 1308 } 1309 getBusinessObjectService().save(propositionBo); 1310 return propositionBo; 1311 } 1312 1313 1314 /** 1315 * Gets the category attribute. 1316 * @return Returns the category 1317 */ 1318 public CategoryBo getCategory() { 1319 return category; 1320 } 1321 1322 /** 1323 * Sets the category attribute value. 1324 * @param category .The category to set. 1325 */ 1326 public void setCategory(CategoryBo category) { 1327 this.category = category; 1328 } 1329 1330 /** 1331 * Gets the context attribute. 1332 * @return Returns the context 1333 */ 1334 public ContextBo getContext() { 1335 return context; 1336 } 1337 1338 /** 1339 * Sets the Context attribute value. 1340 * @param context 1341 */ 1342 public void setContext(ContextBo context) { 1343 this.context = context; 1344 } 1345 1346 /** 1347 * Gets the agendaBo attribute. 1348 * @return Returns agendaBo. 1349 */ 1350 public AgendaBo getAgendaBo() { 1351 return agendaBo; 1352 } 1353 1354 /** 1355 * Sets the agendaBo attribute value. 1356 * @param agendaBo 1357 */ 1358 public void setAgendaBo(AgendaBo agendaBo) { 1359 this.agendaBo = agendaBo; 1360 } 1361 1362 /** 1363 * Gets the ruleBo attribute. 1364 * @return Returns ruleBo. 1365 */ 1366 public RuleBo getRuleBo() { 1367 return ruleBo; 1368 } 1369 1370 /** 1371 * Sets the ruleBo attribute value. 1372 * @param ruleBo 1373 */ 1374 public void setRuleBo(RuleBo ruleBo) { 1375 this.ruleBo = ruleBo; 1376 } 1377}