KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > jforum > dao > generic > GenericBannerDAO


1 /*
2  * Copyright (c) 2003, Rafael Steil
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms,
6  * with or without modification, are permitted provided
7  * that the following conditions are met:
8  *
9  * 1) Redistributions of source code must retain the above
10  * copyright notice, this list of conditions and the
11  * following disclaimer.
12  * 2) Redistributions in binary form must reproduce the
13  * above copyright notice, this list of conditions and
14  * the following disclaimer in the documentation and/or
15  * other materials provided with the distribution.
16  * 3) Neither the name of "Rafael Steil" nor
17  * the names of its contributors may be used to endorse
18  * or promote products derived from this software without
19  * specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
22  * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
23  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
24  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
27  * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
32  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
34  * IN CONTRACT, STRICT LIABILITY, OR TORT
35  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
36  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
37  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
38  *
39  * This file creation date: Mar 6, 2003 / 11:09:34 PM
40  * The JForum Project
41  * http://www.jforum.net
42  */

43 package net.jforum.dao.generic;
44
45 import java.sql.PreparedStatement JavaDoc;
46 import java.sql.ResultSet JavaDoc;
47 import java.util.ArrayList JavaDoc;
48 import java.util.List JavaDoc;
49
50 import net.jforum.JForumExecutionContext;
51 import net.jforum.entities.Banner;
52 import net.jforum.util.preferences.SystemGlobals;
53
54 /**
55  * @author Samuel Yung
56  * @version $Id: GenericBannerDAO.java,v 1.6 2006/01/29 15:06:22 rafaelsteil Exp $
57  */

58 public class GenericBannerDAO extends AutoKeys implements net.jforum.dao.BannerDAO
59 {
60     public Banner selectById(int bannerId) throws Exception JavaDoc
61     {
62         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("BannerDAO.selectById"));
63         p.setInt(1, bannerId);
64
65         ResultSet JavaDoc rs = p.executeQuery();
66
67         Banner b = new Banner();
68         if (rs.next()) {
69             b = this.getBanner(rs);
70         }
71
72         rs.close();
73         p.close();
74
75         return b;
76     }
77
78     public List JavaDoc selectAll() throws Exception JavaDoc
79     {
80         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("BannerDAO.selectAll"));
81         List JavaDoc l = new ArrayList JavaDoc();
82
83         ResultSet JavaDoc rs = p.executeQuery();
84         while (rs.next()) {
85             l.add(this.getBanner(rs));
86         }
87
88         rs.close();
89         p.close();
90
91         return l;
92     }
93
94     protected Banner getBanner(ResultSet JavaDoc rs) throws Exception JavaDoc
95     {
96         Banner b = new Banner();
97
98         b.setId(rs.getInt("banner_id"));
99         b.setName(rs.getString("banner_name"));
100         b.setPlacement(rs.getInt("banner_placement"));
101         b.setDescription(rs.getString("banner_description"));
102         b.setClicks(rs.getInt("banner_clicks"));
103         b.setViews(rs.getInt("banner_views"));
104         b.setUrl(rs.getString("banner_url"));
105         b.setWeight(rs.getInt("banner_weight"));
106         b.setActive(rs.getInt("banner_active") == 1);
107         b.setComment(rs.getString("banner_comment"));
108         b.setType(rs.getInt("banner_type"));
109         b.setWidth(rs.getInt("banner_width"));
110         b.setHeight(rs.getInt("banner_height"));
111         
112         return b;
113     }
114
115     public boolean canDelete(int bannerId) throws Exception JavaDoc
116     {
117         boolean result = true;
118         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("BannerDAO.canDelete"));
119         p.setInt(1, bannerId);
120
121         ResultSet JavaDoc rs = p.executeQuery();
122         if (!rs.next() || rs.getInt("total") < 1) {
123             result = false;
124         }
125
126         rs.close();
127         p.close();
128
129         return result;
130     }
131
132     public void delete(int bannerId) throws Exception JavaDoc
133     {
134         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("BannerDAO.delete"));
135         p.setInt(1, bannerId);
136         p.executeUpdate();
137
138         p.close();
139     }
140
141     public void update(Banner banner) throws Exception JavaDoc
142     {
143         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("BannerDAO.update"));
144         setBannerParam(p, banner);
145         p.setInt(13, banner.getId());
146         p.executeUpdate();
147         p.close();
148     }
149
150     public int addNew(Banner banner) throws Exception JavaDoc
151     {
152         PreparedStatement JavaDoc p = this.getStatementForAutoKeys("BannerDAO.addNew");
153         setBannerParam(p, banner);
154         int id = this.executeAutoKeysQuery(p);
155         p.close();
156
157         banner.setId(id);
158         return id;
159     }
160
161     protected void setBannerParam(PreparedStatement JavaDoc p, Banner b) throws Exception JavaDoc
162     {
163         p.setString(1, b.getName());
164         p.setInt(2, b.getPlacement());
165         p.setString(3, b.getDescription());
166         p.setInt(4, b.getClicks());
167         p.setInt(5, b.getViews());
168         p.setString(6, b.getUrl());
169         p.setInt(7, b.getWeight());
170         p.setInt(8, b.isActive() ? 1 : 0);
171         p.setString(9, b.getComment());
172         p.setInt(10, b.getType());
173         p.setInt(11, b.getWidth());
174         p.setInt(12, b.getHeight());
175     }
176
177     public List JavaDoc selectActiveBannerByPlacement(int placement) throws Exception JavaDoc
178     {
179         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(
180                 SystemGlobals.getSql("BannerDAO.selectActiveBannerByPlacement"));
181         p.setInt(1, placement);
182
183         List JavaDoc l = new ArrayList JavaDoc();
184
185         ResultSet JavaDoc rs = p.executeQuery();
186         while (rs.next()) {
187             l.add(this.getBanner(rs));
188         }
189
190         rs.close();
191         p.close();
192
193         return l;
194     }
195 }
196
Popular Tags