001: /*
002: * (C) Copyright 2003 Nabh Information Systems, Inc.
003: *
004: * All copyright notices regarding Nabh's products MUST remain
005: * intact in the scripts and in the outputted HTML.
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public License
008: * as published by the Free Software Foundation; either version 2.1
009: * of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: */
021:
022: package com.nabhinc.util.db;
023:
024: import java.sql.Connection;
025: import java.sql.PreparedStatement;
026: import java.sql.ResultSet;
027: import java.sql.SQLException;
028:
029: import javax.naming.NamingException;
030:
031: import org.apache.commons.logging.Log;
032: import org.apache.commons.logging.LogFactory;
033: import org.w3c.dom.Element;
034:
035: import com.nabhinc.core.Defaults;
036:
037: import com.nabhinc.util.XMLUtil;
038:
039: /**
040: * Create sequential IDs for database records.
041: *
042: * @author Padmanabh Dabke
043: * (c) 2003 Nabh Information Systems, Inc. All Rights Reserved.
044: */
045: public class SelectUpdateIDCreator implements IDCreator {
046:
047: private Log suiLogger = LogFactory.getLog(this .getClass());
048: private String icSelectSQL = null;
049:
050: private String icUpdateSQL = null;
051:
052: private String icDataSource = null;
053:
054: public SelectUpdateIDCreator() {
055: }
056:
057: /**
058: *
059: * @param ds
060: * @param select
061: * @param update
062: */
063: public SelectUpdateIDCreator(String ds, String select, String update) {
064: icDataSource = ds;
065: icSelectSQL = select;
066: icUpdateSQL = update;
067:
068: if (icDataSource == null)
069: icDataSource = Defaults.getDataSourceName();
070:
071: }
072:
073: /**
074: *
075: * @return
076: * @throws SQLException
077: * @throws NamingException
078: */
079: public int getNextID() throws SQLException, NamingException {
080: Connection conn = null;
081: PreparedStatement st = null;
082: boolean oldAutoCommit = true;
083: ResultSet results = null;
084: try {
085: int newID = 0;
086:
087: conn = DBUtil.getConnection(icDataSource);
088: oldAutoCommit = conn.getAutoCommit();
089: conn.setAutoCommit(false);
090: st = conn.prepareStatement(icSelectSQL);
091: results = st.executeQuery();
092:
093: if (!results.next()) {
094: suiLogger.error("Invalid select SQL: " + icSelectSQL);
095: throw new SQLException("Invalid select SQL: "
096: + icSelectSQL);
097: }
098:
099: newID = results.getInt(1) + 1;
100: st = conn.prepareStatement(icUpdateSQL);
101: st.setInt(1, newID);
102: st.execute();
103: conn.commit();
104: return newID;
105:
106: } catch (java.sql.SQLException ex) {
107: suiLogger.error(ex);
108: throw ex;
109: } finally {
110:
111: try {
112: results.close();
113: } catch (Exception ex) {
114: }
115:
116: try {
117: st.close();
118: } catch (Exception ex) {
119: }
120: try {
121: conn.setAutoCommit(oldAutoCommit);
122: conn.close();
123: } catch (Exception ex) {
124: // Ignore
125: }
126: }
127: }
128:
129: /* (non-Javadoc)
130: * @see com.nabhinc.core.XMLInitable#init(org.w3c.dom.Element)
131: */
132: public void init(Element config) throws Exception {
133: icDataSource = XMLUtil.getSubElementText(config, "data-source");
134: if (icDataSource == null)
135: icDataSource = Defaults.getDataSourceName();
136: icSelectSQL = XMLUtil.getSubElementText(config, "select-sql");
137: if (icSelectSQL == null)
138: throw new Exception("Missing required element: select-sql.");
139: icUpdateSQL = XMLUtil.getSubElementText(config, "update-sql");
140: if (icUpdateSQL == null)
141: throw new Exception("Missing required element: update-sql.");
142:
143: }
144:
145: /**
146: *
147: * @return
148: * @throws SQLException
149: * @throws NamingException
150: */
151: public int getNextID(Connection conn) throws SQLException {
152: PreparedStatement st = null;
153: ResultSet results = null;
154: try {
155: int newID = 0;
156: st = conn.prepareStatement(icSelectSQL);
157: results = st.executeQuery();
158:
159: if (!results.next()) {
160: suiLogger.error("Invalid select SQL: " + icSelectSQL);
161: throw new SQLException("Invalid select SQL: "
162: + icSelectSQL);
163: }
164:
165: newID = results.getInt(1) + 1;
166: st = conn.prepareStatement(icUpdateSQL);
167: st.setInt(1, newID);
168: st.execute();
169: conn.commit();
170: return newID;
171:
172: } finally {
173:
174: try {
175: results.close();
176: } catch (Exception ex) {
177: }
178:
179: try {
180: st.close();
181: } catch (Exception ex) {
182: }
183: }
184: }
185:
186: }
|