1 43 package net.jforum.dao.generic; 44 45 import java.sql.PreparedStatement ; 46 import java.sql.ResultSet ; 47 import java.util.ArrayList ; 48 import java.util.List ; 49 50 import net.jforum.JForumExecutionContext; 51 import net.jforum.entities.Category; 52 import net.jforum.util.preferences.SystemGlobals; 53 54 58 public class GenericCategoryDAO extends AutoKeys implements net.jforum.dao.CategoryDAO 59 { 60 63 public Category selectById(int categoryId) throws Exception 64 { 65 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("CategoryModel.selectById")); 66 p.setInt(1, categoryId); 67 68 ResultSet 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 84 public List selectAll() throws Exception 85 { 86 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("CategoryModel.selectAll")); 87 List l = new ArrayList (); 88 89 ResultSet 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 rs) throws Exception 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 115 public boolean canDelete(int categoryId) throws Exception 116 { 117 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("CategoryModel.canDelete")); 118 p.setInt(1, categoryId); 119 120 ResultSet 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 134 public void delete(int categoryId) throws Exception 135 { 136 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("CategoryModel.delete")); 137 p.setInt(1, categoryId); 138 p.executeUpdate(); 139 140 p.close(); 141 } 142 143 146 public void update(Category category) throws Exception 147 { 148 PreparedStatement 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 159 public int addNew(Category category) throws Exception 160 { 161 int order = 1; 162 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("CategoryModel.getMaxOrder")); 163 164 ResultSet 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 189 public void setOrderUp(Category category, Category relatedCategory) throws Exception 190 { 191 this.setOrder(category, relatedCategory, true); 192 } 193 194 197 public void setOrderDown(Category category, Category relatedCategory) throws Exception 198 { 199 this.setOrder(category, relatedCategory, false); 200 } 201 202 private void setOrder(Category category, Category otherCategory, boolean up) throws Exception 203 { 204 int tmpOrder = otherCategory.getOrder(); 205 otherCategory.setOrder(category.getOrder()); 206 category.setOrder(tmpOrder); 207 208 PreparedStatement 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 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 |