KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* ===============================================================================
2  *
3  * Part of the InfoGlue Properties 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: PropertiesCategoryController.java,v 1.3 2006/03/06 18:11:08 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.kernel.BaseEntityVO;
35 import org.infoglue.cms.entities.management.Category;
36 import org.infoglue.cms.entities.management.PropertiesCategory;
37 import org.infoglue.cms.entities.management.PropertiesCategoryVO;
38 import org.infoglue.cms.entities.management.impl.simple.CategoryImpl;
39 import org.infoglue.cms.entities.management.impl.simple.PropertiesCategoryImpl;
40 import org.infoglue.cms.exception.SystemException;
41
42 /**
43  * The PropertiesCategoryController manages all actions related to persistence
44  * and querying for PropertiesCategory relationships.
45  *
46  * TODO: When we convert have Hibernate manage all of these relationships, it will pull it
47  * TODO: all back with one query and be a helluva lot faster than this basic implementation
48  *
49  * @author Frank Febbraro (frank@phase2technology.com)
50  */

51 public class PropertiesCategoryController extends BaseController
52 {
53     private static final PropertiesCategoryController instance = new PropertiesCategoryController();
54
55     private static final String JavaDoc findByProperties = new StringBuffer JavaDoc("SELECT c ")
56             .append("FROM org.infoglue.cms.entities.management.impl.simple.PropertiesCategoryImpl c ")
57             .append("WHERE c.entityName = $1 AND c.entityId = $2").toString();
58
59     private static final String JavaDoc findByPropertiesAttribute = new StringBuffer JavaDoc("SELECT c ")
60             .append("FROM org.infoglue.cms.entities.management.impl.simple.PropertiesCategoryImpl c ")
61             .append("WHERE c.attributeName = $1 ")
62             .append("AND c.entityName = $2")
63             .append("AND c.entityId = $3")
64             .append("ORDER BY c.category.name").toString();
65
66     private static final String JavaDoc findByCategory = new StringBuffer JavaDoc("SELECT c ")
67             .append("FROM org.infoglue.cms.entities.management.impl.simple.PropertiesCategoryImpl c ")
68             .append("WHERE c.category.categoryId = $1 ").toString();
69
70     public static PropertiesCategoryController getController()
71     {
72         return instance;
73     }
74
75     private PropertiesCategoryController() {}
76
77     /**
78      * Find a PropertiesCategory by it's identifier.
79      * @param id The id of the Category to find
80      * @return The CategoryVO identified by the provided id
81      * @throws SystemException If an error happens
82      */

83     public PropertiesCategoryVO findById(Integer JavaDoc id) throws SystemException
84     {
85         return (PropertiesCategoryVO)getVOWithId(PropertiesCategoryImpl.class, id);
86     }
87
88     /**
89      * Find a List of PropertiesCategories for the specific attribute and Properties Version.
90      * @param attribute The attribute name of the PropertiesCategory to find
91      * @param versionId The Properties Version id of the PropertiesCategory to find
92      * @return A list of PropertiesCategoryVO that have the provided properties version and attribute
93      * @throws SystemException If an error happens
94      */

95     public List JavaDoc findByPropertiesAttribute(String JavaDoc attribute, String JavaDoc entityName, Integer JavaDoc entityId) throws SystemException
96     {
97         List JavaDoc params = new ArrayList JavaDoc();
98         params.add(attribute);
99         params.add(entityName);
100         params.add(entityId);
101         return executeQuery(findByPropertiesAttribute, params);
102     }
103
104     /**
105      * Find a List of PropertiesCategories for the specific attribute and Properties Version.
106      * @param attribute The attribute name of the PropertiesCategory to find
107      * @param versionId The Properties Version id of the PropertiesCategory to find
108      * @return A list of PropertiesCategoryVO that have the provided properties version and attribute
109      * @throws SystemException If an error happens
110      */

111     public List JavaDoc findByPropertiesAttribute(String JavaDoc attribute, String JavaDoc entityName, Integer JavaDoc entityId, Database db) throws SystemException
112     {
113         List JavaDoc params = new ArrayList JavaDoc();
114         params.add(attribute);
115         params.add(entityName);
116         params.add(entityId);
117         return executeQuery(findByPropertiesAttribute, params, db);
118     }
119
120     /**
121      * Find a List of PropertiesCategories for a Properties Version.
122      * @param versionId The Properties Version id of the PropertiesCategory to find
123      * @return A list of PropertiesCategoryVO that have the provided properties version and attribute
124      * @throws SystemException If an error happens
125      */

126     public List JavaDoc findByProperties(String JavaDoc entityName, Integer JavaDoc entityId) throws SystemException
127     {
128         List JavaDoc params = new ArrayList JavaDoc();
129         params.add(entityName);
130         params.add(entityId);
131         return executeQuery(findByProperties, params);
132     }
133
134     /**
135      * Find a List of PropertiesCategories for the specific attribute and Properties Version.
136      * @param categoryId The Category id of the PropertiesCategory to find
137      * @return A list of PropertiesCategoryVO that have the provided category id
138      * @throws SystemException If an error happens
139      */

140     public List JavaDoc findByCategory(Integer JavaDoc categoryId) throws SystemException
141     {
142         List JavaDoc params = new ArrayList JavaDoc();
143         params.add(categoryId);
144         return executeQuery(findByCategory, params);
145     }
146
147     /**
148      * Saves a PropertiesCategoryVO whether it is new or not.
149      * @param c The PropertiesCategoryVO to save
150      * @return The saved PropertiesCategoryVO
151      * @throws SystemException If an error happens
152      */

153     public PropertiesCategoryVO save(PropertiesCategoryVO c) throws SystemException
154     {
155         return c.isUnsaved() ? create(c) : (PropertiesCategoryVO)updateEntity(PropertiesCategoryImpl.class, c);
156     }
157
158     /**
159      * Creates a PropertiesCategory from a PropertiesCategoryVO
160      */

161     private PropertiesCategoryVO create(PropertiesCategoryVO c) throws SystemException
162     {
163         Database db = beginTransaction();
164
165         try
166         {
167             PropertiesCategory propertiesCategory = createWithDatabase(c, db);
168             commitTransaction(db);
169             return propertiesCategory.getValueObject();
170         }
171         catch (Exception JavaDoc e)
172         {
173             rollbackTransaction(db);
174             throw new SystemException(e.getMessage());
175         }
176     }
177
178     public PropertiesCategory createWithDatabase(PropertiesCategoryVO c, Database db) throws SystemException, PersistenceException
179     {
180         // Need this crappy hack to forge the relationship (castor completely sucks like this)
181
// TODO: When hibernate comes, just save the VOs and if it has a child VO with an id set
182
// TODO: it is used to make the relationship...ask me for clarification -frank
183
Category category = (Category)getObjectWithId(CategoryImpl.class, c.getCategory().getId(), db);
184
185         PropertiesCategory propertiesCategory = new PropertiesCategoryImpl();
186         propertiesCategory.setValueObject(c);
187         propertiesCategory.setCategory((CategoryImpl)category);
188         db.create(propertiesCategory);
189         return propertiesCategory;
190     }
191
192     /**
193      * Deletes a PropertiesCategory
194      * @param id The id of the PropertiesCategory to delete
195      * @throws SystemException If an error happens
196      */

197     public void delete(Integer JavaDoc id) throws SystemException
198     {
199         deleteEntity(PropertiesCategoryImpl.class, id);
200     }
201
202     /**
203      * Deletes all PropertiesCategories for a particular PropertiesVersion
204      * @param versionId The id of the PropertiesCategory to delete
205      * @throws SystemException If an error happens
206      */

207     public void deleteByProperties(String JavaDoc entityName, Integer JavaDoc entityId) throws SystemException
208     {
209         delete(findByProperties(entityName, entityId));
210     }
211
212     /**
213      * Deletes all PropertiesCategories for a particular PropertiesVersion using the provided Database
214      * @param versionId The id of the PropertiesCategory to delete
215      * @param db The Database instance to use
216      * @throws SystemException If an error happens
217      */

218     public void deleteByPropertiesVersion(String JavaDoc entityName, Integer JavaDoc entityId, Database db) throws SystemException
219     {
220         delete(findByProperties(entityName, entityId), db);
221     }
222
223     /**
224      * Deletes all PropertiesCategories for a particular Category
225      * @param categoryId The id of the PropertiesCategory to delete
226      * @throws SystemException If an error happens
227      */

228     public void deleteByCategory(Integer JavaDoc categoryId) throws SystemException
229     {
230         delete(findByCategory(categoryId));
231     }
232
233     /**
234      * Deletes all PropertiesCategories for a particular Category using the provided Database
235      * @param categoryId The id of the Category to delete
236      * @param db The Database instance to use
237      * @throws SystemException If an error happens
238      */

239     public void deleteByCategory(Integer JavaDoc categoryId, Database db) throws SystemException
240     {
241         delete(findByCategory(categoryId), db);
242     }
243
244     /**
245      * Deletes all properties categories with a specific attribute for a specific properties version within a single transaction
246      * @param attribute the desired attribute
247      * @param versionId the ID of the desired properties version
248      * @throws SystemException if a database error occurs
249      */

250     public void deleteByPropertiesVersionAttribute(String JavaDoc attribute, String JavaDoc entityName, Integer JavaDoc entityId) throws SystemException
251     {
252         delete(findByPropertiesAttribute(attribute, entityName, entityId));
253     }
254
255     /**
256      * Deletes all properties categories with a specific attribute for a specific properties version using the given database
257      * @param attribute the desired attribute
258      * @param versionId the ID of the desired properties version
259      * @param db the database defining the transaction context for this delete
260      * @throws SystemException if a database error occurs
261      */

262     public void deleteByPropertiesVersionAttribute(String JavaDoc attribute, String JavaDoc entityName, Integer JavaDoc entityId, Database db) throws SystemException
263     {
264         delete(findByPropertiesAttribute(attribute, entityName, entityId), db);
265     }
266
267     /**
268      * Deletes a collection of properties categories within a single transaction
269      * @param propertiesCategories a collection of PropertiesCategoryVOs to delete
270      * @throws SystemException if a database error occurs
271      */

272     private static void delete(Collection JavaDoc propertiesCategories) throws SystemException
273     {
274         Database db = beginTransaction();
275
276         try
277         {
278             delete(propertiesCategories, db);
279             commitTransaction(db);
280         }
281         catch (Exception JavaDoc e)
282         {
283             rollbackTransaction(db);
284             throw new SystemException(e);
285         }
286     }
287
288     /**
289      * Deletes a collection of properties categories using the given database
290      * @param propertiesCategories a collection of PropertiesCategoryVOs to delete
291      * @param db the database to be used for the delete
292      * @throws SystemException if a database error occurs
293      */

294     private static void delete(Collection JavaDoc propertiesCategories, Database db) throws SystemException
295     {
296         for (Iterator JavaDoc i = propertiesCategories.iterator(); i.hasNext();)
297             deleteEntity(PropertiesCategoryImpl.class, ((PropertiesCategoryVO)i.next()).getId(), db);
298     }
299
300     /**
301      * Implemented for BaseController
302      */

303     public BaseEntityVO getNewVO()
304     {
305         return new PropertiesCategoryVO();
306     }
307 }
308
Popular Tags