1/*2 * Copyright 2000-2004 The Apache Software Foundation.3 * 4 * Licensed under the Apache License, Version 2.0 (the "License");5 * you may not use this file except in compliance with the License.6 * You may obtain a copy of the License at7 * 8 * http://www.apache.org/licenses/LICENSE-2.09 * 10 * Unless required by applicable law or agreed to in writing, software11 * distributed under the License is distributed on an "AS IS" BASIS,12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13 * See the License for the specific language governing permissions and14 * limitations under the License.15 */16packageorg.apache.jetspeed.om.registry.database;
1718import java.sql.Connection;
19import java.sql.SQLException;
20import java.util.ArrayList;
21import java.util.List;
2223import org.apache.jetspeed.om.registry.DBRegistry;
24import org.apache.jetspeed.om.registry.Parameter;
25import org.apache.jetspeed.om.registry.base.BaseMetaInfo;
26import org.apache.jetspeed.om.registry.base.BaseSecurity;
27import org.apache.jetspeed.om.registry.base.BaseSkinEntry;
28import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
29import org.apache.jetspeed.services.logging.JetspeedLogger;
30import org.apache.torque.Torque;
31import org.apache.torque.TorqueException;
32import org.apache.torque.om.ObjectKey;
33import org.apache.torque.om.SimpleKey;
34import org.apache.torque.util.BasePeer;
35import org.apache.torque.util.Criteria;
3637import com.workingdogs.village.DataSetException;
38import com.workingdogs.village.QueryDataSet;
39import com.workingdogs.village.Record;
404142/***43 * Base Peer for Skin registry entries.44 * 45 * @author <a href="mailto:susinha@cisco.com">Suchisubhra Sinha</a>46 * @version $Id: BaseJetspeedSkinPeer.java,v 1.3 2004/04/06 23:00:16 morciuch Exp $47 */48publicclassBaseJetspeedSkinPeerextends BasePeer implements DBRegistry49 {
5051/***52 * Static initialization of the logger for this class53 */54protectedstaticfinalJetspeedLogger logger = JetspeedLogFactoryService.getLogger(BaseJetspeedSkinPeer.class.getName());
5556/*** the table name for this class */57publicstaticfinal String TABLE_NAME = "SKIN";
58/*** the column name for the PORTAL_ID field */59publicstaticfinal String ID;
60/*** the column name for the NAME field */61publicstaticfinal String NAME;
62/*** the column name for the HIDDEN field */63publicstaticfinal String HIDDEN;
64/*** the column name for the ROLE field */65publicstaticfinal String ROLE;
66/*** the column name for the TITLE field */67publicstaticfinal String TITLE;
68/*** the column name for the DESCRIPTION field */69publicstaticfinal String DESCRIPTION;
70/*** the column name for the IMAGE field */71publicstaticfinal String IMAGE;
72static {
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/*81 if (Torque.isInit())82 {83 try84 {85 getMapBuilder();86 }87 catch (Exception e)88 {8990 category.error("Could not initialize Peer", e);91 }92 }93 */94 }
95/*** number of columns for this peer */96publicstaticfinalint numColumns = 7;
97/*** A class that can be returned by this peer. */98protectedstaticfinal String CLASSNAME_DEFAULT =
99"org.apache.jetspeed.om.registry.base.BaseSkinEntry";
100/*** A class that can be returned by this peer. */101protectedstaticfinal Class CLASS_DEFAULT = initClass(CLASSNAME_DEFAULT);
102/***103 * Class object initialization method.104 *105 * @param className name of the class to initialize106 * @return the initialized class107 */108privatestatic Class initClass(String className)
109 {
110 Class c = null;
111try112 {
113 c = Class.forName(className);
114 }
115catch (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// Error objects should always be propogated.125if (t instanceof Error)
126 {
127throw (Error) t.fillInStackTrace();
128 }
129 }
130return c;
131 }
132/***133 * Get the list of objects for a ResultSet. Please not that your134 * resultset MUST return columns in the right order. You can use135 * getFieldNames() in BaseObject to get the correct sequence.136 *137 * @param results the ResultSet138 * @return the list of objects139 * @throws TorqueException Any exceptions caught during processing will be140 * rethrown wrapped into a TorqueException.141 */142publicstatic List resultSet2Objects(java.sql.ResultSet results)
143 throws TorqueException
144 {
145try146 {
147 QueryDataSet qds = null;
148 List rows = null;
149try150 {
151 qds = new QueryDataSet(results);
152 rows = getSelectResults(qds);
153 }
154finally155 {
156if (qds != null)
157 {
158 qds.close();
159 }
160 }
161return populateObjects(rows);
162 }
163catch (SQLException e)
164 {
165thrownew TorqueException(e);
166 }
167catch (DataSetException e)
168 {
169thrownew TorqueException(e);
170 }
171 }
172/***173 * Add all the columns needed to create a new object.174 *175 * @param criteria object containing the columns to add.176 * @throws TorqueException Any exceptions caught during processing will be177 * rethrown wrapped into a TorqueException.178 */179publicstaticvoid 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/***191 * Create a new object of type cls from a resultset row starting192 * from a specified offset. This is done so that you can select193 * other rows than just those needed for this object. You may194 * for example want to create two objects from the same row.195 *196 * @throws TorqueException Any exceptions caught during processing will be197 * rethrown wrapped into a TorqueException.198 */199publicstaticBaseSkinEntry row2Object(Record row, int offset, Class cls)
200 throws TorqueException
201 {
202try203 {
204BaseSkinEntry obj = (BaseSkinEntry) cls.newInstance();
205 populateObject(row, offset, obj);
206return obj;
207 }
208catch (InstantiationException e)
209 {
210thrownew TorqueException(e);
211 }
212catch (IllegalAccessException e)
213 {
214thrownew TorqueException(e);
215 }
216 }
217/***218 * Populates an object from a resultset row starting219 * from a specified offset. This is done so that you can select220 * other rows than just those needed for this object. You may221 * for example want to create two objects from the same row.222 *223 * @throws TorqueException Any exceptions caught during processing will be224 * rethrown wrapped into a TorqueException.225 */226publicstaticvoid populateObject(
227 Record row,
228int offset,
229BaseSkinEntry obj)
230 throws TorqueException
231 {
232try233 {
234int 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//set the security240BaseSecurity security =
241newBaseSecurity(row.getValue(offset + 3).asString());
242 obj.setSecurity(security);
243 obj.setBaseSecurity(security);
244//create meta info245BaseMetaInfo baseMetaInfo =
246newBaseMetaInfo(
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 }
253catch (DataSetException e)
254 {
255thrownew TorqueException(e);
256 }
257 }
258/***259 * Method to get regsitered data from database.260 *261 * @param criteria object used to create the SELECT statement.262 * @return List of selected Objects263 * @throws TorqueException Any exceptions caught during processing will be264 * rethrown wrapped into a TorqueException.265 */266public List getXREGDataFromDb() throws TorqueException
267 {
268 Criteria criteria = buildCriteria();
269return doSelect(criteria);
270 }
271publicboolean isModified(String lastUpdateDate)
272 {
273returntrue;
274 }
275/***276 * Method to do selects.277 *278 * @param criteria object used to create the SELECT statement.279 * @return List of selected Objects280 * @throws TorqueException Any exceptions caught during processing will be281 * rethrown wrapped into a TorqueException.282 */283publicstatic List doSelect(Criteria criteria) throws TorqueException
284 {
285return populateObjects(doSelectVillageRecords(criteria));
286 }
287/***288 * Method to do selects within a transaction.289 *290 * @param criteria object used to create the SELECT statement.291 * @param con the connection to use292 * @return List of selected Objects293 * @throws TorqueException Any exceptions caught during processing will be294 * rethrown wrapped into a TorqueException.295 */296publicstatic List doSelect(Criteria criteria, Connection con)
297 throws TorqueException
298 {
299return populateObjects(doSelectVillageRecords(criteria, con));
300 }
301/***302 * Grabs the raw Village records to be formed into objects.303 * This method handles connections internally. The Record objects304 * returned by this method should be considered readonly. Do not305 * alter the data and call save(), your results may vary, but are306 * certainly likely to result in hard to track MT bugs.307 *308 * @throws TorqueException Any exceptions caught during processing will be309 * rethrown wrapped into a TorqueException.310 */311publicstatic List doSelectVillageRecords(Criteria criteria)
312 throws TorqueException
313 {
314return BaseJetspeedSkinPeer.doSelectVillageRecords(
315 criteria,
316 (Connection) null);
317 }
318/***319 * Grabs the raw Village records to be formed into objects.320 * This method should be used for transactions321 *322 * @param con the connection to use323 * @throws TorqueException Any exceptions caught during processing will be324 * rethrown wrapped into a TorqueException.325 */326publicstatic List doSelectVillageRecords(
327 Criteria criteria,
328 Connection con)
329 throws TorqueException
330 {
331if (criteria.getSelectColumns().size() == 0)
332 {
333 addSelectColumns(criteria);
334 }
335// Set the correct dbName if it has not been overridden336// criteria.getDbName will return the same object if not set to337// another value so == check is okay and faster338if (criteria.getDbName() == Torque.getDefaultDB())
339 {
340 criteria.setDbName(DATABASE_NAME);
341 }
342// BasePeer returns a List of Value (Village) arrays. The array343// order follows the order columns were placed in the Select clause.344if (con == null)
345 {
346return BasePeer.doSelect(criteria);
347 }
348else349 {
350return BasePeer.doSelect(criteria, con);
351 }
352 }
353/***354 * The returned List will contain objects of the default type or355 * objects that inherit from the default.356 *357 * @throws TorqueException Any exceptions caught during processing will be358 * rethrown wrapped into a TorqueException.359 */360publicstatic List populateObjects(List records) throws TorqueException
361 {
362 List results = new ArrayList(records.size());
363// populate the object(s)364for (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 }
373return results;
374 }
375/*** Build a Criteria object from an ObjectKey */376publicstatic Criteria buildCriteria(ObjectKey pk)
377 {
378 Criteria criteria = new Criteria();
379 criteria.add(ID, pk);
380return criteria;
381 }
382/*** Build a Criteria object */383publicstatic Criteria buildCriteria()
384 {
385 Criteria criteria = new Criteria();
386return criteria;
387 }
388/***389 * The class that the Peer will make instances of.390 * If the BO is abstract then you must implement this method391 * in the BO.392 *393 * @throws TorqueException Any exceptions caught during processing will be394 * rethrown wrapped into a TorqueException.395 */396publicstatic Class getOMClass() throws TorqueException
397 {
398return CLASS_DEFAULT;
399 }
400/***401 * it will make the parameters for this portlet402 * @param portlet object.403 * 404 */405publicstaticvoid buildSkinParameters(int id, BaseSkinEntry obj)
406 throws TorqueException
407 {
408try409 {
410 List list =
411 BaseJetspeedSkinParameterPeer.retrieveById(
412 SimpleKey.keyFor(id));
413if (list != null)
414for (int i = 0; i < list.size(); i++)
415 {
416Parameter p = (Parameter) list.get(i);
417if (obj.getParameter(p.getName()) == null)
418 obj.addParameter(p);
419 }
420 }
421catch (Exception e)
422 {
423thrownew TorqueException(e);
424 }
425 }
426 }