001: /*
002: * Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
003: *
004: * Project: OpenChronicle
005: *
006: * $Id: EntryDatabaseFactory.java,v 1.6 2007/02/20 02:14:49 bastafidli Exp $
007: *
008: * This program is free software; you can redistribute it and/or modify
009: * it under the terms of the GNU General Public License as published by
010: * the Free Software Foundation; version 2 of the License.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: */
021:
022: package org.opensubsystems.blog.persist.db;
023:
024: import java.sql.Connection;
025: import java.sql.PreparedStatement;
026: import java.sql.ResultSet;
027: import java.sql.SQLException;
028: import java.sql.Timestamp;
029: import java.util.Collection;
030:
031: import org.opensubsystems.blog.data.Entry;
032: import org.opensubsystems.blog.persist.EntryFactory;
033: import org.opensubsystems.core.data.DataConstant;
034: import org.opensubsystems.core.data.DataObject;
035: import org.opensubsystems.core.data.ModifiableDataObject;
036: import org.opensubsystems.core.error.OSSDatabaseAccessException;
037: import org.opensubsystems.core.error.OSSException;
038: import org.opensubsystems.core.persist.db.DatabaseCreateMultipleDataObjectsOperation;
039: import org.opensubsystems.core.persist.db.DatabaseCreateSingleDataObjectOperation;
040: import org.opensubsystems.core.persist.db.DatabaseDeleteSingleDataObjectOperation;
041: import org.opensubsystems.core.persist.db.DatabaseFactoryImpl;
042: import org.opensubsystems.core.persist.db.DatabaseReadOperation;
043: import org.opensubsystems.core.persist.db.DatabaseReadSingleDataObjectOperation;
044: import org.opensubsystems.core.persist.db.DatabaseSchemaManager;
045: import org.opensubsystems.core.persist.db.DatabaseUpdateOperation;
046: import org.opensubsystems.core.persist.db.DatabaseUpdateSingleDataObjectOperation;
047: import org.opensubsystems.core.persist.db.ModifiableDatabaseFactory;
048: import org.opensubsystems.core.util.CallContext;
049: import org.opensubsystems.core.util.DatabaseUtils;
050: import org.opensubsystems.core.util.GlobalConstants;
051: import org.opensubsystems.patterns.listdata.persist.db.impl.ListDatabaseFactoryImpl;
052:
053: /**
054: * Data factory to retrieve and manipulate entries in the persistence store.
055: *
056: * @version $Id: EntryDatabaseFactory.java,v 1.6 2007/02/20 02:14:49 bastafidli Exp $
057: * @author Miro Halas
058: * @code.reviewer Miro Halas
059: * @code.reviewed 1.9 2006/07/26 00:27:07 jlegeny
060: */
061: public class EntryDatabaseFactory extends ListDatabaseFactoryImpl
062: implements EntryFactory, ModifiableDatabaseFactory {
063: // Cached values ////////////////////////////////////////////////////////////
064:
065: /**
066: * Schema to use to execute database dependent operations.
067: */
068: protected BlogDatabaseSchema m_schema;
069:
070: // Constructors /////////////////////////////////////////////////////////////
071:
072: /**
073: * Default constructor.
074: *
075: * @throws OSSException - an error has occured
076: */
077: public EntryDatabaseFactory() throws OSSException {
078: super (DataConstant.BLOGENTRY_DATA_TYPE,
079: Entry.DEFAULT_LIST_SORT_COLUMNS,
080: Entry.DEFAULT_LIST_SORT_ORDER,
081: Entry.DEFAULT_LIST_COLUMNS,
082: ((BlogDatabaseSchema) DatabaseSchemaManager
083: .getInstance(BlogDatabaseSchema.class)));
084: // As a convenience we cast it to correct type
085: m_schema = ((BlogDatabaseSchema) getListDatabaseSchema());
086: }
087:
088: /**
089: * {@inheritDoc}
090: */
091: public int setValuesForInsert(PreparedStatement insertStatement,
092: DataObject data, int iIndex) throws OSSException,
093: SQLException {
094: Entry entry = (Entry) data;
095:
096: // Here you must pass the domain id sent to you in blog object
097: // If you want to check if this id is the same as current domain id
098: // do it at the controller level.
099:
100: insertStatement.setInt(iIndex++, entry.getDomainId());
101: insertStatement.setInt(iIndex++, entry.getParentId());
102: insertStatement.setString(iIndex++, entry.getCaption());
103: insertStatement.setString(iIndex++, entry.getComments());
104: insertStatement.setString(iIndex++, entry.getImageURL());
105: insertStatement.setString(iIndex++, entry.getTargetURL());
106:
107: return iIndex;
108: }
109:
110: /**
111: * {@inheritDoc}
112: */
113: public int setValuesForUpdate(PreparedStatement updateStatement,
114: DataObject data, int iIndex) throws OSSException,
115: SQLException {
116: Entry entry = (Entry) data;
117:
118: updateStatement.setString(iIndex++, entry.getCaption());
119: updateStatement.setString(iIndex++, entry.getComments());
120: updateStatement.setString(iIndex++, entry.getImageURL());
121: updateStatement.setString(iIndex++, entry.getTargetURL());
122: updateStatement.setInt(iIndex++, entry.getId());
123: // Here you must pass the domain id sent to you in user object
124: // If you want to check if this id is the same as current domain id
125: // do it at the controller level. Otherwise the test fails if run
126: // on the empty database
127: updateStatement.setInt(iIndex++, entry.getDomainId());
128: updateStatement.setTimestamp(iIndex++, entry
129: .getModificationTimestamp());
130:
131: return iIndex;
132: }
133:
134: // Basic operations /////////////////////////////////////////////////////////
135:
136: /**
137: * {@inheritDoc}
138: */
139: public DataObject load(ResultSet rsQueryResults, int initialIndex)
140: throws OSSDatabaseAccessException {
141: Entry data;
142:
143: try {
144: // The order must exactly match the order in COLUMNS constant
145: data = new Entry(rsQueryResults.getInt(initialIndex),
146: rsQueryResults.getInt(initialIndex + 1),
147: rsQueryResults.getInt(initialIndex + 2),
148: rsQueryResults.getString(initialIndex + 3),
149: rsQueryResults.getString(initialIndex + 4),
150: rsQueryResults.getString(initialIndex + 5),
151: rsQueryResults.getString(initialIndex + 6),
152: rsQueryResults.getTimestamp(initialIndex + 7),
153: rsQueryResults.getTimestamp(initialIndex + 8));
154: data.setFromPersistenceStore();
155: } catch (SQLException sqleExc) {
156: throw new OSSDatabaseAccessException(
157: "Failed to load data from the database.", sqleExc);
158: }
159:
160: return data;
161: }
162:
163: /**
164: * {@inheritDoc}
165: */
166: public DataObject get(final int iId, final int iDomainId)
167: throws OSSException {
168: DataObject data = null;
169:
170: // If the ID is supplied try to read the data from the database, if it is not,
171: // it is new blog which doesn't have ID yet
172: if (iId == DataObject.NEW_ID) {
173: // These values are used as default values for new blog
174: data = new Entry(iDomainId);
175: } else {
176: DatabaseReadOperation dbop = new DatabaseReadSingleDataObjectOperation(
177: this ,
178: m_schema
179: .getSelectEntryById(BlogDatabaseSchema.ENTRY_COLUMNS),
180: m_schema, iId, iDomainId);
181: data = (DataObject) dbop.executeRead();
182: }
183:
184: return data;
185: }
186:
187: /**
188: * {@inheritDoc}
189: */
190: public DataObject create(final DataObject data) throws OSSException {
191: if (GlobalConstants.ERROR_CHECKING) {
192: assert data.getId() == DataObject.NEW_ID : "Cannot create already created data.";
193: }
194:
195: DatabaseUpdateOperation dbop = new DatabaseCreateSingleDataObjectOperation(
196: this , m_schema.getInsertEntryAndFetchGeneratedValues(),
197: m_schema, data);
198: dbop.executeUpdate();
199:
200: return (DataObject) dbop.getReturnData();
201: }
202:
203: /**
204: * {@inheritDoc}
205: */
206: public int create(Collection colDataObject) throws OSSException {
207: if (GlobalConstants.ERROR_CHECKING) {
208: assert colDataObject != null && !colDataObject.isEmpty() : "Cannot create empty data list.";
209: }
210:
211: DatabaseUpdateOperation dbop = new DatabaseCreateMultipleDataObjectsOperation(
212: this , m_schema.getInsertEntry(), m_schema,
213: colDataObject, false);
214: dbop.executeUpdate();
215:
216: return ((Integer) dbop.getReturnData()).intValue();
217: }
218:
219: /**
220: * {@inheritDoc}
221: */
222: public void delete(final int iId, final int iDomainId)
223: throws OSSException {
224: if (GlobalConstants.ERROR_CHECKING) {
225: assert iId != DataObject.NEW_ID : "Cannot delete data, which wasn't created yet.";
226: }
227:
228: DatabaseUpdateOperation dbop = new DatabaseDeleteSingleDataObjectOperation(
229: this , m_schema.getDeleteEntryById(), m_schema, iId,
230: iDomainId);
231: dbop.executeUpdate();
232: }
233:
234: // Extended operations //////////////////////////////////////////////////////
235:
236: /**
237: * {@inheritDoc}
238: */
239: public ModifiableDataObject save(final ModifiableDataObject data)
240: throws OSSException {
241: if (GlobalConstants.ERROR_CHECKING) {
242: assert data != null : "Data can not be null";
243: assert data.getId() != DataObject.NEW_ID : "Cannot save data which wasn't created yet.";
244: }
245:
246: DatabaseUpdateOperation dbop = new DatabaseUpdateSingleDataObjectOperation(
247: this , m_schema.getUpdateEntryAndFetchGeneratedValues(),
248: m_schema, data);
249: dbop.executeUpdate();
250:
251: return (ModifiableDataObject) dbop.getReturnData();
252: }
253:
254: // Operations specific to this factory //////////////////////////////////////
255:
256: /**
257: * {@inheritDoc}
258: */
259: public Entry getLast(final int iBlogId) throws OSSException {
260: DatabaseReadOperation dbop = new DatabaseReadOperation(
261: this ,
262: m_schema
263: .getSelectLastEntry(BlogDatabaseSchema.ENTRY_COLUMNS),
264: m_schema) {
265: protected Object performOperation(
266: DatabaseFactoryImpl dbfactory,
267: Connection cntConnection,
268: PreparedStatement pstmQuery) throws OSSException,
269: SQLException {
270: int iDomainId = CallContext.getInstance()
271: .getCurrentDomainId();
272: pstmQuery.setInt(1, iBlogId);
273: pstmQuery.setInt(2, iDomainId);
274: return DatabaseUtils
275: .loadAtMostOneData(dbfactory, pstmQuery,
276: "Multiple records loaded from database for domain Id "
277: + iDomainId + " and blog Id "
278: + iBlogId);
279: }
280: };
281: return (Entry) dbop.executeRead();
282: }
283:
284: // List operations //////////////////////////////////////////////////////////
285:
286: /**
287: * {@inheritDoc}
288: */
289: public DataObject load(ResultSet rsQueryResults,
290: int[] selectedColumns, int initialIndex)
291: throws OSSDatabaseAccessException {
292: Entry data;
293:
294: try {
295: int id = DataObject.NEW_ID;
296: int domainId = DataObject.NEW_ID;
297: int parentId = DataObject.NEW_ID;
298: String caption = "";
299: String comments = "";
300: String imageUrl = "";
301: String targetUrl = "";
302: Timestamp created = null;
303: Timestamp modified = null;
304:
305: for (int columnCount = initialIndex; columnCount < (initialIndex + selectedColumns.length); columnCount++) {
306: switch (selectedColumns[columnCount - 1]) {
307: case (Entry.COL_BLOGENTRY_ID): {
308: id = rsQueryResults.getInt(columnCount);
309: break;
310: }
311: case (Entry.COL_BLOGENTRY_DOMAIN_ID): {
312: domainId = rsQueryResults.getInt(columnCount);
313: break;
314: }
315: case (Entry.COL_BLOGENTRY_PARENT_ID): {
316: parentId = rsQueryResults.getInt(columnCount);
317: break;
318: }
319: case (Entry.COL_BLOGENTRY_CAPTION): {
320: caption = rsQueryResults.getString(columnCount);
321: break;
322: }
323: case (Entry.COL_BLOGENTRY_COMMENTS): {
324: comments = rsQueryResults.getString(columnCount);
325: break;
326: }
327: case (Entry.COL_BLOGENTRY_IMAGEURL): {
328: imageUrl = rsQueryResults.getString(columnCount);
329: break;
330: }
331: case (Entry.COL_BLOGENTRY_TARGETURL): {
332: targetUrl = rsQueryResults.getString(columnCount);
333: break;
334: }
335: case (Entry.COL_BLOGENTRY_CREATION_DATE): {
336: created = rsQueryResults.getTimestamp(columnCount);
337: break;
338: }
339: case (Entry.COL_BLOGENTRY_MODIFICATION_DATE): {
340: modified = rsQueryResults.getTimestamp(columnCount);
341: break;
342: }
343: default: {
344: assert false : "Unknown column ID "
345: + selectedColumns[columnCount - 1];
346: }
347: }
348: }
349:
350: data = new Entry(id, domainId, parentId, caption, comments,
351: imageUrl, targetUrl, created, modified);
352: data.setFromPersistenceStore();
353: } catch (SQLException sqleExc) {
354: throw new OSSDatabaseAccessException(
355: "Failed to load data from the database.", sqleExc);
356: }
357:
358: return data;
359: }
360: }
|