1 16 package org.apache.jetspeed.om.registry.database; 17 18 import java.sql.Connection ; 19 import java.sql.SQLException ; 20 import java.util.ArrayList ; 21 import java.util.LinkedList ; 22 import java.util.List ; 23 24 import org.apache.jetspeed.om.registry.base.BaseCategory; 25 import org.apache.jetspeed.om.registry.base.BasePortletEntry; 26 import org.apache.jetspeed.services.logging.JetspeedLogFactoryService; 27 import org.apache.jetspeed.services.logging.JetspeedLogger; 28 import org.apache.torque.Torque; 29 import org.apache.torque.TorqueException; 30 import org.apache.torque.om.ObjectKey; 31 import org.apache.torque.om.SimpleKey; 32 import org.apache.torque.util.BasePeer; 33 import org.apache.torque.util.Criteria; 34 35 import com.workingdogs.village.DataSetException; 36 import com.workingdogs.village.QueryDataSet; 37 import com.workingdogs.village.Record; 38 39 45 public class BaseJetspeedPortletCategoryPeer extends BasePeer 46 { 47 48 51 protected static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(BaseJetspeedPortletCategoryPeer.class.getName()); 52 53 54 public static final String DATABASE_NAME = "default"; 55 56 58 public static final String PORTLET_ID; 59 60 public static final String NAME; 61 62 public static final String GROUP; 63 static { 64 PORTLET_ID = "PORTLET_CATEGORY.ID"; 65 NAME = "PORTLET_CATEGORY.NAME"; 66 GROUP = "PORTLET_CATEGORY.CATEGORY_GROUP"; 67 if (Torque.isInit()) 68 { 69 try 70 { 71 getMapBuilder(); 72 } 73 catch (Exception e) 74 { 75 logger.error("Could not initialize Peer", e); 76 } 77 } 78 } 79 80 public static final int numColumns = 1; 81 82 protected static final String CLASSNAME_DEFAULT = 83 "org.apache.jetspeed.om.registry.base.BaseCategory"; 84 85 protected static final Class CLASS_DEFAULT = initClass(CLASSNAME_DEFAULT); 86 87 93 private static Class initClass(String className) 94 { 95 Class c = null; 96 try 97 { 98 c = Class.forName(className); 99 } 100 catch (Throwable t) 101 { 102 logger.error( 103 "A FATAL ERROR has occurred which should not " 104 + "have happened under any circumstance. Please notify " 105 + "the Turbine developers <turbine-dev@jakarta.apache.org> " 106 + "and give as many details as possible (including the error " 107 + "stack trace).", 108 t); 109 if (t instanceof Error ) 111 { 112 throw (Error ) t.fillInStackTrace(); 113 } 114 } 115 return c; 116 } 117 127 public static List resultSet2Objects(java.sql.ResultSet results) 128 throws TorqueException 129 { 130 try 131 { 132 QueryDataSet qds = null; 133 List rows = null; 134 try 135 { 136 qds = new QueryDataSet(results); 137 rows = getSelectResults(qds); 138 } 139 finally 140 { 141 if (qds != null) 142 { 143 qds.close(); 144 } 145 } 146 return populateObjects(rows); 147 } 148 catch (SQLException e) 149 { 150 throw new TorqueException(e); 151 } 152 catch (DataSetException e) 153 { 154 throw new TorqueException(e); 155 } 156 } 157 164 public static void addSelectColumns(Criteria criteria) 165 throws TorqueException 166 { 167 criteria.addSelectColumn(NAME); 168 criteria.addSelectColumn(GROUP); 169 } 170 179 public static BaseCategory row2Object(Record row, int offset, Class cls) 180 throws TorqueException 181 { 182 try 183 { 184 BaseCategory obj = (BaseCategory) cls.newInstance(); 185 populateObject(row, offset, obj); 186 return obj; 187 } 188 catch (InstantiationException e) 189 { 190 throw new TorqueException(e); 191 } 192 catch (IllegalAccessException e) 193 { 194 throw new TorqueException(e); 195 } 196 } 197 206 public static void populateObject(Record row, int offset, BaseCategory obj) 207 throws TorqueException 208 { 209 try 210 { 211 obj.setName(row.getValue(offset + 0).asString()); 212 if (row.getValue(offset + 1).asString() != null) 213 obj.setGroup(row.getValue(offset + 1).asString()); 214 } 215 catch (DataSetException e) 216 { 217 throw new TorqueException(e); 218 } 219 } 220 228 public static List doSelect(Criteria criteria) throws TorqueException 229 { 230 return populateObjects(doSelectVillageRecords(criteria)); 231 } 232 241 public static List doSelect(Criteria criteria, Connection con) 242 throws TorqueException 243 { 244 return populateObjects(doSelectVillageRecords(criteria, con)); 245 } 246 256 public static List doSelectVillageRecords(Criteria criteria) 257 throws TorqueException 258 { 259 return BaseJetspeedPortletCategoryPeer.doSelectVillageRecords( 260 criteria, 261 (Connection ) null); 262 } 263 271 public static List doSelectVillageRecords( 272 Criteria criteria, 273 Connection con) 274 throws TorqueException 275 { 276 if (criteria.getSelectColumns().size() == 0) 277 { 278 addSelectColumns(criteria); 279 } 280 if (criteria.getDbName() == Torque.getDefaultDB()) 284 { 285 criteria.setDbName(DATABASE_NAME); 286 } 287 if (con == null) 290 { 291 return BasePeer.doSelect(criteria); 292 } 293 else 294 { 295 return BasePeer.doSelect(criteria, con); 296 } 297 } 298 305 public static List populateObjects(List records) throws TorqueException 306 { 307 List results = new ArrayList (records.size()); 308 for (int i = 0; i < records.size(); i++) 310 { 311 Record row = (Record) records.get(i); 312 results.add( 313 BaseJetspeedPortletCategoryPeer.row2Object( 314 row, 315 1, 316 BaseJetspeedPortletCategoryPeer.getOMClass())); 317 } 318 return results; 319 } 320 328 public static Class getOMClass() throws TorqueException 329 { 330 return CLASS_DEFAULT; 331 } 332 338 public static List doSelect(BasePortletEntry obj) throws TorqueException 339 { 340 return doSelect(buildCriteria(obj)); 341 } 342 343 public static Criteria buildCriteria(ObjectKey pk) 344 { 345 Criteria criteria = new Criteria(); 346 criteria.add(PORTLET_ID, pk); 347 return criteria; 348 } 349 350 public static Criteria buildCriteria(BasePortletEntry obj) 351 { 352 Criteria criteria = new Criteria(DATABASE_NAME); 353 return criteria; 355 } 356 363 public static BaseCategory retrieveByPK(int pk) throws TorqueException 364 { 365 return retrieveByPK(SimpleKey.keyFor(pk)); 366 } 367 374 public static BaseCategory retrieveByPK(ObjectKey pk) 375 throws TorqueException 376 { 377 Connection db = null; 378 BaseCategory retVal = null; 379 try 380 { 381 db = Torque.getConnection(DATABASE_NAME); 382 retVal = retrieveByPK(pk, db); 383 } 384 finally 385 { 386 Torque.closeConnection(db); 387 } 388 return (retVal); 389 } 390 398 public static BaseCategory retrieveByPK(ObjectKey pk, Connection con) 399 throws TorqueException 400 { 401 Criteria criteria = buildCriteria(pk); 402 List v = doSelect(criteria, con); 403 if (v.size() != 1) 404 { 405 throw new TorqueException("Failed to select one and only one row."); 406 } 407 else 408 { 409 return (BaseCategory) v.get(0); 410 } 411 } 412 419 public static List retrieveByPKs(List pks) throws TorqueException 420 { 421 Connection db = null; 422 List retVal = null; 423 try 424 { 425 db = Torque.getConnection(DATABASE_NAME); 426 retVal = retrieveByPKs(pks, db); 427 } 428 finally 429 { 430 Torque.closeConnection(db); 431 } 432 return (retVal); 433 } 434 442 public static List retrieveByPKs(List pks, Connection dbcon) 443 throws TorqueException 444 { 445 List objs = null; 446 if (pks == null || pks.size() == 0) 447 { 448 objs = new LinkedList (); 449 } 450 else 451 { 452 Criteria criteria = new Criteria(); 453 criteria.addIn(PORTLET_ID, pks); 454 objs = doSelect(criteria, dbcon); 455 } 456 return objs; 457 } 458 465 public static List retrieveById(int pk) throws TorqueException 466 { 467 return retrieveById(SimpleKey.keyFor(pk)); 468 } 469 476 public static List retrieveById(ObjectKey pk) throws TorqueException 477 { 478 Connection db = null; 479 List retVal = null; 480 try 481 { 482 db = Torque.getConnection(DATABASE_NAME); 483 retVal = retrieveById(pk, db); 484 } 485 finally 486 { 487 Torque.closeConnection(db); 488 } 489 return (retVal); 490 } 491 492 500 public static List retrieveById(ObjectKey pk, Connection con) 501 throws TorqueException 502 { 503 Criteria criteria = buildCriteria(pk); 504 return doSelect(criteria, con); 505 } 506 } 507 | Popular Tags |