1 53 54 package com.Yasna.forum.database; 55 56 import java.util.Iterator ; 57 import java.util.Enumeration ; 58 import java.util.Properties ; 59 import java.util.ArrayList ; 60 import java.util.Date ; 61 import java.sql.*; 62 import java.io.*; 63 64 import com.Yasna.forum.*; 65 import com.Yasna.forum.filter.*; 66 import com.Yasna.util.Cache; 67 import com.Yasna.util.Cacheable; 68 import com.Yasna.util.CacheSizes; 69 70 76 public class DbForumGroup implements ForumGroup, Cacheable { 77 78 79 private static final String LOAD_FORUM_GROUP_BY_ID = 80 "SELECT forumGroupID, name, description, creationDate, modifiedDate,grporder FROM yazdForumGroup WHERE forumGroupID=?"; 81 private static final String UPDATE_FORUM_GROUP_MODIFIED_DATE = 82 "UPDATE yazdForumGroup SET modifiedDate=? WHERE forumGroupID=?"; 83 private static final String ADD_FORUM_GROUP = 84 "INSERT INTO yazdForumGroup(forumGroupID, categoryID, name, description, creationDate, " + 85 "modifiedDate,grporder) VALUES (?,?,?,?,?,?,0)"; 86 private static final String SAVE_FORUM_GROUP = 87 "UPDATE yazdForumGroup SET name=?, description=?, creationDate=?, " + 88 "modifiedDate=?,grporder=? WHERE forumGroupID=?"; 89 90 private int id = -1; 91 private String name; 92 private String description; 93 private int categoryID; 94 private int grporder=0; 95 private java.util.Date creationDate; 96 private java.util.Date modifiedDate; 97 98 private DbForumFactory factory; 99 private DbCategory category; 100 101 104 protected DbForumGroup(int id, DbCategory category, DbForumFactory factory) 105 throws ForumGroupNotFoundException 106 { 107 this.id = id; 108 this.category = category; 109 this.factory = factory; 110 loadFromDb(); 111 } 112 113 116 protected DbForumGroup(String name, String description, DbCategory category, DbForumFactory factory) 117 throws UnauthorizedException 118 { 119 this.id = DbSequenceManager.nextID("ForumGroup"); 120 this.name = name; 121 this.categoryID = category.getID(); 122 this.description = description; 123 long now = System.currentTimeMillis(); 124 creationDate = new java.util.Date (now); 125 modifiedDate = new java.util.Date (now); 126 this.category = category; 127 this.factory = factory; 128 insertIntoDb(); 129 } 130 131 133 public int getID() { 134 return id; 135 } 136 137 public String getName() { 138 return name; 139 } 140 public int getOrder(){ 141 return grporder; 142 } 143 public void setOrder(int param){ 144 this.grporder = param; 145 saveToDb(); 146 } 147 public void setName(String name) throws UnauthorizedException { 148 this.name = name; 149 saveToDb(); 150 } 151 152 public String getDescription() { 153 return description; 154 } 155 156 public void setDescription(String description) throws UnauthorizedException 157 { 158 this.description = description; 159 saveToDb(); 160 } 161 162 public java.util.Date getCreationDate() { 163 return creationDate; 164 } 165 166 public void setCreationDate(java.util.Date creationDate) 167 throws UnauthorizedException 168 { 169 this.creationDate = creationDate; 170 saveToDb(); 171 } 172 173 public java.util.Date getModifiedDate() { 174 return modifiedDate; 175 } 176 177 public void setModifiedDate(java.util.Date modifiedDate) 178 throws UnauthorizedException 179 { 180 this.modifiedDate = modifiedDate; 181 saveToDb(); 182 } 183 public Iterator forumGroups() { 184 return null; } 186 187 188 190 public int getSize() { 191 int size = 0; 194 size += CacheSizes.sizeOfObject(); size += CacheSizes.sizeOfInt(); size += CacheSizes.sizeOfString(name); size += CacheSizes.sizeOfString(description); size += CacheSizes.sizeOfDate(); size += CacheSizes.sizeOfDate(); size += CacheSizes.sizeOfObject(); size += CacheSizes.sizeOfInt(); 203 return size; 204 } 205 206 210 protected void updateModifiedDate(java.util.Date modifiedDate) { 211 this.modifiedDate = modifiedDate; 212 Connection con = null; 213 PreparedStatement pstmt = null; 214 try { 215 con = DbConnectionManager.getConnection(); 216 pstmt = con.prepareStatement(UPDATE_FORUM_GROUP_MODIFIED_DATE); 217 pstmt.setString(1, ""+modifiedDate.getTime()); 218 pstmt.setInt(2, id); 219 pstmt.executeUpdate(); 220 } 221 catch( SQLException sqle ) { 222 System.err.println("Error in DbCategory:updateModifiedDate()-" + sqle); 223 sqle.printStackTrace(); 224 } 225 finally { 226 try { pstmt.close(); } 227 catch (Exception e) { e.printStackTrace(); } 228 try { con.close(); } 229 catch (Exception e) { e.printStackTrace(); } 230 } 231 } 232 233 234 237 private void loadFromDb() throws ForumGroupNotFoundException { 238 Connection con = null; 239 PreparedStatement pstmt = null; 240 try { 241 con = DbConnectionManager.getConnection(); 242 244 pstmt = con.prepareStatement(LOAD_FORUM_GROUP_BY_ID); 245 pstmt.setInt(1, id); 246 ResultSet rs = pstmt.executeQuery(); 247 if( !rs.next() ) { 248 throw new ForumGroupNotFoundException("Category " + getID() + 249 " could not be loaded from the database."); 250 } 251 id = rs.getInt("forumGroupID"); 252 name = rs.getString("name"); 253 description = rs.getString("description"); 254 this.creationDate = 255 new java.util.Date (Long.parseLong(rs.getString("creationDate").trim())); 256 this.modifiedDate = 257 new java.util.Date (Long.parseLong(rs.getString("modifiedDate").trim())); 258 this.grporder = rs.getInt("grporder"); 259 } 260 catch( SQLException sqle ) { 261 sqle.printStackTrace(); 262 throw new ForumGroupNotFoundException("Category " + getID() + 263 " could not be loaded from the database."); 264 } 265 catch (NumberFormatException nfe) { 266 System.err.println("WARNING: In DbCAtegory.loadFromDb() -- there " + 267 "was an error parsing the dates returned from the database. Ensure " + 268 "that they're being stored correctly."); 269 } 270 finally { 271 try { pstmt.close(); } 272 catch (Exception e) { e.printStackTrace(); } 273 try { con.close(); } 274 catch (Exception e) { e.printStackTrace(); } 275 } 276 } 277 278 281 private void insertIntoDb() { 282 Connection con = null; 283 PreparedStatement pstmt = null; 284 try { 285 con = DbConnectionManager.getConnection(); 286 pstmt = con.prepareStatement(ADD_FORUM_GROUP); 287 pstmt.setInt(1,id); 288 pstmt.setInt(2,categoryID); 289 pstmt.setString(3,name); 290 pstmt.setString(4,description); 291 pstmt.setString(5, Long.toString(creationDate.getTime())); 292 pstmt.setString(6, Long.toString(modifiedDate.getTime())); 293 pstmt.executeUpdate(); 294 } 295 catch( SQLException sqle ) { 296 System.err.println("Error in DbCategory:insertIntoDb()-" + sqle); 297 sqle.printStackTrace(); 298 } 299 finally { 300 try { pstmt.close(); } 301 catch (Exception e) { e.printStackTrace(); } 302 try { con.close(); } 303 catch (Exception e) { e.printStackTrace(); } 304 } 305 } 306 307 310 private synchronized void saveToDb() { 311 Connection con = null; 312 PreparedStatement pstmt = null; 313 try { 314 con = DbConnectionManager.getConnection(); 315 pstmt = con.prepareStatement(SAVE_FORUM_GROUP); 316 pstmt.setString(1, name); 317 pstmt.setString(2, description); 318 pstmt.setString(3, Long.toString(creationDate.getTime())); 319 pstmt.setString(4, Long.toString(modifiedDate.getTime())); 320 pstmt.setInt(5,this.grporder); 321 pstmt.setInt(6, id); 322 pstmt.executeUpdate(); 323 } 324 catch( SQLException sqle ) { 325 System.err.println("Error in DbForum:saveToDb()-" + sqle); 326 sqle.printStackTrace(); 327 } 328 finally { 329 try { pstmt.close(); } 330 catch (Exception e) { e.printStackTrace(); } 331 try { con.close(); } 332 catch (Exception e) { e.printStackTrace(); } 333 } 334 } 335 336 public Iterator forums() { 337 return new DbForumFactoryIterator(this, factory); 338 } 339 340 } 341 | Popular Tags |