KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright (c) 2006 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
3  *
4  * Project: OpenSubsystems
5  *
6  * $Id: DatabaseUpdateMultipleDataObjectsOperation.java,v 1.7 2007/01/10 05:10:20 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.DataObject;
31 import org.opensubsystems.core.error.OSSException;
32
33 /**
34  * Adapter to simplify writing of batched database updates, 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 update
40  *
41  * public int create(
42  * final Collection colDataObject
43  * ) throws OSSException
44  * {
45  * DatabaseBatchUpdateOperation dbop = new DatabaseBatchUpdateOperation(
46  * this, m_schema.getUpdateMyData(), m_schema, dataType, colDataObject);
47  * dbop.executeUpdate();
48  *
49  * return ((Integer)dbop.getReturnData()).intValue();
50  * }
51  *
52  * @version $Id: DatabaseUpdateMultipleDataObjectsOperation.java,v 1.7 2007/01/10 05:10:20 bastafidli Exp $
53  * @author Julian Legeny
54  * @code.reviewer Miro Halas
55  * @code.reviewed 1.6 2006/07/26 23:44:33 jlegeny
56  */

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

61    /**
62     * Constructor
63     *
64     * @param factory - factory which is executing this operation
65     * @param query - query to update data
66     * @param schema - schema to set the data to the statement
67     * @param colDataObject - collection of data objects that will be updated
68     */

69    public DatabaseUpdateMultipleDataObjectsOperation(
70       DatabaseFactoryImpl factory,
71       String JavaDoc query,
72       ModifiableDatabaseSchema schema,
73       Collection JavaDoc colDataObject
74    )
75    {
76       super(factory, query, schema, DatabaseUpdateOperation.DBOP_UPDATE, colDataObject);
77    }
78
79    // Helper methods ///////////////////////////////////////////////////////////
80

81    /**
82     * {@inheritDoc}
83     */

84    protected void performOperation(
85       DatabaseFactoryImpl dbfactory,
86       Connection JavaDoc cntConnection,
87       PreparedStatement JavaDoc pstmQuery
88    ) throws OSSException,
89             SQLException JavaDoc
90    {
91       int[] arrUpdatedReturn;
92       int iBatchedCount = 0;
93       Iterator JavaDoc items;
94       DataObject data = null;
95       int size;
96       int iTotalUpdatedReturn = 0;
97       
98       size = ((Collection JavaDoc)m_data).size();
99       for (items = ((Collection JavaDoc)m_data).iterator(); items.hasNext();)
100       {
101          data = (DataObject)items.next();
102          // prepare data if neccessary (update object values)
103
prepareData(data);
104          // set values for prepared statement
105
setValuesForUpdate(pstmQuery, data, 1);
106          pstmQuery.addBatch();
107          iBatchedCount++;
108
109          // test if there is time to execute batch
110
if (((iBatchedCount % Database.BATCH_ITERATOR) == 0)
111             || (iBatchedCount == size))
112          {
113             arrUpdatedReturn = pstmQuery.executeBatch();
114             iTotalUpdatedReturn += arrUpdatedReturn.length;
115          }
116       }
117       // TODO: Performance: Consider defining setReturnData(int)
118
// so we do not have to create extra object
119
setReturnData(new Integer JavaDoc(iTotalUpdatedReturn));
120    }
121 }
122
Popular Tags