1 23 24 package org.infoglue.deliver.controllers.kernel.impl.simple; 25 26 import java.util.ArrayList ; 27 import java.util.Collection ; 28 import java.util.Collections ; 29 import java.util.Iterator ; 30 import java.util.List ; 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 50 51 public abstract class BaseDeliveryController 52 { 53 private final static Logger logger = Logger.getLogger(BaseDeliveryController.class.getName()); 54 55 58 59 protected Object getObjectWithId(Class arg, Integer id, Database db) throws SystemException, Bug 60 { 61 Object object = null; 62 try 63 { 64 object = db.load(arg, id, Database.ReadOnly); 65 } 66 catch(Exception 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 82 83 protected BaseEntityVO getVOWithId(Class arg, Integer 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 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 107 108 public List getAllVOObjects(Class arg, Database db) throws SystemException, Bug 109 { 110 ArrayList resultList = new ArrayList (); 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 o = results.next(); 121 122 resultList.add(o.getClass().getDeclaredMethod("getValueObject", new Class [0]).invoke(o, new Object [0])); 124 } 125 126 results.close(); 127 oql.close(); 128 } 129 catch(NoSuchMethodException e) 130 { 131 throw new Bug("The object [" + arg.getName() + "] is of the wrong type. This should never happen.", e); 132 } 133 catch(Exception 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 144 145 public List getAllVOObjects(Class arg, String orderByField, String direction, Database db) throws SystemException, Bug 146 { 147 ArrayList resultList = new ArrayList (); 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 o = results.next(); 159 160 resultList.add(o.getClass().getDeclaredMethod("getValueObject", new Class [0]).invoke(o, new Object [0])); 162 } 163 164 results.close(); 165 oql.close(); 166 } 167 catch(NoSuchMethodException e) 168 { 169 throw new Bug("The object [" + arg.getName() + "] is of the wrong type. This should never happen.", e); 170 } 171 catch(Exception 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 189 protected static List executeQuery(Database db, String query) throws SystemException, Exception 190 { 191 return executeQuery(db, query, Collections.EMPTY_LIST); 192 } 193 194 195 203 protected static List executeQuery(Database db, String query, List params) throws Exception 204 { 205 List resultList = new ArrayList (); 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 226 protected static OQLQuery createQuery(Database db, String query, List params) throws PersistenceException 227 { 228 OQLQuery oql = db.getOQLQuery(query); 229 if (params != null) 230 for (Iterator i = params.iterator(); i.hasNext();) 231 oql.bind(i.next()); 232 233 return oql; 234 } 235 236 239 protected static List toVOList(Collection entities) throws SystemException, Bug 240 { 241 List resultVOList = new ArrayList (); 242 243 if(entities == null) 244 return Collections.EMPTY_LIST; 245 246 Iterator iterator = entities.iterator(); 247 while (iterator.hasNext()) 248 { 249 Object o = (Object )iterator.next(); 250 251 try 252 { 253 resultVOList.add(o.getClass().getDeclaredMethod("getValueObject", new Class [0]).invoke(o, new Object [0])); 254 } 255 catch(NoSuchMethodException 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 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 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 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 289 290 public static void closeTransaction(Database db) throws SystemException 291 { 292 logger.info("closeTransaction a transaction and closing it..."); 293 commitTransaction(db); 295 } 296 297 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 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 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 e) 335 { 336 logger.info("An error occurred when we tried to rollback an transaction. Reason:" + e.getMessage()); 337 } 339 } 340 341 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 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 |