KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
3  *
4  * Project: OpenSubsystems
5  *
6  * $Id: DatabaseDataUtils.java,v 1.4 2007/01/07 06:14:18 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.ResultSet JavaDoc;
27 import java.sql.SQLException JavaDoc;
28 import java.sql.Timestamp JavaDoc;
29
30 import org.opensubsystems.core.error.OSSConcurentModifyException;
31 import org.opensubsystems.core.error.OSSDatabaseAccessException;
32 import org.opensubsystems.core.error.OSSException;
33 import org.opensubsystems.core.error.OSSInternalErrorException;
34 import org.opensubsystems.core.error.OSSInvalidContextException;
35 import org.opensubsystems.core.util.DatabaseUtils;
36
37 /**
38  * This class collects code fragments which are reusable for managing of the
39  * data in database.
40  *
41  * @version $Id: DatabaseDataUtils.java,v 1.4 2007/01/07 06:14:18 bastafidli Exp $
42  * @author Miro Halas
43  * @code.reviewer Miro Halas
44  * @code.reviewed 1.1 2005/08/20 21:17:14 bastafidli
45  */

46 public final class DatabaseDataUtils
47 {
48    // Constructors /////////////////////////////////////////////////////////////
49

50    /**
51     * Private constructor since this class cannot be instantiated
52     */

53    private DatabaseDataUtils(
54    )
55    {
56       // Do nothing
57
}
58    
59    // Public methods ///////////////////////////////////////////////////////////
60

61    /**
62     * Method to check if there was a concurrent modification error when update
63     * doesn't update anything in the database (updated count == 0).
64     *
65     * @param dbConnection - database connection
66     * @param strDataName - name of the data object
67     * @param strTableName - name of the table
68     * @param iId - id of the data object
69     * @param tmstpModificationDate - modification date of the data object
70     * @throws OSSException - more descriptive exception
71     */

72    public static void checkUpdateError(
73       Connection JavaDoc dbConnection,
74       String JavaDoc strDataName,
75       String JavaDoc strTableName,
76       int iId,
77       Timestamp JavaDoc tmstpModificationDate
78    ) throws OSSException
79    {
80       PreparedStatement JavaDoc psCheck = null;
81       ResultSet JavaDoc rsCheck = null;
82       
83       try
84       {
85          StringBuffer JavaDoc sbBuffer = new StringBuffer JavaDoc();
86          
87          // first try to select from DB existing data identified by ID
88
sbBuffer.append("select ID, MODIFICATION_DATE from ");
89          sbBuffer.append(strTableName);
90          sbBuffer.append(" where ID = ?");
91          
92          psCheck = dbConnection.prepareStatement(sbBuffer.toString());
93          psCheck.setInt(1, iId);
94          rsCheck = psCheck.executeQuery();
95       
96          if (rsCheck.next())
97          {
98             // if there was found data within the DB, check it's modification timestamp
99
if (rsCheck.getTimestamp("MODIFICATION_DATE").equals(tmstpModificationDate))
100             {
101                // there was found data object with specified id and modification
102
// date throw internal exception since this shouldn't happen
103
throw new OSSInternalErrorException(strDataName +
104                   " has been found within the database but it has not been updated.");
105             }
106             else
107             {
108                // There was found data object with specified id but with different
109
// modification date, throw concurent modify exception
110
throw new OSSConcurentModifyException(strDataName +
111                   " has been meanwhile modified by somebody else. Your" +
112                   " modifications were not performed to prevent overwriting" +
113                   " data you have not seen yet. We suggest you save your changes" +
114                   " to a temporary location, reload the data you are trying to" +
115                   " modify, review the changes that have been meanwhile done by" +
116                   " somebody else and then redo your changes if necessary.");
117             }
118          }
119          else
120          {
121             throw new OSSInvalidContextException(
122                strDataName + " does not exists anymore." +
123                " It might have been meanwhile deleted from the system.");
124          }
125       }
126       catch (SQLException JavaDoc eExc)
127       {
128          throw new OSSDatabaseAccessException(
129                       "Unexpected error accessing the database.", eExc);
130       }
131       finally
132       {
133          DatabaseUtils.closeResultSetAndStatement(rsCheck, psCheck);
134       }
135    }
136 }
137
Popular Tags