/*
* Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
*
* Project: OpenChronicle
*
* $Id: BlogDatabaseFactory.java,v 1.6 2007/02/20 02:14:49 bastafidli Exp $
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.opensubsystems.blog.persist.db;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Collection;
import org.opensubsystems.blog.data.Blog;
import org.opensubsystems.blog.persist.BlogFactory;
import org.opensubsystems.core.data.DataConstant;
import org.opensubsystems.core.data.DataObject;
import org.opensubsystems.core.data.ModifiableDataObject;
import org.opensubsystems.core.error.OSSDatabaseAccessException;
import org.opensubsystems.core.error.OSSException;
import org.opensubsystems.core.persist.db.DatabaseCreateMultipleDataObjectsOperation;
import org.opensubsystems.core.persist.db.DatabaseCreateSingleDataObjectOperation;
import org.opensubsystems.core.persist.db.DatabaseDeleteSingleDataObjectOperation;
import org.opensubsystems.core.persist.db.DatabaseFactoryImpl;
import org.opensubsystems.core.persist.db.DatabaseReadOperation;
import org.opensubsystems.core.persist.db.DatabaseReadSingleDataObjectOperation;
import org.opensubsystems.core.persist.db.DatabaseSchemaManager;
import org.opensubsystems.core.persist.db.DatabaseUpdateOperation;
import org.opensubsystems.core.persist.db.DatabaseUpdateSingleDataObjectOperation;
import org.opensubsystems.core.persist.db.ModifiableDatabaseFactory;
import org.opensubsystems.core.util.CallContext;
import org.opensubsystems.core.util.DatabaseUtils;
import org.opensubsystems.core.util.GlobalConstants;
import org.opensubsystems.patterns.listdata.persist.db.impl.ListDatabaseFactoryImpl;
/**
* Data factory to retrieve and manipulate blogs in the persistence store.
*
* @version $Id: BlogDatabaseFactory.java,v 1.6 2007/02/20 02:14:49 bastafidli Exp $
* @author Miro Halas
* @code.reviewer Miro Halas
* @code.reviewed 1.1 2007/01/03 05:46:52 bastafidli
*/
public class BlogDatabaseFactory extends ListDatabaseFactoryImpl
implements BlogFactory,
ModifiableDatabaseFactory
{
// Cached values ////////////////////////////////////////////////////////////
/**
* Schema to use to execute database dependent operations.
*/
protected BlogDatabaseSchema m_schema;
// Constructors /////////////////////////////////////////////////////////////
/**
* Default constructor.
*
* @throws OSSException - an error has occured
*/
public BlogDatabaseFactory(
) throws OSSException
{
super(DataConstant.BLOG_DATA_TYPE,
Blog.DEFAULT_LIST_SORT_COLUMNS,
Blog.DEFAULT_LIST_SORT_ORDER,
Blog.DEFAULT_LIST_COLUMNS,
((BlogDatabaseSchema)DatabaseSchemaManager.getInstance(
BlogDatabaseSchema.class))
);
// As a convenience we cast it to correct type
m_schema = ((BlogDatabaseSchema)getListDatabaseSchema());
}
// Basic operations /////////////////////////////////////////////////////////
/**
* {@inheritDoc}
*/
public DataObject load(
ResultSet rsQueryResults,
int initialIndex
) throws OSSDatabaseAccessException
{
Blog data;
try
{
// The order must exactly match the order in COLUMNS constant
data = new Blog(rsQueryResults.getInt(initialIndex),
rsQueryResults.getInt(initialIndex + 1),
rsQueryResults.getString(initialIndex + 2),
rsQueryResults.getString(initialIndex + 3),
rsQueryResults.getString(initialIndex + 4),
rsQueryResults.getTimestamp(initialIndex + 5),
rsQueryResults.getTimestamp(initialIndex + 6));
data.setFromPersistenceStore();
}
catch (SQLException sqleExc)
{
throw new OSSDatabaseAccessException("Failed to load data from the database.",
sqleExc);
}
return data;
}
/**
* {@inheritDoc}
*/
public int setValuesForInsert(
PreparedStatement insertStatement,
DataObject data,
int iIndex
) throws OSSException,
SQLException
{
Blog blog = (Blog)data;
// Here you must pass the domain id sent to you in blog object
// If you want to check if this id is the same as current domain id
// do it at the controller level.
insertStatement.setInt(iIndex++, blog.getDomainId());
insertStatement.setString(iIndex++, blog.getFolder());
insertStatement.setString(iIndex++, blog.getCaption());
insertStatement.setString(iIndex++, blog.getComments());
return iIndex;
}
/**
* {@inheritDoc}
*/
public int setValuesForUpdate(
PreparedStatement updateStatement,
DataObject data,
int iIndex
) throws OSSException,
SQLException
{
Blog blog = (Blog)data;
updateStatement.setString(iIndex++, blog.getFolder());
updateStatement.setString(iIndex++, blog.getCaption());
updateStatement.setString(iIndex++, blog.getComments());
updateStatement.setInt(iIndex++, blog.getId());
// Here you must pass the domain id sent to you in user object
// If you want to check if this id is the same as current domain id
// do it at the controller level. Otherwise the test fails
// if run on the empty database
updateStatement.setInt(iIndex++, blog.getDomainId());
updateStatement.setTimestamp(iIndex++, blog.getModificationTimestamp());
return iIndex;
}
/**
* {@inheritDoc}
*/
public DataObject get(
final int iId,
final int iDomainId
) throws OSSException
{
DataObject data = null;
// If the ID is supplied try to read the data from the database, if it is not,
// it is new blog which doesn't have ID yet
if (iId == DataObject.NEW_ID)
{
// This blog doesn't exist yet so just create a new one
data = new Blog(iDomainId);
}
else
{
DatabaseReadOperation dbop = new DatabaseReadSingleDataObjectOperation(
this, m_schema.getSelectBlogById(BlogDatabaseSchema.BLOG_COLUMNS),
m_schema, iId, iDomainId);
data = (DataObject)dbop.executeRead();
}
return data;
}
/**
* {@inheritDoc}
*/
public DataObject create(
final DataObject data
) throws OSSException
{
if (GlobalConstants.ERROR_CHECKING)
{
assert data.getId() == DataObject.NEW_ID
: "Cannot create already created data.";
}
DatabaseUpdateOperation dbop = new DatabaseCreateSingleDataObjectOperation(
this, m_schema.getInsertBlogAndFetchGeneratedValues(), m_schema, data);
dbop.executeUpdate();
return (DataObject)dbop.getReturnData();
}
/**
* {@inheritDoc}
*/
public int create(
final Collection colDataObject
) throws OSSException
{
if (GlobalConstants.ERROR_CHECKING)
{
assert colDataObject != null && !colDataObject.isEmpty()
: "Cannot create empty data list.";
}
DatabaseUpdateOperation dbop = new DatabaseCreateMultipleDataObjectsOperation(
this, m_schema.getInsertBlog(), m_schema, colDataObject, false);
dbop.executeUpdate();
return ((Integer)dbop.getReturnData()).intValue();
}
/**
* {@inheritDoc}
*/
public void delete(
final int iId,
final int iDomainId
) throws OSSException
{
if (GlobalConstants.ERROR_CHECKING)
{
assert iId != DataObject.NEW_ID
: "Cannot delete data, which wasn't created yet.";
}
DatabaseUpdateOperation dbop = new DatabaseDeleteSingleDataObjectOperation(
this, m_schema.getDeleteBlogById(), m_schema, iId, iDomainId);
dbop.executeUpdate();
}
// Extended operations //////////////////////////////////////////////////////
/**
* {@inheritDoc}
*/
public ModifiableDataObject save(
final ModifiableDataObject data
) throws OSSException
{
if (GlobalConstants.ERROR_CHECKING)
{
assert data != null
: "Data can not be null";
assert data.getId() != DataObject.NEW_ID
: "Cannot save data which wasn't created yet.";
}
DatabaseUpdateOperation dbop = new DatabaseUpdateSingleDataObjectOperation(
this, m_schema.getUpdateBlogAndFetchGeneratedValues(), m_schema, data);
dbop.executeUpdate();
return (ModifiableDataObject)dbop.getReturnData();
}
// Operations specific to this factory //////////////////////////////////////
/**
* {@inheritDoc}
*/
public Blog get(
final String strFolder
) throws OSSException
{
if (GlobalConstants.ERROR_CHECKING)
{
assert strFolder != null : "Blog folder cannot be null";
}
DatabaseReadOperation dbop = new DatabaseReadOperation(
this, m_schema.getSelectBlogByFolder(BlogDatabaseSchema.BLOG_COLUMNS), m_schema)
{
protected Object performOperation(
DatabaseFactoryImpl dbfactory,
Connection cntConnection,
PreparedStatement pstmQuery
) throws OSSException,
SQLException
{
int iDomainId = CallContext.getInstance().getCurrentDomainId();
pstmQuery.setString(1, strFolder);
pstmQuery.setInt(2, iDomainId);
return DatabaseUtils.loadAtMostOneData(dbfactory, pstmQuery,
"Multiple records loaded from database for domain ID "
+ iDomainId + " and folder " + strFolder);
}
};
return (Blog)dbop.executeRead();
}
// List operations //////////////////////////////////////////////////////////
/**
* {@inheritDoc}
*/
public DataObject load(
ResultSet rsQueryResults,
int[] selectedColumns,
int initialIndex
) throws OSSDatabaseAccessException
{
Blog data;
try
{
int id = DataObject.NEW_ID;
int domainId = DataObject.NEW_ID;
String folder = "";
String caption = "";
String comments = "";
Timestamp created = null;
Timestamp modified = null;
for (int columnCount = initialIndex;
columnCount < (initialIndex + selectedColumns.length);
columnCount++)
{
switch (selectedColumns[columnCount - 1])
{
case (Blog.COL_BLOG_ID) :
{
id = rsQueryResults.getInt(columnCount);
break;
}
case (Blog.COL_BLOG_DOMAIN_ID) :
{
domainId = rsQueryResults.getInt(columnCount);
break;
}
case (Blog.COL_BLOG_FOLDER) :
{
folder = rsQueryResults.getString(columnCount);
break;
}
case (Blog.COL_BLOG_CAPTION) :
{
caption = rsQueryResults.getString(columnCount);
break;
}
case (Blog.COL_BLOG_COMMENTS) :
{
comments = rsQueryResults.getString(columnCount);
break;
}
case (Blog.COL_BLOG_CREATION_DATE) :
{
created = rsQueryResults.getTimestamp(columnCount);
break;
}
case (Blog.COL_BLOG_MODIFICATION_DATE) :
{
modified = rsQueryResults.getTimestamp(columnCount);
break;
}
default:
{
assert false : "Unknown column ID " + selectedColumns[columnCount - 1];
}
}
}
data = new Blog(id,
domainId,
folder,
caption,
comments,
created,
modified);
data.setFromPersistenceStore();
}
catch (SQLException sqleExc)
{
throw new OSSDatabaseAccessException("Failed to load data from the database.",
sqleExc);
}
return data;
}
}
|