1 25 package org.infoglue.cms.controllers.kernel.impl.simple; 26 27 import java.util.ArrayList ; 28 import java.util.Collection ; 29 import java.util.Iterator ; 30 import java.util.List ; 31 32 import org.exolab.castor.jdo.Database; 33 import org.exolab.castor.jdo.PersistenceException; 34 import org.infoglue.cms.entities.content.ContentCategory; 35 import org.infoglue.cms.entities.content.ContentCategoryVO; 36 import org.infoglue.cms.entities.content.ContentVersion; 37 import org.infoglue.cms.entities.content.ContentVersionVO; 38 import org.infoglue.cms.entities.content.impl.simple.ContentCategoryImpl; 39 import org.infoglue.cms.entities.content.impl.simple.ContentVersionImpl; 40 import org.infoglue.cms.entities.kernel.BaseEntityVO; 41 import org.infoglue.cms.entities.management.Category; 42 import org.infoglue.cms.entities.management.CategoryVO; 43 import org.infoglue.cms.entities.management.impl.simple.CategoryImpl; 44 import org.infoglue.cms.exception.SystemException; 45 import org.infoglue.cms.util.ConstraintExceptionBuffer; 46 47 56 public class ContentCategoryController extends BaseController 57 { 58 private static final ContentCategoryController instance = new ContentCategoryController(); 59 60 private static final String findByContentVersion = new StringBuffer ("SELECT c ") 61 .append("FROM org.infoglue.cms.entities.content.impl.simple.ContentCategoryImpl c ") 62 .append("WHERE c.contentVersion.contentVersionId = $1").toString(); 63 64 private static final String findByContentVersionAttribute = new StringBuffer ("SELECT c ") 65 .append("FROM org.infoglue.cms.entities.content.impl.simple.ContentCategoryImpl c ") 66 .append("WHERE c.attributeName = $1 ") 67 .append("AND c.contentVersion.contentVersionId = $2") 68 .append("ORDER BY c.category.name").toString(); 69 70 private static final String findByCategory = new StringBuffer ("SELECT c ") 71 .append("FROM org.infoglue.cms.entities.content.impl.simple.ContentCategoryImpl c ") 72 .append("WHERE c.category.categoryId = $1 ").toString(); 73 74 public static ContentCategoryController getController() 75 { 76 return instance; 77 } 78 79 private ContentCategoryController() {} 80 81 87 public ContentCategoryVO findById(Integer id) throws SystemException 88 { 89 return (ContentCategoryVO)getVOWithId(ContentCategoryImpl.class, id); 90 } 91 92 99 public List findByContentVersionAttribute(String attribute, Integer versionId) throws SystemException 100 { 101 103 109 110 List contentCategoryVOList = new ArrayList (); 111 112 Database db = CastorDatabaseService.getDatabase(); 113 ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer(); 114 115 beginTransaction(db); 116 117 try 118 { 119 List contentCategories = findByContentVersionAttribute(attribute, versionId, db, true); 120 if(contentCategories != null) 121 contentCategoryVOList = toVOList(contentCategories); 122 123 commitTransaction(db); 124 } 125 catch(Exception e) 126 { 127 rollbackTransaction(db); 129 throw new SystemException(e.getMessage()); 130 } 131 132 133 return contentCategoryVOList; 135 } 136 137 144 public List findByContentVersionAttribute(String attribute, Integer versionId, Database db, boolean readOnly) throws SystemException 145 { 146 153 154 List contentCategoryList = new ArrayList (); 155 156 ContentVersion contentVersion = null; 157 if(readOnly) 158 contentVersion = ContentVersionController.getContentVersionController().getReadOnlyContentVersionWithId(versionId, db); 159 else 160 contentVersion = ContentVersionController.getContentVersionController().getContentVersionWithId(versionId, db); 161 162 Collection contentCategories = contentVersion.getContentCategories(); 163 if(contentCategories != null) 164 { 165 Iterator contentCategoriesIterator = contentCategories.iterator(); 166 while(contentCategoriesIterator.hasNext()) 167 { 168 ContentCategory contentCategory = (ContentCategory)contentCategoriesIterator.next(); 169 if(contentCategory.getAttributeName().equals(attribute)) 170 { 171 contentCategoryList.add(contentCategory); 172 } 173 } 174 } 175 176 return contentCategoryList; 177 } 178 179 185 public List findByContentVersion(Integer versionId) throws SystemException 186 { 187 List params = new ArrayList (); 188 params.add(versionId); 189 return executeQuery(findByContentVersion, params); 190 } 191 192 198 public List findByContentVersion(Integer versionId, Database db) throws SystemException 199 { 200 List params = new ArrayList (); 201 params.add(versionId); 202 return executeQuery(findByContentVersion, params, db); 203 } 204 205 206 212 public List findByCategory(Integer categoryId) throws SystemException 213 { 214 List params = new ArrayList (); 215 params.add(categoryId); 216 return executeQuery(findByCategory, params); 217 } 218 219 225 public ContentCategoryVO save(ContentCategoryVO c) throws SystemException 226 { 227 return c.isUnsaved() ? create(c) : (ContentCategoryVO)updateEntity(ContentCategoryImpl.class, c); 228 } 229 230 233 private ContentCategoryVO create(ContentCategoryVO c) throws SystemException 234 { 235 Database db = beginTransaction(); 236 237 try 238 { 239 ContentCategory contentCategory = createWithDatabase(c, db); 240 commitTransaction(db); 241 return contentCategory.getValueObject(); 242 } 243 catch (Exception e) 244 { 245 rollbackTransaction(db); 246 throw new SystemException(e.getMessage()); 247 } 248 } 249 250 public ContentCategory createWithDatabase(ContentCategoryVO c, Database db) throws SystemException, PersistenceException 251 { 252 Category category = (Category)getObjectWithId(CategoryImpl.class, c.getCategory().getId(), db); 256 ContentVersion contentVersion = (ContentVersion)getObjectWithId(ContentVersionImpl.class, c.getContentVersionId(), db); 257 258 ContentCategory contentCategory = new ContentCategoryImpl(); 259 contentCategory.setValueObject(c); 260 contentCategory.setCategory((CategoryImpl)category); 261 contentCategory.setContentVersion((ContentVersionImpl)contentVersion); 262 db.create(contentCategory); 263 contentVersion.getContentCategories().add(contentCategory); 264 265 return contentCategory; 266 } 267 268 271 public List create(List categoryVOList, ContentVersionVO contentVersionVO, String attributeName) throws SystemException 272 { 273 List contentCategoryVOList = new ArrayList (); 274 275 Database db = beginTransaction(); 276 277 try 278 { 279 Iterator categoryVOListIterator = categoryVOList.iterator(); 280 while(categoryVOListIterator.hasNext()) 281 { 282 CategoryVO categoryVO = (CategoryVO)categoryVOListIterator.next(); 283 Category category = (Category)getObjectWithId(CategoryImpl.class, categoryVO.getId(), db); 284 ContentVersion contentVersion = (ContentVersion)getObjectWithId(ContentVersionImpl.class, contentVersionVO.getId(), db); 285 286 ContentCategoryVO contentCategoryVO = new ContentCategoryVO(); 287 contentCategoryVO.setAttributeName(attributeName); 288 contentCategoryVO.setContentVersionId(contentVersionVO.getId()); 289 ContentCategory contentCategory = createWithDatabase(contentCategoryVO, category, contentVersion, db); 290 291 contentCategoryVOList.add(contentCategory.getValueObject()); 292 } 293 294 commitTransaction(db); 295 } 296 catch (Exception e) 297 { 298 rollbackTransaction(db); 299 throw new SystemException(e.getMessage()); 300 } 301 302 return contentCategoryVOList; 303 } 304 305 308 309 public List create(List categoryList, ContentVersion contentVersion, String attributeName, Database db) throws SystemException, Exception 310 { 311 List contentCategoryList = new ArrayList (); 312 313 Iterator categoryListIterator = categoryList.iterator(); 314 while(categoryListIterator.hasNext()) 315 { 316 Category category = (Category)categoryListIterator.next(); 317 318 ContentCategoryVO contentCategoryVO = new ContentCategoryVO(); 319 contentCategoryVO.setAttributeName(attributeName); 320 contentCategoryVO.setContentVersionId(contentVersion.getId()); 321 ContentCategory contentCategory = createWithDatabase(contentCategoryVO, category, contentVersion, db); 322 contentVersion.getContentCategories().add(contentCategory); 323 324 contentCategoryList.add(contentCategory); 325 } 326 327 return contentCategoryList; 328 } 329 330 public ContentCategory createWithDatabase(ContentCategoryVO c, Category category, ContentVersion contentVersion, Database db) throws SystemException, PersistenceException 331 { 332 ContentCategory contentCategory = new ContentCategoryImpl(); 333 contentCategory.setValueObject(c); 334 contentCategory.setCategory((CategoryImpl)category); 335 contentCategory.setContentVersion((ContentVersionImpl)contentVersion); 336 db.create(contentCategory); 337 return contentCategory; 338 } 339 340 345 public void delete(Integer id) throws SystemException 346 { 347 Database db = beginTransaction(); 348 349 try 350 { 351 ContentCategory contentCategory = (ContentCategory)getObjectWithId(ContentCategoryImpl.class, id, db); 352 ContentVersion contentVersion = (ContentVersion)getObjectWithId(ContentVersionImpl.class, contentCategory.getContentVersionId(), db); 353 contentVersion.getContentCategories().remove(contentCategory); 355 db.remove(contentCategory); 357 358 commitTransaction(db); 359 } 360 catch (Exception e) 361 { 362 rollbackTransaction(db); 363 throw new SystemException(e.getMessage()); 364 } 365 366 } 368 369 374 public void deleteByContentVersion(Integer versionId) throws SystemException 375 { 376 delete(findByContentVersion(versionId)); 377 } 378 379 384 public void deleteByContentVersion(ContentVersion contentVersion, Database db) throws SystemException, Exception 385 { 386 Iterator contentVersionIterator = contentVersion.getContentCategories().iterator(); 387 while(contentVersionIterator.hasNext()) 388 { 389 ContentCategory contentCategory = (ContentCategory)contentVersionIterator.next(); 390 contentVersionIterator.remove(); 391 db.remove(contentCategory); 392 } 393 } 394 395 401 public void deleteByContentVersion(Integer versionId, Database db) throws SystemException 402 { 403 delete(findByContentVersion(versionId), db); 404 } 405 406 411 public void deleteByCategory(Integer categoryId) throws SystemException 412 { 413 delete(findByCategory(categoryId)); 414 } 415 416 422 public void deleteByCategory(Integer categoryId, Database db) throws SystemException 423 { 424 delete(findByCategory(categoryId), db); 425 } 426 427 433 public void deleteByContentVersionAttribute(String attribute, Integer versionId) throws SystemException 434 { 435 delete(findByContentVersionAttribute(attribute, versionId)); 436 } 437 438 445 public void deleteByContentVersionAttribute(String attribute, Integer versionId, Database db) throws SystemException 446 { 447 delete(findByContentVersionAttribute(attribute, versionId), db); 448 } 449 450 455 private static void delete(Collection contentCategories) throws SystemException 456 { 457 Database db = beginTransaction(); 458 459 try 460 { 461 delete(contentCategories, db); 462 commitTransaction(db); 463 } 464 catch (Exception e) 465 { 466 rollbackTransaction(db); 467 throw new SystemException(e); 468 } 469 } 470 471 477 private static void delete(Collection contentCategories, Database db) throws SystemException 478 { 479 for (Iterator i = contentCategories.iterator(); i.hasNext();) 480 deleteEntity(ContentCategoryImpl.class, ((ContentCategoryVO)i.next()).getId(), db); 481 } 482 483 486 public BaseEntityVO getNewVO() 487 { 488 return new ContentCategoryVO(); 489 } 490 } 491 | Popular Tags |