KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
3  *
4  * Project: OpenChronicle
5  *
6  * $Id: BlogDatabaseFactory.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.Blog;
32 import org.opensubsystems.blog.persist.BlogFactory;
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 blogs in the persistence store.
55  *
56  * @version $Id: BlogDatabaseFactory.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:26:42 jlegeny
60  */

61 public class BlogDatabaseFactory extends DatabaseFactoryImpl
62                                  implements BlogFactory,
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 BlogDatabaseFactory(
80    ) throws OSSException
81    {
82       super(DataConstant.BLOG_DATA_TYPE);
83
84       m_schema = ((BlogDatabaseSchema)DatabaseSchemaManager.getInstance(
85                                          BlogDatabaseSchema.class));
86    }
87    
88    // Basic operations /////////////////////////////////////////////////////////
89

90    /**
91     * {@inheritDoc}
92     */

93    public DataObject load(
94       ResultSet JavaDoc rsQueryResults,
95       int initialIndex
96    ) throws OSSDatabaseAccessException
97    {
98       Blog data;
99       
100       try
101       {
102          // The order must exactly match the order in COLUMNS constant
103
data = new Blog(rsQueryResults.getInt(initialIndex),
104                          rsQueryResults.getInt(initialIndex + 1),
105                          rsQueryResults.getString(initialIndex + 2),
106                          rsQueryResults.getString(initialIndex + 3),
107                          rsQueryResults.getString(initialIndex + 4),
108                          rsQueryResults.getTimestamp(initialIndex + 5),
109                          rsQueryResults.getTimestamp(initialIndex + 6));
110          data.setFromPersistenceStore();
111       }
112       catch (SQLException JavaDoc sqleExc)
113       {
114          throw new OSSDatabaseAccessException("Failed to load data from the database.",
115                                               sqleExc);
116       }
117       
118       return data;
119    }
120
121    /**
122     * {@inheritDoc}
123     */

124    public int setValuesForInsert(
125       PreparedStatement JavaDoc insertStatement,
126       DataObject data,
127       int iIndex
128    ) throws OSSException,
129             SQLException JavaDoc
130    {
131       Blog blog = (Blog)data;
132       
133       // Here you must pass the domain id sent to you in blog object
134
// If you want to check if this id is the same as current domain id
135
// do it at the controller level.
136

137       insertStatement.setInt(iIndex++, blog.getDomainId());
138       insertStatement.setString(iIndex++, blog.getFolder());
139       insertStatement.setString(iIndex++, blog.getCaption());
140       insertStatement.setString(iIndex++, blog.getComments());
141
142       return iIndex;
143    }
144
145    /**
146     * {@inheritDoc}
147     */

148    public int setValuesForUpdate(
149       PreparedStatement JavaDoc updateStatement,
150       DataObject data,
151       int iIndex
152    ) throws OSSException,
153             SQLException JavaDoc
154    {
155       Blog blog = (Blog)data;
156       
157       updateStatement.setString(iIndex++, blog.getFolder());
158       updateStatement.setString(iIndex++, blog.getCaption());
159       updateStatement.setString(iIndex++, blog.getComments());
160       updateStatement.setInt(iIndex++, blog.getId());
161       // Here you must pass the domain id sent to you in user object
162
// If you want to check if this id is the same as current domain id
163
// do it at the controller level. Otherwise the test fails
164
// if run on the empty database
165
updateStatement.setInt(iIndex++, blog.getDomainId());
166       updateStatement.setTimestamp(iIndex++, blog.getModificationTimestamp());
167
168       return iIndex;
169    }
170
171    /**
172     * {@inheritDoc}
173     */

174    public DataObject get(
175       final int iId,
176       final int iDomainId
177    ) throws OSSException
178    {
179       DataObject data = null;
180       
181       // If the ID is supplied try to read the data from the database, if it is not,
182
// it is new blog which doesn't have ID yet
183
if (iId == DataObject.NEW_ID)
184       {
185          // This blog doesn't exist yet so just create a new one
186
data = new Blog(iDomainId);
187       }
188       else
189       {
190          DatabaseReadOperation dbop = new DatabaseReadSingleDataObjectOperation(
191             this, m_schema.getSelectBlogById(BlogDatabaseSchema.BLOG_COLUMNS),
192             m_schema, iId, iDomainId);
193          data = (DataObject)dbop.executeRead();
194       }
195       
196       return data;
197    }
198
199    /**
200     * {@inheritDoc}
201     */

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

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

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

259    /**
260     * {@inheritDoc}
261     */

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

283    /**
284     * {@inheritDoc}
285     */

286    public Blog get(
287       final String JavaDoc strFolder
288    ) throws OSSException
289    {
290       if (GlobalConstants.ERROR_CHECKING)
291       {
292          assert strFolder != null : "Blog folder cannot be null";
293       }
294       
295       DatabaseReadOperation dbop = new DatabaseReadOperation(
296          this, m_schema.getSelectBlogByFolder(BlogDatabaseSchema.BLOG_COLUMNS), m_schema)
297       {
298          protected Object JavaDoc performOperation(
299             DatabaseFactoryImpl dbfactory,
300             Connection JavaDoc cntConnection,
301             PreparedStatement JavaDoc pstmQuery
302          ) throws OSSException,
303                   SQLException JavaDoc
304          {
305             int iDomainId = CallContext.getInstance().getCurrentDomainId();
306             pstmQuery.setString(1, strFolder);
307             pstmQuery.setInt(2, iDomainId);
308             return DatabaseUtils.loadAtMostOneData(dbfactory, pstmQuery,
309                       "Multiple records loaded from database for domain ID "
310                       + iDomainId + " and folder " + strFolder);
311          }
312       };
313       return (Blog)dbop.executeRead();
314    }
315
316    /**
317     * {@inheritDoc}
318     */

319    public List JavaDoc getAll(
320    ) throws OSSException
321    {
322       DatabaseReadOperation dbop = new DatabaseReadMultipleOperation(
323          this, m_schema.getSelectAllBlogs(BlogDatabaseSchema.BLOG_COLUMNS), m_schema)
324       {
325          protected Object JavaDoc performOperation(
326             DatabaseFactoryImpl dbfactory,
327             Connection JavaDoc cntConnection,
328             PreparedStatement JavaDoc pstmQuery
329          ) throws OSSException,
330                   SQLException JavaDoc
331          {
332             int iDomainId = CallContext.getInstance().getCurrentDomainId();
333             pstmQuery.setInt(1, iDomainId);
334             return DatabaseUtils.loadMultipleData(dbfactory, pstmQuery);
335          }
336       };
337       return (List JavaDoc)dbop.executeRead();
338    }
339 }
340
Popular Tags