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 6, 2003 / 11:09:34 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.Banner;
053: import net.jforum.exceptions.DatabaseException;
054: import net.jforum.util.DbUtils;
055: import net.jforum.util.preferences.SystemGlobals;
056:
057: /**
058: * @author Samuel Yung
059: * @version $Id: GenericBannerDAO.java,v 1.9 2006/08/23 02:13:41 rafaelsteil Exp $
060: */
061: public class GenericBannerDAO extends AutoKeys implements
062: net.jforum.dao.BannerDAO {
063: public Banner selectById(int bannerId) {
064: PreparedStatement p = null;
065: ResultSet rs = null;
066: Banner b = null;
067: try {
068: p = JForumExecutionContext.getConnection()
069: .prepareStatement(
070: SystemGlobals
071: .getSql("BannerDAO.selectById"));
072: p.setInt(1, bannerId);
073:
074: rs = p.executeQuery();
075:
076: b = new Banner();
077: if (rs.next()) {
078: b = this .getBanner(rs);
079: }
080: } catch (SQLException e) {
081: throw new DatabaseException(e);
082: } finally {
083: DbUtils.close(rs, p);
084: }
085:
086: return b;
087: }
088:
089: public List selectAll() {
090: PreparedStatement p = null;
091: ResultSet rs = null;
092: try {
093: p = JForumExecutionContext
094: .getConnection()
095: .prepareStatement(
096: SystemGlobals.getSql("BannerDAO.selectAll"));
097: List l = new ArrayList();
098:
099: rs = p.executeQuery();
100: while (rs.next()) {
101: l.add(this .getBanner(rs));
102: }
103:
104: return l;
105: } catch (SQLException e) {
106: throw new DatabaseException(e);
107: } finally {
108: DbUtils.close(rs, p);
109: }
110: }
111:
112: protected Banner getBanner(ResultSet rs) throws SQLException {
113: Banner b = new Banner();
114:
115: b.setId(rs.getInt("banner_id"));
116: b.setName(rs.getString("banner_name"));
117: b.setPlacement(rs.getInt("banner_placement"));
118: b.setDescription(rs.getString("banner_description"));
119: b.setClicks(rs.getInt("banner_clicks"));
120: b.setViews(rs.getInt("banner_views"));
121: b.setUrl(rs.getString("banner_url"));
122: b.setWeight(rs.getInt("banner_weight"));
123: b.setActive(rs.getInt("banner_active") == 1);
124: b.setComment(rs.getString("banner_comment"));
125: b.setType(rs.getInt("banner_type"));
126: b.setWidth(rs.getInt("banner_width"));
127: b.setHeight(rs.getInt("banner_height"));
128:
129: return b;
130: }
131:
132: public boolean canDelete(int bannerId) {
133: boolean result = true;
134: PreparedStatement p = null;
135: ResultSet rs = null;
136: try {
137: p = JForumExecutionContext
138: .getConnection()
139: .prepareStatement(
140: SystemGlobals.getSql("BannerDAO.canDelete"));
141: p.setInt(1, bannerId);
142:
143: rs = p.executeQuery();
144: if (!rs.next() || rs.getInt("total") < 1) {
145: result = false;
146: }
147:
148: return result;
149: } catch (SQLException e) {
150: throw new DatabaseException(e);
151: } finally {
152: DbUtils.close(rs, p);
153: }
154: }
155:
156: public void delete(int bannerId) {
157: PreparedStatement p = null;
158: try {
159: p = JForumExecutionContext.getConnection()
160: .prepareStatement(
161: SystemGlobals.getSql("BannerDAO.delete"));
162: p.setInt(1, bannerId);
163: p.executeUpdate();
164: } catch (SQLException e) {
165: throw new DatabaseException(e);
166: } finally {
167: DbUtils.close(p);
168: }
169: }
170:
171: public void update(Banner banner) {
172: PreparedStatement p = null;
173: try {
174: p = JForumExecutionContext.getConnection()
175: .prepareStatement(
176: SystemGlobals.getSql("BannerDAO.update"));
177: setBannerParam(p, banner);
178: p.setInt(13, banner.getId());
179: p.executeUpdate();
180: } catch (SQLException e) {
181: throw new DatabaseException(e);
182: } finally {
183: DbUtils.close(p);
184: }
185: }
186:
187: public int addNew(Banner banner) {
188: PreparedStatement p = null;
189: try {
190: p = this .getStatementForAutoKeys("BannerDAO.addNew");
191: setBannerParam(p, banner);
192: int id = this .executeAutoKeysQuery(p);
193:
194: banner.setId(id);
195: return id;
196: } catch (SQLException e) {
197: throw new DatabaseException(e);
198: } finally {
199: DbUtils.close(p);
200: }
201: }
202:
203: protected void setBannerParam(PreparedStatement p, Banner b)
204: throws SQLException {
205: p.setString(1, b.getName());
206: p.setInt(2, b.getPlacement());
207: p.setString(3, b.getDescription());
208: p.setInt(4, b.getClicks());
209: p.setInt(5, b.getViews());
210: p.setString(6, b.getUrl());
211: p.setInt(7, b.getWeight());
212: p.setInt(8, b.isActive() ? 1 : 0);
213: p.setString(9, b.getComment());
214: p.setInt(10, b.getType());
215: p.setInt(11, b.getWidth());
216: p.setInt(12, b.getHeight());
217: }
218:
219: public List selectActiveBannerByPlacement(int placement) {
220: PreparedStatement p = null;
221: ResultSet rs = null;
222: try {
223: p = JForumExecutionContext
224: .getConnection()
225: .prepareStatement(
226: SystemGlobals
227: .getSql("BannerDAO.selectActiveBannerByPlacement"));
228: p.setInt(1, placement);
229:
230: List l = new ArrayList();
231:
232: rs = p.executeQuery();
233: while (rs.next()) {
234: l.add(this .getBanner(rs));
235: }
236:
237: return l;
238: } catch (SQLException e) {
239: throw new DatabaseException(e);
240: } finally {
241: DbUtils.close(rs, p);
242: }
243: }
244: }
|