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.List ; 22 23 import org.apache.jetspeed.om.registry.DBRegistry; 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 42 48 public class BaseJetspeedSkinPeer extends BasePeer implements DBRegistry 49 { 50 51 54 protected static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(BaseJetspeedSkinPeer.class.getName()); 55 56 57 public static final String TABLE_NAME = "SKIN"; 58 59 public static final String ID; 60 61 public static final String NAME; 62 63 public static final String HIDDEN; 64 65 public static final String ROLE; 66 67 public static final String TITLE; 68 69 public static final String DESCRIPTION; 70 71 public static final String IMAGE; 72 static { 73 ID = "SKIN.ID"; 74 NAME = "SKIN.NAME"; 75 HIDDEN = "SKIN.HIDDEN"; 76 ROLE = "SKIN.ROLE"; 77 TITLE = "SKIN.TITLE"; 78 DESCRIPTION = "SKIN.DESCRIPTION"; 79 IMAGE = "SKIN.IMAGE"; 80 94 } 95 96 public static final int numColumns = 7; 97 98 protected static final String CLASSNAME_DEFAULT = 99 "org.apache.jetspeed.om.registry.base.BaseSkinEntry"; 100 101 protected static final Class CLASS_DEFAULT = initClass(CLASSNAME_DEFAULT); 102 108 private static Class initClass(String className) 109 { 110 Class c = null; 111 try 112 { 113 c = Class.forName(className); 114 } 115 catch (Throwable t) 116 { 117 logger.error( 118 "A FATAL ERROR has occurred which should not " 119 + "have happened under any circumstance. Please notify " 120 + "the Turbine developers <turbine-dev@jakarta.apache.org> " 121 + "and give as many details as possible (including the error " 122 + "stack trace).", 123 t); 124 if (t instanceof Error ) 126 { 127 throw (Error ) t.fillInStackTrace(); 128 } 129 } 130 return c; 131 } 132 142 public static List resultSet2Objects(java.sql.ResultSet results) 143 throws TorqueException 144 { 145 try 146 { 147 QueryDataSet qds = null; 148 List rows = null; 149 try 150 { 151 qds = new QueryDataSet(results); 152 rows = getSelectResults(qds); 153 } 154 finally 155 { 156 if (qds != null) 157 { 158 qds.close(); 159 } 160 } 161 return populateObjects(rows); 162 } 163 catch (SQLException e) 164 { 165 throw new TorqueException(e); 166 } 167 catch (DataSetException e) 168 { 169 throw new TorqueException(e); 170 } 171 } 172 179 public static void addSelectColumns(Criteria criteria) 180 throws TorqueException 181 { 182 criteria.addSelectColumn(ID); 183 criteria.addSelectColumn(NAME); 184 criteria.addSelectColumn(HIDDEN); 185 criteria.addSelectColumn(ROLE); 186 criteria.addSelectColumn(TITLE); 187 criteria.addSelectColumn(DESCRIPTION); 188 criteria.addSelectColumn(IMAGE); 189 } 190 199 public static BaseSkinEntry row2Object(Record row, int offset, Class cls) 200 throws TorqueException 201 { 202 try 203 { 204 BaseSkinEntry obj = (BaseSkinEntry) cls.newInstance(); 205 populateObject(row, offset, obj); 206 return obj; 207 } 208 catch (InstantiationException e) 209 { 210 throw new TorqueException(e); 211 } 212 catch (IllegalAccessException e) 213 { 214 throw new TorqueException(e); 215 } 216 } 217 226 public static void populateObject( 227 Record row, 228 int offset, 229 BaseSkinEntry obj) 230 throws TorqueException 231 { 232 try 233 { 234 int id = row.getValue(offset + 0).asInt(); 235 obj.setName(row.getValue(offset + 1).asString()); 236 obj.setHidden(row.getValue(offset + 2).asBoolean()); 237 obj.setTitle(row.getValue(offset + 4).asString()); 238 obj.setDescription(row.getValue(offset + 5).asString()); 239 BaseSecurity security = 241 new BaseSecurity(row.getValue(offset + 3).asString()); 242 obj.setSecurity(security); 243 obj.setBaseSecurity(security); 244 BaseMetaInfo baseMetaInfo = 246 new BaseMetaInfo( 247 row.getValue(offset + 4).asString(), 248 row.getValue(offset + 5).asString(), 249 row.getValue(offset + 6).asString()); 250 obj.setMetaInfo(baseMetaInfo); 251 buildSkinParameters(id, obj); 252 } 253 catch (DataSetException e) 254 { 255 throw new TorqueException(e); 256 } 257 } 258 266 public List getXREGDataFromDb() throws TorqueException 267 { 268 Criteria criteria = buildCriteria(); 269 return doSelect(criteria); 270 } 271 public boolean isModified(String lastUpdateDate) 272 { 273 return true; 274 } 275 283 public static List doSelect(Criteria criteria) throws TorqueException 284 { 285 return populateObjects(doSelectVillageRecords(criteria)); 286 } 287 296 public static List doSelect(Criteria criteria, Connection con) 297 throws TorqueException 298 { 299 return populateObjects(doSelectVillageRecords(criteria, con)); 300 } 301 311 public static List doSelectVillageRecords(Criteria criteria) 312 throws TorqueException 313 { 314 return BaseJetspeedSkinPeer.doSelectVillageRecords( 315 criteria, 316 (Connection ) null); 317 } 318 326 public static List doSelectVillageRecords( 327 Criteria criteria, 328 Connection con) 329 throws TorqueException 330 { 331 if (criteria.getSelectColumns().size() == 0) 332 { 333 addSelectColumns(criteria); 334 } 335 if (criteria.getDbName() == Torque.getDefaultDB()) 339 { 340 criteria.setDbName(DATABASE_NAME); 341 } 342 if (con == null) 345 { 346 return BasePeer.doSelect(criteria); 347 } 348 else 349 { 350 return BasePeer.doSelect(criteria, con); 351 } 352 } 353 360 public static List populateObjects(List records) throws TorqueException 361 { 362 List results = new ArrayList (records.size()); 363 for (int i = 0; i < records.size(); i++) 365 { 366 Record row = (Record) records.get(i); 367 results.add( 368 BaseJetspeedSkinPeer.row2Object( 369 row, 370 1, 371 BaseJetspeedSkinPeer.getOMClass())); 372 } 373 return results; 374 } 375 376 public static Criteria buildCriteria(ObjectKey pk) 377 { 378 Criteria criteria = new Criteria(); 379 criteria.add(ID, pk); 380 return criteria; 381 } 382 383 public static Criteria buildCriteria() 384 { 385 Criteria criteria = new Criteria(); 386 return criteria; 387 } 388 396 public static Class getOMClass() throws TorqueException 397 { 398 return CLASS_DEFAULT; 399 } 400 405 public static void buildSkinParameters(int id, BaseSkinEntry obj) 406 throws TorqueException 407 { 408 try 409 { 410 List list = 411 BaseJetspeedSkinParameterPeer.retrieveById( 412 SimpleKey.keyFor(id)); 413 if (list != null) 414 for (int i = 0; i < list.size(); i++) 415 { 416 Parameter p = (Parameter) list.get(i); 417 if (obj.getParameter(p.getName()) == null) 418 obj.addParameter(p); 419 } 420 } 421 catch (Exception e) 422 { 423 throw new TorqueException(e); 424 } 425 } 426 } 427 | Popular Tags |