KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opensubsystems > blog > persist > db > EntryDatabaseFactory


1 /*
2  * Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
3  *
4  * Project: OpenChronicle
5  *
6  * $Id: EntryDatabaseFactory.java,v 1.15 2007/02/01 07:34:37 bastafidli Exp $
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; version 2 of the License.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */

21
22 package org.opensubsystems.blog.persist.db;
23
24 import java.sql.Connection JavaDoc;
25 import java.sql.PreparedStatement JavaDoc;
26 import java.sql.ResultSet JavaDoc;
27 import java.sql.SQLException JavaDoc;
28 import java.util.Collection JavaDoc;
29 import java.util.List JavaDoc;
30
31 import org.opensubsystems.blog.data.Entry;
32 import org.opensubsystems.blog.persist.EntryFactory;
33 import org.opensubsystems.core.data.DataConstant;
34 import org.opensubsystems.core.data.DataObject;
35 import org.opensubsystems.core.data.ModifiableDataObject;
36 import org.opensubsystems.core.error.OSSDatabaseAccessException;
37 import org.opensubsystems.core.error.OSSException;
38 import org.opensubsystems.core.persist.db.DatabaseCreateMultipleDataObjectsOperation;
39 import org.opensubsystems.core.persist.db.DatabaseCreateSingleDataObjectOperation;
40 import org.opensubsystems.core.persist.db.DatabaseDeleteSingleDataObjectOperation;
41 import org.opensubsystems.core.persist.db.DatabaseFactoryImpl;
42 import org.opensubsystems.core.persist.db.DatabaseReadMultipleOperation;
43 import org.opensubsystems.core.persist.db.DatabaseReadOperation;
44 import org.opensubsystems.core.persist.db.DatabaseReadSingleDataObjectOperation;
45 import org.opensubsystems.core.persist.db.DatabaseSchemaManager;
46 import org.opensubsystems.core.persist.db.DatabaseUpdateOperation;
47 import org.opensubsystems.core.persist.db.DatabaseUpdateSingleDataObjectOperation;
48 import org.opensubsystems.core.persist.db.ModifiableDatabaseFactory;
49 import org.opensubsystems.core.util.CallContext;
50 import org.opensubsystems.core.util.DatabaseUtils;
51 import org.opensubsystems.core.util.GlobalConstants;
52
53 /**
54  * Data factory to retrieve and manipulate entries in the persistence store.
55  *
56  * @version $Id: EntryDatabaseFactory.java,v 1.15 2007/02/01 07:34:37 bastafidli Exp $
57  * @author Miro Halas
58  * @code.reviewer Miro Halas
59  * @code.reviewed 1.9 2006/07/26 00:27:07 jlegeny
60  */

61 public class EntryDatabaseFactory extends DatabaseFactoryImpl
62                                   implements EntryFactory,
63                                              ModifiableDatabaseFactory
64 {
65    // Cached values ////////////////////////////////////////////////////////////
66

67    /**
68     * Schema to use to execute database dependent operations.
69     */

70    protected BlogDatabaseSchema m_schema;
71    
72    // Constructors /////////////////////////////////////////////////////////////
73

74    /**
75     * Default constructor.
76     *
77     * @throws OSSException - an error has occured
78     */

79    public EntryDatabaseFactory(
80    ) throws OSSException
81    {
82       super(DataConstant.BLOGENTRY_DATA_TYPE);
83
84       m_schema = ((BlogDatabaseSchema)DatabaseSchemaManager.getInstance(
85                                          BlogDatabaseSchema.class));
86    }
87    
88    /**
89     * {@inheritDoc}
90     */

91    public int setValuesForInsert(
92       PreparedStatement JavaDoc insertStatement,
93       DataObject data,
94       int iIndex
95    ) throws OSSException,
96             SQLException JavaDoc
97    {
98       Entry entry = (Entry)data;
99       
100       // Here you must pass the domain id sent to you in blog object
101
// If you want to check if this id is the same as current domain id
102
// do it at the controller level.
103

104       insertStatement.setInt(iIndex++, entry.getDomainId());
105       insertStatement.setInt(iIndex++, entry.getParentId());
106       insertStatement.setString(iIndex++, entry.getCaption());
107       insertStatement.setString(iIndex++, entry.getComments());
108       insertStatement.setString(iIndex++, entry.getImageURL());
109       insertStatement.setString(iIndex++, entry.getTargetURL());
110       
111       return iIndex;
112    }
113    
114    /**
115     * {@inheritDoc}
116     */

117    public int setValuesForUpdate(
118       PreparedStatement JavaDoc updateStatement,
119       DataObject data,
120       int iIndex
121    ) throws OSSException,
122             SQLException JavaDoc
123    {
124       Entry entry = (Entry)data;
125
126       updateStatement.setString(iIndex++, entry.getCaption());
127       updateStatement.setString(iIndex++, entry.getComments());
128       updateStatement.setString(iIndex++, entry.getImageURL());
129       updateStatement.setString(iIndex++, entry.getTargetURL());
130       updateStatement.setInt(iIndex++, entry.getId());
131       // Here you must pass the domain id sent to you in user object
132
// If you want to check if this id is the same as current domain id
133
// do it at the controller level. Otherwise the test fails if run
134
// on the empty database
135
updateStatement.setInt(iIndex++, entry.getDomainId());
136       updateStatement.setTimestamp(iIndex++, entry.getModificationTimestamp());
137       
138       return iIndex;
139    }
140
141    // Basic operations /////////////////////////////////////////////////////////
142

143    /**
144     * {@inheritDoc}
145     */

146    public DataObject load(
147       ResultSet JavaDoc rsQueryResults,
148       int initialIndex
149    ) throws OSSDatabaseAccessException
150    {
151       Entry data;
152       
153       try
154       {
155          // The order must exactly match the order in COLUMNS constant
156
data = new Entry(rsQueryResults.getInt(initialIndex),
157                           rsQueryResults.getInt(initialIndex + 1),
158                           rsQueryResults.getInt(initialIndex + 2),
159                           rsQueryResults.getString(initialIndex + 3),
160                           rsQueryResults.getString(initialIndex + 4),
161                           rsQueryResults.getString(initialIndex + 5),
162                           rsQueryResults.getString(initialIndex + 6),
163                           rsQueryResults.getTimestamp(initialIndex + 7),
164                           rsQueryResults.getTimestamp(initialIndex + 8));
165          data.setFromPersistenceStore();
166       }
167       catch (SQLException JavaDoc sqleExc)
168       {
169          throw new OSSDatabaseAccessException("Failed to load data from the database.",
170                                              sqleExc);
171       }
172       
173       return data;
174    }
175
176    /**
177     * {@inheritDoc}
178     */

179    public DataObject get(
180       final int iId,
181       final int iDomainId
182    ) throws OSSException
183    {
184       DataObject data = null;
185
186       // If the ID is supplied try to read the data from the database, if it is not,
187
// it is new blog which doesn't have ID yet
188
if (iId == DataObject.NEW_ID)
189       {
190          // These values are used as default values for new blog
191
data = new Entry(iDomainId);
192       }
193       else
194       {
195          DatabaseReadOperation dbop = new DatabaseReadSingleDataObjectOperation(
196             this, m_schema.getSelectEntryById(BlogDatabaseSchema.ENTRY_COLUMNS),
197             m_schema, iId, iDomainId);
198          data = (DataObject)dbop.executeRead();
199       }
200    
201       return data;
202    }
203
204    /**
205     * {@inheritDoc}
206     */

207    public DataObject create(
208       final DataObject data
209    ) throws OSSException
210    {
211       if (GlobalConstants.ERROR_CHECKING)
212       {
213          assert data.getId() == DataObject.NEW_ID
214                 : "Cannot create already created data.";
215       }
216
217       DatabaseUpdateOperation dbop = new DatabaseCreateSingleDataObjectOperation(
218          this, m_schema.getInsertEntryAndFetchGeneratedValues(), m_schema, data);
219       dbop.executeUpdate();
220      
221       return (DataObject)dbop.getReturnData();
222    }
223    
224    /**
225     * {@inheritDoc}
226     */

227    public int create(
228       Collection JavaDoc colDataObject
229    ) throws OSSException
230    {
231       if (GlobalConstants.ERROR_CHECKING)
232       {
233          assert colDataObject != null && !colDataObject.isEmpty()
234                 : "Cannot create empty data list.";
235       }
236
237       DatabaseUpdateOperation dbop = new DatabaseCreateMultipleDataObjectsOperation(
238          this, m_schema.getInsertEntry(), m_schema, colDataObject, false);
239       dbop.executeUpdate();
240      
241       return ((Integer JavaDoc)dbop.getReturnData()).intValue();
242    }
243
244    /**
245     * {@inheritDoc}
246     */

247    public void delete(
248       final int iId,
249       final int iDomainId
250    ) throws OSSException
251    {
252       if (GlobalConstants.ERROR_CHECKING)
253       {
254          assert iId != DataObject.NEW_ID
255                 : "Cannot delete data, which wasn't created yet.";
256       }
257
258       DatabaseUpdateOperation dbop = new DatabaseDeleteSingleDataObjectOperation(
259          this, m_schema.getDeleteEntryById(), m_schema, iId, iDomainId);
260       dbop.executeUpdate();
261    }
262
263    // Extended operations //////////////////////////////////////////////////////
264

265    /**
266     * {@inheritDoc}
267     */

268    public ModifiableDataObject save(
269       final ModifiableDataObject data
270    ) throws OSSException
271    {
272       if (GlobalConstants.ERROR_CHECKING)
273       {
274          assert data != null
275                   : "Data can not be null";
276          assert data.getId() != DataObject.NEW_ID
277                   : "Cannot save data which wasn't created yet.";
278       }
279
280       DatabaseUpdateOperation dbop = new DatabaseUpdateSingleDataObjectOperation(
281          this, m_schema.getUpdateEntryAndFetchGeneratedValues(), m_schema, data);
282       dbop.executeUpdate();
283      
284       return (ModifiableDataObject)dbop.getReturnData();
285    }
286
287    // Operations specific to this factory //////////////////////////////////////
288

289
290    /**
291     * {@inheritDoc}
292     */

293    public List JavaDoc getAll(
294       final int iBlogId
295    ) throws OSSException
296    {
297       DatabaseReadOperation dbop = new DatabaseReadMultipleOperation(
298          this, m_schema.getSelectAllEntries(BlogDatabaseSchema.ENTRY_COLUMNS), m_schema)
299       {
300          protected Object JavaDoc performOperation(
301             DatabaseFactoryImpl dbfactory,
302             Connection JavaDoc cntConnection,
303             PreparedStatement JavaDoc pstmQuery
304          ) throws OSSException,
305                   SQLException JavaDoc
306          {
307             int iDomainId = CallContext.getInstance().getCurrentDomainId();
308             pstmQuery.setInt(1, iBlogId);
309             pstmQuery.setInt(2, iDomainId);
310             return DatabaseUtils.loadMultipleData(dbfactory, pstmQuery);
311          }
312       };
313       return (List JavaDoc)dbop.executeRead();
314    }
315
316    /**
317     * {@inheritDoc}
318     */

319    public Entry getLast(
320       final int iBlogId
321    ) throws OSSException
322    {
323       DatabaseReadOperation dbop = new DatabaseReadOperation(
324          this, m_schema.getSelectLastEntry(BlogDatabaseSchema.ENTRY_COLUMNS), m_schema)
325       {
326          protected Object JavaDoc performOperation(
327             DatabaseFactoryImpl dbfactory,
328             Connection JavaDoc cntConnection,
329             PreparedStatement JavaDoc pstmQuery
330          ) throws OSSException,
331                   SQLException JavaDoc
332          {
333             int iDomainId = CallContext.getInstance().getCurrentDomainId();
334             pstmQuery.setInt(1, iBlogId);
335             pstmQuery.setInt(2, iDomainId);
336             return DatabaseUtils.loadAtMostOneData(dbfactory, pstmQuery,
337                       "Multiple records loaded from database for domain Id "
338                       + iDomainId + " and blog Id " + iBlogId);
339          }
340       };
341       return (Entry)dbop.executeRead();
342    }
343 }
344
Popular Tags