GenericCategoryDAO.java
Upload User: gdxydsw
Upload Date: 2019-01-29
Package Size: 16721k
Code Size: 7k
Category:

Java Develop

Development Platform:

Java

  1. /*
  2.  * Copyright (c) JForum Team
  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. import java.sql.PreparedStatement;
  45. import java.sql.ResultSet;
  46. import java.sql.SQLException;
  47. import java.util.ArrayList;
  48. import java.util.List;
  49. import net.jforum.JForumExecutionContext;
  50. import net.jforum.entities.Category;
  51. import net.jforum.exceptions.DatabaseException;
  52. import net.jforum.util.DbUtils;
  53. import net.jforum.util.preferences.SystemGlobals;
  54. /**
  55.  * @author Rafael Steil
  56.  * @version $Id: GenericCategoryDAO.java,v 1.9 2007/07/28 14:17:10 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)
  64. {
  65. PreparedStatement p = null;
  66. ResultSet rs = null;
  67. try {
  68. p = JForumExecutionContext.getConnection().prepareStatement(
  69. SystemGlobals.getSql("CategoryModel.selectById"));
  70. p.setInt(1, categoryId);
  71. rs = p.executeQuery();
  72. Category c = new Category();
  73. if (rs.next()) {
  74. c = this.getCategory(rs);
  75. }
  76. return c;
  77. }
  78. catch (SQLException e) {
  79. throw new DatabaseException(e);
  80. }
  81. finally {
  82. DbUtils.close(rs, p);
  83. }
  84. }
  85. /**
  86.  * @see net.jforum.dao.CategoryDAO#selectAll()
  87.  */
  88. public List selectAll()
  89. {
  90. PreparedStatement p = null;
  91. ResultSet rs = null;
  92. try {
  93. p = JForumExecutionContext.getConnection()
  94. .prepareStatement(SystemGlobals.getSql("CategoryModel.selectAll"));
  95. List l = new ArrayList();
  96. rs = p.executeQuery();
  97. while (rs.next()) {
  98. l.add(this.getCategory(rs));
  99. }
  100. return l;
  101. }
  102. catch (SQLException e) {
  103. throw new DatabaseException(e);
  104. }
  105. finally {
  106. DbUtils.close(rs, p);
  107. }
  108. }
  109. protected Category getCategory(ResultSet rs) throws SQLException
  110. {
  111. Category c = new Category();
  112. c.setId(rs.getInt("categories_id"));
  113. c.setName(rs.getString("title"));
  114. c.setOrder(rs.getInt("display_order"));
  115. c.setModerated(rs.getInt("moderated") == 1);
  116. return c;
  117. }
  118. /**
  119.  * @see net.jforum.dao.CategoryDAO#canDelete(int)
  120.  */
  121. public boolean canDelete(int categoryId)
  122. {
  123. PreparedStatement p = null;
  124. ResultSet rs = null;
  125. try {
  126. p = JForumExecutionContext.getConnection()
  127. .prepareStatement(SystemGlobals.getSql("CategoryModel.canDelete"));
  128. p.setInt(1, categoryId);
  129. rs = p.executeQuery();
  130. return !rs.next() || rs.getInt("total") < 1;
  131. }
  132. catch (SQLException e) {
  133. throw new DatabaseException(e);
  134. }
  135. finally {
  136. DbUtils.close(rs, p);
  137. }
  138. }
  139. /**
  140.  * @see net.jforum.dao.CategoryDAO#delete(int)
  141.  */
  142. public void delete(int categoryId)
  143. {
  144. PreparedStatement p = null;
  145. try {
  146. p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("CategoryModel.delete"));
  147. p.setInt(1, categoryId);
  148. p.executeUpdate();
  149. }
  150. catch (SQLException e) {
  151. throw new DatabaseException(e);
  152. }
  153. finally {
  154. DbUtils.close(p);
  155. }
  156. }
  157. /**
  158.  * @see net.jforum.dao.CategoryDAO#update(net.jforum.entities.Category)
  159.  */
  160. public void update(Category category)
  161. {
  162. PreparedStatement p = null;
  163. try {
  164. p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("CategoryModel.update"));
  165. p.setString(1, category.getName());
  166. p.setInt(2, category.isModerated() ? 1 : 0);
  167. p.setInt(3, category.getId());
  168. p.executeUpdate();
  169. }
  170. catch (SQLException e) {
  171. throw new DatabaseException(e);
  172. }
  173. finally {
  174. DbUtils.close(p);
  175. }
  176. }
  177. /**
  178.  * @see net.jforum.dao.CategoryDAO#addNew(net.jforum.entities.Category)
  179.  */
  180. public int addNew(Category category)
  181. {
  182. int order = 1;
  183. ResultSet rs = null;
  184. PreparedStatement p = null;
  185. try {
  186. p = JForumExecutionContext.getConnection().prepareStatement(
  187. SystemGlobals.getSql("CategoryModel.getMaxOrder"));
  188. rs = p.executeQuery();
  189. if (rs.next()) {
  190. order = rs.getInt(1) + 1;
  191. }
  192. rs.close();
  193. rs = null;
  194. p.close();
  195. p = null;
  196. p = this.getStatementForAutoKeys("CategoryModel.addNew");
  197. p.setString(1, category.getName());
  198. p.setInt(2, order);
  199. p.setInt(3, category.isModerated() ? 1 : 0);
  200. this.setAutoGeneratedKeysQuery(SystemGlobals.getSql("CategoryModel.lastGeneratedCategoryId"));
  201. int id = this.executeAutoKeysQuery(p);
  202. category.setId(id);
  203. category.setOrder(order);
  204. return id;
  205. }
  206. catch (SQLException e) {
  207. throw new DatabaseException(e);
  208. }
  209. finally {
  210. DbUtils.close(rs, p);
  211. }
  212. }
  213. /**
  214.  * @see net.jforum.dao.CategoryDAO#setOrderUp(Category, Category)
  215.  */
  216. public void setOrderUp(Category category, Category relatedCategory)
  217. {
  218. this.setOrder(category, relatedCategory);
  219. }
  220. /**
  221.  * @see net.jforum.dao.CategoryDAO#setOrderDown(Category, Category)
  222.  */
  223. public void setOrderDown(Category category, Category relatedCategory)
  224. {
  225. this.setOrder(category, relatedCategory);
  226. }
  227. /**
  228.  * @param category Category
  229.  * @param otherCategory Category
  230.  */
  231. private void setOrder(Category category, Category otherCategory)
  232. {
  233. int tmpOrder = otherCategory.getOrder();
  234. otherCategory.setOrder(category.getOrder());
  235. category.setOrder(tmpOrder);
  236. PreparedStatement p = null;
  237. try {
  238. p = JForumExecutionContext.getConnection().prepareStatement(
  239. SystemGlobals.getSql("CategoryModel.setOrderById"));
  240. p.setInt(1, otherCategory.getOrder());
  241. p.setInt(2, otherCategory.getId());
  242. p.executeUpdate();
  243. p.close();
  244. p = null;
  245. p = JForumExecutionContext.getConnection().prepareStatement(
  246. SystemGlobals.getSql("CategoryModel.setOrderById"));
  247. p.setInt(1, category.getOrder());
  248. p.setInt(2, category.getId());
  249. p.executeUpdate();
  250. }
  251. catch (SQLException e) {
  252. throw new DatabaseException(e);
  253. }
  254. finally {
  255. DbUtils.close(p);
  256. }
  257. }
  258. }