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.BaseMediaType; 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 40 46 public class BaseJetspeedPortletMediaTypePeer extends BasePeer 47 { 48 51 protected static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(BaseJetspeedPortletMediaTypePeer.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 MEDIA_NAME; 61 62 public static final String MEDIA_ID; 63 64 public static final String PORTLET_MEDIA_ID; 65 static { 66 PORTLET_ID = "PORTLET_MEDIATYPE.ID"; 67 MEDIA_NAME = "MEDIATYPE.NAME"; 68 PORTLET_MEDIA_ID = "PORTLET_MEDIATYPE.MEDIA_ID"; 69 MEDIA_ID = "MEDIATYPE.ID"; 70 if (Torque.isInit()) 71 { 72 try 73 { 74 getMapBuilder(); 75 } 76 catch (Exception e) 77 { 78 logger.error("Could not initialize Peer", e); 79 } 80 } 81 } 82 83 public static final int numColumns = 1; 84 85 protected static final String CLASSNAME_DEFAULT = 86 "org.apache.jetspeed.om.registry.base.BaseMediaType"; 87 88 protected static final Class CLASS_DEFAULT = initClass(CLASSNAME_DEFAULT); 89 95 private static Class initClass(String className) 96 { 97 Class c = null; 98 try 99 { 100 c = Class.forName(className); 101 } 102 catch (Throwable t) 103 { 104 logger.error( 105 "A FATAL ERROR has occurred which should not " 106 + "have happened under any circumstance. Please notify " 107 + "the Turbine developers <turbine-dev@jakarta.apache.org> " 108 + "and give as many details as possible (including the error " 109 + "stack trace).", 110 t); 111 if (t instanceof Error ) 113 { 114 throw (Error ) t.fillInStackTrace(); 115 } 116 } 117 return c; 118 } 119 129 public static List resultSet2Objects(java.sql.ResultSet results) 130 throws TorqueException 131 { 132 try 133 { 134 QueryDataSet qds = null; 135 List rows = null; 136 try 137 { 138 qds = new QueryDataSet(results); 139 rows = getSelectResults(qds); 140 } 141 finally 142 { 143 if (qds != null) 144 { 145 qds.close(); 146 } 147 } 148 return populateObjects(rows); 149 } 150 catch (SQLException e) 151 { 152 throw new TorqueException(e); 153 } 154 catch (DataSetException e) 155 { 156 throw new TorqueException(e); 157 } 158 } 159 166 public static void addSelectColumns(Criteria criteria) 167 throws TorqueException 168 { 169 criteria.addSelectColumn(MEDIA_NAME); 170 criteria.addSelectColumn(MEDIA_ID); 171 } 172 181 public static BaseMediaType row2Object(Record row, int offset, Class cls) 182 throws TorqueException 183 { 184 try 185 { 186 BaseMediaType obj = (BaseMediaType) cls.newInstance(); 187 populateObject(row, offset, obj); 188 return obj; 191 } 192 catch (InstantiationException e) 193 { 194 throw new TorqueException(e); 195 } 196 catch (IllegalAccessException e) 197 { 198 throw new TorqueException(e); 199 } 200 } 201 210 public static void populateObject( 211 Record row, 212 int offset, 213 BaseMediaType obj) 214 throws TorqueException 215 { 216 try 217 { 218 obj.setRef(row.getValue(offset + 0).asString()); 219 } 220 catch (DataSetException e) 221 { 222 throw new TorqueException(e); 223 } 224 } 225 233 public static List doSelect(Criteria criteria) throws TorqueException 234 { 235 return populateObjects(doSelectVillageRecords(criteria)); 236 } 237 246 public static List doSelect(Criteria criteria, Connection con) 247 throws TorqueException 248 { 249 return populateObjects(doSelectVillageRecords(criteria, con)); 250 } 251 261 public static List doSelectVillageRecords(Criteria criteria) 262 throws TorqueException 263 { 264 return BaseJetspeedPortletMediaTypePeer.doSelectVillageRecords( 265 criteria, 266 (Connection ) null); 267 } 268 276 public static List doSelectVillageRecords( 277 Criteria criteria, 278 Connection con) 279 throws TorqueException 280 { 281 if (criteria.getSelectColumns().size() == 0) 282 { 283 addSelectColumns(criteria); 284 } 285 if (criteria.getDbName() == Torque.getDefaultDB()) 289 { 290 criteria.setDbName(DATABASE_NAME); 291 } 292 if (con == null) 295 { 296 return BasePeer.doSelect(criteria); 297 } 298 else 299 { 300 return BasePeer.doSelect(criteria, con); 301 } 302 } 303 310 public static List populateObjects(List records) throws TorqueException 311 { 312 List results = new ArrayList (records.size()); 313 for (int i = 0; i < records.size(); i++) 315 { 316 Record row = (Record) records.get(i); 317 results.add( 318 BaseJetspeedPortletMediaTypePeer.row2Object( 319 row, 320 1, 321 BaseJetspeedPortletMediaTypePeer.getOMClass())); 322 } 323 return results; 324 } 325 333 public static Class getOMClass() throws TorqueException 334 { 335 return CLASS_DEFAULT; 336 } 337 343 public static List doSelect(BasePortletEntry obj) throws TorqueException 344 { 345 return doSelect(buildCriteria(obj)); 346 } 347 348 public static Criteria buildCriteria(ObjectKey pk) 349 { 350 Criteria criteria = new Criteria(); 351 criteria.add(PORTLET_ID, pk); 352 criteria.addJoin(PORTLET_MEDIA_ID, MEDIA_ID); 353 return criteria; 354 } 355 356 public static Criteria buildCriteria(BasePortletEntry obj) 357 { 358 Criteria criteria = new Criteria(DATABASE_NAME); 359 return criteria; 361 } 362 369 public static BaseMediaType retrieveByPK(int pk) throws TorqueException 370 { 371 return retrieveByPK(SimpleKey.keyFor(pk)); 372 } 373 380 public static BaseMediaType retrieveByPK(ObjectKey pk) 381 throws TorqueException 382 { 383 Connection db = null; 384 BaseMediaType retVal = null; 385 try 386 { 387 db = Torque.getConnection(DATABASE_NAME); 388 retVal = retrieveByPK(pk, db); 389 } 390 finally 391 { 392 Torque.closeConnection(db); 393 } 394 return (retVal); 395 } 396 404 public static BaseMediaType retrieveByPK(ObjectKey pk, Connection con) 405 throws TorqueException 406 { 407 Criteria criteria = buildCriteria(pk); 408 List v = doSelect(criteria, con); 409 if (v.size() != 1) 410 { 411 throw new TorqueException("Failed to select one and only one row."); 412 } 413 else 414 { 415 return (BaseMediaType) v.get(0); 416 } 417 } 418 425 public static List retrieveByPKs(List pks) throws TorqueException 426 { 427 Connection db = null; 428 List retVal = null; 429 try 430 { 431 db = Torque.getConnection(DATABASE_NAME); 432 retVal = retrieveByPKs(pks, db); 433 } 434 finally 435 { 436 Torque.closeConnection(db); 437 } 438 return (retVal); 439 } 440 448 public static List retrieveByPKs(List pks, Connection dbcon) 449 throws TorqueException 450 { 451 List objs = null; 452 if (pks == null || pks.size() == 0) 453 { 454 objs = new LinkedList (); 455 } 456 else 457 { 458 Criteria criteria = new Criteria(); 459 criteria.addIn(PORTLET_ID, pks); 460 objs = doSelect(criteria, dbcon); 461 } 462 return objs; 463 } 464 471 public static List retrieveById(int pk) throws TorqueException 472 { 473 return retrieveById(SimpleKey.keyFor(pk)); 474 } 475 482 public static List retrieveById(ObjectKey pk) throws TorqueException 483 { 484 Connection db = null; 485 List retVal = null; 486 try 487 { 488 db = Torque.getConnection(DATABASE_NAME); 489 retVal = retrieveById(pk, db); 490 } 491 finally 492 { 493 Torque.closeConnection(db); 494 } 495 return (retVal); 496 } 497 505 public static List retrieveById(ObjectKey pk, Connection con) 506 throws TorqueException 507 { 508 Criteria criteria = buildCriteria(pk); 509 return doSelect(criteria, con); 510 } 511 } 512 | Popular Tags |