1 23 24 package org.infoglue.cms.controllers.kernel.impl.simple; 25 26 import java.util.ArrayList ; 27 import java.util.Collection ; 28 import java.util.HashMap ; 29 import java.util.Iterator ; 30 import java.util.List ; 31 import java.util.Map ; 32 33 import org.apache.log4j.Logger; 34 import org.exolab.castor.jdo.Database; 35 import org.exolab.castor.jdo.OQLQuery; 36 import org.exolab.castor.jdo.QueryResults; 37 import org.infoglue.cms.applications.common.VisualFormatter; 38 import org.infoglue.cms.entities.content.Content; 39 import org.infoglue.cms.entities.content.ContentVO; 40 import org.infoglue.cms.entities.content.ContentVersionVO; 41 import org.infoglue.cms.entities.kernel.BaseEntityVO; 42 import org.infoglue.cms.entities.management.Language; 43 import org.infoglue.cms.entities.management.LanguageVO; 44 import org.infoglue.cms.entities.management.Repository; 45 import org.infoglue.cms.entities.management.ServiceDefinition; 46 import org.infoglue.cms.entities.management.ServiceDefinitionVO; 47 import org.infoglue.cms.entities.management.SiteNodeTypeDefinition; 48 import org.infoglue.cms.entities.management.impl.simple.RepositoryImpl; 49 import org.infoglue.cms.entities.management.impl.simple.SiteNodeTypeDefinitionImpl; 50 import org.infoglue.cms.entities.structure.ServiceBindingVO; 51 import org.infoglue.cms.entities.structure.SiteNode; 52 import org.infoglue.cms.entities.structure.SiteNodeVO; 53 import org.infoglue.cms.entities.structure.SiteNodeVersion; 54 import org.infoglue.cms.entities.structure.SiteNodeVersionVO; 55 import org.infoglue.cms.entities.structure.impl.simple.SiteNodeImpl; 56 import org.infoglue.cms.entities.structure.impl.simple.SmallSiteNodeImpl; 57 import org.infoglue.cms.exception.Bug; 58 import org.infoglue.cms.exception.ConstraintException; 59 import org.infoglue.cms.exception.SystemException; 60 import org.infoglue.cms.security.InfoGluePrincipal; 61 import org.infoglue.cms.util.ConstraintExceptionBuffer; 62 63 import com.opensymphony.module.propertyset.PropertySet; 64 import com.opensymphony.module.propertyset.PropertySetManager; 65 66 public class SiteNodeController extends BaseController 67 { 68 private final static Logger logger = Logger.getLogger(SiteNodeController.class.getName()); 69 70 73 74 public static SiteNodeController getController() 75 { 76 return new SiteNodeController(); 77 } 78 79 82 83 public static SiteNodeVO getSiteNodeVOWithId(Integer siteNodeId) throws SystemException, Bug 84 { 85 return (SiteNodeVO) getVOWithId(SiteNodeImpl.class, siteNodeId); 86 } 87 88 91 92 public static SiteNodeVO getSiteNodeVOWithId(Integer siteNodeId, Database db) throws SystemException, Bug 93 { 94 return (SiteNodeVO) getVOWithId(SiteNodeImpl.class, siteNodeId, db); 95 } 96 97 100 101 public static SiteNodeVO getSmallSiteNodeVOWithId(Integer siteNodeId, Database db) throws SystemException, Bug 102 { 103 return (SiteNodeVO) getVOWithId(SmallSiteNodeImpl.class, siteNodeId, db); 104 } 105 106 107 public SiteNode getSiteNodeWithId(Integer siteNodeId, Database db) throws SystemException, Bug 108 { 109 return getSiteNodeWithId(siteNodeId, db, false); 110 } 111 112 public static SiteNode getSiteNodeWithId(Integer siteNodeId, Database db, boolean readOnly) throws SystemException, Bug 113 { 114 SiteNode siteNode = null; 115 try 116 { 117 if(readOnly) 118 siteNode = (SiteNode)db.load(org.infoglue.cms.entities.structure.impl.simple.SiteNodeImpl.class, siteNodeId, Database.ReadOnly); 119 else 120 { 121 logger.info("Loading " + siteNodeId + " in read/write mode."); 122 siteNode = (SiteNode)db.load(org.infoglue.cms.entities.structure.impl.simple.SiteNodeImpl.class, siteNodeId); 123 } 124 } 125 catch(Exception e) 126 { 127 throw new SystemException("An error occurred when we tried to fetch the SiteNode. Reason:" + e.getMessage(), e); 128 } 129 130 if(siteNode == null) 131 { 132 throw new Bug("The SiteNode with id [" + siteNodeId + "] was not found in SiteNodeHelper.getSiteNodeWithId. This should never happen."); 133 } 134 135 return siteNode; 136 } 137 138 139 142 143 public void delete(SiteNodeVO siteNodeVO) throws ConstraintException, SystemException 144 { 145 Database db = CastorDatabaseService.getDatabase(); 146 beginTransaction(db); 147 try 148 { 149 delete(siteNodeVO, db); 150 151 commitTransaction(db); 152 } 153 catch(ConstraintException ce) 154 { 155 logger.error("An error occurred so we should not complete the transaction:" + ce, ce); 156 rollbackTransaction(db); 157 throw ce; 158 } 159 catch(Exception e) 160 { 161 logger.error("An error occurred so we should not complete the transaction:" + e, e); 162 rollbackTransaction(db); 163 throw new SystemException(e.getMessage()); 164 } 165 } 166 167 170 171 public void delete(SiteNodeVO siteNodeVO, Database db) throws ConstraintException, SystemException, Exception 172 { 173 SiteNode siteNode = getSiteNodeWithId(siteNodeVO.getSiteNodeId(), db); 174 SiteNode parent = siteNode.getParentSiteNode(); 175 if(parent != null) 176 { 177 Iterator childSiteNodeIterator = parent.getChildSiteNodes().iterator(); 178 while(childSiteNodeIterator.hasNext()) 179 { 180 SiteNode candidate = (SiteNode)childSiteNodeIterator.next(); 181 if(candidate.getId().equals(siteNodeVO.getSiteNodeId())) 182 deleteRecursive(siteNode, childSiteNodeIterator, db); 183 } 184 } 185 else 186 { 187 deleteRecursive(siteNode, null, db); 188 } 189 } 190 191 192 197 198 private static void deleteRecursive(SiteNode siteNode, Iterator parentIterator, Database db) throws ConstraintException, SystemException, Exception 199 { 200 List referenceBeanList = RegistryController.getController().getReferencingObjectsForSiteNode(siteNode.getId(), db); 201 if(referenceBeanList != null && referenceBeanList.size() > 0) 202 throw new ConstraintException("SiteNode.stateId", "3405"); 203 204 Collection children = siteNode.getChildSiteNodes(); 205 Iterator i = children.iterator(); 206 while(i.hasNext()) 207 { 208 SiteNode childSiteNode = (SiteNode)i.next(); 209 deleteRecursive(childSiteNode, i, db); 210 } 211 siteNode.setChildSiteNodes(new ArrayList ()); 212 213 if(getIsDeletable(siteNode)) 214 { 215 SiteNodeVersionController.deleteVersionsForSiteNode(siteNode, db); 216 217 ServiceBindingController.deleteServiceBindingsReferencingSiteNode(siteNode, db); 218 219 if(parentIterator != null) 220 parentIterator.remove(); 221 222 db.remove(siteNode); 223 } 224 else 225 { 226 throw new ConstraintException("SiteNodeVersion.stateId", "3400"); 227 } 228 } 229 230 234 235 private static boolean getIsDeletable(SiteNode siteNode) throws SystemException 236 { 237 boolean isDeletable = true; 238 239 Collection siteNodeVersions = siteNode.getSiteNodeVersions(); 240 Iterator versionIterator = siteNodeVersions.iterator(); 241 while (versionIterator.hasNext()) 242 { 243 SiteNodeVersion siteNodeVersion = (SiteNodeVersion)versionIterator.next(); 244 if(siteNodeVersion.getStateId().intValue() == SiteNodeVersionVO.PUBLISHED_STATE.intValue() && siteNodeVersion.getIsActive().booleanValue() == true) 245 { 246 logger.warn("The siteNode had a published version so we cannot delete it.."); 247 isDeletable = false; 248 break; 249 } 250 } 251 252 return isDeletable; 253 } 254 255 256 public SiteNodeVO create(Integer parentSiteNodeId, Integer siteNodeTypeDefinitionId, InfoGluePrincipal infoGluePrincipal, Integer repositoryId, SiteNodeVO siteNodeVO) throws ConstraintException, SystemException 257 { 258 Database db = CastorDatabaseService.getDatabase(); 259 ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer(); 260 261 SiteNode siteNode = null; 262 263 beginTransaction(db); 264 265 try 266 { 267 siteNode = create(db, parentSiteNodeId, siteNodeTypeDefinitionId, infoGluePrincipal, repositoryId, siteNodeVO); 269 270 ceb.throwIfNotEmpty(); 272 273 commitTransaction(db); 274 } 275 catch(ConstraintException ce) 276 { 277 logger.warn("An error occurred so we should not complete the transaction:" + ce, ce); 278 throw ce; 280 } 281 catch(Exception e) 282 { 283 logger.error("An error occurred so we should not complete the transaction:" + e, e); 284 throw new SystemException(e.getMessage()); 286 } 287 288 return siteNode.getValueObject(); 289 } 290 291 public SiteNode create(Database db, Integer parentSiteNodeId, Integer siteNodeTypeDefinitionId, InfoGluePrincipal infoGluePrincipal, Integer repositoryId, SiteNodeVO siteNodeVO) throws SystemException, Exception 292 { 293 SiteNode siteNode = null; 294 295 logger.info("******************************************"); 296 logger.info("parentSiteNode:" + parentSiteNodeId); 297 logger.info("siteNodeTypeDefinition:" + siteNodeTypeDefinitionId); 298 logger.info("repository:" + repositoryId); 299 logger.info("******************************************"); 300 301 303 SiteNode parentSiteNode = null; 304 SiteNodeTypeDefinition siteNodeTypeDefinition = null; 305 306 if(parentSiteNodeId != null) 307 { 308 parentSiteNode = getSiteNodeWithId(parentSiteNodeId, db); 309 if(repositoryId == null) 310 repositoryId = parentSiteNode.getRepository().getRepositoryId(); 311 } 312 313 if(siteNodeTypeDefinitionId != null) 314 siteNodeTypeDefinition = SiteNodeTypeDefinitionController.getController().getSiteNodeTypeDefinitionWithId(siteNodeTypeDefinitionId, db); 315 316 Repository repository = RepositoryController.getController().getRepositoryWithId(repositoryId, db); 317 318 319 siteNode = new SiteNodeImpl(); 320 siteNode.setValueObject(siteNodeVO); 321 siteNode.setParentSiteNode((SiteNodeImpl)parentSiteNode); 322 siteNode.setRepository((RepositoryImpl)repository); 323 siteNode.setSiteNodeTypeDefinition((SiteNodeTypeDefinitionImpl)siteNodeTypeDefinition); 324 siteNode.setCreator(infoGluePrincipal.getName()); 325 326 db.create(siteNode); 327 328 if(parentSiteNode != null) 329 parentSiteNode.getChildSiteNodes().add(siteNode); 330 331 334 SiteNodeVersionController.createInitialSiteNodeVersion(db, siteNode, infoGluePrincipal); 336 337 return siteNode; 338 } 339 340 352 353 public SiteNode createNewSiteNode(Database db, Integer parentSiteNodeId, Integer siteNodeTypeDefinitionId, InfoGluePrincipal infoGluePrincipal, Integer repositoryId, SiteNodeVO siteNodeVO) throws SystemException 354 { 355 SiteNode siteNode = null; 356 357 try 358 { 359 logger.info("******************************************"); 360 logger.info("parentSiteNode:" + parentSiteNodeId); 361 logger.info("siteNodeTypeDefinition:" + siteNodeTypeDefinitionId); 362 logger.info("repository:" + repositoryId); 363 logger.info("******************************************"); 364 365 367 SiteNode parentSiteNode = null; 368 SiteNodeTypeDefinition siteNodeTypeDefinition = null; 369 370 if(parentSiteNodeId != null) 371 { 372 parentSiteNode = getSiteNodeWithId(parentSiteNodeId, db); 373 if(repositoryId == null) 374 repositoryId = parentSiteNode.getRepository().getRepositoryId(); 375 } 376 377 if(siteNodeTypeDefinitionId != null) 378 siteNodeTypeDefinition = SiteNodeTypeDefinitionController.getController().getSiteNodeTypeDefinitionWithId(siteNodeTypeDefinitionId, db); 379 380 Repository repository = RepositoryController.getController().getRepositoryWithId(repositoryId, db); 381 382 siteNode = new SiteNodeImpl(); 383 siteNode.setValueObject(siteNodeVO); 384 siteNode.setParentSiteNode((SiteNodeImpl)parentSiteNode); 385 siteNode.setRepository((RepositoryImpl)repository); 386 siteNode.setSiteNodeTypeDefinition((SiteNodeTypeDefinitionImpl)siteNodeTypeDefinition); 387 siteNode.setCreator(infoGluePrincipal.getName()); 388 389 db.create((SiteNode)siteNode); 391 392 SiteNodeVersion siteNodeVersion = SiteNodeVersionController.createInitialSiteNodeVersion(db, siteNode, infoGluePrincipal); 394 395 List siteNodeVersions = new ArrayList (); 396 siteNodeVersions.add(siteNodeVersion); 397 siteNode.setSiteNodeVersions(siteNodeVersions); 398 } 399 catch(Exception e) 400 { 401 throw new SystemException("An error occurred when we tried to create the SiteNode in the database. Reason:" + e.getMessage(), e); 402 } 403 404 return siteNode; 405 } 406 407 408 411 412 public static SiteNodeVO getParentSiteNode(Integer siteNodeId) throws SystemException, Bug 413 { 414 Database db = CastorDatabaseService.getDatabase(); 415 ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer(); 416 SiteNodeVO parentSiteNodeVO = null; 417 418 beginTransaction(db); 419 420 try 421 { 422 SiteNode parent = getParentSiteNode(siteNodeId, db); 423 if(parent != null) 424 parentSiteNodeVO = parent.getValueObject(); 425 426 commitTransaction(db); 427 } 428 catch(Exception e) 429 { 430 logger.error("An error occurred so we should not complete the transaction:" + e, e); 431 rollbackTransaction(db); 432 throw new SystemException(e.getMessage()); 433 } 434 return parentSiteNodeVO; 435 } 436 437 440 441 public static SiteNode getParentSiteNode(Integer siteNodeId, Database db) throws SystemException, Bug 442 { 443 SiteNode siteNode = (SiteNode) getObjectWithId(SiteNodeImpl.class, siteNodeId, db); 444 SiteNode parent = siteNode.getParentSiteNode(); 445 446 return parent; 447 } 448 449 452 453 public List getSiteNodeChildren(Integer parentSiteNodeId) throws ConstraintException, SystemException 454 { 455 Database db = CastorDatabaseService.getDatabase(); 456 ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer(); 457 458 List childrenVOList = null; 459 460 beginTransaction(db); 461 462 try 463 { 464 SiteNode siteNode = getSiteNodeWithId(parentSiteNodeId, db); 465 Collection children = siteNode.getChildSiteNodes(); 466 childrenVOList = SiteNodeController.toVOList(children); 467 468 ceb.throwIfNotEmpty(); 470 471 commitTransaction(db); 472 } 473 catch(ConstraintException ce) 474 { 475 logger.warn("An error occurred so we should not complete the transaction:" + ce, ce); 476 rollbackTransaction(db); 477 throw ce; 478 } 479 catch(Exception e) 480 { 481 logger.error("An error occurred so we should not complete the transaction:" + e, e); 482 rollbackTransaction(db); 483 throw new SystemException(e.getMessage()); 484 } 485 486 return childrenVOList; 487 } 488 489 490 494 495 public static List getSiteNodeVOList(HashMap argumentHashMap) throws SystemException, Bug 496 { 497 List siteNodes = null; 498 499 String method = (String )argumentHashMap.get("method"); 500 logger.info("method:" + method); 501 502 if(method.equalsIgnoreCase("selectSiteNodeListOnIdList")) 503 { 504 siteNodes = new ArrayList (); 505 List arguments = (List )argumentHashMap.get("arguments"); 506 logger.info("Arguments:" + arguments.size()); 507 Iterator argumentIterator = arguments.iterator(); 508 while(argumentIterator.hasNext()) 509 { 510 HashMap argument = (HashMap )argumentIterator.next(); 511 logger.info("argument:" + argument.size()); 512 513 Iterator iterator = argument.keySet().iterator(); 514 while ( iterator.hasNext() ) 515 logger.info( " " + iterator.next() ); 516 517 518 Integer siteNodeId = new Integer ((String )argument.get("siteNodeId")); 519 logger.info("Getting the siteNode with Id:" + siteNodeId); 520 siteNodes.add(getSiteNodeVOWithId(siteNodeId)); 521 } 522 } 523 524 return siteNodes; 525 } 526 527 531 532 public static List getSiteNodeVOList(HashMap argumentHashMap, Database db) throws SystemException, Bug 533 { 534 List siteNodes = null; 535 536 String method = (String )argumentHashMap.get("method"); 537 logger.info("method:" + method); 538 539 if(method.equalsIgnoreCase("selectSiteNodeListOnIdList")) 540 { 541 siteNodes = new ArrayList (); 542 List arguments = (List )argumentHashMap.get("arguments"); 543 logger.info("Arguments:" + arguments.size()); 544 Iterator argumentIterator = arguments.iterator(); 545 while(argumentIterator.hasNext()) 546 { 547 HashMap argument = (HashMap )argumentIterator.next(); 548 logger.info("argument:" + argument.size()); 549 550 Iterator iterator = argument.keySet().iterator(); 551 while ( iterator.hasNext() ) 552 logger.info( " " + iterator.next() ); 553 554 555 Integer siteNodeId = new Integer ((String )argument.get("siteNodeId")); 556 logger.info("Getting the siteNode with Id:" + siteNodeId); 557 siteNodes.add(getSmallSiteNodeVOWithId(siteNodeId, db)); 558 } 559 } 560 561 return siteNodes; 562 } 563 566 567 public SiteNodeVO getRootSiteNodeVO(Integer repositoryId) throws ConstraintException, SystemException 568 { 569 Database db = CastorDatabaseService.getDatabase(); 570 ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer(); 571 572 SiteNode siteNode = null; 573 574 beginTransaction(db); 575 576 try 577 { 578 siteNode = getRootSiteNode(repositoryId, db); 579 580 commitTransaction(db); 581 582 ceb.throwIfNotEmpty(); 584 } 585 catch(Exception e) 586 { 587 logger.error("An error occurred so we should not complete the transaction:" + e, e); 588 rollbackTransaction(db); 589 throw new SystemException(e.getMessage()); 590 } 591 592 return (siteNode == null) ? null : siteNode.getValueObject(); 593 } 594 595 598 599 public SiteNode getRootSiteNode(Integer repositoryId, Database db) throws ConstraintException, SystemException, Exception 600 { 601 SiteNode siteNode = null; 602 603 OQLQuery oql = db.getOQLQuery( "SELECT s FROM org.infoglue.cms.entities.structure.impl.simple.SiteNodeImpl s WHERE is_undefined(s.parentSiteNode) AND s.repository.repositoryId = $1"); 604 oql.bind(repositoryId); 605 606 QueryResults results = oql.execute(); 607 this.logger.info("Fetching entity in read/write mode" + repositoryId); 608 609 if (results.hasMore()) 610 { 611 siteNode = (SiteNode)results.next(); 612 } 613 614 results.close(); 615 oql.close(); 616 617 return siteNode; 618 } 619 620 621 624 625 public void moveSiteNode(SiteNodeVO siteNodeVO, Integer newParentSiteNodeId) throws ConstraintException, SystemException 626 { 627 Database db = CastorDatabaseService.getDatabase(); 628 ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer(); 629 630 SiteNode siteNode = null; 631 SiteNode newParentSiteNode = null; 632 SiteNode oldParentSiteNode = null; 633 634 beginTransaction(db); 635 636 try 637 { 638 siteNodeVO.validate(); 640 641 if(newParentSiteNodeId == null) 642 { 643 logger.warn("You must specify the new parent-siteNode......"); 644 throw new ConstraintException("SiteNode.parentSiteNodeId", "3403"); 645 } 646 647 if(siteNodeVO.getId().intValue() == newParentSiteNodeId.intValue()) 648 { 649 logger.warn("You cannot have the siteNode as it's own parent......"); 650 throw new ConstraintException("SiteNode.parentSiteNodeId", "3401"); 651 } 652 653 siteNode = getSiteNodeWithId(siteNodeVO.getSiteNodeId(), db); 654 oldParentSiteNode = siteNode.getParentSiteNode(); 655 newParentSiteNode = getSiteNodeWithId(newParentSiteNodeId, db); 656 657 if(oldParentSiteNode.getId().intValue() == newParentSiteNodeId.intValue()) 658 { 659 logger.warn("You cannot specify the same node as it originally was located in......"); 660 throw new ConstraintException("SiteNode.parentSiteNodeId", "3404"); 661 } 662 663 SiteNode tempSiteNode = newParentSiteNode.getParentSiteNode(); 664 while(tempSiteNode != null) 665 { 666 if(tempSiteNode.getId().intValue() == siteNode.getId().intValue()) 667 { 668 logger.warn("You cannot move the node to a child under it......"); 669 throw new ConstraintException("SiteNode.parentSiteNodeId", "3402"); 670 } 671 tempSiteNode = tempSiteNode.getParentSiteNode(); 672 } 673 674 logger.info("Setting the new Parent siteNode:" + siteNode.getSiteNodeId() + " " + newParentSiteNode.getSiteNodeId()); 675 siteNode.setParentSiteNode((SiteNodeImpl)newParentSiteNode); 676 677 changeRepositoryRecursive(siteNode, newParentSiteNode.getRepository()); 678 newParentSiteNode.getChildSiteNodes().add(siteNode); 680 oldParentSiteNode.getChildSiteNodes().remove(siteNode); 681 682 ceb.throwIfNotEmpty(); 684 685 commitTransaction(db); 686 } 687 catch(ConstraintException ce) 688 { 689 logger.warn("An error occurred so we should not complete the transaction:" + ce, ce); 690 rollbackTransaction(db); 691 throw ce; 692 } 693 catch(Exception e) 694 { 695 logger.error("An error occurred so we should not complete the transaction:" + e, e); 696 rollbackTransaction(db); 697 throw new SystemException(e.getMessage()); 698 } 699 700 } 701 702 707 708 private void changeRepositoryRecursive(SiteNode siteNode, Repository newRepository) 709 { 710 if(siteNode.getRepository().getId().intValue() != newRepository.getId().intValue()) 711 { 712 siteNode.setRepository((RepositoryImpl)newRepository); 713 Iterator ChildSiteNodesIterator = siteNode.getChildSiteNodes().iterator(); 714 while(ChildSiteNodesIterator.hasNext()) 715 { 716 SiteNode childSiteNode = (SiteNode)ChildSiteNodesIterator.next(); 717 changeRepositoryRecursive(childSiteNode, newRepository); 718 } 719 } 720 } 721 722 723 727 728 public BaseEntityVO getNewVO() 729 { 730 return new SiteNodeVO(); 731 } 732 733 736 737 public List getRepositorySiteNodes(Integer repositoryId, Database db) throws SystemException, Exception 738 { 739 List siteNodes = new ArrayList (); 740 741 OQLQuery oql = db.getOQLQuery("SELECT sn FROM org.infoglue.cms.entities.structure.impl.simple.SiteNodeImpl sn WHERE sn.repository.repositoryId = $1"); 742 oql.bind(repositoryId); 743 744 QueryResults results = oql.execute(Database.ReadOnly); 745 746 while(results.hasMore()) 747 { 748 SiteNode siteNode = (SiteNodeImpl)results.next(); 749 siteNodes.add(siteNode); 750 } 751 752 results.close(); 753 oql.close(); 754 755 return siteNodes; 756 } 757 758 769 770 public Content createSiteNodeMetaInfoContent(Database db, SiteNode newSiteNode, Integer repositoryId, InfoGluePrincipal principal, Integer pageTemplateContentId) throws SystemException, Bug, Exception , ConstraintException 771 { 772 Content content = null; 773 774 String basePath = "Meta info folder"; 775 String path = ""; 776 777 SiteNode parentSiteNode = newSiteNode.getParentSiteNode(); 778 while(parentSiteNode != null) 779 { 780 path = "/" + parentSiteNode.getName() + path; 781 parentSiteNode = parentSiteNode.getParentSiteNode(); 782 } 783 path = basePath + path; 784 785 SiteNodeVersion siteNodeVersion = SiteNodeVersionController.getController().getLatestSiteNodeVersion(db, newSiteNode.getId(), false); 786 Language masterLanguage = LanguageController.getController().getMasterLanguage(db, repositoryId); 787 788 ServiceDefinitionVO singleServiceDefinitionVO = null; 789 790 Integer metaInfoContentTypeDefinitionId = ContentTypeDefinitionController.getController().getContentTypeDefinitionVOWithName("Meta info", db).getId(); 791 Integer availableServiceBindingId = AvailableServiceBindingController.getController().getAvailableServiceBindingWithName("Meta information", db, false).getId(); 792 793 List serviceDefinitions = AvailableServiceBindingController.getController().getServiceDefinitionVOList(db, availableServiceBindingId); 794 if(serviceDefinitions == null || serviceDefinitions.size() == 0) 795 { 796 ServiceDefinition serviceDefinition = ServiceDefinitionController.getController().getServiceDefinitionWithName("Core content service", db, false); 797 String [] values = {serviceDefinition.getId().toString()}; 798 AvailableServiceBindingController.getController().update(availableServiceBindingId, values, db); 799 singleServiceDefinitionVO = serviceDefinition.getValueObject(); 800 } 801 else if(serviceDefinitions.size() == 1) 802 { 803 singleServiceDefinitionVO = (ServiceDefinitionVO)serviceDefinitions.get(0); 804 } 805 806 ContentVO parentFolderContentVO = null; 807 808 Content rootContent = ContentControllerProxy.getController().getRootContent(db, repositoryId, principal.getName(), true); 809 if(rootContent != null) 810 { 811 ContentVO parentFolderContent = ContentController.getContentController().getContentVOWithPath(repositoryId, path, true, principal, db); 812 813 ContentVO contentVO = new ContentVO(); 814 contentVO.setCreatorName(principal.getName()); 815 contentVO.setIsBranch(new Boolean (false)); 816 contentVO.setName(newSiteNode.getName() + " Metainfo"); 817 contentVO.setRepositoryId(repositoryId); 818 819 content = ContentControllerProxy.getController().create(db, parentFolderContent.getId(), metaInfoContentTypeDefinitionId, repositoryId, contentVO); 820 821 newSiteNode.setMetaInfoContentId(contentVO.getId()); 822 823 String componentStructure = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><components></components>"; 824 if(pageTemplateContentId != null) 825 { 826 Integer languageId = LanguageController.getController().getMasterLanguage(db, repositoryId).getId(); 827 ContentVersionVO contentVersionVO = ContentVersionController.getContentVersionController().getLatestActiveContentVersionVO(pageTemplateContentId, languageId, db); 828 829 componentStructure = ContentVersionController.getContentVersionController().getAttributeValue(contentVersionVO, "ComponentStructure", false); 830 } 831 832 String versionValue = "<?xml version='1.0' encoding='UTF-8'?><article xmlns=\"x-schema:ArticleSchema.xml\"><attributes><Title><![CDATA[" + newSiteNode.getName() + "]]></Title><NavigationTitle><![CDATA[" + newSiteNode.getName() + "]]></NavigationTitle><NiceURIName><![CDATA[" + new VisualFormatter().replaceNonAscii(newSiteNode.getName(), '_') + "]]></NiceURIName><Description><![CDATA[" + newSiteNode.getName() + "]]></Description><MetaInfo><![CDATA[" + newSiteNode.getName() + "]]></MetaInfo><ComponentStructure><![CDATA[" + componentStructure + "]]></ComponentStructure></attributes></article>"; 834 ContentVersionVO contentVersionVO = new ContentVersionVO(); 835 contentVersionVO.setVersionComment("Autogenerated version"); 836 contentVersionVO.setVersionModifier(principal.getName()); 837 contentVersionVO.setVersionValue(versionValue); 838 ContentVersionController.getContentVersionController().create(contentVO.getId(), masterLanguage.getId(), contentVersionVO, null, db); 839 840 LanguageVO localMasterLanguageVO = getInitialLanguageVO(db, parentFolderContent.getId(), repositoryId); 842 if(localMasterLanguageVO.getId().intValue() != masterLanguage.getId().intValue()) 843 { 844 String versionValueLocalMaster = "<?xml version='1.0' encoding='UTF-8'?><article xmlns=\"x-schema:ArticleSchema.xml\"><attributes><Title><![CDATA[" + newSiteNode.getName() + "]]></Title><NavigationTitle><![CDATA[" + newSiteNode.getName() + "]]></NavigationTitle><NiceURIName><![CDATA[" + new VisualFormatter().replaceNonAscii(newSiteNode.getName(), '_') + "]]></NiceURIName><Description><![CDATA[" + newSiteNode.getName() + "]]></Description><MetaInfo><![CDATA[" + newSiteNode.getName() + "]]></MetaInfo><ComponentStructure><![CDATA[]]></ComponentStructure></attributes></article>"; 845 ContentVersionVO contentVersionVOLocalMaster = new ContentVersionVO(); 846 contentVersionVOLocalMaster.setVersionComment("Autogenerated version"); 847 contentVersionVOLocalMaster.setVersionModifier(principal.getName()); 848 contentVersionVOLocalMaster.setVersionValue(versionValueLocalMaster); 849 ContentVersionController.getContentVersionController().create(contentVO.getId(), localMasterLanguageVO.getId(), contentVersionVOLocalMaster, null, db); 850 } 851 852 ServiceBindingVO serviceBindingVO = new ServiceBindingVO(); 853 serviceBindingVO.setName(newSiteNode.getName() + " Metainfo"); 854 serviceBindingVO.setPath("/None specified/"); 855 856 String qualifyerXML = "<?xml version='1.0' encoding='UTF-8'?><qualifyer><contentId>" + contentVO.getId() + "</contentId></qualifyer>"; 857 858 ServiceBindingController.getController().create(db, serviceBindingVO, qualifyerXML, availableServiceBindingId, siteNodeVersion.getId(), singleServiceDefinitionVO.getId()); 859 } 860 861 return content; 862 } 863 864 public LanguageVO getInitialLanguageVO(Database db, Integer contentId, Integer repositoryId) throws Exception 865 { 866 Map args = new HashMap (); 867 args.put("globalKey", "infoglue"); 868 PropertySet ps = PropertySetManager.getInstance("jdbc", args); 869 870 String initialLanguageId = ps.getString("content_" + contentId + "_initialLanguageId"); 871 Content content = ContentController.getContentController().getContentWithId(contentId, db); 872 Content parentContent = content.getParentContent(); 873 while((initialLanguageId == null || initialLanguageId.equalsIgnoreCase("-1")) && parentContent != null) 874 { 875 initialLanguageId = ps.getString("content_" + parentContent.getId() + "_initialLanguageId"); 876 parentContent = parentContent.getParentContent(); 877 } 878 879 if(initialLanguageId != null && !initialLanguageId.equals("") && !initialLanguageId.equals("-1")) 880 return LanguageController.getController().getLanguageVOWithId(new Integer (initialLanguageId)); 881 else 882 return LanguageController.getController().getMasterLanguage(repositoryId); 883 } 884 885 888 889 public List getSiteNodeVOWithParentRecursive(Integer siteNodeId) throws ConstraintException, SystemException 890 { 891 return getSiteNodeVOWithParentRecursive(siteNodeId, new ArrayList ()); 892 } 893 894 private List getSiteNodeVOWithParentRecursive(Integer siteNodeId, List resultList) throws ConstraintException, SystemException 895 { 896 resultList.add(getSiteNodeVOWithId(siteNodeId)); 898 899 List childSiteNodeList = SiteNodeController.getController().getSiteNodeChildren(siteNodeId); 901 Iterator cit = childSiteNodeList.iterator(); 902 while (cit.hasNext()) 903 { 904 SiteNodeVO siteNodeVO = (SiteNodeVO) cit.next(); 905 getSiteNodeVOWithParentRecursive(siteNodeVO.getId(), resultList); 906 } 907 908 return resultList; 909 } 910 911 912 public void setMetaInfoContentId(Integer siteNodeId, Integer metaInfoContentId) throws ConstraintException, SystemException 913 { 914 Database db = CastorDatabaseService.getDatabase(); 915 ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer(); 916 917 beginTransaction(db); 918 919 try 920 { 921 setMetaInfoContentId(siteNodeId, metaInfoContentId, db); 922 923 commitTransaction(db); 924 } 925 catch(Exception e) 926 { 927 logger.error("An error occurred so we should not complete the transaction:" + e, e); 928 rollbackTransaction(db); 929 throw new SystemException(e.getMessage()); 930 } 931 932 } 933 934 public void setMetaInfoContentId(Integer siteNodeId, Integer metaInfoContentId, Database db) throws ConstraintException, SystemException 935 { 936 SiteNode siteNode = getSiteNodeWithId(siteNodeId, db); 937 siteNode.setMetaInfoContentId(metaInfoContentId); 938 } 939 940 941 public List getSiteNodeVOListWithoutMetaInfoContentId() throws ConstraintException, SystemException 942 { 943 List siteNodeVOList = new ArrayList (); 944 945 Database db = CastorDatabaseService.getDatabase(); 946 ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer(); 947 948 beginTransaction(db); 949 950 try 951 { 952 List siteNodes = getSiteNodesWithoutMetaInfoContentId(db); 953 siteNodeVOList = toVOList(siteNodes); 954 955 commitTransaction(db); 956 } 957 catch(Exception e) 958 { 959 logger.error("An error occurred so we should not complete the transaction:" + e, e); 960 rollbackTransaction(db); 961 throw new SystemException(e.getMessage()); 962 } 963 964 return siteNodeVOList; 965 } 966 967 public List getSiteNodesWithoutMetaInfoContentId(Database db) throws ConstraintException, SystemException, Exception 968 { 969 List siteNodes = new ArrayList (); 970 971 OQLQuery oql = db.getOQLQuery("SELECT sn FROM org.infoglue.cms.entities.structure.impl.simple.SiteNodeImpl sn WHERE sn.metaInfoContentId = $1"); 972 oql.bind(new Integer (-1)); 973 974 QueryResults results = oql.execute(); 975 976 while(results.hasMore()) 977 { 978 SiteNode siteNode = (SiteNodeImpl)results.next(); 979 siteNodes.add(siteNode); 980 } 981 982 results.close(); 983 oql.close(); 984 985 return siteNodes; 986 } 987 988 } 989 990 | Popular Tags |