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.Collections ; 29 import java.util.Iterator ; 30 import java.util.List ; 31 32 import org.apache.log4j.Logger; 33 import org.exolab.castor.jdo.Database; 34 import org.exolab.castor.jdo.OQLQuery; 35 import org.exolab.castor.jdo.QueryResults; 36 import org.infoglue.cms.entities.content.Content; 37 import org.infoglue.cms.entities.content.ContentVO; 38 import org.infoglue.cms.entities.content.ContentVersion; 39 import org.infoglue.cms.entities.kernel.BaseEntityVO; 40 import org.infoglue.cms.entities.management.AvailableServiceBinding; 41 import org.infoglue.cms.entities.management.Language; 42 import org.infoglue.cms.entities.management.RegistryVO; 43 import org.infoglue.cms.entities.structure.ServiceBinding; 44 import org.infoglue.cms.entities.structure.SiteNode; 45 import org.infoglue.cms.entities.structure.SiteNodeVO; 46 import org.infoglue.cms.entities.structure.SiteNodeVersion; 47 import org.infoglue.cms.entities.structure.SiteNodeVersionVO; 48 import org.infoglue.cms.entities.structure.impl.simple.SiteNodeImpl; 49 import org.infoglue.cms.entities.structure.impl.simple.SiteNodeVersionImpl; 50 import org.infoglue.cms.exception.Bug; 51 import org.infoglue.cms.exception.ConstraintException; 52 import org.infoglue.cms.exception.SystemException; 53 import org.infoglue.cms.security.InfoGluePrincipal; 54 import org.infoglue.cms.util.ConstraintExceptionBuffer; 55 import org.infoglue.cms.util.DateHelper; 56 57 public class SiteNodeVersionController extends BaseController 58 { 59 private final static Logger logger = Logger.getLogger(SiteNodeVersionController.class.getName()); 60 61 private final RegistryController registryController = RegistryController.getController(); 62 63 66 67 public static SiteNodeVersionController getController() 68 { 69 return new SiteNodeVersionController(); 70 } 71 72 public SiteNodeVersionVO getSiteNodeVersionVOWithId(Integer siteNodeVersionId) throws SystemException, Bug 73 { 74 return (SiteNodeVersionVO) getVOWithId(SiteNodeVersionImpl.class, siteNodeVersionId); 75 } 76 77 public SiteNodeVersion getSiteNodeVersionWithId(Integer siteNodeVersionId, Database db) throws SystemException, Bug 78 { 79 return (SiteNodeVersion) getObjectWithId(SiteNodeVersionImpl.class, siteNodeVersionId, db); 80 } 81 82 public static SiteNodeVersion getSiteNodeVersionWithIdAsReadOnly(Integer siteNodeVersionId, Database db) throws SystemException, Bug 83 { 84 return (SiteNodeVersion) getObjectWithIdAsReadOnly(SiteNodeVersionImpl.class, siteNodeVersionId, db); 85 } 86 87 public List getSiteNodeVersionVOList() throws SystemException, Bug 88 { 89 return getAllVOObjects(SiteNodeVersionImpl.class, "siteNodeVersionId"); 90 } 91 92 public static void delete(SiteNodeVersionVO siteNodeVersionVO) throws ConstraintException, SystemException 93 { 94 deleteEntity(SiteNodeVersionImpl.class, siteNodeVersionVO.getSiteNodeVersionId()); 95 } 96 97 104 public void delete(SiteNodeVersion siteNodeVersion, Database db) throws ConstraintException, SystemException 105 { 106 ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer(); 107 108 try 109 { 110 SiteNode siteNode = siteNodeVersion.getOwningSiteNode(); 111 Collection serviceBindings = siteNodeVersion.getServiceBindings(); 112 Iterator serviceBindingsIterator = serviceBindings.iterator(); 113 while(serviceBindingsIterator.hasNext()) 114 { 115 ServiceBinding serviceBinding = (ServiceBinding)serviceBindingsIterator.next(); 116 serviceBindingsIterator.remove(); 117 db.remove(serviceBinding); 118 } 119 120 if(siteNode != null) 121 siteNode.getSiteNodeVersions().remove(siteNodeVersion); 122 123 db.remove(siteNodeVersion); 124 } 125 catch(Exception e) 126 { 127 logger.error("An error occurred so we should not completes the transaction:" + e, e); 128 throw new SystemException(e.getMessage()); 129 } 130 } 131 132 133 136 137 public static SiteNodeVersion createInitialSiteNodeVersion(Database db, SiteNode siteNode, InfoGluePrincipal infoGluePrincipal) throws SystemException, Bug 138 { 139 ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer(); 140 141 SiteNodeVersion siteNodeVersion = null; 142 143 try 144 { 145 147 siteNodeVersion = new SiteNodeVersionImpl(); 148 siteNodeVersion.setIsCheckedOut(new Boolean (false)); 149 siteNodeVersion.setModifiedDateTime(DateHelper.getSecondPreciseDate()); 150 siteNodeVersion.setOwningSiteNode((SiteNodeImpl)siteNode); 151 siteNodeVersion.setStateId(new Integer (0)); 152 siteNodeVersion.setVersionComment("Initial version"); 153 siteNodeVersion.setVersionModifier(infoGluePrincipal.getName()); 154 siteNodeVersion.setVersionNumber(new Integer (1)); 155 156 db.create((SiteNodeVersion)siteNodeVersion); 157 158 List siteNodeVersions = new ArrayList (); 159 siteNodeVersions.add(siteNodeVersion); 160 siteNode.setSiteNodeVersions(siteNodeVersions); 161 } 162 catch(Exception e) 163 { 164 logger.error("An error occurred so we should not completes the transaction:" + e, e); 165 throw new SystemException(e.getMessage()); 166 } 167 168 return siteNodeVersion; 169 } 170 171 174 175 public static SiteNodeVersion create(Integer siteNodeId, InfoGluePrincipal infoGluePrincipal, SiteNodeVersionVO siteNodeVersionVO) throws SystemException, Bug 176 { 177 Database db = CastorDatabaseService.getDatabase(); 178 ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer(); 179 180 SiteNodeVersion siteNodeVersion = null; 181 182 beginTransaction(db); 183 184 try 185 { 186 SiteNode siteNode = SiteNodeController.getController().getSiteNodeWithId(siteNodeId, db); 187 188 siteNodeVersion = new SiteNodeVersionImpl(); 189 siteNodeVersion.setOwningSiteNode((SiteNodeImpl)siteNode); 190 siteNodeVersion.setVersionModifier(infoGluePrincipal.getName()); 191 siteNodeVersion.setValueObject(siteNodeVersionVO); 192 193 siteNodeVersion.setVersionNumber(new Integer (1)); 195 196 siteNodeVersion = (SiteNodeVersion)createEntity(siteNodeVersion, db); 197 } 199 catch(Exception e) 200 { 201 logger.error("An error occurred so we should not completes the transaction:" + e, e); 202 throw new SystemException(e.getMessage()); 204 } 205 206 return siteNodeVersion; 207 } 208 209 210 213 214 public static SiteNodeVersion create(Integer siteNodeId, InfoGluePrincipal infoGluePrincipal, SiteNodeVersionVO siteNodeVersionVO, Database db) throws SystemException, Bug, Exception 215 { 216 SiteNodeVersion siteNodeVersion = null; 217 218 SiteNode siteNode = SiteNodeController.getController().getSiteNodeWithId(siteNodeId, db); 219 220 siteNodeVersion = new SiteNodeVersionImpl(); 221 siteNodeVersion.setOwningSiteNode((SiteNodeImpl)siteNode); 222 siteNodeVersion.setVersionModifier(infoGluePrincipal.getName()); 223 siteNodeVersion.setValueObject(siteNodeVersionVO); 224 siteNodeVersion.setVersionNumber(new Integer (1)); 226 227 db.create(siteNodeVersion); 228 229 return siteNodeVersion; 230 } 231 232 233 public SiteNodeVersionVO getLatestActiveSiteNodeVersionVO(Integer siteNodeId) throws SystemException, Bug 234 { 235 Database db = CastorDatabaseService.getDatabase(); 236 ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer(); 237 238 SiteNodeVersionVO siteNodeVersionVO = null; 239 240 beginTransaction(db); 241 242 try 243 { 244 SiteNodeVersion siteNodeVersion = getLatestActiveSiteNodeVersion(db, siteNodeId); 245 if(siteNodeVersion != null) 246 siteNodeVersionVO = siteNodeVersion.getValueObject(); 247 248 commitTransaction(db); 249 } 250 catch(Exception e) 251 { 252 logger.error("An error occurred so we should not completes the transaction:" + e, e); 253 rollbackTransaction(db); 254 throw new SystemException(e.getMessage()); 255 } 256 257 return siteNodeVersionVO; 258 } 259 260 public SiteNodeVersion getLatestActiveSiteNodeVersion(Database db, Integer siteNodeId) throws SystemException, Bug, Exception 261 { 262 SiteNodeVersion siteNodeVersion = null; 263 264 OQLQuery oql = db.getOQLQuery( "SELECT cv FROM org.infoglue.cms.entities.structure.impl.simple.SiteNodeVersionImpl cv WHERE cv.owningSiteNode.siteNodeId = $1 AND cv.isActive = $2 ORDER BY cv.siteNodeVersionId desc"); 265 oql.bind(siteNodeId); 266 oql.bind(new Boolean (true)); 267 268 QueryResults results = oql.execute(Database.ReadOnly); 269 270 if (results.hasMore()) 271 { 272 siteNodeVersion = (SiteNodeVersion)results.next(); 273 } 274 275 results.close(); 276 oql.close(); 277 278 return siteNodeVersion; 279 } 280 281 public SiteNodeVersionVO getLatestActiveSiteNodeVersionVO(Database db, Integer siteNodeId) throws SystemException, Bug, Exception 282 { 283 SiteNodeVersionVO siteNodeVersionVO = null; 284 285 SiteNodeVersion siteNodeVersion = getLatestActiveSiteNodeVersion(db, siteNodeId); 286 if(siteNodeVersion != null) 287 siteNodeVersionVO = siteNodeVersion.getValueObject(); 288 else 289 logger.warn("The siteNode " + siteNodeId + " did not have a latest active siteNodeVersion - very strange."); 290 291 return siteNodeVersionVO; 292 } 293 294 public SiteNodeVersionVO getLatestSiteNodeVersionVO(Integer siteNodeId) throws SystemException, Bug 295 { 296 Database db = CastorDatabaseService.getDatabase(); 297 ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer(); 298 299 SiteNodeVersionVO siteNodeVersionVO = null; 300 301 beginTransaction(db); 302 303 try 304 { 305 OQLQuery oql = db.getOQLQuery( "SELECT cv FROM org.infoglue.cms.entities.structure.impl.simple.SiteNodeVersionImpl cv WHERE cv.owningSiteNode.siteNodeId = $1 ORDER BY cv.siteNodeVersionId desc"); 306 oql.bind(siteNodeId); 307 308 QueryResults results = oql.execute(Database.ReadOnly); 309 310 if (results.hasMore()) 311 { 312 SiteNodeVersion siteNodeVersion = (SiteNodeVersion)results.next(); 313 logger.info("found one:" + siteNodeVersion.getValueObject()); 314 siteNodeVersionVO = siteNodeVersion.getValueObject(); 315 } 316 317 results.close(); 318 oql.close(); 319 320 commitTransaction(db); 321 } 322 catch(Exception e) 323 { 324 logger.error("An error occurred so we should not completes the transaction:" + e, e); 325 rollbackTransaction(db); 326 throw new SystemException(e.getMessage()); 327 } 328 329 return siteNodeVersionVO; 330 } 331 332 333 public SiteNodeVersionVO getLatestSiteNodeVersionVO(Database db, Integer siteNodeId) throws SystemException, Bug, Exception 334 { 335 SiteNodeVersionVO siteNodeVersionVO = null; 336 337 OQLQuery oql = db.getOQLQuery( "SELECT cv FROM org.infoglue.cms.entities.structure.impl.simple.SiteNodeVersionImpl cv WHERE cv.owningSiteNode.siteNodeId = $1 ORDER BY cv.siteNodeVersionId desc"); 338 oql.bind(siteNodeId); 339 340 QueryResults results = oql.execute(Database.ReadOnly); 341 342 if (results.hasMore()) 343 { 344 SiteNodeVersion siteNodeVersion = (SiteNodeVersion)results.next(); 345 logger.info("found one:" + siteNodeVersion.getValueObject()); 346 siteNodeVersionVO = siteNodeVersion.getValueObject(); 347 } 348 349 results.close(); 350 oql.close(); 351 352 return siteNodeVersionVO; 353 } 354 355 358 359 public SiteNodeVersion getLatestSiteNodeVersion(Database db, Integer siteNodeId, boolean readOnly) throws SystemException, Bug 360 { 361 ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer(); 362 363 SiteNodeVersion siteNodeVersion = null; 364 365 try 366 { 367 OQLQuery oql = db.getOQLQuery( "SELECT cv FROM org.infoglue.cms.entities.structure.impl.simple.SiteNodeVersionImpl cv WHERE cv.owningSiteNode.siteNodeId = $1 ORDER BY cv.siteNodeVersionId desc"); 368 oql.bind(siteNodeId); 369 370 QueryResults results = null; 371 if(readOnly) 372 results = oql.execute(Database.ReadOnly); 373 else 374 { 375 this.logger.info("Fetching entity in read/write mode"); 376 results = oql.execute(); 377 } 378 379 if (results.hasMore()) 380 { 381 siteNodeVersion = (SiteNodeVersion)results.next(); 382 } 383 384 results.close(); 385 oql.close(); 386 } 387 catch(Exception e) 388 { 389 logger.error("An error occurred so we should not completes the transaction:" + e, e); 390 throw new SystemException(e.getMessage()); 391 } 392 393 return siteNodeVersion; 394 } 395 396 397 public SiteNodeVersionVO updateStateId(Integer siteNodeVersionId, Integer stateId, String versionComment, InfoGluePrincipal infoGluePrincipal, Integer siteNodeId) throws ConstraintException, SystemException 398 { 399 SiteNodeVersionVO siteNodeVersionVO = getSiteNodeVersionVOWithId(siteNodeVersionId); 400 SiteNodeVersionVO returnVO = null; 401 402 if(stateId.intValue() == 2) 404 { 405 siteNodeVersionVO.setStateId(stateId); 406 siteNodeVersionVO.setVersionComment(versionComment); 407 returnVO = (SiteNodeVersionVO) updateEntity(SiteNodeVersionImpl.class, siteNodeVersionVO); 408 } 409 410 if(stateId.intValue() == 0) 412 { 413 siteNodeVersionVO.setStateId(stateId); 414 siteNodeVersionVO.setVersionComment(""); 415 create(siteNodeId, infoGluePrincipal, siteNodeVersionVO); 416 returnVO = getLatestSiteNodeVersionVO(siteNodeId); 417 } 418 419 return returnVO; 420 } 421 422 public SiteNodeVersionVO updateStateId(Integer siteNodeVersionId, Integer stateId, String versionComment, InfoGluePrincipal infoGluePrincipal, Integer siteNodeId, Database db) throws ConstraintException, SystemException, Exception 423 { 424 SiteNodeVersionVO siteNodeVersionVO = getSiteNodeVersionWithId(siteNodeVersionId, db).getValueObject(); 425 SiteNodeVersionVO returnVO = null; 426 427 if(stateId.intValue() == 2) 429 { 430 siteNodeVersionVO.setStateId(stateId); 431 siteNodeVersionVO.setVersionComment(versionComment); 432 returnVO = (SiteNodeVersionVO) updateEntity(SiteNodeVersionImpl.class, siteNodeVersionVO, db); 433 } 434 435 if(stateId.intValue() == 0) 437 { 438 siteNodeVersionVO.setStateId(stateId); 439 siteNodeVersionVO.setVersionComment(""); 440 returnVO = create(siteNodeId, infoGluePrincipal, siteNodeVersionVO, db).getValueObject(); 441 } 443 444 return returnVO; 445 } 446 447 448 public static void deleteVersionsForSiteNodeWithId(Integer siteNodeId) throws ConstraintException, SystemException, Bug 449 { 450 Database db = CastorDatabaseService.getDatabase(); 451 ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer(); 452 453 beginTransaction(db); 454 List siteNodeVersions = new ArrayList (); 455 try 456 { 457 OQLQuery oql = db.getOQLQuery( "SELECT cv FROM org.infoglue.cms.entities.structure.impl.simple.SiteNodeVersionImpl cv WHERE cv.owningSiteNode.siteNodeId = $1"); 458 oql.bind(siteNodeId); 459 460 QueryResults results = oql.execute(Database.ReadOnly); 461 462 while (results.hasMore()) 463 { 464 SiteNodeVersion siteNodeVersion = (SiteNodeVersion)results.next(); 465 siteNodeVersions.add(siteNodeVersion.getValueObject()); 466 } 467 468 results.close(); 469 oql.close(); 470 471 commitTransaction(db); 472 } 473 catch(Exception e) 474 { 475 e.printStackTrace(); 476 logger.error("An error occurred so we should not completes the transaction:" + e, e); 477 rollbackTransaction(db); 478 throw new SystemException(e.getMessage()); 479 } 480 481 Iterator i = siteNodeVersions.iterator(); 482 while(i.hasNext()) 483 { 484 SiteNodeVersionVO siteNodeVersionVO = (SiteNodeVersionVO)i.next(); 485 delete(siteNodeVersionVO); 486 } 487 } 488 489 492 493 public static void deleteVersionsForSiteNode(SiteNode siteNode, Database db) throws ConstraintException, SystemException, Bug, Exception 494 { 495 Collection siteNodeVersions = Collections.synchronizedCollection(siteNode.getSiteNodeVersions()); 496 Iterator siteNodeVersionIterator = siteNodeVersions.iterator(); 497 498 boolean metaInfoContentDeleted = false; 499 while (siteNodeVersionIterator.hasNext()) 500 { 501 SiteNodeVersion siteNodeVersion = (SiteNodeVersion)siteNodeVersionIterator.next(); 502 Collection serviceBindings = Collections.synchronizedCollection(siteNodeVersion.getServiceBindings()); 503 Iterator serviceBindingIterator = serviceBindings.iterator(); 504 while(serviceBindingIterator.hasNext()) 505 { 506 ServiceBinding serviceBinding = (ServiceBinding)serviceBindingIterator.next(); 507 if(serviceBinding.getAvailableServiceBinding().getName().equalsIgnoreCase("Meta information")) 508 { 509 if(!metaInfoContentDeleted) 510 { 511 deleteMetaInfoForSiteNodeVersion(db, serviceBinding); 512 metaInfoContentDeleted = true; 513 } 514 serviceBindingIterator.remove(); 515 db.remove(serviceBinding); 516 } 517 else 518 { 519 serviceBindingIterator.remove(); 520 db.remove(serviceBinding); 521 } 522 } 523 524 logger.info("Deleting siteNodeVersion:" + siteNodeVersion.getSiteNodeVersionId()); 525 siteNodeVersionIterator.remove(); 526 db.remove(siteNodeVersion); 527 } 528 } 529 530 538 private static void deleteMetaInfoForSiteNodeVersion(Database db, ServiceBinding serviceBinding) throws ConstraintException, SystemException, Bug, Exception 539 { 540 List boundContents = ContentController.getBoundContents(db, serviceBinding.getId()); 541 if(boundContents.size() > 0) 542 { 543 ContentVO contentVO = (ContentVO)boundContents.get(0); 544 ContentController.getContentController().delete(contentVO, db, true, true, true); 545 } 546 } 547 548 549 553 554 public static List getServiceBindningVOList(Integer siteNodeVersionId) throws ConstraintException, SystemException 555 { 556 Database db = CastorDatabaseService.getDatabase(); 557 ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer(); 558 559 List serviceBindningVOList = null; 560 561 beginTransaction(db); 562 563 try 564 { 565 Collection serviceBindningList = getServiceBindningList(siteNodeVersionId, db); 566 serviceBindningVOList = toVOList(serviceBindningList); 567 568 ceb.throwIfNotEmpty(); 570 commitTransaction(db); 571 } 572 catch(ConstraintException ce) 573 { 574 logger.warn("An error occurred so we should not complete the transaction:" + ce, ce); 575 rollbackTransaction(db); 576 throw ce; 577 } 578 catch(Exception e) 579 { 580 logger.error("An error occurred so we should not complete the transaction:" + e, e); 581 rollbackTransaction(db); 582 throw new SystemException(e.getMessage()); 583 } 584 585 return serviceBindningVOList; 586 } 587 588 589 593 594 public static List getServiceBindningVOList(Integer siteNodeVersionId, Database db) throws ConstraintException, SystemException 595 { 596 List serviceBindningVOList = null; 597 598 Collection serviceBindningList = getServiceBindningList(siteNodeVersionId, db); 599 serviceBindningVOList = toVOList(serviceBindningList); 600 601 return serviceBindningVOList; 602 } 603 604 608 609 public static Collection getServiceBindningList(Integer siteNodeVersionId, Database db) throws ConstraintException, SystemException 610 { 611 SiteNodeVersion siteNodeVersion = getSiteNodeVersionWithIdAsReadOnly(siteNodeVersionId, db); 612 return siteNodeVersion.getServiceBindings(); 613 } 614 615 616 public static SiteNodeVersion getLatestPublishedSiteNodeVersion(Integer siteNodeId) throws SystemException, Bug, Exception 617 { 618 SiteNodeVersion siteNodeVersion = null; 619 620 Database db = CastorDatabaseService.getDatabase(); 621 ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer(); 622 623 beginTransaction(db); 624 try 625 { 626 siteNodeVersion = getLatestPublishedSiteNodeVersion(siteNodeId, db); 627 628 commitTransaction(db); 629 } 630 catch(Exception e) 631 { 632 logger.error("An error occurred so we should not completes the transaction:" + e, e); 633 rollbackTransaction(db); 634 throw new SystemException(e.getMessage()); 635 } 636 637 return siteNodeVersion; 638 } 639 640 641 642 public static SiteNodeVersion getLatestPublishedSiteNodeVersion(Integer siteNodeId, Database db) throws SystemException, Bug, Exception 643 { 644 SiteNodeVersion siteNodeVersion = null; 645 646 OQLQuery oql = db.getOQLQuery( "SELECT cv FROM org.infoglue.cms.entities.structure.impl.simple.SiteNodeVersionImpl cv WHERE cv.owningSiteNode.siteNodeId = $1 AND cv.stateId = $2 AND cv.isActive = $3 ORDER BY cv.siteNodeVersionId desc"); 647 oql.bind(siteNodeId); 648 oql.bind(SiteNodeVersionVO.PUBLISHED_STATE); 649 oql.bind(true); 650 651 QueryResults results = oql.execute(); 652 logger.info("Fetching entity in read/write mode"); 653 654 if (results.hasMore()) 655 { 656 siteNodeVersion = (SiteNodeVersion)results.next(); 657 } 658 659 results.close(); 660 oql.close(); 661 662 return siteNodeVersion; 663 } 664 665 666 669 670 public static SiteNodeVersionVO getPreviousSiteNodeVersionVO(Integer siteNodeId, Integer siteNodeVersionId) throws SystemException, Bug 671 { 672 Database db = CastorDatabaseService.getDatabase(); 673 ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer(); 674 675 SiteNodeVersionVO siteNodeVersionVO = null; 676 677 beginTransaction(db); 678 679 try 680 { 681 OQLQuery oql = db.getOQLQuery( "SELECT cv FROM org.infoglue.cms.entities.structure.impl.simple.SiteNodeVersionImpl cv WHERE cv.owningSiteNode.siteNodeId = $1 AND cv.siteNodeVersionId < $2 ORDER BY cv.siteNodeVersionId desc"); 682 oql.bind(siteNodeId); 683 oql.bind(siteNodeVersionId); 684 685 QueryResults results = oql.execute(Database.ReadOnly); 686 687 if (results.hasMore()) 688 { 689 SiteNodeVersion siteNodeVersion = (SiteNodeVersion)results.next(); 690 logger.info("found one:" + siteNodeVersion.getValueObject()); 691 siteNodeVersionVO = siteNodeVersion.getValueObject(); 692 } 693 694 results.close(); 695 oql.close(); 696 697 commitTransaction(db); 698 } 699 catch(Exception e) 700 { 701 logger.error("An error occurred so we should not completes the transaction:" + e, e); 702 rollbackTransaction(db); 703 throw new SystemException(e.getMessage()); 704 } 705 706 return siteNodeVersionVO; 707 } 708 709 710 713 714 public SiteNodeVersionVO getPreviousActiveSiteNodeVersionVO(Integer siteNodeId, Integer siteNodeVersionId, Database db) throws SystemException, Bug, Exception 715 { 716 SiteNodeVersionVO siteNodeVersionVO = null; 717 718 OQLQuery oql = db.getOQLQuery( "SELECT cv FROM org.infoglue.cms.entities.structure.impl.simple.SiteNodeVersionImpl cv WHERE cv.owningSiteNode.siteNodeId = $1 AND cv.isActive = $2 AND cv.siteNodeVersionId < $3 ORDER BY cv.siteNodeVersionId desc"); 719 oql.bind(siteNodeId); 720 oql.bind(new Boolean (true)); 721 oql.bind(siteNodeVersionId); 722 723 QueryResults results = oql.execute(Database.ReadOnly); 724 725 if (results.hasMore()) 726 { 727 SiteNodeVersion siteNodeVersion = (SiteNodeVersion)results.next(); 728 logger.info("found one:" + siteNodeVersion.getValueObject()); 729 siteNodeVersionVO = siteNodeVersion.getValueObject(); 730 } 731 732 results.close(); 733 oql.close(); 734 735 return siteNodeVersionVO; 736 } 737 738 741 742 public SiteNodeVersion getPreviousActiveSiteNodeVersion(Integer siteNodeId, Integer siteNodeVersionId, Database db) throws SystemException, Bug, Exception 743 { 744 SiteNodeVersion siteNodeVersion = null; 745 746 OQLQuery oql = db.getOQLQuery( "SELECT cv FROM org.infoglue.cms.entities.structure.impl.simple.SiteNodeVersionImpl cv WHERE cv.owningSiteNode.siteNodeId = $1 AND cv.isActive = $2 AND cv.siteNodeVersionId < $3 ORDER BY cv.siteNodeVersionId desc"); 747 oql.bind(siteNodeId); 748 oql.bind(new Boolean (true)); 749 oql.bind(siteNodeVersionId); 750 751 QueryResults results = oql.execute(Database.ReadOnly); 752 753 if (results.hasMore()) 754 { 755 siteNodeVersion = (SiteNodeVersion)results.next(); 756 logger.info("found one:" + siteNodeVersion.getValueObject()); 757 } 758 759 results.close(); 760 oql.close(); 761 762 return siteNodeVersion; 763 } 764 765 766 770 771 public List getSiteNodeVersionVOWithParentRecursive(Integer siteNodeId, Integer stateId) throws ConstraintException, SystemException 772 { 773 return getSiteNodeVersionVOWithParentRecursive(siteNodeId, stateId, new ArrayList ()); 774 } 775 776 private List getSiteNodeVersionVOWithParentRecursive(Integer siteNodeId, Integer stateId, List resultList) throws ConstraintException, SystemException 777 { 778 SiteNodeVersionVO siteNodeVersionVO = getLatestSiteNodeVersionVO(siteNodeId); 779 if(siteNodeVersionVO.getStateId().intValue() == stateId.intValue()) 780 resultList.add(siteNodeVersionVO); 781 782 List childSiteNodeList = SiteNodeController.getController().getSiteNodeChildren(siteNodeId); 784 Iterator childSiteNodeListIterator = childSiteNodeList.iterator(); 785 while(childSiteNodeListIterator.hasNext()) 786 { 787 SiteNodeVO siteNodeVO = (SiteNodeVO)childSiteNodeListIterator.next(); 788 getSiteNodeVersionVOWithParentRecursive(siteNodeVO.getId(), stateId, resultList); 789 } 790 791 return resultList; 792 } 793 794 798 799 public List getPublishedSiteNodeVersionVOWithParentRecursive(Integer siteNodeId) throws ConstraintException, SystemException 800 { 801 List publishedSiteNodeVersionVOList = new ArrayList (); 802 803 Database db = CastorDatabaseService.getDatabase(); 804 805 beginTransaction(db); 806 807 try 808 { 809 SiteNode siteNode = SiteNodeController.getController().getSiteNodeWithId(siteNodeId, db); 810 List publishedSiteNodeVersions = new ArrayList (); 811 getPublishedSiteNodeVersionWithParentRecursive(siteNode, publishedSiteNodeVersions, db); 812 publishedSiteNodeVersionVOList = toVOList(publishedSiteNodeVersions); 813 814 commitTransaction(db); 815 } 816 catch(Exception e) 817 { 818 logger.error("An error occurred so we should not completes the transaction:" + e, e); 819 rollbackTransaction(db); 820 throw new SystemException(e.getMessage()); 821 } 822 823 return publishedSiteNodeVersionVOList; 824 } 825 826 private List getPublishedSiteNodeVersionWithParentRecursive(SiteNode siteNode, List resultList, Database db) throws ConstraintException, SystemException, Exception 827 { 828 SiteNodeVersion siteNodeVersion = getLatestPublishedSiteNodeVersion(siteNode.getId(), db); 829 if(siteNodeVersion != null) 830 resultList.add(siteNodeVersion); 831 832 Collection childSiteNodeList = siteNode.getChildSiteNodes(); 834 Iterator childSiteNodeListIterator = childSiteNodeList.iterator(); 835 while(childSiteNodeListIterator.hasNext()) 836 { 837 SiteNode childSiteNode = (SiteNode)childSiteNodeListIterator.next(); 838 getPublishedSiteNodeVersionWithParentRecursive(childSiteNode, resultList, db); 839 } 840 841 return resultList; 842 } 843 844 845 848 849 public void getSiteNodeAndAffectedItemsRecursive(Integer siteNodeId, Integer stateId, List siteNodeVersionVOList, List contenteVersionVOList, boolean includeMetaInfo) throws ConstraintException, SystemException 850 { 851 Database db = CastorDatabaseService.getDatabase(); 852 853 beginTransaction(db); 854 855 try 856 { 857 SiteNode siteNode = SiteNodeController.getController().getSiteNodeWithId(siteNodeId, db); 858 859 getSiteNodeAndAffectedItemsRecursive(siteNode, stateId, new ArrayList (), new ArrayList (), db, siteNodeVersionVOList, contenteVersionVOList, includeMetaInfo); 860 861 commitTransaction(db); 862 } 863 catch(Exception e) 864 { 865 logger.error("An error occurred so we should not completes the transaction:" + e, e); 866 rollbackTransaction(db); 867 throw new SystemException(e.getMessage()); 868 } 869 } 870 871 private void getSiteNodeAndAffectedItemsRecursive(SiteNode siteNode, Integer stateId, List checkedSiteNodes, List checkedContents, Database db, List siteNodeVersionVOList, List contentVersionVOList, boolean includeMetaInfo) throws ConstraintException, SystemException, Exception 872 { 873 checkedSiteNodes.add(siteNode.getId()); 874 875 SiteNodeVersion siteNodeVersion = getLatestActiveSiteNodeVersion(db, siteNode.getId()); 878 if(siteNodeVersion != null && siteNodeVersion.getStateId().intValue() == stateId.intValue()) 879 { 880 siteNodeVersionVOList.add(siteNodeVersion.getValueObject()); 881 } 882 883 List relatedEntities = RegistryController.getController().getMatchingRegistryVOListForReferencingEntity(SiteNodeVersion.class.getName(), siteNodeVersion.getId().toString(), db); 884 Iterator relatedEntitiesIterator = relatedEntities.iterator(); 885 886 while(relatedEntitiesIterator.hasNext()) 887 { 888 RegistryVO registryVO = (RegistryVO)relatedEntitiesIterator.next(); 889 if(registryVO.getEntityName().equals(SiteNode.class.getName()) && !checkedSiteNodes.contains(new Integer (registryVO.getEntityId()))) 890 { 891 try 892 { 893 SiteNode relatedSiteNode = SiteNodeController.getController().getSiteNodeWithId(new Integer (registryVO.getEntityId()), db); 894 SiteNodeVersion relatedSiteNodeVersion = getLatestActiveSiteNodeVersion(db, new Integer (registryVO.getEntityId())); 896 if(relatedSiteNodeVersion != null && siteNodeVersion.getStateId().intValue() == stateId.intValue() && siteNode.getRepository().getId().intValue() == relatedSiteNodeVersion.getOwningSiteNode().getRepository().getId().intValue()) 897 { 898 siteNodeVersionVOList.add(relatedSiteNodeVersion.getValueObject()); 899 } 900 } 901 catch(Exception e) 902 { 903 logger.warn("A siteNode referenced by ID:" + registryVO.getEntityId() + " was not found - must be a invalid reference from " + siteNode.getName() + "[" + siteNode.getId() + "].", e); 904 } 905 906 checkedSiteNodes.add(new Integer (registryVO.getEntityId())); 907 } 908 else if(registryVO.getEntityName().equals(Content.class.getName()) && !checkedContents.contains(new Integer (registryVO.getEntityId()))) 909 { 910 try 911 { 912 Content relatedContent = ContentController.getContentController().getContentWithId(new Integer (registryVO.getEntityId()), db); 913 if(includeMetaInfo || (!includeMetaInfo && (relatedContent.getContentTypeDefinition() == null || !relatedContent.getContentTypeDefinition().getName().equalsIgnoreCase("Meta info")))) 914 { 915 List relatedContentVersions = ContentVersionController.getContentVersionController().getLatestActiveContentVersionIfInState(relatedContent, stateId, db); 916 917 Iterator relatedContentVersionsIterator = relatedContentVersions.iterator(); 918 while(relatedContentVersionsIterator.hasNext()) 919 { 920 ContentVersion relatedContentVersion = (ContentVersion)relatedContentVersionsIterator.next(); 921 if(relatedContentVersion != null && siteNode.getRepository().getId().intValue() == relatedContentVersion.getOwningContent().getRepository().getId().intValue()) 922 { 923 contentVersionVOList.add(relatedContentVersion.getValueObject()); 924 } 925 } 926 } 927 } 928 catch(Exception e) 929 { 930 logger.warn("A content referenced by ID:" + registryVO.getEntityId() + " was not found - must be a invalid reference from " + siteNode.getName() + "[" + siteNode.getId() + "].", e); 931 } 932 933 checkedContents.add(new Integer (registryVO.getEntityId())); 934 } 935 937 } 938 939 Collection childSiteNodeList = siteNode.getChildSiteNodes(); 941 Iterator cit = childSiteNodeList.iterator(); 942 while (cit.hasNext()) 943 { 944 SiteNode childSiteNode = (SiteNode) cit.next(); 945 getSiteNodeAndAffectedItemsRecursive(childSiteNode, stateId, checkedSiteNodes, checkedContents, db, siteNodeVersionVOList, contentVersionVOList, includeMetaInfo); 946 } 947 948 } 949 950 953 954 public SiteNodeVersion getLatestActiveSiteNodeVersionIfInState(SiteNode siteNode, Integer stateId, Database db) throws SystemException, Exception 955 { 956 SiteNodeVersion siteNodeVersion = null; 957 958 Collection siteNodeVersions = siteNode.getSiteNodeVersions(); 959 960 SiteNodeVersion latestSiteNodeVersion = null; 961 962 Iterator versionIterator = siteNodeVersions.iterator(); 963 while(versionIterator.hasNext()) 964 { 965 SiteNodeVersion siteNodeVersionCandidate = (SiteNodeVersion)versionIterator.next(); 966 967 if(latestSiteNodeVersion == null || (latestSiteNodeVersion.getId().intValue() < siteNodeVersionCandidate.getId().intValue() && siteNodeVersionCandidate.getIsActive().booleanValue())) 968 latestSiteNodeVersion = siteNodeVersionCandidate; 969 970 if(siteNodeVersionCandidate.getIsActive().booleanValue() && siteNodeVersionCandidate.getStateId().intValue() == stateId.intValue()) 971 { 972 if(siteNodeVersionCandidate.getOwningSiteNode().getSiteNodeId().intValue() == siteNode.getId().intValue()) 973 { 974 if(siteNodeVersion == null || siteNodeVersion.getSiteNodeVersionId().intValue() < siteNodeVersionCandidate.getId().intValue()) 975 { 976 siteNodeVersion = siteNodeVersionCandidate; 977 } 978 } 979 } 980 } 981 982 if(siteNodeVersion != latestSiteNodeVersion) 983 siteNodeVersion = null; 984 985 return siteNodeVersion; 986 } 987 988 989 996 public List getMetaInfoContentVersionVOList(Integer siteNodeVersionId, InfoGluePrincipal infoGluePrincipal) throws ConstraintException, SystemException, Exception 997 { 998 List contentVersionVOList = new ArrayList (); 999 1000 Database db = CastorDatabaseService.getDatabase(); 1001 1002 beginTransaction(db); 1003 1004 try 1005 { 1006 SiteNodeVersion siteNodeVersion = SiteNodeVersionController.getController().getSiteNodeVersionWithId(siteNodeVersionId, db); 1007 List contentVersions = getMetaInfoContentVersions(db, siteNodeVersion, infoGluePrincipal); 1008 contentVersionVOList = toVOList(contentVersions); 1009 1010 commitTransaction(db); 1011 } 1012 catch(Exception e) 1013 { 1014 logger.error("An error occurred so we should not completes the transaction:" + e, e); 1015 rollbackTransaction(db); 1016 throw new SystemException(e.getMessage()); 1017 } 1018 1019 return contentVersionVOList; 1020 } 1021 1022 1029 private List getMetaInfoContentVersions(Database db, SiteNodeVersion siteNodeVersion, InfoGluePrincipal infoGluePrincipal) throws ConstraintException, SystemException, Exception 1030 { 1031 List contentVersions = new ArrayList (); 1032 1033 List languages = LanguageController.getController().getLanguageList(siteNodeVersion.getOwningSiteNode().getRepository().getId(), db); 1034 Language masterLanguage = LanguageController.getController().getMasterLanguage(db, siteNodeVersion.getOwningSiteNode().getRepository().getId()); 1035 1036 Integer metaInfoAvailableServiceBindingId = null; 1037 Integer serviceBindingId = null; 1038 AvailableServiceBinding availableServiceBinding = AvailableServiceBindingController.getController().getAvailableServiceBindingWithName("Meta information", db, true); 1039 if(availableServiceBinding != null) 1040 metaInfoAvailableServiceBindingId = availableServiceBinding.getAvailableServiceBindingId(); 1041 1042 Collection serviceBindings = siteNodeVersion.getServiceBindings(); 1043 Iterator serviceBindingIterator = serviceBindings.iterator(); 1044 while(serviceBindingIterator.hasNext()) 1045 { 1046 ServiceBinding serviceBinding = (ServiceBinding)serviceBindingIterator.next(); 1047 if(serviceBinding.getAvailableServiceBinding().getId().intValue() == metaInfoAvailableServiceBindingId.intValue()) 1048 { 1049 serviceBindingId = serviceBinding.getId(); 1050 break; 1051 } 1052 } 1053 1054 if(serviceBindingId != null) 1055 { 1056 List boundContents = ContentController.getBoundContents(serviceBindingId); 1057 if(boundContents.size() > 0) 1058 { 1059 ContentVO contentVO = (ContentVO)boundContents.get(0); 1060 1061 Iterator languageIterator = languages.iterator(); 1062 while(languageIterator.hasNext()) 1063 { 1064 Language language = (Language)languageIterator.next(); 1065 ContentVersion contentVersion = ContentVersionController.getContentVersionController().getLatestActiveContentVersion(contentVO.getId(), language.getId(), db); 1066 1067 if(contentVersion != null) 1068 contentVersions.add(contentVersion); 1069 } 1070 } 1071 } 1072 1073 return contentVersions; 1074 } 1075 1076 1077 1080 1081 public SiteNodeVersionVO update(SiteNodeVersionVO siteNodeVersionVO) throws ConstraintException, SystemException 1082 { 1083 registryController.updateSiteNodeVersion(siteNodeVersionVO); 1084 1085 return (SiteNodeVersionVO) updateEntity(SiteNodeVersionImpl.class, (BaseEntityVO)siteNodeVersionVO); 1086 } 1087 1088 1091 1092 public SiteNodeVersionVO update(SiteNodeVersionVO siteNodeVersionVO, Database db) throws ConstraintException, SystemException, Exception 1093 { 1094 SiteNodeVersion siteNodeVersion = getSiteNodeVersionWithId(siteNodeVersionVO.getId(), db); 1095 registryController.updateSiteNodeVersion(siteNodeVersion, db); 1096 1097 siteNodeVersion.setValueObject(siteNodeVersionVO); 1098 return siteNodeVersionVO; 1099 } 1101 1102 1103 1110 public List getPublishedActiveSiteNodeVersionVOList(Integer siteNodeId) throws SystemException, Bug, Exception 1111 { 1112 List siteNodeVersionVOList = new ArrayList (); 1113 1114 Database db = CastorDatabaseService.getDatabase(); 1115 1116 beginTransaction(db); 1117 1118 try 1119 { 1120 List siteNodeVersionList = getPublishedActiveSiteNodeVersionVOList(siteNodeId, db); 1121 siteNodeVersionVOList = toVOList(siteNodeVersionList); 1122 1123 commitTransaction(db); 1124 } 1125 catch(Exception e) 1126 { 1127 logger.error("An error occurred so we should not completes the transaction:" + e, e); 1128 rollbackTransaction(db); 1129 throw new SystemException(e.getMessage()); 1130 } 1131 1132 return siteNodeVersionVOList; 1133 } 1134 1135 1136 public List getPublishedActiveSiteNodeVersionVOList(Integer siteNodeId, Database db) throws SystemException, Bug, Exception 1137 { 1138 List siteNodeVersionList = new ArrayList (); 1139 1140 OQLQuery oql = db.getOQLQuery( "SELECT cv FROM org.infoglue.cms.entities.structure.impl.simple.SiteNodeVersionImpl cv WHERE cv.owningSiteNode.siteNodeId = $1 AND cv.stateId = $2 AND cv.isActive = $3 ORDER BY cv.siteNodeVersionId desc"); 1141 oql.bind(siteNodeId); 1142 oql.bind(SiteNodeVersionVO.PUBLISHED_STATE); 1143 oql.bind(true); 1144 1145 QueryResults results = oql.execute(); 1146 logger.info("Fetching entity in read/write mode"); 1147 1148 while (results.hasMore()) 1149 { 1150 SiteNodeVersion siteNodeVersion = (SiteNodeVersion)results.next(); 1151 siteNodeVersionList.add(siteNodeVersion); 1152 } 1153 1154 results.close(); 1155 oql.close(); 1156 1157 return siteNodeVersionList; 1158 } 1159 1160 1164 1165 public BaseEntityVO getNewVO() 1166 { 1167 return new SiteNodeVersionVO(); 1168 } 1169 1170 1171} 1172 1173 | Popular Tags |