KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opensubsystems > core > persist > db > postgresql > PostgreSQLDataUtils


1 /*
2  * Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
3  *
4  * Project: OpenSubsystems
5  *
6  * $Id: PostgreSQLDataUtils.java,v 1.3 2007/01/07 06:14:43 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.postgresql;
23
24 import java.sql.PreparedStatement JavaDoc;
25 import java.sql.ResultSet JavaDoc;
26 import java.sql.SQLException JavaDoc;
27 import java.sql.Timestamp 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 import org.opensubsystems.core.util.DatabaseUtils;
34
35 /**
36  * This class collects code fragments which are reusable for managing data
37  * objects in Postgre SQL.
38  *
39  * @version $Id: PostgreSQLDataUtils.java,v 1.3 2007/01/07 06:14:43 bastafidli Exp $
40  * @author Julo Legeny
41  * @code.reviewer Miro Halas
42  * @code.reviewed 1.1 2005/08/20 21:17:01 bastafidli
43  */

44 public final class PostgreSQLDataUtils
45 {
46    // Constructors /////////////////////////////////////////////////////////////
47

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

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

59    /**
60     * Insert the data, fetch from the database id and generated creation and
61     * modification timestamps for the newly created data object.
62     *
63     * Note: Since the caller created the prepared statement, the caller is
64     * responsible for its closing.
65     *
66     * @param insertStatement - statement used to insert the data
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       PreparedStatement JavaDoc insertStatement,
73       BasicDataObject data
74    ) throws SQLException JavaDoc,
75             OSSException
76    {
77       ResultSet JavaDoc rsResults = null;
78       int iGeneratedKey = -1;
79       Timestamp JavaDoc tmTimestamp = null;
80
81       try
82       {
83          rsResults = insertStatement.executeQuery();
84             
85          if (rsResults.next())
86          {
87             iGeneratedKey = rsResults.getInt(1);
88             tmTimestamp = rsResults.getTimestamp(2);
89          }
90       }
91       finally
92       {
93          DatabaseUtils.closeResultSet(rsResults);
94       }
95
96       data.setId(iGeneratedKey);
97       data.setCreationTimestamp(tmTimestamp);
98       if (data instanceof ModifiableDataObject)
99       {
100          ((ModifiableDataObject)data).setModificationTimestamp(tmTimestamp);
101       }
102    }
103
104    /**
105     * Update the data, fetch from the database id and generated modification
106     * timestamps for the newly created data object.
107     *
108     * Note: Since the caller created the prepared statement, the caller is
109     * responsible for its closing.
110     *
111     * @param updateStatement - statement used to update the data
112     * @param data - data object to update
113     * @throws SQLException - an error has occured
114     * @throws OSSException - an error has occured
115     */

116    public static void updateAndFetchGeneratedValues(
117       PreparedStatement JavaDoc updateStatement,
118       ModifiableDataObject data
119    ) throws SQLException JavaDoc,
120             OSSException
121    {
122       ResultSet JavaDoc rsResults = null;
123       int iUpdateCount = 0;
124       Timestamp JavaDoc tmTimestamp = null;
125       
126       try
127       {
128          rsResults = updateStatement.executeQuery();
129             
130          if (rsResults.next())
131          {
132             iUpdateCount = rsResults.getInt(1);
133             tmTimestamp = rsResults.getTimestamp(2);
134          }
135       }
136       finally
137       {
138          DatabaseUtils.closeResultSet(rsResults);
139       }
140
141       if (iUpdateCount > 1)
142       {
143          throw new OSSInconsistentDataException(
144             "Inconsistent database contains multiple (" + iUpdateCount +
145             ") data with the same ID modified at the same time");
146       }
147       if (iUpdateCount == 1)
148       {
149         data.setModificationTimestamp(tmTimestamp);
150       }
151    }
152 }
153
Popular Tags