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: Mar 23, 2003 / 7:52:13 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.Ranking;
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: GenericRankingDAO.java,v 1.9 2006/12/02 03:19:44 rafaelsteil Exp $
060: */
061: public class GenericRankingDAO implements net.jforum.dao.RankingDAO {
062: /**
063: * @see net.jforum.dao.RankingDAO#selectById(int)
064: */
065: public Ranking selectById(int rankingId) {
066: Ranking ranking = new Ranking();
067:
068: PreparedStatement p = null;
069: ResultSet rs = null;
070: try {
071: p = JForumExecutionContext.getConnection()
072: .prepareStatement(
073: SystemGlobals
074: .getSql("RankingModel.selectById"));
075: p.setInt(1, rankingId);
076:
077: rs = p.executeQuery();
078:
079: if (rs.next()) {
080: ranking = this .buildRanking(rs);
081: }
082:
083: return ranking;
084: } catch (SQLException e) {
085: throw new DatabaseException(e);
086: } finally {
087: DbUtils.close(rs, p);
088: }
089: }
090:
091: /**
092: * @see net.jforum.dao.RankingDAO#selectAll()
093: */
094: public List selectAll() {
095: List l = new ArrayList();
096: PreparedStatement p = null;
097: ResultSet rs = null;
098: try {
099: p = JForumExecutionContext.getConnection()
100: .prepareStatement(
101: SystemGlobals
102: .getSql("RankingModel.selectAll"));
103: rs = p.executeQuery();
104:
105: while (rs.next()) {
106: Ranking ranking = buildRanking(rs);
107: l.add(ranking);
108: }
109:
110: return l;
111: } catch (SQLException e) {
112: throw new DatabaseException(e);
113: } finally {
114: DbUtils.close(rs, p);
115: }
116: }
117:
118: /**
119: * @see net.jforum.dao.RankingDAO#delete(int)
120: */
121: public void delete(int rankingId) {
122: PreparedStatement p = null;
123: try {
124: p = JForumExecutionContext
125: .getConnection()
126: .prepareStatement(
127: SystemGlobals.getSql("RankingModel.delete"));
128: p.setInt(1, rankingId);
129:
130: p.executeUpdate();
131: } catch (SQLException e) {
132: throw new DatabaseException(e);
133: } finally {
134: DbUtils.close(p);
135: }
136: }
137:
138: /**
139: * @see net.jforum.dao.RankingDAO#update(net.jforum.entities.Ranking)
140: */
141: public void update(Ranking ranking) {
142: PreparedStatement p = null;
143: try {
144: p = JForumExecutionContext
145: .getConnection()
146: .prepareStatement(
147: SystemGlobals.getSql("RankingModel.update"));
148:
149: p.setString(1, ranking.getTitle());
150: p.setString(2, ranking.getImage());
151: p.setInt(3, ranking.isSpecial() ? 1 : 0);
152: p.setInt(4, ranking.getMin());
153: p.setInt(5, ranking.getId());
154:
155: p.executeUpdate();
156: } catch (SQLException e) {
157: throw new DatabaseException(e);
158: } finally {
159: DbUtils.close(p);
160: }
161: }
162:
163: /**
164: * @see net.jforum.dao.RankingDAO#addNew(net.jforum.entities.Ranking)
165: */
166: public void addNew(Ranking ranking) {
167: PreparedStatement p = null;
168: try {
169: p = JForumExecutionContext
170: .getConnection()
171: .prepareStatement(
172: SystemGlobals.getSql("RankingModel.addNew"));
173:
174: p.setString(1, ranking.getTitle());
175: p.setInt(2, ranking.getMin());
176: p.setInt(3, ranking.isSpecial() ? 1 : 0);
177:
178: p.executeUpdate();
179: } catch (SQLException e) {
180: throw new DatabaseException(e);
181: } finally {
182: DbUtils.close(p);
183: }
184: }
185:
186: public List selectSpecials() {
187: List l = new ArrayList();
188:
189: PreparedStatement p = null;
190: ResultSet rs = null;
191:
192: try {
193: p = JForumExecutionContext
194: .getConnection()
195: .prepareStatement(
196: SystemGlobals
197: .getSql("RankingModel.selectSpecials"));
198: rs = p.executeQuery();
199:
200: while (rs.next()) {
201: Ranking ranking = this .buildRanking(rs);
202: l.add(ranking);
203: }
204: } catch (SQLException e) {
205: throw new DatabaseException(e);
206: } finally {
207: DbUtils.close(rs, p);
208: }
209:
210: return l;
211: }
212:
213: private Ranking buildRanking(ResultSet rs) throws SQLException {
214: Ranking ranking = new Ranking();
215:
216: ranking.setId(rs.getInt("rank_id"));
217: ranking.setTitle(rs.getString("rank_title"));
218: ranking.setImage(rs.getString("rank_image"));
219: ranking.setMin(rs.getInt("rank_min"));
220: ranking.setSpecial(rs.getInt("rank_special") == 1);
221:
222: return ranking;
223: }
224: }
|