KickJava   Java API By Example, From Geeks To Geeks.

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


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

57 public class DatabaseCreateMultipleDataObjectsOperation extends DatabaseUpdateOperation
58 {
59    // Attributes ///////////////////////////////////////////////////////////////
60

61    /**
62     * Flag signaling if there will be fetched generated values.
63     */

64    private boolean m_bFetchGeneratedValues;
65
66    // Constructors /////////////////////////////////////////////////////////////
67

68    /**
69     * Constructor
70     *
71     * @param factory - factory which is executing this operation
72     * @param query - query to insert data
73     * @param schema - schema to set the data to the statement
74     * @param colDataObject - collection of data objects that will be created
75     * @param bFetchGeneratedValues - flag signaling if there have to be returned
76     * generated values
77     * true = there will be returned generated values
78     * false = will be returned number of inserted records
79     */

80    public DatabaseCreateMultipleDataObjectsOperation(
81       DatabaseFactoryImpl factory,
82       String JavaDoc query,
83       ModifiableDatabaseSchema schema,
84       Collection JavaDoc colDataObject,
85       boolean bFetchGeneratedValues
86    )
87    {
88       super(factory, query, schema, DatabaseUpdateOperation.DBOP_INSERT, colDataObject);
89       
90       m_bFetchGeneratedValues = bFetchGeneratedValues;
91    }
92
93    // Helper methods ///////////////////////////////////////////////////////////
94

95    /**
96     * {@inheritDoc}
97     */

98    protected void performOperation(
99       DatabaseFactoryImpl dbfactory,
100       Connection JavaDoc cntConnection,
101       PreparedStatement JavaDoc pstmQuery
102    ) throws OSSException,
103             SQLException JavaDoc
104    {
105       int[] arrInsertedReturn;
106       int iBatchedCount = 0;
107       Iterator JavaDoc items;
108       BasicDataObject data = null;
109       int size;
110       int iTotalInsertedReturn = 0;
111       int iIndex = 0;
112       
113       if (!m_bFetchGeneratedValues)
114       {
115          size = ((Collection JavaDoc)m_data).size();
116          for (items = ((Collection JavaDoc)m_data).iterator(); items.hasNext();)
117          {
118             data = (BasicDataObject)items.next();
119             // prepare data if neccessary (update object values)
120
prepareData(data);
121             // set values for prepared statement
122
setValuesForInsert(pstmQuery, data, 1);
123             pstmQuery.addBatch();
124             iBatchedCount++;
125    
126             // test if there is time to execute batch
127
if (((iBatchedCount % Database.BATCH_ITERATOR) == 0)
128                || (iBatchedCount == size))
129             {
130                arrInsertedReturn = pstmQuery.executeBatch();
131                iTotalInsertedReturn += arrInsertedReturn.length;
132             }
133          }
134          // TODO: Performance: Consider defining setReturnData(int)
135
// so we do not have to create extra object
136
setReturnData(new Integer JavaDoc(iTotalInsertedReturn));
137       }
138       else
139       {
140          for (items = ((Collection JavaDoc)m_data).iterator(); items.hasNext();)
141          {
142             data = (BasicDataObject)items.next();
143             // prepare data if neccessary (update object values)
144
prepareData(data);
145             pstmQuery.clearParameters();
146             iIndex = ((BasicDatabaseFactory)dbfactory).setValuesForInsert(pstmQuery, data, 1);
147             DatabaseImpl.getInstance().insertAndFetchGeneratedValues(
148                cntConnection, pstmQuery,
149                m_dbschema.isInDomain(),
150                ((ModifiableDatabaseSchema)m_dbschema).getModifiableTableNames().get(
151                   new Integer JavaDoc(m_iDataType)).toString(),
152                iIndex, data);
153          }
154          setReturnData(m_data);
155       }
156    }
157 }
158
Free Books   Free Magazines  
Popular Tags