KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opensubsystems > core > persist > db > DatabaseUpdateSingleDataObjectOperation


1 /*
2  * Copyright (c) 2006 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
3  *
4  * Project: OpenSubsystems
5  *
6  * $Id: DatabaseUpdateSingleDataObjectOperation.java,v 1.10 2007/01/28 06:54:42 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.core.persist.db;
23
24 import java.sql.Connection JavaDoc;
25 import java.sql.PreparedStatement JavaDoc;
26 import java.sql.SQLException JavaDoc;
27
28 import org.opensubsystems.core.data.DataConstant;
29 import org.opensubsystems.core.data.ModifiableDataObject;
30 import org.opensubsystems.core.error.OSSException;
31
32 /**
33  * Adapter to simplify writing of database updates that updates single data
34  * object to the database. The adapter takes care of requesting and returning
35  * connections, transaction management and exception handling. To use this
36  * adapter you just need to create an instance of this class and call
37  * executeUpdate method.
38  *
39  * Example of method in factory which creates single data object
40  *
41  * public int create(
42  * final BasicDataObject data
43  * ) throws OSSException
44  * {
45  * DatabaseUpdateSingleDataObjectOperation dbop
46  * = new DatabaseUpdateSingleDataObjectOperation(
47  * this, m_schema.getInsertMyData(), m_schema.isInDomain(), m_schema,
48  * dataType, data, "MyDataName");
49  * dbop.executeUpdate();
50  *
51  * return ((Integer)dbop.getReturnData()).intValue();
52  * }
53  *
54  * @version $Id: DatabaseUpdateSingleDataObjectOperation.java,v 1.10 2007/01/28 06:54:42 bastafidli Exp $
55  * @author Julian Legeny
56  * @code.reviewer Miro Halas
57  * @code.reviewed 1.1 2006/07/26 23:44:50 jlegeny
58  */

59 public class DatabaseUpdateSingleDataObjectOperation extends DatabaseUpdateOperation
60 {
61    // Attributes ///////////////////////////////////////////////////////////////
62

63    /**
64     * TODO: Improve: We should be able to get this from DataConstants.getDataTypeName
65     * We can define another constructor that doesn't require the name and which
66     * initializes this variable using DataConstants.getDataTypeName
67     *
68     * Name of dataobject to be used into message.
69     */

70    private String JavaDoc m_strDataObjectName;
71
72    // Constructors /////////////////////////////////////////////////////////////
73

74    /**
75     * Constructor to use when database update doesn't require any prepared
76     * statement.
77     *
78     * @param factory - factory which is executing this operation
79     * @param strQueryToPrepare - query which should be used to construct prepared
80     * statement which will be passed in to executeUpdate
81     * @param schema - database schema used with this operation
82     * @param data - data used for operation
83     */

84    public DatabaseUpdateSingleDataObjectOperation(
85       DatabaseFactoryImpl factory,
86       String JavaDoc strQueryToPrepare,
87       ModifiableDatabaseSchema schema,
88       Object JavaDoc data
89    )
90    {
91       super(factory, strQueryToPrepare, schema, DatabaseOperations.DBOP_UPDATE, data);
92       
93       m_strDataObjectName = DataConstant.getDataTypeName(factory.getDataType());
94    }
95
96    // Overwritten methods //////////////////////////////////////////////////////
97

98    /**
99     * {@inheritDoc}
100     */

101    protected void performOperation(
102       DatabaseFactoryImpl dbfactory,
103       Connection JavaDoc cntConnection,
104       PreparedStatement JavaDoc pstmQuery
105    ) throws OSSException, SQLException JavaDoc
106    {
107       ModifiableDataObject objData = (ModifiableDataObject)m_data;
108       int iIndex = 1;
109       iIndex = setValuesForUpdate(pstmQuery, objData, iIndex);
110       
111       DatabaseImpl.getInstance().updatedAndFetchGeneratedValues(
112          m_strDataObjectName, cntConnection, pstmQuery, m_dbschema.isInDomain(),
113          ((ModifiableDatabaseSchema)m_dbschema).getModifiableTableNames().get(
114             new Integer JavaDoc(m_iDataType)).toString(),
115          iIndex, objData);
116       setReturnData(objData);
117    }
118 }
119
Popular Tags