KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > jforum > dao > generic > AutoKeys


1 /*
2  * Copyright (c) Rafael Steil
3  * All rights reserved.
4
5  * Redistribution and use in source and binary forms,
6  * with or without modification, are permitted provided
7  * that the following conditions are met:
8
9  * 1) Redistributions of source code must retain the above
10  * copyright notice, this list of conditions and the
11  * following disclaimer.
12  * 2) Redistributions in binary form must reproduce the
13  * above copyright notice, this list of conditions and
14  * the following disclaimer in the documentation and/or
15  * other materials provided with the distribution.
16  * 3) Neither the name of "Rafael Steil" nor
17  * the names of its contributors may be used to endorse
18  * or promote products derived from this software without
19  * specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
22  * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
23  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
24  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
27  * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
32  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
34  * IN CONTRACT, STRICT LIABILITY, OR TORT
35  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
36  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
37  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
38  *
39  * Created on 24/05/2004 17:40:25
40  * The JForum Project
41  * http://www.jforum.net
42  */

43 package net.jforum.dao.generic;
44
45 import java.sql.Connection JavaDoc;
46 import java.sql.PreparedStatement JavaDoc;
47 import java.sql.ResultSet JavaDoc;
48 import java.sql.SQLException JavaDoc;
49 import java.sql.Statement JavaDoc;
50
51 import net.jforum.JForumExecutionContext;
52 import net.jforum.util.preferences.ConfigKeys;
53 import net.jforum.util.preferences.SystemGlobals;
54
55 /**
56  * @author Rafael Steil
57  * @version $Id: AutoKeys.java,v 1.5 2006/01/29 15:06:24 rafaelsteil Exp $
58  */

59 public class AutoKeys
60 {
61     private static boolean supportAutoGeneratedKeys = SystemGlobals.getBoolValue(ConfigKeys.DATABASE_AUTO_KEYS);
62     private String JavaDoc autoGeneratedKeysQuery;
63     
64     protected boolean supportAutoGeneratedKeys()
65     {
66         return supportAutoGeneratedKeys;
67     }
68
69     /**
70      * Query to execute when {@link #setSupportAutoGeneratedKey(boolean)} is set
71      * to <code>false</code>
72      * @param query The query to execute to retreive the last generated key
73      */

74     protected void setAutoGeneratedKeysQuery(String JavaDoc query)
75     {
76         this.autoGeneratedKeysQuery = query;
77     }
78     
79     protected String JavaDoc getAutoGeneratedKeysQuery()
80     {
81         return this.autoGeneratedKeysQuery;
82     }
83     
84     protected String JavaDoc getErrorMessage()
85     {
86         return "Could not obtain the latest generated key. This error may be associated"
87             +" to some invalid database driver operation or server failure."
88             +" Please check the database configurations and code logic.";
89     }
90     
91     protected PreparedStatement JavaDoc getStatementForAutoKeys(String JavaDoc queryName, Connection JavaDoc conn) throws SQLException JavaDoc
92     {
93         PreparedStatement JavaDoc p = null;
94         if (this.supportAutoGeneratedKeys()) {
95             p = conn.prepareStatement(SystemGlobals.getSql(queryName),
96                 Statement.RETURN_GENERATED_KEYS);
97         }
98         else {
99             p = conn.prepareStatement(SystemGlobals.getSql(queryName));
100         }
101         
102         return p;
103     }
104     
105     protected PreparedStatement JavaDoc getStatementForAutoKeys(String JavaDoc queryName) throws SQLException JavaDoc
106     {
107         return this.getStatementForAutoKeys(queryName, JForumExecutionContext.getConnection());
108     }
109     
110     protected int executeAutoKeysQuery(PreparedStatement JavaDoc p) throws SQLException JavaDoc
111     {
112         return this.executeAutoKeysQuery(p, JForumExecutionContext.getConnection());
113     }
114     
115     protected int executeAutoKeysQuery(PreparedStatement JavaDoc p, Connection JavaDoc conn) throws SQLException JavaDoc
116     {
117         int id = -1;
118         p.executeUpdate();
119         
120         if (this.supportAutoGeneratedKeys()) {
121             ResultSet JavaDoc rs = p.getGeneratedKeys();
122             if (rs.next()) {
123                 id = rs.getInt(1);
124             }
125             rs.close();
126         }
127         else {
128             p = conn.prepareStatement(this.getAutoGeneratedKeysQuery());
129             ResultSet JavaDoc rs = p.executeQuery();
130             
131             if (rs.next()) {
132                 id = rs.getInt(1);
133             }
134             
135             rs.close();
136         }
137     
138         boolean valid = true;
139         if (id == -1) {
140             valid = false;
141         }
142         
143         if (!valid) {
144             throw new SQLException JavaDoc(this.getErrorMessage());
145         }
146         
147         return id;
148     }
149 }
150
Popular Tags