KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
3  *
4  * Project: OpenSubsystems
5  *
6  * $Id: DatabaseReadMultipleOperation.java,v 1.7 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.error.OSSException;
29
30 /**
31  * Adapter to simplify writing of database reads which read multiple items,
32  * which takes care of requesting and returning connections, transaction
33  * management, query preparation and exception handling. To use this adapter you
34  * just need to define anonymous class and override method performOperation to
35  * provide the actual database read. Optionally you may want to override one of
36  * the handleXXX methods to provide custom error handling.
37  *
38  * This class is optimized for preparing queries, which need to retrieve multiple
39  * items from the database.
40  *
41  * Example of method in factory which reads data using query produced by its schema
42  *
43  * public int[] getActualIds(
44  * final int iDomainId,
45  * String strIds,
46  * SimpleRule listSecurityData
47  * ) throws OSSException
48  * {
49  * int[] arrActual = null;
50  *
51  * DatabaseReadOperation dbop = new DatabaseReadMultipleOperation(
52  * this, m_schema.getSelectActualIds(strIds, listSecurityData),
53  * m_schema, dataType)
54  * {
55  * protected Object performOperation(
56  * DatabaseFactoryImpl dbfactory,
57  * Connection cntConnection,
58  * PreparedStatement pstmQuery
59  * ) throws OSSException,
60  * SQLException
61  * {
62  * pstmQuery.setInt(1, iDomainId);
63  * return DatabaseUtils.loadMultipleIntsAsArray(pstmQuery);
64  * }
65  * };
66  * arrActual = (int[])dbop.executeRead();
67  *
68  * return arrActual;
69  * }
70  *
71  * @version $Id: DatabaseReadMultipleOperation.java,v 1.7 2007/01/28 06:54:42 bastafidli Exp $
72  * @author Miro Halas
73  * @code.reviewer Miro Halas
74  * @code.reviewed 1.2 2005/10/25 05:17:37 bastafidli
75  */

76 public abstract class DatabaseReadMultipleOperation extends DatabaseReadOperation
77 {
78    // Constructors /////////////////////////////////////////////////////////////
79

80    /**
81     * Constructor to use when the database read doesn't require any
82     * prepared statement.
83     *
84     * @param factory - factory which is executing this operation
85     */

86    public DatabaseReadMultipleOperation(
87       DatabaseFactoryImpl factory
88    )
89    {
90       this(factory, null, null);
91    }
92
93    /**
94     * Constructor to use when database read doesn't require any prepared
95     * statement.
96     *
97     * @param factory - factory which is executing this operation
98     * @param strQueryToPrepare - query which should be used to construct prepared
99     * statement which will be passed in to executeUpdate
100     * @param schema - database schema used with this operation
101     */

102    public DatabaseReadMultipleOperation(
103       DatabaseFactoryImpl factory,
104       String JavaDoc strQueryToPrepare,
105       DatabaseSchema schema
106    )
107    {
108       super(factory, strQueryToPrepare, schema, factory.getDataType());
109    }
110
111    // Helper methods ///////////////////////////////////////////////////////////
112

113    /**
114     * Prepare the query if it was specified using the provided connection.
115     *
116     * @param dbfactory - database factory executing this operation
117     * @param cntConnection - ready to use connection to perform the database
118     * operation. No need to return this connection.
119     * @param strQuery - query to prepare, might be null or empty if there is
120     * nothing to prepare
121     * @return PreparedStatement - prepared statement for query passed in as a
122     * parameter to the constructor. If no query was
123     * passed into constructor, this will be null.
124     * @throws OSSException - an error has occured
125     * @throws SQLException - an error has occured
126     */

127    protected PreparedStatement JavaDoc prepareQuery(
128       DatabaseFactoryImpl dbfactory,
129       Connection JavaDoc cntConnection,
130       String JavaDoc strQuery
131    ) throws OSSException,
132             SQLException JavaDoc
133    {
134       PreparedStatement JavaDoc pstmQuery = null;
135       
136       if ((strQuery != null) && (strQuery.length() > 0))
137       {
138          pstmQuery = cntConnection.prepareStatement(strQuery,
139                         dbfactory.getTypeSelectListResultSet(),
140                         dbfactory.getConcurrencySelectListResultSet());
141       }
142       
143       return pstmQuery;
144    }
145 }
146
Popular Tags