KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.ArrayList JavaDoc;
27 import java.util.List JavaDoc;
28
29 import org.apache.log4j.Logger;
30 import org.exolab.castor.jdo.Database;
31 import org.exolab.castor.jdo.OQLQuery;
32 import org.exolab.castor.jdo.QueryResults;
33 import org.infoglue.cms.entities.content.ContentVersion;
34 import org.infoglue.cms.entities.kernel.BaseEntityVO;
35 import org.infoglue.cms.entities.management.Repository;
36 import org.infoglue.cms.entities.management.impl.simple.RepositoryImpl;
37 import org.infoglue.cms.entities.structure.SiteNodeVersion;
38 import org.infoglue.cms.entities.workflow.Event;
39 import org.infoglue.cms.entities.workflow.EventVO;
40 import org.infoglue.cms.entities.workflow.impl.simple.EventImpl;
41 import org.infoglue.cms.exception.Bug;
42 import org.infoglue.cms.exception.SystemException;
43 import org.infoglue.cms.security.InfoGluePrincipal;
44
45 /**
46  * @author Mattias Bogeblad
47  *
48  * This class implements all operations we can do on the cmEvent-entity.
49  */

50
51 public class EventController extends BaseController
52 {
53     private final static Logger logger = Logger.getLogger(EventController.class.getName());
54
55     /**
56      * Gets the eventVO in a readonly transaction.
57      */

58     
59     public static EventVO getEventVOWithId(Integer JavaDoc eventId) throws SystemException, Bug
60     {
61         return (EventVO) getVOWithId(EventImpl.class, eventId);
62     }
63     
64     /**
65      * Gets the event in the given transaction.
66      */

67     
68     public static Event getEventWithId(Integer JavaDoc eventId, Database db) throws SystemException, Bug
69     {
70         return (Event) getObjectWithId(EventImpl.class, eventId, db);
71     }
72
73     /**
74      * Gets all events in a read only transaction.
75      */

76
77     public List JavaDoc getEventVOList() throws SystemException, Bug
78     {
79         return getAllVOObjects(EventImpl.class, "eventId");
80     }
81
82     /**
83      * Creates a new Event with the values in the eventVO sent in.
84      */

85     
86     public static EventVO create(EventVO eventVO, Integer JavaDoc repositoryId, InfoGluePrincipal infoGluePrincipal, Database db) throws SystemException
87     {
88         //Fetch related entities here if they should be referenced
89
Repository repository = RepositoryController.getController().getRepositoryWithId(repositoryId, db);
90         
91         Event event = new EventImpl();
92         event.setValueObject(eventVO);
93         event.setRepository((RepositoryImpl)repository);
94         event.setCreator(infoGluePrincipal.getName());
95         
96         try
97         {
98             db.create(event);
99         }
100         catch(Exception JavaDoc e)
101         {
102             logger.error("An error occurred so we should not complete the transaction:" + e, e);
103             throw new SystemException(e.getMessage());
104         }
105         
106         return event.getValueObject();
107     }
108
109
110     /**
111      * Creates a new Event with the values in the eventVO sent in in a new transaction.
112      */

113     
114     public static EventVO create(EventVO eventVO, Integer JavaDoc repositoryId, InfoGluePrincipal infoGluePrincipal) throws SystemException
115     {
116         Event event = null;
117         
118         Database db = CastorDatabaseService.getDatabase();
119         beginTransaction(db);
120         try
121         {
122             //Fetch related entities here if they should be referenced
123
Repository repository = RepositoryController.getController().getRepositoryWithId(repositoryId, db);
124             
125             event = new EventImpl();
126             event.setValueObject(eventVO);
127             event.setRepository((RepositoryImpl)repository);
128             event.setCreator(infoGluePrincipal.getName());
129             db.create(event);
130     
131             commitTransaction(db);
132         }
133         catch(Exception JavaDoc e)
134         {
135             logger.error("An error occurred so we should not completes the transaction:" + e, e);
136             rollbackTransaction(db);
137             throw new SystemException(e.getMessage());
138         }
139         
140         return event.getValueObject();
141     }
142     
143
144     
145     /**
146      * This method removes an event from the database.
147      */

148                        
149     public static void delete(EventVO eventVO) throws SystemException
150     {
151         deleteEntity(EventImpl.class, eventVO.getEventId());
152     }
153
154
155     /**
156      * This method removes an event from the database.
157      */

158                        
159     public static void delete(Event event, Database db) throws SystemException
160     {
161         try
162         {
163             db.remove(event);
164         }
165         catch (Exception JavaDoc e)
166         {
167             throw new SystemException(e);
168         }
169     }
170
171     /**
172      * This method updates an event.
173      */

174     
175     public static EventVO update(EventVO eventVO) throws SystemException
176     {
177         return (EventVO) updateEntity(EventImpl.class, eventVO);
178     }
179
180
181
182
183
184     /**
185      * Returns a list of events currently available for the certain entity.
186      */

187     
188     public static List JavaDoc getEventVOListForEntity(String JavaDoc entityClass, Integer JavaDoc entityId) throws SystemException, Bug
189     {
190         List JavaDoc events = new ArrayList JavaDoc();
191         Database db = CastorDatabaseService.getDatabase();
192         beginTransaction(db);
193
194         try
195         {
196             OQLQuery oql = db.getOQLQuery("SELECT e FROM org.infoglue.cms.entities.workflow.impl.simple.EventImpl e WHERE e.entityClass = $1 AND e.entityId = $2");
197             oql.bind(entityClass);
198             oql.bind(entityId);
199
200             QueryResults results = oql.execute(Database.ReadOnly);
201
202             while (results.hasMore())
203             {
204                 Event event = (Event)results.next();
205                 events.add(event.getValueObject());
206             }
207
208             results.close();
209             oql.close();
210
211             commitTransaction(db);
212         }
213         catch (Exception JavaDoc e)
214         {
215             logger.error("An error occurred so we should not completes the transaction:" + e, e);
216             rollbackTransaction(db);
217             throw new SystemException(e.getMessage());
218         }
219
220         return events;
221     }
222     
223     /**
224      * Returns a list of events with either publish or unpublish-state currently available for the repository stated.
225      */

226     
227     public static List JavaDoc getPublicationEventVOListForRepository(Integer JavaDoc repositoryId) throws SystemException, Bug
228     {
229         List JavaDoc events = new ArrayList JavaDoc();
230         
231         Database db = CastorDatabaseService.getDatabase();
232         beginTransaction(db);
233         try
234         {
235             OQLQuery oql = db.getOQLQuery( "SELECT e FROM org.infoglue.cms.entities.workflow.impl.simple.EventImpl e WHERE (e.typeId = $1 OR e.typeId = $2) AND e.repository.repositoryId = $3 ORDER BY e.eventId desc");
236             oql.bind(EventVO.PUBLISH);
237             oql.bind(EventVO.UNPUBLISH_LATEST);
238             oql.bind(repositoryId);
239             
240             //logger.info("Fetching entity in read/write mode" + repositoryId);
241
QueryResults results = oql.execute();
242             
243             while (results.hasMore())
244             {
245                 Event event = (Event)results.next();
246                 //logger.warn("event:" + event.getId());
247
//logger.warn("entityClass:" + event.getEntityClass());
248
//logger.warn("entityId:" + event.getEntityId());
249

250                 boolean isBroken = false;
251                 boolean isValid = true;
252                 try
253                 {
254                     if(event.getEntityClass().equalsIgnoreCase(ContentVersion.class.getName()))
255                     {
256                         ContentVersion contentVersion = ContentVersionController.getContentVersionController().getContentVersionWithId(event.getEntityId(), db);
257                         //logger.warn("contentVersion:" + contentVersion.getId() + ":" + contentVersion.getOwningContent());
258
if(contentVersion == null || contentVersion.getOwningContent() == null)
259                         {
260                             isBroken = true;
261                             isValid = false;
262                             ContentVersionController.getContentVersionController().delete(contentVersion, db);
263                         }
264                     }
265                     else if(event.getEntityClass().equalsIgnoreCase(SiteNodeVersion.class.getName()))
266                     {
267                         SiteNodeVersion siteNodeVersion = SiteNodeVersionController.getController().getSiteNodeVersionWithId(event.getEntityId(), db);
268                         //logger.warn("siteNodeVersion:" + siteNodeVersion.getId() + ":" + siteNodeVersion.getOwningSiteNode());
269
if(siteNodeVersion == null || siteNodeVersion.getOwningSiteNode() == null)
270                         {
271                             isBroken = true;
272                             isValid = false;
273                             SiteNodeVersionController.getController().delete(siteNodeVersion, db);
274                         }
275                     }
276                 }
277                 catch(Exception JavaDoc e)
278                 {
279                     isValid = false;
280                     //delete(event, db);
281
}
282                     
283                 if(isValid && !isBroken)
284                     events.add(event.getValueObject());
285             
286                 if(isBroken)
287                     delete(event, db);
288             }
289             
290             results.close();
291             oql.close();
292
293             commitTransaction(db);
294         }
295         catch(Exception JavaDoc e)
296         {
297             logger.error("An error occurred so we should not completes the transaction:" + e, e);
298             rollbackTransaction(db);
299             throw new SystemException(e.getMessage());
300         }
301         
302         return events;
303     }
304
305     /**
306      * This is a method that gives the user back an newly initialized ValueObject for this entity that the controller
307      * is handling.
308      */

309
310     public BaseEntityVO getNewVO()
311     {
312         return new EventVO();
313     }
314
315 }
316
Popular Tags