KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > cms > controllers > kernel > impl > simple > PublicationController


1 /* ===============================================================================
2  *
3  * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4  *
5  * ===============================================================================
6  *
7  * Copyright (C)
8  *
9  * This program is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License version 2, as published by the
11  * Free Software Foundation. See the file LICENSE.html for more information.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19  * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20  *
21  * ===============================================================================
22  */

23
24 package org.infoglue.cms.controllers.kernel.impl.simple;
25
26 import java.io.File JavaDoc;
27 import java.io.PrintWriter JavaDoc;
28 import java.io.StringWriter JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.Calendar JavaDoc;
31 import java.util.Collection JavaDoc;
32 import java.util.Collections JavaDoc;
33 import java.util.HashMap JavaDoc;
34 import java.util.Iterator JavaDoc;
35 import java.util.List JavaDoc;
36 import java.util.Map JavaDoc;
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 /**
75  * PublicationController.java
76  *
77  * @author Stefan Sik, Mattias Bogeblad
78  */

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     /**
93      * This method just returns the publication with the given id within the given transaction.
94      */

95     public static Publication getPublicationWithId(Integer JavaDoc publicationId, Database db) throws SystemException
96     {
97         return (Publication) getObjectWithId(PublicationImpl.class, publicationId, db);
98     }
99
100     /**
101      * This method just returns the publication detail with the given id.
102      */

103     public PublicationDetailVO getPublicationDetailVOWithId(Integer JavaDoc publicationDetailId) throws SystemException
104     {
105         return (PublicationDetailVO) getVOWithId(PublicationDetailImpl.class, publicationDetailId);
106     }
107
108     /**
109      * This method returns a list of those events that are publication events and
110      * concerns this repository
111      */

112     public static List JavaDoc getPublicationEvents(Integer JavaDoc repositoryId) throws SystemException, Exception JavaDoc
113     {
114         return EventController.getPublicationEventVOListForRepository(repositoryId);
115     }
116
117     /**
118      * This method returns a list of earlier editions for this site.
119      */

120     public static List JavaDoc getAllEditions(Integer JavaDoc repositoryId) throws SystemException
121     {
122         Database db = CastorDatabaseService.getDatabase();
123         beginTransaction(db);
124         List JavaDoc res = new ArrayList JavaDoc();
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 JavaDoc 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     /**
154      * This method returns a list of earlier editions for this site.
155      */

156     public static EditionBrowser getEditionPage(Integer JavaDoc repositoryId, int startIndex) throws SystemException
157     {
158         int pageSize = new Integer JavaDoc(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 JavaDoc allEditions = Collections.list(results);
169             
170             List JavaDoc page = allEditions.subList(startIndex, Math.min(startIndex+pageSize, allEditions.size()));
171
172             EditionBrowser browser = new EditionBrowser(allEditions.size(), pageSize, startIndex);
173
174             List JavaDoc editionVOs = new ArrayList JavaDoc();
175             for (Iterator JavaDoc iter = page.iterator(); iter.hasNext();)
176             {
177                 Publication pub = (Publication) iter.next();
178                 PublicationVO pubVO = pub.getValueObject();
179                 //pubVO.setPublicationDetails(toVOList(pub.getPublicationDetails()));
180
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 JavaDoc 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     /**
202      * This method denies a requested publishing. What that means is that the entity specified in the
203      * event does not get published and that the request-event is deleted and a new one created to
204      * deliver the message back to the requester. If it is a deny of publishing we also deletes the
205      * publish-version as it no longer has any purpose.
206      */

207     public static void denyPublicationRequest(Integer JavaDoc eventId, String JavaDoc publisherUserName, String JavaDoc comment, String JavaDoc 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                         //If its a published version we just deletes the event - we don't want to delete the version.
224
EventController.delete(event, db);
225                     }
226                     else
227                     {
228                         Content content = contentVersion.getOwningContent();
229                         Language language = contentVersion.getLanguage();
230                         //event.setEntityId(ContentVersionController.getPreviousContentVersionVO(content.getId(), language.getId(), contentVersion.getId()).getId());
231
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                         //If its a published version we just deletes the event - we don't want to delete the version.
241
EventController.delete(event, db);
242                     }
243                     else
244                     {
245                         SiteNode siteNode = siteNodeVersion.getOwningSiteNode();
246                         //event.setEntityId(SiteNodeVersionController.getPreviousSiteNodeVersionVO(siteNode.getId(), siteNodeVersion.getId()).getId());
247
event.setEntityId(SiteNodeVersionController.getController().getPreviousActiveSiteNodeVersionVO(siteNode.getId(), siteNodeVersion.getId(), db).getId());
248                         SiteNodeVersionController.getController().delete(siteNodeVersion, db);
249                         //db.remove(siteNodeVersion);
250
}
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 JavaDoc 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     /**
283      * This method denies a list of requested publishing. What that means is that the entities specified in the
284      * event does not get published and that the request-event is deleted and a new one created to
285      * deliver the message back to the requester. If it is a deny of publishing we also deletes the
286      * publish-version as it no longer has any purpose.
287      */

288     public static void denyPublicationRequest(List JavaDoc eventVOList, String JavaDoc publisherUserName, String JavaDoc comment, String JavaDoc referenceUrl) throws SystemException
289     {
290         Database db = CastorDatabaseService.getDatabase();
291         beginTransaction(db);
292
293         try
294         {
295             Iterator JavaDoc 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                             //If its a published version we just deletes the event - we don't want to delete the version.
312
EventController.delete(event, db);
313                         }
314                         else
315                         {
316                             Content content = contentVersion.getOwningContent();
317                             Language language = contentVersion.getLanguage();
318                             //event.setEntityId(ContentVersionController.getPreviousContentVersionVO(content.getId(), language.getId(), contentVersion.getId()).getId());
319
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                             //If its a published version we just deletes the event - we don't want to delete the version.
329
EventController.delete(event, db);
330                         }
331                         else
332                         {
333                             SiteNode siteNode = siteNodeVersion.getOwningSiteNode();
334                             //event.setEntityId(SiteNodeVersionController.getPreviousSiteNodeVersionVO(siteNode.getId(), siteNodeVersion.getId()).getId());
335
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 JavaDoc());
339                             //db.remove(siteNodeVersion);
340
}
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 JavaDoc 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     /**
374      * This method mails the rejection to the recipient.
375      */

376     private static void mailNotification(Event event, String JavaDoc editorName, String JavaDoc recipient, String JavaDoc comment, String JavaDoc referenceUrl)
377     {
378         String JavaDoc email = "";
379         
380         try
381         {
382             String JavaDoc template;
383             
384             String JavaDoc 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 JavaDoc(CmsPropertyHandler.getContextRootPath() + "cms/publishingtool/deniedPublication_plain.vm"));
390             else
391                 template = FileHelper.getFileAsString(new File JavaDoc(CmsPropertyHandler.getContextRootPath() + "cms/publishingtool/deniedPublication_html.vm"));
392                 
393             Map JavaDoc parameters = new HashMap JavaDoc();
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 JavaDoc tempString = new StringWriter JavaDoc();
401             PrintWriter JavaDoc pw = new PrintWriter JavaDoc(tempString);
402             new VelocityTemplateProcessor().renderTemplate(parameters, pw, template);
403             email = tempString.toString();
404         
405             String JavaDoc 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 JavaDoc e)
413         {
414             logger.error("The notification was not sent. Reason:" + e.getMessage(), e);
415         }
416     }
417
418
419     /**
420      * This method creates a new publication with the concerned events carried out.
421      */

422     public PublicationVO createAndPublish(PublicationVO publicationVO, List JavaDoc 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 JavaDoc 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     /**
444      * This method creates a new publication with the concerned events carried out.
445      */

446     public PublicationVO createAndPublish(PublicationVO publicationVO, List JavaDoc events, boolean overrideVersionModifyer, InfoGluePrincipal infoGluePrincipal, Database db) throws SystemException, Exception JavaDoc
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 JavaDoc 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         // Replicate database!!!
467
try
468         {
469             logger.info("Starting replication...");
470             ReplicationMySqlController.updateSlaveServer();
471             logger.info("Finished replication...");
472         }
473         catch (Exception JavaDoc e)
474         {
475             logger.error("An error occurred when we tried to replicate the data:" + e.getMessage(), e);
476         }
477
478         // Notify the listeners!!!
479
try
480         {
481             Map JavaDoc hashMap = new HashMap JavaDoc();
482             hashMap.put("publicationId", publicationVO.getId());
483             
484             intercept(hashMap, "Publication.Write", infoGluePrincipal);
485         }
486         catch (Exception JavaDoc e)
487         {
488             logger.error("An error occurred when we tried to replicate the data:" + e.getMessage(), e);
489         }
490
491         // Update live site!!!
492
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             //NotificationMessage notificationMessage = new NotificationMessage("PublicationController.createAndPublish():", NotificationMessage.PUBLISHING_TEXT, infoGluePrincipal.getName(), NotificationMessage.PUBLISHING, publicationVO.getId(), "org.infoglue.cms.entities.publishing.impl.simple.PublicationImpl");
497
ChangeNotificationController.getInstance().addNotificationMessage(notificationMessage);
498             RemoteCacheUpdater.clearSystemNotificationMessages();
499             logger.info("Finished Notifying...");
500         }
501         catch (Exception JavaDoc 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     /**
511      * Creates a connection between contentversion or siteNodeVersion and publication, ie adds a contentversion
512      * to the publication.
513      */

514     private static void createPublicationInformation(Publication publication, Event event, boolean overrideVersionModifyer, InfoGluePrincipal infoGluePrincipal, Database db) throws Exception JavaDoc
515     {
516         String JavaDoc entityClass = event.getEntityClass();
517         Integer JavaDoc entityId = event.getEntityId();
518         Integer JavaDoc typeId = event.getTypeId();
519         logger.info("entityClass:" + entityClass);
520         logger.info("entityId:" + entityId);
521         logger.info("typeId:" + typeId);
522         
523         // Publish contentversions
524
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 JavaDoc(false));
531                 contentVersion = oldContentVersion;
532                 /*
533                 contentVersion = ContentVersionController.getContentVersionController().getLatestPublishedContentVersion(oldContentVersion.getOwningContent().getContentId(), oldContentVersion.getLanguage().getLanguageId(), db);
534                 if(contentVersion != null)
535                 {
536                     //We just set the published version to not active.
537                     contentVersion.setIsActive(new Boolean(false));
538                 }
539                 */

540             }
541             /*
542             else if(oldContentVersion != null && oldContentVersion.getOwningContent() != null && typeId.intValue() == EventVO.UNPUBLISH_ALL.intValue())
543             {
544                 //We just set the published version to not active.
545                 oldContentVersion.setIsActive(new Boolean(false));
546             }
547             */

548             else if(oldContentVersion != null && oldContentVersion.getOwningContent() != null)
549             {
550                 List JavaDoc events = new ArrayList JavaDoc();
551                 Integer JavaDoc 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                 //The contentVersion in here is the version we have done something with...
559
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 JavaDoc publicationDetails = publication.getPublicationDetails();
570                 if(publicationDetails == null)
571                     publication.setPublicationDetails(new ArrayList JavaDoc());
572
573                 publication.getPublicationDetails().add(publicationDetail);
574                 db.remove(event);
575             }
576         }
577
578         // Publish sitenodeversions
579
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 JavaDoc(false));
586                 siteNodeVersion = oldSiteNodeVersion;
587                 /*
588                 siteNodeVersion = SiteNodeVersionController.getLatestPublishedSiteNodeVersion(oldSiteNodeVersion.getOwningSiteNode().getSiteNodeId(), db);
589                 if(siteNodeVersion != null)
590                 {
591                     //We just set the published version to not active.
592                     siteNodeVersion.setIsActive(new Boolean(false));
593                 }
594                 */

595             }
596             /*
597             else if(oldSiteNodeVersion != null && oldSiteNodeVersion.getOwningSiteNode() != null && typeId.intValue() == EventVO.UNPUBLISH_ALL.intValue())
598             {
599                 //We just set the published version to not active.
600                 siteNodeVersion.setIsActive(new Boolean(false));
601             }
602             */

603             else if(oldSiteNodeVersion != null && oldSiteNodeVersion.getOwningSiteNode() != null)
604             {
605                 List JavaDoc events = new ArrayList JavaDoc();
606                 Integer JavaDoc 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                 //The siteNodeVersion in here is the version we have done something with...
614
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 JavaDoc publicationDetails = publication.getPublicationDetails();
625                 if(publicationDetails == null)
626                     publication.setPublicationDetails(new ArrayList JavaDoc());
627
628                 publication.getPublicationDetails().add(publicationDetail);
629                 db.remove(event);
630             }
631         }
632     }
633
634     /**
635      * This method (currently used for testing) will create a Publication with associated PublicationDetails children.
636      */

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 JavaDoc());
646             for (Iterator JavaDoc 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 JavaDoc e)
664         {
665             e.printStackTrace();
666             rollbackTransaction(db);
667             throw new SystemException(e.getMessage());
668         }
669     }
670
671     /**
672      * This method returns a list of all details a publication has.
673      */

674     public static List JavaDoc getPublicationDetailVOList(Integer JavaDoc publicationId) throws SystemException
675     {
676         List JavaDoc publicationDetails = new ArrayList JavaDoc();
677
678         Database db = CastorDatabaseService.getDatabase();
679         beginTransaction(db);
680
681         try
682         {
683             Publication publication = getPublicationWithId(publicationId, db);
684             Collection JavaDoc details = publication.getPublicationDetails();
685             publicationDetails = toVOList(details);
686
687             commitTransaction(db);
688         }
689         catch(Exception JavaDoc 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     /**
701      * This method unpublishes all entities in an edition if they are not unpublish-events.
702      */

703     public static PublicationVO unPublish(Integer JavaDoc 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 JavaDoc publicationDetails = publication.getPublicationDetails();
716
717             Iterator JavaDoc i = publicationDetails.iterator();
718             while (i.hasNext())
719             {
720                 PublicationDetail publicationDetail = (PublicationDetail)i.next();
721                 logger.info("publicationDetail:" + publicationDetail.getId() + ":" + publicationDetail.getTypeId());
722                 //We unpublish them as long as they are not unpublish-requests.
723
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 JavaDoc 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 JavaDoc e)
752         {
753             logger.error("An error occurred when we tried to replicate the data:" + e.getMessage(), e);
754         }
755
756         //Update live site!!!
757
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 JavaDoc 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     /**
775      * Unpublished a entity by just setting it to isActive = false.
776      */

777     private static void unpublishEntity(PublicationDetail publicationDetail, InfoGluePrincipal infoGluePrincipal, Database db) throws ConstraintException, SystemException
778     {
779         Integer JavaDoc 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 JavaDoc(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 JavaDoc(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 JavaDoc e)
805         {
806             logger.info("Could not unpublish entity:" + e.getMessage(), e);
807         }
808     }
809     
810     /**
811      * Republished an entity by just setting it to isActive = true.
812      */

813     private static void republishEntity(PublicationDetail publicationDetail, InfoGluePrincipal infoGluePrincipal, Database db) throws ConstraintException, SystemException
814     {
815         Integer JavaDoc 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 JavaDoc(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 JavaDoc(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 JavaDoc e)
864         {
865             logger.warn("Could not republish entity:" + e.getMessage(), e);
866         }
867     }
868
869
870     /**
871      * This method returns the owning content to a contentVersion.
872      */

873     public static ContentVO getOwningContentVO(Integer JavaDoc 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             //Content content = ContentController.getContentController().getContentWithId(contentVersion.getValueObject().getContentId(), db);
885
//contentVO = content.getValueObject();
886

887             commitTransaction(db);
888         }
889         catch(Exception JavaDoc 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     /**
900      * This method returns the owning siteNode to a siteNodeVersion.
901      */

902     public static SiteNodeVO getOwningSiteNodeVO(Integer JavaDoc 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 JavaDoc 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     /**
924      * This is a method that gives the user back an newly initialized ValueObject for this entity that the controller
925      * is handling.
926      */

927     public BaseEntityVO getNewVO()
928     {
929         return new PublicationVO();
930     }
931 }
932
Popular Tags