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.Parameter; 25 import org.apache.jetspeed.om.registry.base.BaseMetaInfo; 26 import org.apache.jetspeed.om.registry.base.BaseSecurity; 27 import org.apache.jetspeed.om.registry.base.BaseSkinEntry; 28 import org.apache.jetspeed.services.logging.JetspeedLogFactoryService; 29 import org.apache.jetspeed.services.logging.JetspeedLogger; 30 import org.apache.torque.Torque; 31 import org.apache.torque.TorqueException; 32 import org.apache.torque.om.ObjectKey; 33 import org.apache.torque.om.SimpleKey; 34 import org.apache.torque.util.BasePeer; 35 import org.apache.torque.util.Criteria; 36 37 import com.workingdogs.village.DataSetException; 38 import com.workingdogs.village.QueryDataSet; 39 import com.workingdogs.village.Record; 40 41 47 public class BaseJetspeedSkinParameterPeer extends BasePeer 48 { 49 50 53 protected static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(BaseJetspeedSkinParameterPeer.class.getName()); 54 55 56 public static final String DATABASE_NAME = "default"; 57 58 public static final String TABLE_NAME = "SKIN_PARAMETER"; 59 60 public static final String ID; 61 62 public static final String NAME; 63 64 public static final String VALUE; 65 66 public static final String HIDDEN; 67 68 public static final String TYPE; 69 70 public static final String ROLE; 71 72 public static final String TITLE; 73 74 public static final String DESCRIPTION; 75 76 public static final String IMAGE; 77 78 public static final String SKIN_ID; 79 static { 80 ID = "SKIN_PARAMETER.ID"; 81 NAME = "SKIN_PARAMETER.NAME"; 82 VALUE = "SKIN_PARAMETER.VALUE"; 83 HIDDEN = "SKIN_PARAMETER.HIDDEN"; 84 TYPE = "SKIN_PARAMETER.TYPE"; 85 ROLE = "SKIN_PARAMETER.ROLE"; 86 TITLE = "SKIN_PARAMETER.TITLE"; 87 DESCRIPTION = "SKIN_PARAMETER.DESCRIPTION"; 88 IMAGE = "SKIN_PARAMETER.IMAGE"; 89 SKIN_ID = "SKIN_PARAMETER.SKIN_ID"; 90 if (Torque.isInit()) 91 { 92 try 93 { 94 getMapBuilder(); 95 } 96 catch (Exception e) 97 { 98 logger.error("Could not initialize Peer", e); 99 } 100 } 101 } 102 103 public static final int numColumns = 12; 104 105 protected static final String CLASSNAME_DEFAULT = 106 "org.apache.jetspeed.om.registry.base.BaseParameter"; 107 108 protected static final Class CLASS_DEFAULT = initClass(CLASSNAME_DEFAULT); 109 115 private static Class initClass(String className) 116 { 117 Class c = null; 118 try 119 { 120 c = Class.forName(className); 121 } 122 catch (Throwable t) 123 { 124 logger.error( 125 "A FATAL ERROR has occurred which should not " 126 + "have happened under any circumstance. Please notify " 127 + "the Turbine developers <turbine-dev@jakarta.apache.org> " 128 + "and give as many details as possible (including the error " 129 + "stack trace).", 130 t); 131 if (t instanceof Error ) 133 { 134 throw (Error ) t.fillInStackTrace(); 135 } 136 } 137 return c; 138 } 139 149 public static List resultSet2Objects(java.sql.ResultSet results) 150 throws TorqueException 151 { 152 try 153 { 154 QueryDataSet qds = null; 155 List rows = null; 156 try 157 { 158 qds = new QueryDataSet(results); 159 rows = getSelectResults(qds); 160 } 161 finally 162 { 163 if (qds != null) 164 { 165 qds.close(); 166 } 167 } 168 return populateObjects(rows); 169 } 170 catch (SQLException e) 171 { 172 throw new TorqueException(e); 173 } 174 catch (DataSetException e) 175 { 176 throw new TorqueException(e); 177 } 178 } 179 186 public static void addSelectColumns(Criteria criteria) 187 throws TorqueException 188 { 189 criteria.addSelectColumn(ID); 190 criteria.addSelectColumn(NAME); 191 criteria.addSelectColumn(VALUE); 192 criteria.addSelectColumn(HIDDEN); 193 criteria.addSelectColumn(TYPE); 194 criteria.addSelectColumn(ROLE); 195 criteria.addSelectColumn(TITLE); 196 criteria.addSelectColumn(DESCRIPTION); 197 criteria.addSelectColumn(IMAGE); 198 criteria.addSelectColumn(SKIN_ID); 199 } 200 209 public static Parameter row2Object(Record row, int offset, Class cls) 210 throws TorqueException 211 { 212 try 213 { 214 Parameter obj = (Parameter) cls.newInstance(); 215 populateObject(row, offset, obj); 216 return obj; 219 } 220 catch (InstantiationException e) 221 { 222 throw new TorqueException(e); 223 } 224 catch (IllegalAccessException e) 225 { 226 throw new TorqueException(e); 227 } 228 } 229 238 public static void populateObject(Record row, int offset, Parameter obj) 239 throws TorqueException 240 { 241 try 242 { 243 obj.setName(row.getValue(offset + 1).asString()); 244 obj.setValue(row.getValue(offset + 2).asString()); 245 obj.setHidden(row.getValue(offset + 3).asBoolean()); 246 obj.setType(row.getValue(offset + 4).asString()); 247 BaseMetaInfo baseMetaInfo = 248 new BaseMetaInfo( 249 row.getValue(offset + 6).asString(), 250 row.getValue(offset + 7).asString(), 251 row.getValue(offset + 8).asString()); 252 obj.setMetaInfo(baseMetaInfo); 253 BaseSecurity security = 255 new BaseSecurity(row.getValue(offset + 5).asString()); 256 obj.setSecurity(security); 257 } 258 catch (DataSetException e) 259 { 260 throw new TorqueException(e); 261 } 262 } 263 271 public static List doSelect(Criteria criteria) throws TorqueException 272 { 273 return populateObjects(doSelectVillageRecords(criteria)); 274 } 275 284 public static List doSelect(Criteria criteria, Connection con) 285 throws TorqueException 286 { 287 return populateObjects(doSelectVillageRecords(criteria, con)); 288 } 289 299 public static List doSelectVillageRecords(Criteria criteria) 300 throws TorqueException 301 { 302 return BaseJetspeedSkinPeer.doSelectVillageRecords( 303 criteria, 304 (Connection ) null); 305 } 306 314 public static List doSelectVillageRecords( 315 Criteria criteria, 316 Connection con) 317 throws TorqueException 318 { 319 if (criteria.getSelectColumns().size() == 0) 320 { 321 addSelectColumns(criteria); 322 } 323 if (criteria.getDbName() == Torque.getDefaultDB()) 327 { 328 criteria.setDbName(DATABASE_NAME); 329 } 330 if (con == null) 333 { 334 return BasePeer.doSelect(criteria); 335 } 336 else 337 { 338 return BasePeer.doSelect(criteria, con); 339 } 340 } 341 348 public static List populateObjects(List records) throws TorqueException 349 { 350 List results = new ArrayList (records.size()); 351 for (int i = 0; i < records.size(); i++) 353 { 354 Record row = (Record) records.get(i); 355 results.add( 356 BaseJetspeedSkinParameterPeer.row2Object( 357 row, 358 1, 359 BaseJetspeedSkinParameterPeer.getOMClass())); 360 } 361 return results; 362 } 363 371 public static Class getOMClass() throws TorqueException 372 { 373 return CLASS_DEFAULT; 374 } 375 381 public static List doSelect(BaseSkinEntry obj) throws TorqueException 382 { 383 return doSelect(buildCriteria(obj)); 384 } 385 386 public static Criteria buildCriteria(ObjectKey pk) 387 { 388 Criteria criteria = new Criteria(); 389 criteria.add(SKIN_ID, pk); 390 return criteria; 391 } 392 393 public static Criteria buildCriteria(BaseSkinEntry obj) 394 { 395 Criteria criteria = new Criteria(DATABASE_NAME); 396 406 return criteria; 407 } 408 415 public static Parameter retrieveByPK(int pk) throws TorqueException 416 { 417 return retrieveByPK(SimpleKey.keyFor(pk)); 418 } 419 426 public static Parameter retrieveByPK(ObjectKey pk) throws TorqueException 427 { 428 Connection db = null; 429 Parameter retVal = null; 430 try 431 { 432 db = Torque.getConnection(DATABASE_NAME); 433 retVal = retrieveByPK(pk, db); 434 } 435 finally 436 { 437 Torque.closeConnection(db); 438 } 439 return (retVal); 440 } 441 449 public static Parameter retrieveByPK(ObjectKey pk, Connection con) 450 throws TorqueException 451 { 452 Criteria criteria = buildCriteria(pk); 453 List v = doSelect(criteria, con); 454 if (v.size() != 1) 455 { 456 throw new TorqueException("Failed to select one and only one row."); 457 } 458 else 459 { 460 return (Parameter) v.get(0); 461 } 462 } 463 470 public static List retrieveByPKs(List pks) throws TorqueException 471 { 472 Connection db = null; 473 List retVal = null; 474 try 475 { 476 db = Torque.getConnection(DATABASE_NAME); 477 retVal = retrieveByPKs(pks, db); 478 } 479 finally 480 { 481 Torque.closeConnection(db); 482 } 483 return (retVal); 484 } 485 493 public static List retrieveByPKs(List pks, Connection dbcon) 494 throws TorqueException 495 { 496 List objs = null; 497 if (pks == null || pks.size() == 0) 498 { 499 objs = new LinkedList (); 500 } 501 else 502 { 503 Criteria criteria = new Criteria(); 504 criteria.addIn(SKIN_ID, pks); 505 objs = doSelect(criteria, dbcon); 506 } 507 return objs; 508 } 509 516 public static List retrieveById(int pk) throws TorqueException 517 { 518 return retrieveById(SimpleKey.keyFor(pk)); 519 } 520 527 public static List retrieveById(ObjectKey pk) throws TorqueException 528 { 529 Connection db = null; 530 List retVal = null; 531 try 532 { 533 db = Torque.getConnection(DATABASE_NAME); 534 retVal = retrieveById(pk, db); 535 } 536 finally 537 { 538 Torque.closeConnection(db); 539 } 540 return (retVal); 541 } 542 550 public static List retrieveById(ObjectKey pk, Connection con) 551 throws TorqueException 552 { 553 Criteria criteria = buildCriteria(pk); 554 return doSelect(criteria, con); 555 } 556 } 557 | Popular Tags |