KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opensubsystems > core > persist > db > mssql > MSSQLDataUtils


1 /*
2  * Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
3  *
4  * Project: OpenSubsystems
5  *
6  * $Id: MSSQLDataUtils.java,v 1.3 2007/01/07 06:14:54 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.mssql;
23
24 import java.sql.CallableStatement JavaDoc;
25 import java.sql.SQLException JavaDoc;
26 import java.sql.Timestamp JavaDoc;
27 import java.sql.Types JavaDoc;
28
29 import org.opensubsystems.core.data.BasicDataObject;
30 import org.opensubsystems.core.data.ModifiableDataObject;
31 import org.opensubsystems.core.error.OSSException;
32 import org.opensubsystems.core.error.OSSInconsistentDataException;
33
34 /**
35  * This class collects code fragments which are reusable for managing data
36  * objects in MS SQL Server 2000.
37  *
38  * @version $Id: MSSQLDataUtils.java,v 1.3 2007/01/07 06:14:54 bastafidli Exp $
39  * @author Julo Legeny
40  * @code.reviewer Miro Halas
41  * @code.reviewed 1.1 2005/08/20 21:17:02 bastafidli
42  */

43 public final class MSSQLDataUtils
44 {
45    // Constructors /////////////////////////////////////////////////////////////
46

47    /**
48     * Private constructor since this class cannot be instantiated
49     */

50    private MSSQLDataUtils(
51    )
52    {
53       // Do nothing
54
}
55    
56    // Public methods ///////////////////////////////////////////////////////////
57

58    /**
59     * Insert the data, fetch from the database id and generated creation and
60     * modification timestamps for the newly created data object.
61     *
62     * Note: Since the caller created the callable statement, the caller is
63     * responsible for its closing.
64     *
65     * @param insertStatement - statement used to insert the data
66     * @param iIndex - index of the ID OUT parameter of the stored procedure
67     * @param data - data object to insert
68     * @throws SQLException - an error has occured
69     * @throws OSSException - an error has occured
70     */

71    public static void insertAndFetchGeneratedValues(
72       CallableStatement JavaDoc insertStatement,
73       int iIndex,
74       BasicDataObject data
75    ) throws SQLException JavaDoc,
76             OSSException
77    {
78       int iGeneratedKey;
79       Timestamp JavaDoc tmTimestamp;
80       
81       insertStatement.registerOutParameter(iIndex++, Types.INTEGER);
82       insertStatement.registerOutParameter(iIndex++, Types.TIMESTAMP);
83       insertStatement.executeUpdate();
84       iGeneratedKey = insertStatement.getInt(iIndex - 2);
85       tmTimestamp = insertStatement.getTimestamp(iIndex - 1);
86       data.setId(iGeneratedKey);
87       data.setCreationTimestamp(tmTimestamp);
88       if (data instanceof ModifiableDataObject)
89       {
90          ((ModifiableDataObject)data).setModificationTimestamp(tmTimestamp);
91       }
92    }
93
94    /**
95     * Update the data, fetch from the database id and generated modification
96     * timestamps for the newly created data object.
97     *
98     * Note: Since the caller created the callable statement, the caller is
99     * responsible for its closing.
100     *
101     * @param updateStatement - statement used to update the data
102     * @param iIndex - index of the MODIFIED_TIMESTAMP OUT parameter of the
103     * stored procedure
104     * @param data - data object to update
105     * @throws SQLException - an error has occured
106     * @throws OSSException - an error has occured
107     */

108    public static void updateAndFetchGeneratedValues(
109       CallableStatement JavaDoc updateStatement,
110       int iIndex,
111       ModifiableDataObject data
112    ) throws SQLException JavaDoc,
113             OSSException
114    {
115       int iUpdateCount;
116       Timestamp JavaDoc tmTimestamp = null;
117          
118       updateStatement.registerOutParameter(iIndex++, Types.TIMESTAMP);
119       iUpdateCount = updateStatement.executeUpdate();
120       if (iUpdateCount > 1)
121       {
122          throw new OSSInconsistentDataException(
123             "Inconsistent database contains multiple (" + iUpdateCount +
124             ") data with the same ID modified at the same time");
125       }
126       if (iUpdateCount == 1)
127       {
128          tmTimestamp = updateStatement.getTimestamp(iIndex - 1);
129          data.setModificationTimestamp(tmTimestamp);
130       }
131    }
132 }
133
Popular Tags