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.BasePortletEntry; 27 import org.apache.jetspeed.om.registry.base.BaseSecurity; 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 BaseJetspeedPortletParameterPeer extends BasePeer 48 { 49 50 53 protected static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(BaseJetspeedPortletParameterPeer.class.getName()); 54 55 56 public static final String DATABASE_NAME = "default"; 57 58 public static final String TABLE_NAME = "PORTLET_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 CACHEDONNAME; 71 72 public static final String CACHEDONVALUE; 73 74 public static final String ROLE; 75 76 public static final String TITLE; 77 78 public static final String DESCRIPTION; 79 80 public static final String IMAGE; 81 82 public static final String PORTLET_ID; 83 static { 84 ID = "PORTLET_PARAMETER.ID"; 85 NAME = "PORTLET_PARAMETER.NAME"; 86 VALUE = "PORTLET_PARAMETER.VALUE"; 87 HIDDEN = "PORTLET_PARAMETER.HIDDEN"; 88 TYPE = "PORTLET_PARAMETER.TYPE"; 89 CACHEDONNAME = "PORTLET_PARAMETER.CACHEDONNAME"; 90 CACHEDONVALUE = "PORTLET_PARAMETER.CACHEDONVALUE"; 91 ROLE = "PORTLET_PARAMETER.ROLE"; 92 TITLE = "PORTLET_PARAMETER.TITLE"; 93 DESCRIPTION = "PORTLET_PARAMETER.DESCRIPTION"; 94 IMAGE = "PORTLET_PARAMETER.IMAGE"; 95 PORTLET_ID = "PORTLET_PARAMETER.PORTLET_ID"; 96 105 } 106 107 public static final int numColumns = 12; 108 109 protected static final String CLASSNAME_DEFAULT = 110 "org.apache.jetspeed.om.registry.base.BaseParameter"; 111 112 protected static final Class CLASS_DEFAULT = initClass(CLASSNAME_DEFAULT); 113 119 private static Class initClass(String className) 120 { 121 Class c = null; 122 try 123 { 124 c = Class.forName(className); 125 } 126 catch (Throwable t) 127 { 128 logger.error( 129 "A FATAL ERROR has occurred which should not " 130 + "have happened under any circumstance. Please notify " 131 + "the Turbine developers <turbine-dev@jakarta.apache.org> " 132 + "and give as many details as possible (including the error " 133 + "stack trace).", 134 t); 135 if (t instanceof Error ) 137 { 138 throw (Error ) t.fillInStackTrace(); 139 } 140 } 141 return c; 142 } 143 153 public static List resultSet2Objects(java.sql.ResultSet results) 154 throws TorqueException 155 { 156 try 157 { 158 QueryDataSet qds = null; 159 List rows = null; 160 try 161 { 162 qds = new QueryDataSet(results); 163 rows = getSelectResults(qds); 164 } 165 finally 166 { 167 if (qds != null) 168 { 169 qds.close(); 170 } 171 } 172 return populateObjects(rows); 173 } 174 catch (SQLException e) 175 { 176 throw new TorqueException(e); 177 } 178 catch (DataSetException e) 179 { 180 throw new TorqueException(e); 181 } 182 } 183 190 public static void addSelectColumns(Criteria criteria) 191 throws TorqueException 192 { 193 criteria.addSelectColumn(ID); 194 criteria.addSelectColumn(NAME); 195 criteria.addSelectColumn(VALUE); 196 criteria.addSelectColumn(HIDDEN); 197 criteria.addSelectColumn(TYPE); 198 criteria.addSelectColumn(CACHEDONNAME); 199 criteria.addSelectColumn(CACHEDONVALUE); 200 criteria.addSelectColumn(ROLE); 201 criteria.addSelectColumn(TITLE); 202 criteria.addSelectColumn(DESCRIPTION); 203 criteria.addSelectColumn(IMAGE); 204 criteria.addSelectColumn(PORTLET_ID); 205 } 206 215 public static Parameter row2Object(Record row, int offset, Class cls) 216 throws TorqueException 217 { 218 try 219 { 220 Parameter obj = (Parameter) cls.newInstance(); 221 populateObject(row, offset, obj); 222 return obj; 225 } 226 catch (InstantiationException e) 227 { 228 throw new TorqueException(e); 229 } 230 catch (IllegalAccessException e) 231 { 232 throw new TorqueException(e); 233 } 234 } 235 244 public static void populateObject(Record row, int offset, Parameter obj) 245 throws TorqueException 246 { 247 try 248 { 249 obj.setName(row.getValue(offset + 1).asString()); 250 obj.setValue(row.getValue(offset + 2).asString()); 251 obj.setHidden(row.getValue(offset + 3).asBoolean()); 252 obj.setType(row.getValue(offset + 4).asString()); 253 BaseMetaInfo baseMetaInfo = 254 new BaseMetaInfo( 255 row.getValue(offset + 8).asString(), 256 row.getValue(offset + 9).asString(), 257 row.getValue(offset + 10).asString()); 258 obj.setMetaInfo(baseMetaInfo); 259 BaseSecurity security = 261 new BaseSecurity(row.getValue(offset + 7).asString()); 262 obj.setSecurity(security); 263 } 264 catch (DataSetException e) 265 { 266 throw new TorqueException(e); 267 } 268 } 269 277 public static List doSelect(Criteria criteria) throws TorqueException 278 { 279 return populateObjects(doSelectVillageRecords(criteria)); 280 } 281 290 public static List doSelect(Criteria criteria, Connection con) 291 throws TorqueException 292 { 293 return populateObjects(doSelectVillageRecords(criteria, con)); 294 } 295 305 public static List doSelectVillageRecords(Criteria criteria) 306 throws TorqueException 307 { 308 return BaseJetspeedPortletPeer.doSelectVillageRecords( 309 criteria, 310 (Connection ) null); 311 } 312 320 public static List doSelectVillageRecords( 321 Criteria criteria, 322 Connection con) 323 throws TorqueException 324 { 325 if (criteria.getSelectColumns().size() == 0) 326 { 327 addSelectColumns(criteria); 328 } 329 if (criteria.getDbName() == Torque.getDefaultDB()) 333 { 334 criteria.setDbName(DATABASE_NAME); 335 } 336 if (con == null) 339 { 340 return BasePeer.doSelect(criteria); 341 } 342 else 343 { 344 return BasePeer.doSelect(criteria, con); 345 } 346 } 347 354 public static List populateObjects(List records) throws TorqueException 355 { 356 List results = new ArrayList (records.size()); 357 for (int i = 0; i < records.size(); i++) 359 { 360 Record row = (Record) records.get(i); 361 results.add( 362 BaseJetspeedPortletParameterPeer.row2Object( 363 row, 364 1, 365 BaseJetspeedPortletParameterPeer.getOMClass())); 366 } 367 return results; 368 } 369 377 public static Class getOMClass() throws TorqueException 378 { 379 return CLASS_DEFAULT; 380 } 381 387 public static List doSelect(BasePortletEntry obj) throws TorqueException 388 { 389 return doSelect(buildCriteria(obj)); 390 } 391 392 public static Criteria buildCriteria(ObjectKey pk) 393 { 394 Criteria criteria = new Criteria(); 395 criteria.add(PORTLET_ID, pk); 396 return criteria; 397 } 398 399 public static Criteria buildCriteria(BasePortletEntry obj) 400 { 401 Criteria criteria = new Criteria(DATABASE_NAME); 402 412 return criteria; 413 } 414 421 public static Parameter retrieveByPK(int pk) throws TorqueException 422 { 423 return retrieveByPK(SimpleKey.keyFor(pk)); 424 } 425 432 public static Parameter retrieveByPK(ObjectKey pk) throws TorqueException 433 { 434 Connection db = null; 435 Parameter retVal = null; 436 try 437 { 438 db = Torque.getConnection(DATABASE_NAME); 439 retVal = retrieveByPK(pk, db); 440 } 441 finally 442 { 443 Torque.closeConnection(db); 444 } 445 return (retVal); 446 } 447 455 public static Parameter retrieveByPK(ObjectKey pk, Connection con) 456 throws TorqueException 457 { 458 Criteria criteria = buildCriteria(pk); 459 List v = doSelect(criteria, con); 460 if (v.size() != 1) 461 { 462 throw new TorqueException("Failed to select one and only one row."); 463 } 464 else 465 { 466 return (Parameter) v.get(0); 467 } 468 } 469 476 public static List retrieveByPKs(List pks) throws TorqueException 477 { 478 Connection db = null; 479 List retVal = null; 480 try 481 { 482 db = Torque.getConnection(DATABASE_NAME); 483 retVal = retrieveByPKs(pks, db); 484 } 485 finally 486 { 487 Torque.closeConnection(db); 488 } 489 return (retVal); 490 } 491 499 public static List retrieveByPKs(List pks, Connection dbcon) 500 throws TorqueException 501 { 502 List objs = null; 503 if (pks == null || pks.size() == 0) 504 { 505 objs = new LinkedList (); 506 } 507 else 508 { 509 Criteria criteria = new Criteria(); 510 criteria.addIn(PORTLET_ID, pks); 511 objs = doSelect(criteria, dbcon); 512 } 513 return objs; 514 } 515 522 public static List retrieveById(int pk) throws TorqueException 523 { 524 return retrieveById(SimpleKey.keyFor(pk)); 525 } 526 533 public static List retrieveById(ObjectKey pk) throws TorqueException 534 { 535 Connection db = null; 536 List retVal = null; 537 try 538 { 539 db = Torque.getConnection(DATABASE_NAME); 540 retVal = retrieveById(pk, db); 541 } 542 finally 543 { 544 Torque.closeConnection(db); 545 } 546 return (retVal); 547 } 548 556 public static List retrieveById(ObjectKey pk, Connection con) 557 throws TorqueException 558 { 559 Criteria criteria = buildCriteria(pk); 560 return doSelect(criteria, con); 561 } 562 } 563 | Popular Tags |