KickJava   Java API By Example, From Geeks To Geeks.

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


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  * $Id: ContentCategoryController.java,v 1.16 2006/03/06 18:09:40 mattias Exp $
24  */

25 package org.infoglue.cms.controllers.kernel.impl.simple;
26
27 import java.util.ArrayList JavaDoc;
28 import java.util.Collection JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.List JavaDoc;
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 /**
48  * The ContentCategoryController manages all actions related to persistence
49  * and querying for ContentCategory relationships.
50  *
51  * TODO: When we convert have Hibernate manage all of these relationships, it will pull it
52  * TODO: all back with one query and be a helluva lot faster than this basic implementation
53  *
54  * @author Frank Febbraro (frank@phase2technology.com)
55  */

56 public class ContentCategoryController extends BaseController
57 {
58     private static final ContentCategoryController instance = new ContentCategoryController();
59
60     private static final String JavaDoc findByContentVersion = new StringBuffer JavaDoc("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 JavaDoc findByContentVersionAttribute = new StringBuffer JavaDoc("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 JavaDoc findByCategory = new StringBuffer JavaDoc("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     /**
82      * Find a ContentCategory by it's identifier.
83      * @param id The id of the Category to find
84      * @return The CategoryVO identified by the provided id
85      * @throws SystemException If an error happens
86      */

87     public ContentCategoryVO findById(Integer JavaDoc id) throws SystemException
88     {
89         return (ContentCategoryVO)getVOWithId(ContentCategoryImpl.class, id);
90     }
91
92     /**
93      * Find a List of ContentCategories for the specific attribute and Content Version.
94      * @param attribute The attribute name of the ContentCategory to find
95      * @param versionId The Content Version id of the ContentCategory to find
96      * @return A list of ContentCategoryVO that have the provided content version and attribute
97      * @throws SystemException If an error happens
98      */

99     public List JavaDoc findByContentVersionAttribute(String JavaDoc attribute, Integer JavaDoc versionId) throws SystemException
100     {
101         //System.out.println("findByContentVersionAttribute with " + attribute + " and " + versionId + " no db " + System.currentTimeMillis());
102

103         /*
104         List params = new ArrayList();
105         params.add(attribute);
106         params.add(versionId);
107         return executeQuery(findByContentVersionAttribute, params);
108         */

109         
110         List JavaDoc contentCategoryVOList = new ArrayList JavaDoc();
111         
112         Database db = CastorDatabaseService.getDatabase();
113         ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer();
114
115         beginTransaction(db);
116
117         try
118         {
119             List JavaDoc contentCategories = findByContentVersionAttribute(attribute, versionId, db, true);
120             if(contentCategories != null)
121                 contentCategoryVOList = toVOList(contentCategories);
122             
123             commitTransaction(db);
124         }
125         catch(Exception JavaDoc e)
126         {
127             //logger.error("An error occurred so we should not complete the transaction:" + e, e);
128
rollbackTransaction(db);
129             throw new SystemException(e.getMessage());
130         }
131
132         
133         //System.out.println("findByContentVersionAttribute with " + attribute + " and " + versionId + " no db " + System.currentTimeMillis());
134
return contentCategoryVOList;
135     }
136
137     /**
138      * Find a List of ContentCategories for the specific attribute and Content Version.
139      * @param attribute The attribute name of the ContentCategory to find
140      * @param versionId The Content Version id of the ContentCategory to find
141      * @return A list of ContentCategoryVO that have the provided content version and attribute
142      * @throws SystemException If an error happens
143      */

144     public List JavaDoc findByContentVersionAttribute(String JavaDoc attribute, Integer JavaDoc versionId, Database db, boolean readOnly) throws SystemException
145     {
146         /*
147         System.out.println("findByContentVersionAttribute with " + attribute + " and " + versionId);
148         List params = new ArrayList();
149         params.add(attribute);
150         params.add(versionId);
151         return executeQuery(findByContentVersionAttribute, params, db);
152         */

153
154         List JavaDoc contentCategoryList = new ArrayList JavaDoc();
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 JavaDoc contentCategories = contentVersion.getContentCategories();
163         if(contentCategories != null)
164         {
165             Iterator JavaDoc 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     /**
180      * Find a List of ContentCategories for a Content Version.
181      * @param versionId The Content Version id of the ContentCategory to find
182      * @return A list of ContentCategoryVO that have the provided content version and attribute
183      * @throws SystemException If an error happens
184      */

185     public List JavaDoc findByContentVersion(Integer JavaDoc versionId) throws SystemException
186     {
187         List JavaDoc params = new ArrayList JavaDoc();
188         params.add(versionId);
189         return executeQuery(findByContentVersion, params);
190     }
191
192     /**
193      * Find a List of ContentCategories for a Content Version.
194      * @param versionId The Content Version id of the ContentCategory to find
195      * @return A list of ContentCategoryVO that have the provided content version and attribute
196      * @throws SystemException If an error happens
197      */

198     public List JavaDoc findByContentVersion(Integer JavaDoc versionId, Database db) throws SystemException
199     {
200         List JavaDoc params = new ArrayList JavaDoc();
201         params.add(versionId);
202         return executeQuery(findByContentVersion, params, db);
203     }
204
205     
206     /**
207      * Find a List of ContentCategories for the specific attribute and Content Version.
208      * @param categoryId The Category id of the ContentCategory to find
209      * @return A list of ContentCategoryVO that have the provided category id
210      * @throws SystemException If an error happens
211      */

212     public List JavaDoc findByCategory(Integer JavaDoc categoryId) throws SystemException
213     {
214         List JavaDoc params = new ArrayList JavaDoc();
215         params.add(categoryId);
216         return executeQuery(findByCategory, params);
217     }
218
219     /**
220      * Saves a ContentCategoryVO whether it is new or not.
221      * @param c The ContentCategoryVO to save
222      * @return The saved ContentCategoryVO
223      * @throws SystemException If an error happens
224      */

225     public ContentCategoryVO save(ContentCategoryVO c) throws SystemException
226     {
227         return c.isUnsaved() ? create(c) : (ContentCategoryVO)updateEntity(ContentCategoryImpl.class, c);
228     }
229
230     /**
231      * Creates a ContentCategory from a ContentCategoryVO
232      */

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 JavaDoc 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         // Need this crappy hack to forge the relationship (castor completely sucks like this)
253
// TODO: When hibernate comes, just save the VOs and if it has a child VO with an id set
254
// TODO: it is used to make the relationship...ask me for clarification -frank
255
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     /**
269      * Creates a number of ContentCategories from a list of categories and a contentVersion.
270      */

271     public List JavaDoc create(List JavaDoc categoryVOList, ContentVersionVO contentVersionVO, String JavaDoc attributeName) throws SystemException
272     {
273         List JavaDoc contentCategoryVOList = new ArrayList JavaDoc();
274         
275         Database db = beginTransaction();
276
277         try
278         {
279             Iterator JavaDoc 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 JavaDoc e)
297         {
298             rollbackTransaction(db);
299             throw new SystemException(e.getMessage());
300         }
301         
302         return contentCategoryVOList;
303     }
304
305     /**
306      * Creates a number of ContentCategories from a list of categories and a contentVersion.
307      */

308     
309     public List JavaDoc create(List JavaDoc categoryList, ContentVersion contentVersion, String JavaDoc attributeName, Database db) throws SystemException, Exception JavaDoc
310     {
311         List JavaDoc contentCategoryList = new ArrayList JavaDoc();
312         
313         Iterator JavaDoc 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     /**
341      * Deletes a ContentCategory
342      * @param id The id of the ContentCategory to delete
343      * @throws SystemException If an error happens
344      */

345     public void delete(Integer JavaDoc 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             //System.out.println("contentVersion:" + contentVersion.getContentCategories().size());
354
contentVersion.getContentCategories().remove(contentCategory);
355             //System.out.println("contentVersion:" + contentVersion.getContentCategories().size());
356
db.remove(contentCategory);
357             
358             commitTransaction(db);
359         }
360         catch (Exception JavaDoc e)
361         {
362             rollbackTransaction(db);
363             throw new SystemException(e.getMessage());
364         }
365         
366         //deleteEntity(ContentCategoryImpl.class, id);
367
}
368
369     /**
370      * Deletes all ContentCategories for a particular ContentVersion
371      * @param versionId The id of the ContentCategory to delete
372      * @throws SystemException If an error happens
373      */

374     public void deleteByContentVersion(Integer JavaDoc versionId) throws SystemException
375     {
376         delete(findByContentVersion(versionId));
377     }
378
379     /**
380      * Deletes all ContentCategories for a particular ContentVersion
381      * @param versionId The id of the ContentCategory to delete
382      * @throws SystemException If an error happens
383      */

384     public void deleteByContentVersion(ContentVersion contentVersion, Database db) throws SystemException, Exception JavaDoc
385     {
386         Iterator JavaDoc 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     /**
396      * Deletes all ContentCategories for a particular ContentVersion using the provided Database
397      * @param versionId The id of the ContentCategory to delete
398      * @param db The Database instance to use
399      * @throws SystemException If an error happens
400      */

401     public void deleteByContentVersion(Integer JavaDoc versionId, Database db) throws SystemException
402     {
403         delete(findByContentVersion(versionId), db);
404     }
405
406     /**
407      * Deletes all ContentCategories for a particular Category
408      * @param categoryId The id of the ContentCategory to delete
409      * @throws SystemException If an error happens
410      */

411     public void deleteByCategory(Integer JavaDoc categoryId) throws SystemException
412     {
413         delete(findByCategory(categoryId));
414     }
415
416     /**
417      * Deletes all ContentCategories for a particular Category using the provided Database
418      * @param categoryId The id of the Category to delete
419      * @param db The Database instance to use
420      * @throws SystemException If an error happens
421      */

422     public void deleteByCategory(Integer JavaDoc categoryId, Database db) throws SystemException
423     {
424         delete(findByCategory(categoryId), db);
425     }
426
427     /**
428      * Deletes all content categories with a specific attribute for a specific content version within a single transaction
429      * @param attribute the desired attribute
430      * @param versionId the ID of the desired content version
431      * @throws SystemException if a database error occurs
432      */

433     public void deleteByContentVersionAttribute(String JavaDoc attribute, Integer JavaDoc versionId) throws SystemException
434     {
435         delete(findByContentVersionAttribute(attribute, versionId));
436     }
437
438     /**
439      * Deletes all content categories with a specific attribute for a specific content version using the given database
440      * @param attribute the desired attribute
441      * @param versionId the ID of the desired content version
442      * @param db the database defining the transaction context for this delete
443      * @throws SystemException if a database error occurs
444      */

445     public void deleteByContentVersionAttribute(String JavaDoc attribute, Integer JavaDoc versionId, Database db) throws SystemException
446     {
447         delete(findByContentVersionAttribute(attribute, versionId), db);
448     }
449
450     /**
451      * Deletes a collection of content categories within a single transaction
452      * @param contentCategories a collection of ContentCategoryVOs to delete
453      * @throws SystemException if a database error occurs
454      */

455     private static void delete(Collection JavaDoc contentCategories) throws SystemException
456     {
457         Database db = beginTransaction();
458
459         try
460         {
461             delete(contentCategories, db);
462             commitTransaction(db);
463         }
464         catch (Exception JavaDoc e)
465         {
466             rollbackTransaction(db);
467             throw new SystemException(e);
468         }
469     }
470
471     /**
472      * Deletes a collection of content categories using the given database
473      * @param contentCategories a collection of ContentCategoryVOs to delete
474      * @param db the database to be used for the delete
475      * @throws SystemException if a database error occurs
476      */

477     private static void delete(Collection JavaDoc contentCategories, Database db) throws SystemException
478     {
479         for (Iterator JavaDoc i = contentCategories.iterator(); i.hasNext();)
480             deleteEntity(ContentCategoryImpl.class, ((ContentCategoryVO)i.next()).getId(), db);
481     }
482
483     /**
484      * Implemented for BaseController
485      */

486     public BaseEntityVO getNewVO()
487     {
488         return new ContentCategoryVO();
489     }
490 }
491
Popular Tags