001: /*
002: * Copyright (c) JForum Team
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms,
006: * with or without modification, are permitted provided
007: * that the following conditions are met:
008: *
009: * 1) Redistributions of source code must retain the above
010: * copyright notice, this list of conditions and the
011: * following disclaimer.
012: * 2) Redistributions in binary form must reproduce the
013: * above copyright notice, this list of conditions and
014: * the following disclaimer in the documentation and/or
015: * other materials provided with the distribution.
016: * 3) Neither the name of "Rafael Steil" nor
017: * the names of its contributors may be used to endorse
018: * or promote products derived from this software without
019: * specific prior written permission.
020: *
021: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
022: * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
023: * EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
024: * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
025: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR
026: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
027: * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
028: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
029: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES
030: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
031: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
032: * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
033: * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
034: * IN CONTRACT, STRICT LIABILITY, OR TORT
035: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
036: * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
037: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
038: *
039: * Created on Dec 29, 2004 1:29:52 PM
040: * The JForum Project
041: * http://www.jforum.net
042: */
043: package net.jforum.dao.generic;
044:
045: import java.sql.PreparedStatement;
046: import java.sql.ResultSet;
047: import java.sql.SQLException;
048: import java.util.ArrayList;
049: import java.util.List;
050:
051: import net.jforum.JForumExecutionContext;
052: import net.jforum.entities.Config;
053: import net.jforum.exceptions.DatabaseException;
054: import net.jforum.util.DbUtils;
055: import net.jforum.util.preferences.SystemGlobals;
056:
057: /**
058: * @author Rafael Steil
059: * @version $Id: GenericConfigDAO.java,v 1.8 2006/08/23 02:13:42 rafaelsteil Exp $
060: */
061: public class GenericConfigDAO implements net.jforum.dao.ConfigDAO {
062: /**
063: * @see net.jforum.dao.ConfigDAO#insert(net.jforum.entities.Config)
064: */
065: public void insert(Config config) {
066: PreparedStatement p = null;
067: try {
068: p = JForumExecutionContext.getConnection()
069: .prepareStatement(
070: SystemGlobals.getSql("ConfigModel.insert"));
071: p.setString(1, config.getName());
072: p.setString(2, config.getValue());
073: p.executeUpdate();
074: } catch (SQLException e) {
075: throw new DatabaseException(e);
076: } finally {
077: DbUtils.close(p);
078: }
079: }
080:
081: /**
082: * @see net.jforum.dao.ConfigDAO#update(net.jforum.entities.Config)
083: */
084: public void update(Config config) {
085: PreparedStatement p = null;
086: try {
087: p = JForumExecutionContext.getConnection()
088: .prepareStatement(
089: SystemGlobals.getSql("ConfigModel.update"));
090: p.setString(1, config.getValue());
091: p.setString(2, config.getName());
092: p.executeUpdate();
093: } catch (SQLException e) {
094: throw new DatabaseException(e);
095: } finally {
096: DbUtils.close(p);
097: }
098: }
099:
100: /**
101: * @see net.jforum.dao.ConfigDAO#delete(net.jforum.entities.Config)
102: */
103: public void delete(Config config) {
104: PreparedStatement p = null;
105: try {
106: p = JForumExecutionContext.getConnection()
107: .prepareStatement(
108: SystemGlobals.getSql("ConfigModel.delete"));
109: p.setInt(1, config.getId());
110: p.executeUpdate();
111: } catch (SQLException e) {
112: throw new DatabaseException(e);
113: } finally {
114: DbUtils.close(p);
115: }
116: }
117:
118: /**
119: * @see net.jforum.dao.ConfigDAO#selectAll()
120: */
121: public List selectAll() {
122: List l = new ArrayList();
123:
124: PreparedStatement p = null;
125: ResultSet rs = null;
126: try {
127: p = JForumExecutionContext.getConnection()
128: .prepareStatement(
129: SystemGlobals
130: .getSql("ConfigModel.selectAll"));
131: rs = p.executeQuery();
132: while (rs.next()) {
133: l.add(this .makeConfig(rs));
134: }
135:
136: return l;
137: } catch (SQLException e) {
138: throw new DatabaseException(e);
139: } finally {
140: DbUtils.close(rs, p);
141: }
142: }
143:
144: /**
145: * @see net.jforum.dao.ConfigDAO#selectByName(java.lang.String)
146: */
147: public Config selectByName(String name) {
148: PreparedStatement p = null;
149: ResultSet rs = null;
150: try {
151: p = JForumExecutionContext
152: .getConnection()
153: .prepareStatement(
154: SystemGlobals
155: .getSql("ConfigModel.selectByName"));
156: p.setString(1, name);
157: rs = p.executeQuery();
158: Config c = null;
159:
160: if (rs.next()) {
161: c = this .makeConfig(rs);
162: }
163:
164: return c;
165: } catch (SQLException e) {
166: throw new DatabaseException(e);
167: } finally {
168: DbUtils.close(rs, p);
169: }
170: }
171:
172: protected Config makeConfig(ResultSet rs) throws SQLException {
173: Config c = new Config();
174: c.setId(rs.getInt("config_id"));
175: c.setName(rs.getString("config_name"));
176: c.setValue(rs.getString("config_value"));
177:
178: return c;
179: }
180: }
|