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.BaseSecurityReference; 24 import org.apache.jetspeed.om.registry.DBRegistry; 25 import org.apache.jetspeed.om.registry.Parameter; 26 import org.apache.jetspeed.om.registry.base.BaseCategory; 27 import org.apache.jetspeed.om.registry.base.BaseContentURL; 28 import org.apache.jetspeed.om.registry.base.BaseMediaType; 29 import org.apache.jetspeed.om.registry.base.BaseMetaInfo; 30 import org.apache.jetspeed.om.registry.base.BasePortletEntry; 31 import org.apache.jetspeed.om.registry.base.BaseSecurity; 32 import org.apache.jetspeed.services.logging.JetspeedLogFactoryService; 33 import org.apache.jetspeed.services.logging.JetspeedLogger; 34 import org.apache.torque.Torque; 35 import org.apache.torque.TorqueException; 36 import org.apache.torque.om.ObjectKey; 37 import org.apache.torque.om.SimpleKey; 38 import org.apache.torque.util.BasePeer; 39 import org.apache.torque.util.Criteria; 40 41 import com.workingdogs.village.DataSetException; 42 import com.workingdogs.village.QueryDataSet; 43 import com.workingdogs.village.Record; 44 45 51 public class BaseJetspeedPortletPeer extends BasePeer implements DBRegistry 52 { 53 54 57 protected static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(BaseJetspeedPortletPeer.class.getName()); 58 59 60 public static final String TABLE_NAME = "PORTLET"; 61 62 public static final String PORTAL_ID; 63 64 public static final String NAME; 65 66 public static final String HIDDEN; 67 68 public static final String CLASSNAME; 69 70 public static final String TYPE; 71 72 public static final String APPLICATION; 73 74 public static final String PARENT; 75 76 public static final String URL; 77 78 public static final String CACHEDONURL; 79 80 public static final String ROLE; 81 82 public static final String TITLE; 83 84 public static final String DESCRIPTION; 85 86 public static final String IMAGE; 87 88 public static final String SECURITY; 89 90 public static final String LASTUPDATE; 91 static { 92 PORTAL_ID = "PORTLET.ID"; 93 NAME = "PORTLET.NAME"; 94 HIDDEN = "PORTLET.HIDDEN"; 95 CLASSNAME = "PORTLET.CLASSNAME"; 96 TYPE = "PORTLET.TYPE"; 97 APPLICATION = "PORTLET.APPLICATION"; 98 PARENT = "PORTLET.PARENT"; 99 URL = "PORTLET.URL"; 100 CACHEDONURL = "PORTLET.CACHEDONURL"; 101 ROLE = "PORTLET.ROLE"; 102 TITLE = "PORTLET.TITLE"; 103 DESCRIPTION = "PORTLET.DESCRIPTION"; 104 IMAGE = "PORTLET.IMAGE"; 105 SECURITY = "PORTLET.SECURITY"; 106 LASTUPDATE = "PORTLET.LASTUPDATE"; 107 121 } 122 123 public static final int numColumns = 14; 124 125 protected static final String CLASSNAME_DEFAULT = 126 "org.apache.jetspeed.om.registry.base.BasePortletEntry"; 127 128 protected static final Class CLASS_DEFAULT = initClass(CLASSNAME_DEFAULT); 129 135 private static Class initClass(String className) 136 { 137 Class c = null; 138 try 139 { 140 c = Class.forName(className); 141 } 142 catch (Throwable t) 143 { 144 logger.error( 145 "A FATAL ERROR has occurred which should not " 146 + "have happened under any circumstance. Please notify " 147 + "the Turbine developers <turbine-dev@jakarta.apache.org> " 148 + "and give as many details as possible (including the error " 149 + "stack trace).", 150 t); 151 if (t instanceof Error ) 153 { 154 throw (Error ) t.fillInStackTrace(); 155 } 156 } 157 return c; 158 } 159 169 public static List resultSet2Objects(java.sql.ResultSet results) 170 throws TorqueException 171 { 172 try 173 { 174 QueryDataSet qds = null; 175 List rows = null; 176 try 177 { 178 qds = new QueryDataSet(results); 179 rows = getSelectResults(qds); 180 } 181 finally 182 { 183 if (qds != null) 184 { 185 qds.close(); 186 } 187 } 188 return populateObjects(rows); 189 } 190 catch (SQLException e) 191 { 192 throw new TorqueException(e); 193 } 194 catch (DataSetException e) 195 { 196 throw new TorqueException(e); 197 } 198 } 199 206 public static void addSelectColumns(Criteria criteria) 207 throws TorqueException 208 { 209 criteria.addSelectColumn(PORTAL_ID); 210 criteria.addSelectColumn(NAME); 211 criteria.addSelectColumn(HIDDEN); 212 criteria.addSelectColumn(CLASSNAME); 213 criteria.addSelectColumn(TYPE); 214 criteria.addSelectColumn(APPLICATION); 215 criteria.addSelectColumn(PARENT); 216 criteria.addSelectColumn(URL); 217 criteria.addSelectColumn(CACHEDONURL); 218 criteria.addSelectColumn(ROLE); 219 criteria.addSelectColumn(TITLE); 220 criteria.addSelectColumn(DESCRIPTION); 221 criteria.addSelectColumn(IMAGE); 222 criteria.addSelectColumn(SECURITY); 223 } 224 233 public static BasePortletEntry row2Object( 234 Record row, 235 int offset, 236 Class cls) 237 throws TorqueException 238 { 239 try 240 { 241 BasePortletEntry obj = (BasePortletEntry) cls.newInstance(); 242 populateObject(row, offset, obj); 243 return obj; 246 } 247 catch (InstantiationException e) 248 { 249 throw new TorqueException(e); 250 } 251 catch (IllegalAccessException e) 252 { 253 throw new TorqueException(e); 254 } 255 } 256 265 public static void populateObject( 266 Record row, 267 int offset, 268 BasePortletEntry obj) 269 throws TorqueException 270 { 271 try 272 { 273 int id = row.getValue(offset + 0).asInt(); 274 obj.setName(row.getValue(offset + 1).asString()); 275 obj.setHidden(row.getValue(offset + 2).asBoolean()); 276 obj.setClassname(row.getValue(offset + 3).asString()); 277 obj.setType(row.getValue(offset + 4).asString()); 278 obj.setApplication(row.getValue(offset + 5).asBoolean()); 279 obj.setParent(row.getValue(offset + 6).asString()); 280 obj.setURL(row.getValue(offset + 7).asString()); 281 obj.setCachedOnURL(row.getValue(offset + 8).asBoolean()); 282 BaseSecurity security = 284 new BaseSecurity(row.getValue(offset + 9).asString()); 285 obj.setSecurity(security); 286 obj.setBaseSecurity(security); 287 BaseContentURL baseContentURL = new BaseContentURL(); 289 baseContentURL.setCachedOnURL(row.getValue(offset + 8).asBoolean()); 290 baseContentURL.setURL(row.getValue(offset + 7).asString()); 291 obj.setContentURL(baseContentURL); 292 BaseMetaInfo baseMetaInfo = 294 new BaseMetaInfo( 295 row.getValue(offset + 10).asString(), 296 row.getValue(offset + 11).asString(), 297 row.getValue(offset + 12).asString()); 298 obj.setMetaInfo(baseMetaInfo); 299 String securityRef = row.getValue(offset + 13).asString(); 300 if ((securityRef != null) || ("".equals(securityRef))) 301 { 302 BaseSecurityReference paramSecurityRef = 303 new BaseSecurityReference(); 304 paramSecurityRef.setParent(securityRef); 305 obj.setSecurityRef(paramSecurityRef); 306 } 307 buildPortletCategory(id, obj); 308 buildPortletMedia(id, obj); 309 buildPortletParameters(id, obj); 310 } 311 catch (DataSetException e) 312 { 313 throw new TorqueException(e); 314 } 315 } 316 324 public List getXREGDataFromDb() throws TorqueException 325 { 326 Criteria criteria = buildCriteria(); 327 return doSelect(criteria); 328 } 329 public boolean isModified(String lastUpdateDate) 330 { 331 return true; 332 } 333 341 public static List doSelect(Criteria criteria) throws TorqueException 342 { 343 return populateObjects(doSelectVillageRecords(criteria)); 344 } 345 354 public static List doSelect(Criteria criteria, Connection con) 355 throws TorqueException 356 { 357 return populateObjects(doSelectVillageRecords(criteria, con)); 358 } 359 369 public static List doSelectVillageRecords(Criteria criteria) 370 throws TorqueException 371 { 372 return BaseJetspeedPortletPeer.doSelectVillageRecords( 373 criteria, 374 (Connection ) null); 375 } 376 384 public static List doSelectVillageRecords( 385 Criteria criteria, 386 Connection con) 387 throws TorqueException 388 { 389 if (criteria.getSelectColumns().size() == 0) 390 { 391 addSelectColumns(criteria); 392 } 393 if (criteria.getDbName() == Torque.getDefaultDB()) 397 { 398 criteria.setDbName(DATABASE_NAME); 399 } 400 if (con == null) 403 { 404 return BasePeer.doSelect(criteria); 405 } 406 else 407 { 408 return BasePeer.doSelect(criteria, con); 409 } 410 } 411 418 public static List populateObjects(List records) throws TorqueException 419 { 420 List results = new ArrayList (records.size()); 421 for (int i = 0; i < records.size(); i++) 423 { 424 Record row = (Record) records.get(i); 425 results.add( 426 BaseJetspeedPortletPeer.row2Object( 427 row, 428 1, 429 BaseJetspeedPortletPeer.getOMClass())); 430 } 431 return results; 432 } 433 434 public static Criteria buildCriteria(ObjectKey pk) 435 { 436 Criteria criteria = new Criteria(); 437 criteria.add(PORTAL_ID, pk); 438 return criteria; 439 } 440 441 public static Criteria buildCriteria() 442 { 443 Criteria criteria = new Criteria(); 444 return criteria; 445 } 446 454 public static Class getOMClass() throws TorqueException 455 { 456 return CLASS_DEFAULT; 457 } 458 463 public static void buildPortletParameters(int id, BasePortletEntry obj) 464 throws TorqueException 465 { 466 try 467 { 468 List list = 469 BaseJetspeedPortletParameterPeer.retrieveById( 470 SimpleKey.keyFor(id)); 471 for (int i = 0; i < list.size(); i++) 472 { 473 Parameter p = (Parameter) list.get(i); 474 obj.addParameter(p); 475 } 476 } 477 catch (Exception e) 478 { 479 throw new TorqueException(e); 480 } 481 } 482 487 public static void buildPortletMedia(int id, BasePortletEntry obj) 488 throws TorqueException 489 { 490 try 491 { 492 List list = 493 BaseJetspeedPortletMediaTypePeer.retrieveById( 494 SimpleKey.keyFor(id)); 495 for (int i = 0; i < list.size(); i++) 496 { 497 BaseMediaType p = (BaseMediaType) list.get(i); 498 String mediaName = p.getRef(); 499 if (!obj.hasMediaType(mediaName)) 500 { 501 obj.addMediaType(mediaName); 502 } 503 } 504 } 505 catch (Exception e) 506 { 507 throw new TorqueException(e); 508 } 509 } 510 515 public static void buildPortletCategory(int id, BasePortletEntry obj) 516 throws TorqueException 517 { 518 try 519 { 520 List list = 521 BaseJetspeedPortletCategoryPeer.retrieveById( 522 SimpleKey.keyFor(id)); 523 for (int i = 0; i < list.size(); i++) 524 { 525 BaseCategory p = (BaseCategory) list.get(i); 526 String name = p.getName(); 527 String group = p.getGroup(); 528 if (group == null) 529 { 530 if (!obj.hasCategory(name)) 531 obj.addCategory(name); 532 } 533 else 534 if (!obj.hasCategory(name, group)) 535 { 536 obj.addCategory(name, group); 537 } 538 } 539 } 540 catch (Exception e) 541 { 542 throw new TorqueException(e); 543 } 544 } 545 } 546 | Popular Tags |