KickJava   Java API By Example, From Geeks To Geeks.

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


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.Category;
52 import net.jforum.util.preferences.SystemGlobals;
53
54 /**
55  * @author Rafael Steil
56  * @version $Id: GenericCategoryDAO.java,v 1.5 2006/01/29 15:06:26 rafaelsteil Exp $
57  */

58 public class GenericCategoryDAO extends AutoKeys implements net.jforum.dao.CategoryDAO
59 {
60     /**
61      * @see net.jforum.dao.CategoryDAO#selectById(int)
62      */

63     public Category selectById(int categoryId) throws Exception JavaDoc
64     {
65         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("CategoryModel.selectById"));
66         p.setInt(1, categoryId);
67         
68         ResultSet JavaDoc rs = p.executeQuery();
69         
70         Category c = new Category();
71         if (rs.next()) {
72             c = this.getCategory(rs);
73         }
74         
75         rs.close();
76         p.close();
77         
78         return c;
79     }
80
81     /**
82      * @see net.jforum.dao.CategoryDAO#selectAll()
83      */

84     public List JavaDoc selectAll() throws Exception JavaDoc
85     {
86         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("CategoryModel.selectAll"));
87         List JavaDoc l = new ArrayList JavaDoc();
88         
89         ResultSet JavaDoc rs = p.executeQuery();
90         while (rs.next()) {
91             l.add(this.getCategory(rs));
92         }
93         
94         rs.close();
95         p.close();
96             
97         return l;
98     }
99     
100     protected Category getCategory(ResultSet JavaDoc rs) throws Exception JavaDoc
101     {
102         Category c = new Category();
103         
104         c.setId(rs.getInt("categories_id"));
105         c.setName(rs.getString("title"));
106         c.setOrder(rs.getInt("display_order"));
107         c.setModerated(rs.getInt("moderated") == 1);
108         
109         return c;
110     }
111
112     /**
113      * @see net.jforum.dao.CategoryDAO#canDelete(int)
114      */

115     public boolean canDelete(int categoryId) throws Exception JavaDoc
116     {
117         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("CategoryModel.canDelete"));
118         p.setInt(1, categoryId);
119         
120         ResultSet JavaDoc rs = p.executeQuery();
121         if (!rs.next() || rs.getInt("total") < 1) {
122             return true;
123         }
124         
125         rs.close();
126         p.close();
127         
128         return false;
129     }
130
131     /**
132      * @see net.jforum.dao.CategoryDAO#delete(int)
133      */

134     public void delete(int categoryId) throws Exception JavaDoc
135     {
136         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("CategoryModel.delete"));
137         p.setInt(1, categoryId);
138         p.executeUpdate();
139         
140         p.close();
141     }
142
143     /**
144      * @see net.jforum.dao.CategoryDAO#update(net.jforum.Category)
145      */

146     public void update(Category category) throws Exception JavaDoc
147     {
148         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("CategoryModel.update"));
149         p.setString(1, category.getName());
150         p.setInt(2, category.isModerated() ? 1 : 0);
151         p.setInt(3, category.getId());
152         p.executeUpdate();
153         p.close();
154     }
155
156     /**
157      * @see net.jforum.dao.CategoryDAO#addNew(net.jforum.Category)
158      */

159     public int addNew(Category category) throws Exception JavaDoc
160     {
161         int order = 1;
162         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("CategoryModel.getMaxOrder"));
163         
164         ResultSet JavaDoc rs = p.executeQuery();
165         if (rs.next()) {
166             order = rs.getInt(1) + 1;
167         }
168         rs.close();
169         p.close();
170
171         p = this.getStatementForAutoKeys("CategoryModel.addNew");
172         p.setString(1, category.getName());
173         p.setInt(2, order);
174         p.setInt(3, category.isModerated() ? 1 : 0);
175         
176         this.setAutoGeneratedKeysQuery(SystemGlobals.getSql("CategoryModel.lastGeneratedCategoryId"));
177         int id = this.executeAutoKeysQuery(p);
178         
179         p.close();
180         
181         category.setId(id);
182         category.setOrder(order);
183         return id;
184     }
185     
186     /**
187      * @see net.jforum.dao.CategoryDAO#setOrderUp(Category, Category)
188      */

189     public void setOrderUp(Category category, Category relatedCategory) throws Exception JavaDoc
190     {
191         this.setOrder(category, relatedCategory, true);
192     }
193     
194     /**
195      * @see net.jforum.dao.CategoryDAO#setOrderDown(Category, Category)
196      */

197     public void setOrderDown(Category category, Category relatedCategory) throws Exception JavaDoc
198     {
199         this.setOrder(category, relatedCategory, false);
200     }
201     
202     private void setOrder(Category category, Category otherCategory, boolean up) throws Exception JavaDoc
203     {
204         int tmpOrder = otherCategory.getOrder();
205         otherCategory.setOrder(category.getOrder());
206         category.setOrder(tmpOrder);
207
208         // *********
209
PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("CategoryModel.setOrderById"));
210         p.setInt(1, otherCategory.getOrder());
211         p.setInt(2, otherCategory.getId());
212         p.executeUpdate();
213         p.close();
214
215         // *********
216
p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("CategoryModel.setOrderById"));
217         p.setInt(1, category.getOrder());
218         p.setInt(2, category.getId());
219         p.executeUpdate();
220         p.close();
221     }
222 }
223
Popular Tags