KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > deliver > controllers > kernel > impl > simple > BaseDeliveryController


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.deliver.controllers.kernel.impl.simple;
25
26 import java.util.ArrayList JavaDoc;
27 import java.util.Collection JavaDoc;
28 import java.util.Collections JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.List JavaDoc;
31
32 import org.apache.log4j.Logger;
33 import org.exolab.castor.jdo.Database;
34 import org.exolab.castor.jdo.OQLQuery;
35 import org.exolab.castor.jdo.PersistenceException;
36 import org.exolab.castor.jdo.QueryResults;
37 import org.infoglue.cms.entities.kernel.BaseEntityVO;
38 import org.infoglue.cms.entities.kernel.IBaseEntity;
39 import org.infoglue.cms.exception.Bug;
40 import org.infoglue.cms.exception.SystemException;
41
42
43 /**
44  * BaseDeliveryController.java
45  *
46  * Baseclass for Castor Controller Classes.
47  * Various methods to handle transactions and so on
48  *
49  */

50
51 public abstract class BaseDeliveryController
52 {
53     private final static Logger logger = Logger.getLogger(BaseDeliveryController.class.getName());
54
55     /**
56      * This method fetches one object / entity within a transaction.
57      **/

58     
59     protected Object JavaDoc getObjectWithId(Class JavaDoc arg, Integer JavaDoc id, Database db) throws SystemException, Bug
60     {
61         Object JavaDoc object = null;
62         try
63         {
64             object = db.load(arg, id, Database.ReadOnly);
65         }
66         catch(Exception JavaDoc e)
67         {
68             throw new SystemException("An error occurred when we tried to fetch the object " + arg.getName() + ". Reason:" + e.getMessage(), e);
69         }
70     
71         if(object == null)
72         {
73             throw new Bug("The object with id [" + id + "] was not found. This should never happen.");
74         }
75         return object;
76     }
77     
78     
79     /**
80      * This method fetches one object in read only mode and returns it's value object.
81      */

82     
83     protected BaseEntityVO getVOWithId(Class JavaDoc arg, Integer JavaDoc id, Database db) throws SystemException, Bug
84     {
85         IBaseEntity vo = null;
86         try
87         {
88             vo = (IBaseEntity)db.load(arg, id, Database.ReadOnly);
89         }
90         catch(Exception JavaDoc e)
91         {
92             throw new SystemException("An error occurred when we tried to fetch the object " + arg.getName() + ". Reason:" + e.getMessage(), e);
93         }
94     
95         if(vo == null)
96         {
97             throw new Bug("The object with id [" + id + "] was not found. This should never happen.");
98         }
99         
100         return vo.getVO();
101     }
102
103
104     /**
105      * This method fetches all object in read only mode and returns a list of value objects.
106      */

107
108     public List JavaDoc getAllVOObjects(Class JavaDoc arg, Database db) throws SystemException, Bug
109     {
110         ArrayList JavaDoc resultList = new ArrayList JavaDoc();
111         
112         try
113         {
114             logger.info("BaseHelper::GetAllObjects for " + arg.getName());
115             OQLQuery oql = db.getOQLQuery( "SELECT u FROM " + arg.getName() + " u" );
116             QueryResults results = oql.execute(Database.ReadOnly);
117             
118             while (results.hasMore())
119             {
120                 Object JavaDoc o = results.next();
121
122                 // Om metoden getValueObject saknas, kastas ett undantag.
123
resultList.add(o.getClass().getDeclaredMethod("getValueObject", new Class JavaDoc[0]).invoke(o, new Object JavaDoc[0]));
124             }
125             
126             results.close();
127             oql.close();
128         }
129         catch(NoSuchMethodException JavaDoc e)
130         {
131             throw new Bug("The object [" + arg.getName() + "] is of the wrong type. This should never happen.", e);
132         }
133         catch(Exception JavaDoc e)
134         {
135             throw new SystemException("An error occurred when we tried to fetch " + arg.getName() + " Reason:" + e.getMessage(), e);
136         }
137
138         return Collections.unmodifiableList(resultList);
139     }
140     
141     /**
142      * This method fetches all object in read only mode and returns a list of value objects.
143      */

144
145     public List JavaDoc getAllVOObjects(Class JavaDoc arg, String JavaDoc orderByField, String JavaDoc direction, Database db) throws SystemException, Bug
146     {
147         ArrayList JavaDoc resultList = new ArrayList JavaDoc();
148         
149         try
150         {
151             
152             logger.info("BaseHelper::GetAllObjects for " + arg.getName());
153             OQLQuery oql = db.getOQLQuery( "SELECT u FROM " + arg.getName() + " u ORDER BY u." + orderByField + " " + direction);
154             QueryResults results = oql.execute(Database.ReadOnly);
155             
156             while (results.hasMore())
157             {
158                 Object JavaDoc o = results.next();
159
160                 // Om metoden getValueObject saknas, kastas ett undantag.
161
resultList.add(o.getClass().getDeclaredMethod("getValueObject", new Class JavaDoc[0]).invoke(o, new Object JavaDoc[0]));
162             }
163             
164             results.close();
165             oql.close();
166         }
167         catch(NoSuchMethodException JavaDoc e)
168         {
169             throw new Bug("The object [" + arg.getName() + "] is of the wrong type. This should never happen.", e);
170         }
171         catch(Exception JavaDoc e)
172         {
173             throw new SystemException("An error occurred when we tried to fetch " + arg.getName() + " Reason:" + e.getMessage(), e);
174         }
175
176         return Collections.unmodifiableList(resultList);
177     }
178     
179     //---------------------------------------------------------------------
180
// Dynamic Query specific operations
181
//---------------------------------------------------------------------
182
/**
183      * Executes a Query with no parameters
184      *
185      * @param query An OQL Query
186      * @return A list of the query results as Impls
187      * @throws SystemException If an error occurs
188      */

189     protected static List JavaDoc executeQuery(Database db, String JavaDoc query) throws SystemException, Exception JavaDoc
190     {
191         return executeQuery(db, query, Collections.EMPTY_LIST);
192     }
193     
194
195     /**
196      * Executes a Query, also binds the provided parameters
197      *
198      * @param query An OQL Query
199      * @param params A List of paramters
200      * @return A list of the query results as Castor Impl
201      * @throws SystemException If an error occurs
202      */

203     protected static List JavaDoc executeQuery(Database db, String JavaDoc query, List JavaDoc params) throws Exception JavaDoc
204     {
205         List JavaDoc resultList = new ArrayList JavaDoc();
206         
207         OQLQuery oql = createQuery(db, query, params);
208         QueryResults results = oql.execute(Database.ReadOnly);
209         resultList = Collections.list(results);
210         
211         results.close();
212         oql.close();
213
214         return resultList;
215     }
216          
217     /**
218      * Creates an OQLQuery for the provided Database and binds the parameters to it.
219      *
220      * @param db The Database to create the OQLQuery on
221      * @param query The String OQL query
222      * @param params A List of Objects to bind to the query sequentially
223      * @return An OQLQuery instance that can be executer
224      * @throws PersistenceException
225      */

226     protected static OQLQuery createQuery(Database db, String JavaDoc query, List JavaDoc params) throws PersistenceException
227     {
228         OQLQuery oql = db.getOQLQuery(query);
229         if (params != null)
230             for (Iterator JavaDoc i = params.iterator(); i.hasNext();)
231                 oql.bind(i.next());
232
233         return oql;
234     }
235
236     /**
237      * This method converts a List of entities to a list of value-objects.
238      */

239     protected static List JavaDoc toVOList(Collection JavaDoc entities) throws SystemException, Bug
240     {
241         List JavaDoc resultVOList = new ArrayList JavaDoc();
242
243         if(entities == null)
244             return Collections.EMPTY_LIST;
245
246         Iterator JavaDoc iterator = entities.iterator();
247         while (iterator.hasNext())
248         {
249             Object JavaDoc o = (Object JavaDoc)iterator.next();
250
251             try
252             {
253                 resultVOList.add(o.getClass().getDeclaredMethod("getValueObject", new Class JavaDoc[0]).invoke(o, new Object JavaDoc[0]));
254             }
255             catch(NoSuchMethodException JavaDoc e)
256             {
257                 throw new Bug("The object in list was of the wrong type: " + o.getClass().getName() + ". This should never happen.", e);
258             }
259             catch(Exception JavaDoc e)
260             {
261                 throw new SystemException("An error occurred when we tried to convert the collection to a valueList. Reason:" + e.getMessage(), e);
262             }
263         }
264
265         return resultVOList;
266     }
267
268     /**
269      * Begins a transaction on the named database
270      */

271          
272     public static void beginTransaction(Database db) throws SystemException
273     {
274         try
275         {
276             db.begin();
277             logger.info("Opening a new Transaction...");
278         }
279         catch(Exception JavaDoc e)
280         {
281             e.printStackTrace();
282             throw new SystemException("An error occurred when we tried to begin an transaction. Reason:" + e.getMessage(), e);
283         }
284     }
285     
286     /**
287      * Rollbacks a transaction on the named database
288      */

289      
290     public static void closeTransaction(Database db) throws SystemException
291     {
292         logger.info("closeTransaction a transaction and closing it...");
293         //rollbackTransaction(db);
294
commitTransaction(db);
295     }
296        
297     /**
298      * Ends a transaction on the named database
299      */

300      
301     public static void commitTransaction(Database db) throws SystemException
302     {
303         try
304         {
305             logger.info("Committing a transaction and closing it...");
306             
307             db.commit();
308             db.close();
309         }
310         catch(Exception JavaDoc e)
311         {
312             e.printStackTrace();
313             throw new SystemException("An error occurred when we tried to commit an transaction. Reason:" + e.getMessage(), e);
314         }
315     }
316  
317  
318     /**
319      * Rollbacks a transaction on the named database
320      */

321      
322     public static void rollbackTransaction(Database db) throws SystemException
323     {
324         logger.info("Rollback a transaction...");
325
326         try
327         {
328             if (db.isActive())
329             {
330                 db.rollback();
331                 db.close();
332             }
333         }
334         catch(Exception JavaDoc e)
335         {
336             logger.info("An error occurred when we tried to rollback an transaction. Reason:" + e.getMessage());
337             //throw new SystemException("An error occurred when we tried to rollback an transaction. Reason:" + e.getMessage(), e);
338
}
339     }
340
341     /**
342      * Close the database
343      */

344      
345     public static void closeDatabase(Database db) throws SystemException
346     {
347         try
348         {
349             logger.info("Closing database...");
350
351             db.close();
352         }
353         catch(Exception JavaDoc e)
354         {
355             e.printStackTrace();
356             throw new SystemException("An error occurred when we tried to close a database. Reason:" + e.getMessage(), e);
357         }
358     }
359
360 }
Popular Tags