1 23 24 package org.infoglue.cms.controllers.kernel.impl.simple; 25 26 import java.io.File ; 27 import java.io.PrintWriter ; 28 import java.io.StringWriter ; 29 import java.util.ArrayList ; 30 import java.util.Calendar ; 31 import java.util.Collection ; 32 import java.util.Collections ; 33 import java.util.HashMap ; 34 import java.util.Iterator ; 35 import java.util.List ; 36 import java.util.Map ; 37 38 import org.apache.log4j.Logger; 39 import org.exolab.castor.jdo.Database; 40 import org.exolab.castor.jdo.OQLQuery; 41 import org.exolab.castor.jdo.QueryResults; 42 import org.infoglue.cms.entities.content.Content; 43 import org.infoglue.cms.entities.content.ContentVO; 44 import org.infoglue.cms.entities.content.ContentVersion; 45 import org.infoglue.cms.entities.content.ContentVersionVO; 46 import org.infoglue.cms.entities.kernel.BaseEntityVO; 47 import org.infoglue.cms.entities.management.Language; 48 import org.infoglue.cms.entities.publishing.EditionBrowser; 49 import org.infoglue.cms.entities.publishing.Publication; 50 import org.infoglue.cms.entities.publishing.PublicationDetail; 51 import org.infoglue.cms.entities.publishing.PublicationDetailVO; 52 import org.infoglue.cms.entities.publishing.PublicationVO; 53 import org.infoglue.cms.entities.publishing.impl.simple.PublicationDetailImpl; 54 import org.infoglue.cms.entities.publishing.impl.simple.PublicationImpl; 55 import org.infoglue.cms.entities.structure.SiteNode; 56 import org.infoglue.cms.entities.structure.SiteNodeVO; 57 import org.infoglue.cms.entities.structure.SiteNodeVersion; 58 import org.infoglue.cms.entities.structure.SiteNodeVersionVO; 59 import org.infoglue.cms.entities.workflow.Event; 60 import org.infoglue.cms.entities.workflow.EventVO; 61 import org.infoglue.cms.exception.ConstraintException; 62 import org.infoglue.cms.exception.SystemException; 63 import org.infoglue.cms.io.FileHelper; 64 import org.infoglue.cms.security.InfoGluePrincipal; 65 import org.infoglue.cms.util.ChangeNotificationController; 66 import org.infoglue.cms.util.CmsPropertyHandler; 67 import org.infoglue.cms.util.DateHelper; 68 import org.infoglue.cms.util.NotificationMessage; 69 import org.infoglue.cms.util.RemoteCacheUpdater; 70 import org.infoglue.cms.util.mail.MailServiceFactory; 71 import org.infoglue.deliver.util.VelocityTemplateProcessor; 72 73 74 79 80 public class PublicationController extends BaseController 81 { 82 private final static Logger logger = Logger.getLogger(PublicationController.class.getName()); 83 84 public static final int OVERIDE_WORKING = 1; 85 public static final int LEAVE_WORKING = 2; 86 87 public static PublicationController getController() 88 { 89 return new PublicationController(); 90 } 91 92 95 public static Publication getPublicationWithId(Integer publicationId, Database db) throws SystemException 96 { 97 return (Publication) getObjectWithId(PublicationImpl.class, publicationId, db); 98 } 99 100 103 public PublicationDetailVO getPublicationDetailVOWithId(Integer publicationDetailId) throws SystemException 104 { 105 return (PublicationDetailVO) getVOWithId(PublicationDetailImpl.class, publicationDetailId); 106 } 107 108 112 public static List getPublicationEvents(Integer repositoryId) throws SystemException, Exception 113 { 114 return EventController.getPublicationEventVOListForRepository(repositoryId); 115 } 116 117 120 public static List getAllEditions(Integer repositoryId) throws SystemException 121 { 122 Database db = CastorDatabaseService.getDatabase(); 123 beginTransaction(db); 124 List res = new ArrayList (); 125 try 126 { 127 OQLQuery oql = db.getOQLQuery( "SELECT c FROM org.infoglue.cms.entities.publishing.impl.simple.PublicationImpl c WHERE c.repositoryId = $1 order by publicationDateTime desc"); 128 oql.bind(repositoryId); 129 130 QueryResults results = oql.execute(Database.ReadOnly); 131 132 while (results.hasMore()) 133 { 134 Publication publication = (Publication)results.next(); 135 res.add(publication.getValueObject()); 136 } 137 138 results.close(); 139 oql.close(); 140 141 commitTransaction(db); 142 } 143 catch(Exception e) 144 { 145 logger.error("An error occurred so we should not completes the transaction:" + e, e); 146 rollbackTransaction(db); 147 throw new SystemException(e.getMessage()); 148 } 149 150 return res; 151 } 152 153 156 public static EditionBrowser getEditionPage(Integer repositoryId, int startIndex) throws SystemException 157 { 158 int pageSize = new Integer (CmsPropertyHandler.getEditionPageSize()).intValue(); 159 160 Database db = CastorDatabaseService.getDatabase(); 161 beginTransaction(db); 162 try 163 { 164 OQLQuery oql = db.getOQLQuery("SELECT c FROM org.infoglue.cms.entities.publishing.impl.simple.PublicationImpl c WHERE c.repositoryId = $1 order by publicationDateTime desc"); 165 oql.bind(repositoryId); 166 QueryResults results = oql.execute(Database.ReadOnly); 167 168 List allEditions = Collections.list(results); 169 170 List page = allEditions.subList(startIndex, Math.min(startIndex+pageSize, allEditions.size())); 171 172 EditionBrowser browser = new EditionBrowser(allEditions.size(), pageSize, startIndex); 173 174 List editionVOs = new ArrayList (); 175 for (Iterator iter = page.iterator(); iter.hasNext();) 176 { 177 Publication pub = (Publication) iter.next(); 178 PublicationVO pubVO = pub.getValueObject(); 179 editionVOs.add(pubVO); 181 } 182 183 browser.setEditions(editionVOs); 184 185 results.close(); 186 oql.close(); 187 188 commitTransaction(db); 189 190 return browser; 191 } 192 catch(Exception e) 193 { 194 logger.error("An error occurred so we should not completes the transaction:" + e, e); 195 rollbackTransaction(db); 196 throw new SystemException(e.getMessage()); 197 } 198 } 199 200 201 207 public static void denyPublicationRequest(Integer eventId, String publisherUserName, String comment, String referenceUrl) throws SystemException 208 { 209 Database db = CastorDatabaseService.getDatabase(); 210 beginTransaction(db); 211 212 try 213 { 214 Event event = EventController.getEventWithId(eventId, db); 215 if(event.getTypeId().intValue() == EventVO.PUBLISH.intValue()) 216 { 217 event.setTypeId(EventVO.PUBLISH_DENIED); 218 if(event.getEntityClass().equals(ContentVersion.class.getName())) 219 { 220 ContentVersion contentVersion = ContentVersionController.getContentVersionController().getContentVersionWithId(event.getEntityId(), db); 221 if(contentVersion.getStateId().intValue() == ContentVersionVO.PUBLISHED_STATE.intValue()) 222 { 223 EventController.delete(event, db); 225 } 226 else 227 { 228 Content content = contentVersion.getOwningContent(); 229 Language language = contentVersion.getLanguage(); 230 event.setEntityId(ContentVersionController.getContentVersionController().getPreviousActiveContentVersionVO(content.getId(), language.getId(), contentVersion.getId(), db).getId()); 232 ContentVersionController.getContentVersionController().delete(contentVersion, db); 233 } 234 } 235 else if(event.getEntityClass().equals(SiteNodeVersion.class.getName())) 236 { 237 SiteNodeVersion siteNodeVersion = SiteNodeVersionController.getController().getSiteNodeVersionWithId(event.getEntityId(), db); 238 if(siteNodeVersion.getStateId().intValue() == SiteNodeVersionVO.PUBLISHED_STATE.intValue()) 239 { 240 EventController.delete(event, db); 242 } 243 else 244 { 245 SiteNode siteNode = siteNodeVersion.getOwningSiteNode(); 246 event.setEntityId(SiteNodeVersionController.getController().getPreviousActiveSiteNodeVersionVO(siteNode.getId(), siteNodeVersion.getId(), db).getId()); 248 SiteNodeVersionController.getController().delete(siteNodeVersion, db); 249 } 251 } 252 } 253 else if(event.getTypeId().intValue() == EventVO.UNPUBLISH_LATEST.intValue()) 254 { 255 event.setTypeId(EventVO.UNPUBLISH_DENIED); 256 if(event.getEntityClass().equals(ContentVersion.class.getName())) 257 { 258 event.setEntityClass(Content.class.getName()); 259 event.setEntityId(ContentVersionController.getContentVersionController().getContentVersionWithId(event.getEntityId(), db).getOwningContent().getId()); 260 } 261 else if(event.getEntityClass().equals(SiteNodeVersion.class.getName())) 262 { 263 event.setEntityClass(SiteNode.class.getName()); 264 event.setEntityId(SiteNodeVersionController.getController().getSiteNodeVersionWithId(event.getEntityId(), db).getOwningSiteNode().getId()); 265 } 266 } 267 268 InfoGluePrincipal infoGluePrincipal = InfoGluePrincipalControllerProxy.getController().getInfoGluePrincipal(event.getCreator()); 269 mailNotification(event, publisherUserName, infoGluePrincipal.getEmail(), comment, referenceUrl); 270 271 commitTransaction(db); 272 } 273 catch(Exception e) 274 { 275 logger.error("An error occurred so we should not completes the transaction:" + e, e); 276 rollbackTransaction(db); 277 throw new SystemException(e.getMessage()); 278 } 279 } 280 281 282 288 public static void denyPublicationRequest(List eventVOList, String publisherUserName, String comment, String referenceUrl) throws SystemException 289 { 290 Database db = CastorDatabaseService.getDatabase(); 291 beginTransaction(db); 292 293 try 294 { 295 Iterator eventIterator = eventVOList.iterator(); 296 while(eventIterator.hasNext()) 297 { 298 EventVO eventVO = (EventVO)eventIterator.next(); 299 300 Event event = EventController.getEventWithId(eventVO.getId(), db); 301 InfoGluePrincipal infoGluePrincipal = InfoGluePrincipalControllerProxy.getController().getInfoGluePrincipal(event.getCreator()); 302 303 if(event.getTypeId().intValue() == EventVO.PUBLISH.intValue()) 304 { 305 event.setTypeId(EventVO.PUBLISH_DENIED); 306 if(event.getEntityClass().equals(ContentVersion.class.getName())) 307 { 308 ContentVersion contentVersion = ContentVersionController.getContentVersionController().getContentVersionWithId(event.getEntityId(), db); 309 if(contentVersion.getStateId().intValue() == ContentVersionVO.PUBLISHED_STATE.intValue()) 310 { 311 EventController.delete(event, db); 313 } 314 else 315 { 316 Content content = contentVersion.getOwningContent(); 317 Language language = contentVersion.getLanguage(); 318 event.setEntityId(ContentVersionController.getContentVersionController().getPreviousActiveContentVersionVO(content.getId(), language.getId(), contentVersion.getId(), db).getId()); 320 ContentVersionController.getContentVersionController().delete(contentVersion, db); 321 } 322 } 323 else if(event.getEntityClass().equals(SiteNodeVersion.class.getName())) 324 { 325 SiteNodeVersion siteNodeVersion = SiteNodeVersionController.getController().getSiteNodeVersionWithId(event.getEntityId(), db); 326 if(siteNodeVersion.getStateId().intValue() == SiteNodeVersionVO.PUBLISHED_STATE.intValue()) 327 { 328 EventController.delete(event, db); 330 } 331 else 332 { 333 SiteNode siteNode = siteNodeVersion.getOwningSiteNode(); 334 SiteNodeVersion previousSiteNodeVersion = SiteNodeVersionController.getController().getPreviousActiveSiteNodeVersion(siteNode.getId(), siteNodeVersion.getId(), db); 336 event.setEntityId(previousSiteNodeVersion.getId()); 337 SiteNodeVersionController.getController().delete(siteNodeVersion, db); 338 SiteNodeStateController.getController().changeStateOnMetaInfo(db, previousSiteNodeVersion, previousSiteNodeVersion.getStateId(), "Denied publication", true, infoGluePrincipal, new ArrayList ()); 339 } 341 } 342 } 343 else if(event.getTypeId().intValue() == EventVO.UNPUBLISH_LATEST.intValue()) 344 { 345 event.setTypeId(EventVO.UNPUBLISH_DENIED); 346 if(event.getEntityClass().equals(ContentVersion.class.getName())) 347 { 348 event.setEntityClass(Content.class.getName()); 349 event.setEntityId(ContentVersionController.getContentVersionController().getContentVersionWithId(event.getEntityId(), db).getOwningContent().getId()); 350 } 351 else if(event.getEntityClass().equals(SiteNodeVersion.class.getName())) 352 { 353 event.setEntityClass(SiteNode.class.getName()); 354 event.setEntityId(SiteNodeVersionController.getController().getSiteNodeVersionWithId(event.getEntityId(), db).getOwningSiteNode().getId()); 355 } 356 } 357 358 mailNotification(event, publisherUserName, infoGluePrincipal.getEmail(), comment, referenceUrl); 359 } 360 361 commitTransaction(db); 362 } 363 catch(Exception e) 364 { 365 logger.error("An error occurred so we should not completes the transaction:" + e, e); 366 rollbackTransaction(db); 367 throw new SystemException(e.getMessage()); 368 } 369 } 370 371 372 373 376 private static void mailNotification(Event event, String editorName, String recipient, String comment, String referenceUrl) 377 { 378 String email = ""; 379 380 try 381 { 382 String template; 383 384 String contentType = CmsPropertyHandler.getMailContentType(); 385 if(contentType == null || contentType.length() == 0) 386 contentType = "text/html"; 387 388 if(contentType.equalsIgnoreCase("text/plain")) 389 template = FileHelper.getFileAsString(new File (CmsPropertyHandler.getContextRootPath() + "cms/publishingtool/deniedPublication_plain.vm")); 390 else 391 template = FileHelper.getFileAsString(new File (CmsPropertyHandler.getContextRootPath() + "cms/publishingtool/deniedPublication_html.vm")); 392 393 Map parameters = new HashMap (); 394 parameters.put("event", event); 395 parameters.put("editorName", editorName); 396 parameters.put("recipient", recipient); 397 parameters.put("comment", comment); 398 parameters.put("referenceUrl", referenceUrl); 399 400 StringWriter tempString = new StringWriter (); 401 PrintWriter pw = new PrintWriter (tempString); 402 new VelocityTemplateProcessor().renderTemplate(parameters, pw, template); 403 email = tempString.toString(); 404 405 String systemEmailSender = CmsPropertyHandler.getSystemEmailSender(); 406 if(systemEmailSender == null || systemEmailSender.equalsIgnoreCase("")) 407 systemEmailSender = "InfoGlueCMS@" + CmsPropertyHandler.getMailSmtpHost(); 408 409 logger.info("email:" + email); 410 MailServiceFactory.getService().send(systemEmailSender, recipient, "CMS - Publishing was denied!!", email, contentType, "UTF-8"); 411 } 412 catch(Exception e) 413 { 414 logger.error("The notification was not sent. Reason:" + e.getMessage(), e); 415 } 416 } 417 418 419 422 public PublicationVO createAndPublish(PublicationVO publicationVO, List events, boolean overrideVersionModifyer, InfoGluePrincipal infoGluePrincipal) throws SystemException 423 { 424 Database db = CastorDatabaseService.getDatabase(); 425 426 try 427 { 428 beginTransaction(db); 429 430 publicationVO = createAndPublish(publicationVO, events, overrideVersionModifyer, infoGluePrincipal, db); 431 432 commitTransaction(db); 433 } 434 catch(Exception e) 435 { 436 logger.error("An error occurred when we tried to commit the publication: " + e.getMessage(), e); 437 rollbackTransaction(db); 438 } 439 440 return publicationVO; 441 } 442 443 446 public PublicationVO createAndPublish(PublicationVO publicationVO, List events, boolean overrideVersionModifyer, InfoGluePrincipal infoGluePrincipal, Database db) throws SystemException, Exception 447 { 448 logger.info("*********************************"); 449 logger.info("Creating edition "); 450 logger.info("*********************************"); 451 452 Publication publication = new PublicationImpl(); 453 publicationVO.setPublicationDateTime(Calendar.getInstance().getTime()); 454 publication.setValueObject(publicationVO); 455 publication.setPublisher(infoGluePrincipal.getName()); 456 457 Iterator eventIterator = events.iterator(); 458 while(eventIterator.hasNext()) 459 { 460 EventVO event = (EventVO)eventIterator.next(); 461 createPublicationInformation(publication, EventController.getEventWithId(event.getId(), db), overrideVersionModifyer, infoGluePrincipal, db); 462 } 463 464 db.create(publication); 465 466 try 468 { 469 logger.info("Starting replication..."); 470 ReplicationMySqlController.updateSlaveServer(); 471 logger.info("Finished replication..."); 472 } 473 catch (Exception e) 474 { 475 logger.error("An error occurred when we tried to replicate the data:" + e.getMessage(), e); 476 } 477 478 try 480 { 481 Map hashMap = new HashMap (); 482 hashMap.put("publicationId", publicationVO.getId()); 483 484 intercept(hashMap, "Publication.Write", infoGluePrincipal); 485 } 486 catch (Exception e) 487 { 488 logger.error("An error occurred when we tried to replicate the data:" + e.getMessage(), e); 489 } 490 491 try 493 { 494 logger.info("Notifying the entire system about a publishing..."); 495 NotificationMessage notificationMessage = new NotificationMessage("PublicationController.createAndPublish():", PublicationImpl.class.getName(), infoGluePrincipal.getName(), NotificationMessage.PUBLISHING, publicationVO.getId(), publicationVO.getName()); 496 ChangeNotificationController.getInstance().addNotificationMessage(notificationMessage); 498 RemoteCacheUpdater.clearSystemNotificationMessages(); 499 logger.info("Finished Notifying..."); 500 } 501 catch (Exception e) 502 { 503 logger.error("An error occurred when we tried to replicate the data:" + e.getMessage(), e); 504 } 505 506 return publicationVO; 507 } 508 509 510 514 private static void createPublicationInformation(Publication publication, Event event, boolean overrideVersionModifyer, InfoGluePrincipal infoGluePrincipal, Database db) throws Exception 515 { 516 String entityClass = event.getEntityClass(); 517 Integer entityId = event.getEntityId(); 518 Integer typeId = event.getTypeId(); 519 logger.info("entityClass:" + entityClass); 520 logger.info("entityId:" + entityId); 521 logger.info("typeId:" + typeId); 522 523 if(entityClass.equals(ContentVersion.class.getName())) 525 { 526 ContentVersion contentVersion = null; 527 ContentVersion oldContentVersion = ContentVersionController.getContentVersionController().getContentVersionWithId(entityId, db); 528 if(oldContentVersion != null && oldContentVersion.getOwningContent() != null && typeId.intValue() == EventVO.UNPUBLISH_LATEST.intValue()) 529 { 530 oldContentVersion.setIsActive(new Boolean (false)); 531 contentVersion = oldContentVersion; 532 540 } 541 548 else if(oldContentVersion != null && oldContentVersion.getOwningContent() != null) 549 { 550 List events = new ArrayList (); 551 Integer contentId = oldContentVersion.getOwningContent().getContentId(); 552 ContentVersion newContentVersion = ContentStateController.changeState(entityId, ContentVersionVO.PUBLISHED_STATE, "Published", overrideVersionModifyer, infoGluePrincipal, contentId, db, events); 553 contentVersion = ContentVersionController.getContentVersionController().getContentVersionWithId(newContentVersion.getContentVersionId(), db); 554 } 555 556 if(contentVersion != null) 557 { 558 PublicationDetail publicationDetail = new PublicationDetailImpl(); 560 publicationDetail.setCreationDateTime(DateHelper.getSecondPreciseDate()); 561 publicationDetail.setDescription(event.getDescription()); 562 publicationDetail.setEntityClass(entityClass); 563 publicationDetail.setEntityId(contentVersion.getId()); 564 publicationDetail.setName(event.getName()); 565 publicationDetail.setTypeId(event.getTypeId()); 566 publicationDetail.setPublication((PublicationImpl)publication); 567 publicationDetail.setCreator(event.getCreator()); 568 569 Collection publicationDetails = publication.getPublicationDetails(); 570 if(publicationDetails == null) 571 publication.setPublicationDetails(new ArrayList ()); 572 573 publication.getPublicationDetails().add(publicationDetail); 574 db.remove(event); 575 } 576 } 577 578 if(entityClass.equals(SiteNodeVersion.class.getName())) 580 { 581 SiteNodeVersion siteNodeVersion = null; 582 SiteNodeVersion oldSiteNodeVersion = SiteNodeVersionController.getController().getSiteNodeVersionWithId(entityId, db); 583 if(oldSiteNodeVersion != null && oldSiteNodeVersion.getOwningSiteNode() != null && typeId.intValue() == EventVO.UNPUBLISH_LATEST.intValue()) 584 { 585 oldSiteNodeVersion.setIsActive(new Boolean (false)); 586 siteNodeVersion = oldSiteNodeVersion; 587 595 } 596 603 else if(oldSiteNodeVersion != null && oldSiteNodeVersion.getOwningSiteNode() != null) 604 { 605 List events = new ArrayList (); 606 Integer siteNodeId = oldSiteNodeVersion.getOwningSiteNode().getSiteNodeId(); 607 SiteNodeVersion newSiteNodeVersion = SiteNodeStateController.getController().changeState(entityId, SiteNodeVersionVO.PUBLISHED_STATE, "Published", overrideVersionModifyer, infoGluePrincipal, siteNodeId, db, events); 608 siteNodeVersion = SiteNodeVersionController.getController().getSiteNodeVersionWithId(newSiteNodeVersion.getSiteNodeVersionId(), db); 609 } 610 611 if(siteNodeVersion != null) 612 { 613 PublicationDetail publicationDetail = new PublicationDetailImpl(); 615 publicationDetail.setCreationDateTime(DateHelper.getSecondPreciseDate()); 616 publicationDetail.setDescription(event.getDescription()); 617 publicationDetail.setEntityClass(entityClass); 618 publicationDetail.setEntityId(siteNodeVersion.getId()); 619 publicationDetail.setName(event.getName()); 620 publicationDetail.setTypeId(event.getTypeId()); 621 publicationDetail.setPublication((PublicationImpl)publication); 622 publicationDetail.setCreator(event.getCreator()); 623 624 Collection publicationDetails = publication.getPublicationDetails(); 625 if(publicationDetails == null) 626 publication.setPublicationDetails(new ArrayList ()); 627 628 publication.getPublicationDetails().add(publicationDetail); 629 db.remove(event); 630 } 631 } 632 } 633 634 637 public static PublicationVO create(PublicationVO publication) throws SystemException 638 { 639 Database db = beginTransaction(); 640 641 try 642 { 643 PublicationImpl p = new PublicationImpl(); 644 p.setValueObject(publication); 645 p.setPublicationDetails(new ArrayList ()); 646 for (Iterator iter = publication.getPublicationDetails().iterator(); iter.hasNext();) 647 { 648 PublicationDetailVO detailVO = (PublicationDetailVO) iter.next(); 649 PublicationDetail pd = new PublicationDetailImpl(); 650 pd.setPublication(p); 651 pd.setValueObject(detailVO); 652 p.getPublicationDetails().add(pd); 653 } 654 655 db.create(p); 656 657 PublicationVO returnPub = p.getValueObject(); 658 returnPub.setPublicationDetails(toVOList(p.getPublicationDetails())); 659 660 commitTransaction(db); 661 return returnPub; 662 } 663 catch(Exception e) 664 { 665 e.printStackTrace(); 666 rollbackTransaction(db); 667 throw new SystemException(e.getMessage()); 668 } 669 } 670 671 674 public static List getPublicationDetailVOList(Integer publicationId) throws SystemException 675 { 676 List publicationDetails = new ArrayList (); 677 678 Database db = CastorDatabaseService.getDatabase(); 679 beginTransaction(db); 680 681 try 682 { 683 Publication publication = getPublicationWithId(publicationId, db); 684 Collection details = publication.getPublicationDetails(); 685 publicationDetails = toVOList(details); 686 687 commitTransaction(db); 688 } 689 catch(Exception e) 690 { 691 logger.error("An error occurred so we should not completes the transaction:" + e, e); 692 rollbackTransaction(db); 693 throw new SystemException(e.getMessage()); 694 } 695 696 return publicationDetails; 697 } 698 699 700 703 public static PublicationVO unPublish(Integer publicationId, InfoGluePrincipal infoGluePrincipal) throws SystemException 704 { 705 logger.info("Starting unpublishing operation..."); 706 707 Database db = CastorDatabaseService.getDatabase(); 708 Publication publication = null; 709 710 beginTransaction(db); 711 712 try 713 { 714 publication = getPublicationWithId(publicationId, db); 715 Collection publicationDetails = publication.getPublicationDetails(); 716 717 Iterator i = publicationDetails.iterator(); 718 while (i.hasNext()) 719 { 720 PublicationDetail publicationDetail = (PublicationDetail)i.next(); 721 logger.info("publicationDetail:" + publicationDetail.getId() + ":" + publicationDetail.getTypeId()); 722 if(publicationDetail.getTypeId().intValue() != PublicationDetailVO.UNPUBLISH_LATEST.intValue()) 724 { 725 unpublishEntity(publicationDetail, infoGluePrincipal, db); 726 } 727 else 728 { 729 republishEntity(publicationDetail, infoGluePrincipal, db); 730 } 731 } 732 733 db.remove(publication); 734 735 commitTransaction(db); 736 logger.info("Done unpublishing operation..."); 737 } 738 catch(Exception e) 739 { 740 logger.error("An error occurred so we should not completes the transaction:" + e, e); 741 rollbackTransaction(db); 742 throw new SystemException(e.getMessage()); 743 } 744 745 try 746 { 747 logger.info("Starting replication operation..."); 748 ReplicationMySqlController.updateSlaveServer(); 749 logger.info("Done replication operation..."); 750 } 751 catch (Exception e) 752 { 753 logger.error("An error occurred when we tried to replicate the data:" + e.getMessage(), e); 754 } 755 756 try 758 { 759 logger.info("Notifying the entire system about an unpublishing..."); 760 NotificationMessage notificationMessage = new NotificationMessage("PublicationController.unPublish():", PublicationImpl.class.getName(), infoGluePrincipal.getName(), NotificationMessage.UNPUBLISHING, publication.getId(), publication.getName()); 761 ChangeNotificationController.getInstance().addNotificationMessage(notificationMessage); 762 RemoteCacheUpdater.clearSystemNotificationMessages(); 763 logger.info("Finished Notifying..."); 764 } 765 catch (Exception e) 766 { 767 logger.error("An error occurred when we tried to replicate the data:" + e.getMessage(), e); 768 } 769 770 return publication.getValueObject(); 771 } 772 773 774 777 private static void unpublishEntity(PublicationDetail publicationDetail, InfoGluePrincipal infoGluePrincipal, Database db) throws ConstraintException, SystemException 778 { 779 Integer repositoryId = null; 780 781 try 782 { 783 if(publicationDetail.getEntityClass().equals(ContentVersion.class.getName())) 784 { 785 ContentVersion contentVersion = ContentVersionController.getContentVersionController().getContentVersionWithId(publicationDetail.getEntityId(), db); 786 contentVersion.setIsActive(new Boolean (false)); 787 repositoryId = contentVersion.getOwningContent().getRepository().getId(); 788 } 789 else if(publicationDetail.getEntityClass().equals(SiteNodeVersion.class.getName())) 790 { 791 SiteNodeVersion siteNodeVersion = SiteNodeVersionController.getController().getSiteNodeVersionWithId(publicationDetail.getEntityId(), db); 792 siteNodeVersion.setIsActive(new Boolean (false)); 793 repositoryId = siteNodeVersion.getOwningSiteNode().getRepository().getId(); 794 } 795 796 EventVO eventVO = new EventVO(); 797 eventVO.setDescription(publicationDetail.getDescription()); 798 eventVO.setEntityClass(publicationDetail.getEntityClass()); 799 eventVO.setEntityId(publicationDetail.getEntityId()); 800 eventVO.setName(publicationDetail.getName()); 801 eventVO.setTypeId(EventVO.PUBLISH); 802 EventController.create(eventVO, repositoryId, infoGluePrincipal, db); 803 } 804 catch(Exception e) 805 { 806 logger.info("Could not unpublish entity:" + e.getMessage(), e); 807 } 808 } 809 810 813 private static void republishEntity(PublicationDetail publicationDetail, InfoGluePrincipal infoGluePrincipal, Database db) throws ConstraintException, SystemException 814 { 815 Integer repositoryId = null; 816 817 try 818 { 819 boolean createEvent = false; 820 821 if(publicationDetail.getEntityClass().equals(ContentVersion.class.getName())) 822 { 823 ContentVersion contentVersion = ContentVersionController.getContentVersionController().getContentVersionWithId(publicationDetail.getEntityId(), db); 824 if(contentVersion.getOwningContent() != null) 825 { 826 contentVersion.setIsActive(new Boolean (true)); 827 repositoryId = contentVersion.getOwningContent().getRepository().getId(); 828 createEvent = true; 829 } 830 else 831 { 832 logger.warn("The contentVersion:" + contentVersion.getId() + " had no content - this should never happen, investigate why. Removing invalid content version."); 833 ContentVersionController.getContentVersionController().delete(contentVersion, db); 834 } 835 } 836 else if(publicationDetail.getEntityClass().equals(SiteNodeVersion.class.getName())) 837 { 838 SiteNodeVersion siteNodeVersion = SiteNodeVersionController.getController().getSiteNodeVersionWithId(publicationDetail.getEntityId(), db); 839 if(siteNodeVersion.getOwningSiteNode() != null) 840 { 841 siteNodeVersion.setIsActive(new Boolean (true)); 842 repositoryId = siteNodeVersion.getOwningSiteNode().getRepository().getId(); 843 createEvent = true; 844 } 845 else 846 { 847 logger.warn("The siteNodeVersion:" + siteNodeVersion.getId() + " had no siteNode - this should never happen, investigate why. Removing invalid sitenode version."); 848 SiteNodeVersionController.getController().delete(siteNodeVersion, db); 849 } 850 } 851 852 if(createEvent) 853 { 854 EventVO eventVO = new EventVO(); 855 eventVO.setDescription(publicationDetail.getDescription()); 856 eventVO.setEntityClass(publicationDetail.getEntityClass()); 857 eventVO.setEntityId(publicationDetail.getEntityId()); 858 eventVO.setName(publicationDetail.getName()); 859 eventVO.setTypeId(EventVO.UNPUBLISH_LATEST); 860 EventController.create(eventVO, repositoryId, infoGluePrincipal, db); 861 } 862 } 863 catch(Exception e) 864 { 865 logger.warn("Could not republish entity:" + e.getMessage(), e); 866 } 867 } 868 869 870 873 public static ContentVO getOwningContentVO(Integer id) throws SystemException 874 { 875 ContentVO contentVO = null; 876 877 Database db = CastorDatabaseService.getDatabase(); 878 ContentVersion contentVersion = null; 879 beginTransaction(db); 880 try 881 { 882 contentVersion = ContentVersionController.getContentVersionController().getContentVersionWithId(id, db); 883 contentVO = contentVersion.getOwningContent().getValueObject(); 884 887 commitTransaction(db); 888 } 889 catch(Exception e) 890 { 891 logger.error("An error occurred so we should not completes the transaction:" + e, e); 892 rollbackTransaction(db); 893 throw new SystemException(e.getMessage()); 894 } 895 896 return contentVO; 897 } 898 899 902 public static SiteNodeVO getOwningSiteNodeVO(Integer id) throws SystemException 903 { 904 Database db = CastorDatabaseService.getDatabase(); 905 SiteNodeVersion siteNodeVersion = null; 906 beginTransaction(db); 907 try 908 { 909 siteNodeVersion = SiteNodeVersionController.getController().getSiteNodeVersionWithId(id, db); 910 commitTransaction(db); 911 } 912 catch(Exception e) 913 { 914 logger.error("An error occurred so we should not completes the transaction:" + e, e); 915 rollbackTransaction(db); 916 throw new SystemException(e.getMessage()); 917 } 918 919 return siteNodeVersion.getOwningSiteNode().getValueObject(); 920 } 921 922 923 927 public BaseEntityVO getNewVO() 928 { 929 return new PublicationVO(); 930 } 931 } 932 | Popular Tags |