1 23 24 package org.infoglue.cms.controllers.kernel.impl.simple; 25 26 import java.util.ArrayList ; 27 import java.util.List ; 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 50 51 public class EventController extends BaseController 52 { 53 private final static Logger logger = Logger.getLogger(EventController.class.getName()); 54 55 58 59 public static EventVO getEventVOWithId(Integer eventId) throws SystemException, Bug 60 { 61 return (EventVO) getVOWithId(EventImpl.class, eventId); 62 } 63 64 67 68 public static Event getEventWithId(Integer eventId, Database db) throws SystemException, Bug 69 { 70 return (Event) getObjectWithId(EventImpl.class, eventId, db); 71 } 72 73 76 77 public List getEventVOList() throws SystemException, Bug 78 { 79 return getAllVOObjects(EventImpl.class, "eventId"); 80 } 81 82 85 86 public static EventVO create(EventVO eventVO, Integer repositoryId, InfoGluePrincipal infoGluePrincipal, Database db) throws SystemException 87 { 88 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 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 113 114 public static EventVO create(EventVO eventVO, Integer repositoryId, InfoGluePrincipal infoGluePrincipal) throws SystemException 115 { 116 Event event = null; 117 118 Database db = CastorDatabaseService.getDatabase(); 119 beginTransaction(db); 120 try 121 { 122 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 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 148 149 public static void delete(EventVO eventVO) throws SystemException 150 { 151 deleteEntity(EventImpl.class, eventVO.getEventId()); 152 } 153 154 155 158 159 public static void delete(Event event, Database db) throws SystemException 160 { 161 try 162 { 163 db.remove(event); 164 } 165 catch (Exception e) 166 { 167 throw new SystemException(e); 168 } 169 } 170 171 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 187 188 public static List getEventVOListForEntity(String entityClass, Integer entityId) throws SystemException, Bug 189 { 190 List events = new ArrayList (); 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 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 226 227 public static List getPublicationEventVOListForRepository(Integer repositoryId) throws SystemException, Bug 228 { 229 List events = new ArrayList (); 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 QueryResults results = oql.execute(); 242 243 while (results.hasMore()) 244 { 245 Event event = (Event)results.next(); 246 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 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 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 e) 278 { 279 isValid = false; 280 } 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 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 309 310 public BaseEntityVO getNewVO() 311 { 312 return new EventVO(); 313 } 314 315 } 316 | Popular Tags |