KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright (c) 2006 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
3  *
4  * Project: OpenSubsystems
5  *
6  * $Id: DatabaseReadSingleDataObjectOperation.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 import org.opensubsystems.core.util.DatabaseUtils;
30
31
32 /**
33  * Adapter to simplify writing of database reads which read single data object,
34  * which takes care of requesting and returning connections, transaction
35  * management, query preparation and exception handling.
36  *
37  * @version $Id: DatabaseReadSingleDataObjectOperation.java,v 1.7 2007/01/28 06:54:42 bastafidli Exp $
38  * @author Julian Legeny
39  * @code.reviewer TODO: Review this code
40  * @code.reviewed
41  */

42 public class DatabaseReadSingleDataObjectOperation extends DatabaseReadOperation
43 {
44    // Attributes ///////////////////////////////////////////////////////////////
45

46    /**
47     * ID of data object.
48     */

49    private int m_iId;
50
51    /**
52     * ID of domain the data object belongs to.
53     */

54    private int m_iDomainId;
55    
56    // Constructors /////////////////////////////////////////////////////////////
57

58    /**
59     * Constructor to use when database update doesn't require any prepared
60     * statement.
61     *
62     * @param factory - factory which is executing this operation
63     * @param strQuery - sql query that has to be processed
64     * @param schema - database schema used with this operation
65     * @param iId - ID of data object to read
66     * @param iDomainId - ID of domain the data object belongs to
67     */

68    public DatabaseReadSingleDataObjectOperation(
69       DatabaseFactoryImpl factory,
70       String JavaDoc strQuery,
71       DatabaseSchema schema,
72       int iId,
73       int iDomainId
74    )
75    {
76       super(factory, strQuery, schema, factory.getDataType());
77       
78       m_iId = iId;
79       m_iDomainId = iDomainId;
80    }
81    
82    // Overwritten method ///////////////////////////////////////////////////////
83

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

87    protected Object JavaDoc performOperation(
88       DatabaseFactoryImpl dbfactory,
89       Connection JavaDoc cntConnection,
90       PreparedStatement JavaDoc pstmQuery
91    ) throws OSSException, SQLException JavaDoc
92    {
93       StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
94       // construct error message
95
buffer.append("Multiple records loaded from database for ID ");
96       buffer.append(m_iId);
97       if (m_dbschema.isInDomain())
98       {
99          buffer.append(" and domain ID ");
100          buffer.append(m_iDomainId);
101       }
102
103       pstmQuery.setInt(1, m_iId);
104       if (m_dbschema.isInDomain())
105       {
106          // set up domain ID parameter if data object is in domain
107
pstmQuery.setInt(2, m_iDomainId);
108       }
109
110       return DatabaseUtils.loadAtMostOneData(dbfactory, pstmQuery, buffer.toString());
111    }
112 }
113
Popular Tags