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: * This file creation date: 13/01/2004 / 12:02:54
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.Smilie;
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: GenericSmilieDAO.java,v 1.9 2007/02/25 13:48:33 rafaelsteil Exp $
060: */
061: public class GenericSmilieDAO extends AutoKeys implements
062: net.jforum.dao.SmilieDAO {
063: /**
064: * @see net.jforum.repository.SmilieDAO#addNew(net.jforum.entities.Smilie)
065: */
066: public int addNew(Smilie smilie) {
067: PreparedStatement p = null;
068: try {
069: p = this .getStatementForAutoKeys("SmiliesModel.addNew");
070:
071: p.setString(1, smilie.getCode());
072: p.setString(2, smilie.getUrl());
073: p.setString(3, smilie.getDiskName());
074:
075: this .setAutoGeneratedKeysQuery(SystemGlobals
076: .getSql("SmiliesModel.lastGeneratedSmilieId"));
077:
078: return this .executeAutoKeysQuery(p);
079: } catch (SQLException e) {
080: throw new DatabaseException(e);
081: } finally {
082: DbUtils.close(p);
083: }
084: }
085:
086: /**
087: * @see net.jforum.repository.SmilieDAO#delete(int, int)
088: */
089: public void delete(int id) {
090: PreparedStatement p = null;
091: try {
092: p = JForumExecutionContext
093: .getConnection()
094: .prepareStatement(
095: SystemGlobals.getSql("SmiliesModel.delete"));
096: p.setInt(1, id);
097: p.executeUpdate();
098: } catch (SQLException e) {
099: throw new DatabaseException(e);
100: } finally {
101: DbUtils.close(p);
102: }
103: }
104:
105: /**
106: * @see net.jforum.repository.SmilieDAO#update(net.jforum.entities.Smilie)
107: */
108: public void update(Smilie smilie) {
109: PreparedStatement p = null;
110: try {
111: p = JForumExecutionContext
112: .getConnection()
113: .prepareStatement(
114: SystemGlobals.getSql("SmiliesModel.update"));
115: p.setString(1, smilie.getCode());
116: p.setString(2, smilie.getUrl());
117: p.setString(3, smilie.getDiskName());
118: p.setInt(4, smilie.getId());
119:
120: p.executeUpdate();
121: } catch (SQLException e) {
122: throw new DatabaseException(e);
123: } finally {
124: DbUtils.close(p);
125: }
126: }
127:
128: private Smilie getSmilie(ResultSet rs) throws SQLException {
129: Smilie s = new Smilie();
130:
131: s.setId(rs.getInt("smilie_id"));
132: s.setCode(rs.getString("code"));
133: s.setUrl(rs.getString("url"));
134: s.setDiskName(rs.getString("disk_name"));
135:
136: return s;
137: }
138:
139: /**
140: * @see net.jforum.repository.SmilieDAO#selectAll()
141: */
142: public List selectAll() {
143: List l = new ArrayList();
144:
145: PreparedStatement p = null;
146: ResultSet rs = null;
147: try {
148: p = JForumExecutionContext.getConnection()
149: .prepareStatement(
150: SystemGlobals
151: .getSql("SmiliesModel.selectAll"));
152: rs = p.executeQuery();
153: while (rs.next()) {
154: l.add(this .getSmilie(rs));
155: }
156:
157: return l;
158: } catch (SQLException e) {
159: throw new DatabaseException(e);
160: } finally {
161: DbUtils.close(rs, p);
162: }
163: }
164:
165: /**
166: * @see net.jforum.dao.SmilieDAO#selectById(int)
167: */
168: public Smilie selectById(int id) {
169: PreparedStatement p = null;
170: ResultSet rs = null;
171: try {
172: p = JForumExecutionContext.getConnection()
173: .prepareStatement(
174: SystemGlobals
175: .getSql("SmiliesModel.selectById"));
176: p.setInt(1, id);
177:
178: Smilie s = new Smilie();
179:
180: rs = p.executeQuery();
181: if (rs.next()) {
182: s = this .getSmilie(rs);
183: }
184:
185: return s;
186: } catch (SQLException e) {
187: throw new DatabaseException(e);
188: } finally {
189: DbUtils.close(rs, p);
190: }
191: }
192: }
|