KickJava   Java API By Example, From Geeks To Geeks.

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


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

57 public class DatabaseCreateSingleDataObjectOperation extends DatabaseUpdateOperation
58 {
59    // Constructors /////////////////////////////////////////////////////////////
60

61    /**
62     * Constructor to use when database update doesn't require any prepared
63     * statement.
64     *
65     * @param factory - factory which is executing this operation
66     * @param strQueryToPrepare - query which should be used to construct prepared
67     * statement which will be passed in to executeUpdate
68     * @param schema - database schema used with this operation
69     * @param data - data used for operation
70     */

71    public DatabaseCreateSingleDataObjectOperation(
72       DatabaseFactoryImpl factory,
73       String JavaDoc strQueryToPrepare,
74       ModifiableDatabaseSchema schema,
75       Object JavaDoc data
76    )
77    {
78       super(factory, strQueryToPrepare, schema, DatabaseOperations.DBOP_INSERT, data);
79    }
80
81    // Overwritten methods //////////////////////////////////////////////////////
82

83    /**
84     * {@inheritDoc}
85     */

86    protected void performOperation(
87       DatabaseFactoryImpl dbfactory,
88       Connection JavaDoc cntConnection,
89       PreparedStatement JavaDoc pstmQuery
90    ) throws OSSException, SQLException JavaDoc
91    {
92       BasicDataObject objData = (BasicDataObject)m_data;
93       int iIndex = 1;
94       iIndex = setValuesForInsert(pstmQuery, objData, iIndex);
95       
96       DatabaseImpl.getInstance().insertAndFetchGeneratedValues(
97          cntConnection, pstmQuery, m_dbschema.isInDomain(),
98          ((ModifiableDatabaseSchema)m_dbschema).getModifiableTableNames().get(
99             new Integer JavaDoc(m_iDataType)).toString(),
100          iIndex, objData);
101       setReturnData(objData);
102    }
103 }
104
Popular Tags