KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright (c) 2006 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
3  *
4  * Project: OpenSubsystems
5  *
6  * $Id: DatabaseDeleteSingleDataObjectOperation.java,v 1.8 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.OSSDataNotFoundException;
29 import org.opensubsystems.core.error.OSSException;
30 import org.opensubsystems.core.error.OSSInconsistentDataException;
31
32 /**
33  * Adapter to simplify writing of database updates which delete single data object,
34  * which takes care of requesting and returning connections, transaction
35  * management, query preparation and exception handling.
36  *
37  * @version $Id: DatabaseDeleteSingleDataObjectOperation.java,v 1.8 2007/01/28 06:54:42 bastafidli Exp $
38  * @author Julian Legeny
39  * @code.reviewer Miro Halas
40  * @code.reviewed 1.6 2007/01/07 06:14:18 bastafidli
41  */

42 public class DatabaseDeleteSingleDataObjectOperation extends DatabaseUpdateOperation
43 {
44    // Attributes ///////////////////////////////////////////////////////////////
45

46    /**
47     * Schema to use to execute database dependent operations.
48     */

49    private ModifiableDatabaseSchema m_schema;
50
51    /**
52     * Data type of the related object to be deleted cascade.
53     */

54    private int m_iDataType;
55
56    /**
57     * ID of data object.
58     */

59    private int m_iId;
60
61    /**
62     * ID of domain the data object belongs to.
63     */

64    private int m_iDomainId;
65    
66    // Constructors /////////////////////////////////////////////////////////////
67

68    /**
69     * Copy constructor to use when database update doesn't require any prepared
70     * statement.
71     * @param factory - factory which is executing this operation
72     * @param strQuery - sql query that has to be processed
73     * @param schema - schema to use to execute database dependent operations
74     * @param iId - ID of data object to delete
75     * @param iDomainId - ID of domain the data object belongs to
76     */

77    public DatabaseDeleteSingleDataObjectOperation(
78       DatabaseFactoryImpl factory,
79       String JavaDoc strQuery,
80       ModifiableDatabaseSchema schema,
81       int iId,
82       int iDomainId
83    )
84    {
85       super(factory, strQuery, schema, DatabaseUpdateOperation.DBOP_DELETE, null);
86       
87       m_schema = schema;
88       m_iDataType = factory.getDataType();
89       m_iId = iId;
90       m_iDomainId = iDomainId;
91    }
92
93    /**
94     * {@inheritDoc}
95     */

96    protected void performOperation(
97       DatabaseFactoryImpl dbfactory,
98       Connection JavaDoc cntConnection,
99       PreparedStatement JavaDoc pstmQuery
100    ) throws OSSException, SQLException JavaDoc
101    {
102       m_schema.deleteRelatedData(cntConnection, m_iDataType, m_iId);
103
104       int iDeleted;
105       
106       pstmQuery.setInt(1, m_iId);
107       if (m_schema.isInDomain())
108       {
109          // set up domaion ID parameter if data object is in domain
110
pstmQuery.setInt(2, m_iDomainId);
111       }
112       iDeleted = pstmQuery.executeUpdate();
113       
114       if (iDeleted == 0)
115       {
116          throw new OSSDataNotFoundException(
117                       "Data to delete cannot be found in the database");
118       }
119       else if (iDeleted != 1)
120       {
121          throw new OSSInconsistentDataException(
122                       "Inconsistent database contains multiple ("
123                       + iDeleted + ") data object with the same ID");
124       }
125    }
126 }
127
Popular Tags