1 43 package net.jforum.dao.generic; 44 45 import java.sql.PreparedStatement ; 46 import java.sql.ResultSet ; 47 import java.sql.Timestamp ; 48 import java.util.ArrayList ; 49 import java.util.HashMap ; 50 import java.util.List ; 51 import java.util.Map ; 52 53 import net.jforum.JForumExecutionContext; 54 import net.jforum.entities.Attachment; 55 import net.jforum.entities.AttachmentExtension; 56 import net.jforum.entities.AttachmentExtensionGroup; 57 import net.jforum.entities.AttachmentInfo; 58 import net.jforum.entities.QuotaLimit; 59 import net.jforum.util.preferences.ConfigKeys; 60 import net.jforum.util.preferences.SystemGlobals; 61 62 66 public class GenericAttachmentDAO extends AutoKeys implements net.jforum.dao.AttachmentDAO 67 { 68 71 public void addQuotaLimit(QuotaLimit limit) throws Exception 72 { 73 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement( 74 SystemGlobals.getSql("AttachmentModel.addQuotaLimit")); 75 p.setString(1, limit.getDescription()); 76 p.setInt(2, limit.getSize()); 77 p.setInt(3, limit.getType()); 78 p.executeUpdate(); 79 p.close(); 80 } 81 82 85 public void updateQuotaLimit(QuotaLimit limit) throws Exception 86 { 87 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement( 88 SystemGlobals.getSql("AttachmentModel.updateQuotaLimit")); 89 p.setString(1, limit.getDescription()); 90 p.setInt(2, limit.getSize()); 91 p.setInt(3, limit.getType()); 92 p.setInt(4, limit.getId()); 93 p.executeUpdate(); 94 p.close(); 95 } 96 97 100 public void cleanGroupQuota() throws Exception 101 { 102 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement( 103 SystemGlobals.getSql("AttachmentModel.deleteGroupQuota")); 104 p.executeUpdate(); 105 p.close(); 106 } 107 108 111 public void setGroupQuota(int groupId, int quotaId) throws Exception 112 { 113 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement( 114 SystemGlobals.getSql("AttachmentModel.setGroupQuota")); 115 p.setInt(1, groupId); 116 p.setInt(2, quotaId); 117 p.executeUpdate(); 118 p.close(); 119 } 120 121 124 public void removeQuotaLimit(int id) throws Exception 125 { 126 this.removeQuotaLimit(new String [] { Integer.toString(id) }); 127 } 128 129 132 public void removeQuotaLimit(String [] ids) throws Exception 133 { 134 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement( 135 SystemGlobals.getSql("AttachmentModel.removeQuotaLimit")); 136 137 for (int i = 0; i < ids.length; i++) { 138 p.setInt(1, Integer.parseInt(ids[i])); 139 p.executeUpdate(); 140 } 141 142 p.close(); 143 } 144 145 148 public List selectQuotaLimit() throws Exception 149 { 150 List l = new ArrayList (); 151 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement( 152 SystemGlobals.getSql("AttachmentModel.selectQuotaLimit")); 153 154 ResultSet rs = p.executeQuery(); 155 while (rs.next()) { 156 l.add(this.getQuotaLimit(rs)); 157 } 158 159 rs.close(); 160 p.close(); 161 162 return l; 163 } 164 165 168 public QuotaLimit selectQuotaLimitByGroup(int groupId) throws Exception 169 { 170 QuotaLimit ql = null; 171 172 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement( 173 SystemGlobals.getSql("AttachmentModel.selectQuotaLimitByGroup")); 174 p.setInt(1, groupId); 175 176 ResultSet rs = p.executeQuery(); 177 if (rs.next()) { 178 ql = this.getQuotaLimit(rs); 179 } 180 181 rs.close(); 182 p.close(); 183 184 return ql; 185 } 186 187 190 public Map selectGroupsQuotaLimits() throws Exception 191 { 192 Map m = new HashMap (); 193 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement( 194 SystemGlobals.getSql("AttachmentModel.selectGroupsQuotaLimits")); 195 196 ResultSet rs = p.executeQuery(); 197 while (rs.next()) { 198 m.put(new Integer (rs.getInt("group_id")), new Integer (rs.getInt("quota_limit_id"))); 199 } 200 201 return m; 202 } 203 204 protected QuotaLimit getQuotaLimit(ResultSet rs) throws Exception 205 { 206 QuotaLimit ql = new QuotaLimit(); 207 ql.setDescription(rs.getString("quota_desc")); 208 ql.setId(rs.getInt("quota_limit_id")); 209 ql.setSize(rs.getInt("quota_limit")); 210 ql.setType(rs.getInt("quota_type")); 211 212 return ql; 213 } 214 215 218 public void addExtensionGroup(AttachmentExtensionGroup g) throws Exception 219 { 220 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement( 221 SystemGlobals.getSql("AttachmentModel.addExtensionGroup")); 222 p.setString(1, g.getName()); 223 p.setInt(2, g.isAllow() ? 1 : 0); 224 p.setString(3, g.getUploadIcon()); 225 p.setInt(4, g.getDownloadMode()); 226 p.executeUpdate(); 227 p.close(); 228 } 229 230 233 public void removeExtensionGroups(String [] ids) throws Exception 234 { 235 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement( 236 SystemGlobals.getSql("AttachmentModel.removeExtensionGroups")); 237 238 for (int i = 0; i < ids.length; i++) { 239 p.setInt(1, Integer.parseInt(ids[i])); 240 p.executeUpdate(); 241 } 242 243 p.close(); 244 } 245 246 249 public List selectExtensionGroups() throws Exception 250 { 251 List l = new ArrayList (); 252 253 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement( 254 SystemGlobals.getSql("AttachmentModel.selectExtensionGroups")); 255 256 ResultSet rs = p.executeQuery(); 257 while (rs.next()) { 258 l.add(this.getExtensionGroup(rs)); 259 } 260 261 rs.close(); 262 p.close(); 263 264 return l; 265 } 266 267 270 public Map extensionsForSecurity() throws Exception 271 { 272 Map m = new HashMap (); 273 274 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement( 275 SystemGlobals.getSql("AttachmentModel.extensionsForSecurity")); 276 277 ResultSet rs = p.executeQuery(); 278 while (rs.next()) { 279 int allow = rs.getInt("group_allow"); 280 if (allow == 1) { 281 allow = rs.getInt("allow"); 282 } 283 284 m.put(rs.getString("extension"), new Boolean (allow == 1)); 285 } 286 287 rs.close(); 288 p.close(); 289 290 return m; 291 } 292 293 296 public void updateExtensionGroup(AttachmentExtensionGroup g) throws Exception 297 { 298 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement( 299 SystemGlobals.getSql("AttachmentModel.updateExtensionGroups")); 300 p.setString(1, g.getName()); 301 p.setInt(2, g.isAllow() ? 1 : 0); 302 p.setString(3, g.getUploadIcon()); 303 p.setInt(4, g.getDownloadMode()); 304 p.setInt(5, g.getId()); 305 p.executeUpdate(); 306 p.close(); 307 } 308 309 protected AttachmentExtensionGroup getExtensionGroup(ResultSet rs) throws Exception 310 { 311 AttachmentExtensionGroup g = new AttachmentExtensionGroup(); 312 g.setId(rs.getInt("extension_group_id")); 313 g.setName(rs.getString("name")); 314 g.setUploadIcon(rs.getString("upload_icon")); 315 g.setAllow(rs.getInt("allow") == 1); 316 g.setDownloadMode(rs.getInt("download_mode")); 317 318 return g; 319 } 320 321 324 public void addExtension(AttachmentExtension e) throws Exception 325 { 326 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement( 327 SystemGlobals.getSql("AttachmentModel.addExtension")); 328 p.setInt(1, e.getExtensionGroupId()); 329 p.setString(2, e.getComment()); 330 p.setString(3, e.getUploadIcon()); 331 p.setString(4, e.getExtension().toLowerCase()); 332 p.setInt(5, e.isAllow() ? 1 : 0); 333 p.executeUpdate(); 334 p.close(); 335 } 336 337 340 public void removeExtensions(String [] ids) throws Exception 341 { 342 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement( 343 SystemGlobals.getSql("AttachmentModel.removeExtension")); 344 for (int i = 0; i < ids.length; i++) { 345 p.setInt(1, Integer.parseInt(ids[i])); 346 p.executeUpdate(); 347 } 348 p.close(); 349 } 350 351 354 public List selectExtensions() throws Exception 355 { 356 List l = new ArrayList (); 357 358 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement( 359 SystemGlobals.getSql("AttachmentModel.selectExtensions")); 360 361 ResultSet rs = p.executeQuery(); 362 while (rs.next()) { 363 l.add(this.getExtension(rs)); 364 } 365 366 rs.close(); 367 p.close(); 368 369 return l; 370 } 371 372 375 public void updateExtension(AttachmentExtension e) throws Exception 376 { 377 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement( 378 SystemGlobals.getSql("AttachmentModel.updateExtension")); 379 p.setInt(1, e.getExtensionGroupId()); 380 p.setString(2, e.getComment()); 381 p.setString(3, e.getUploadIcon()); 382 p.setString(4, e.getExtension().toLowerCase()); 383 p.setInt(5, e.isAllow() ? 1 : 0); 384 p.setInt(6, e.getId()); 385 p.executeUpdate(); 386 p.close(); 387 } 388 389 392 public AttachmentExtension selectExtension(String extension) throws Exception 393 { 394 return this.searchExtension(SystemGlobals.getValue(ConfigKeys.EXTENSION_FIELD), 395 extension); 396 } 397 398 private AttachmentExtension selectExtension(int extensionId) throws Exception 399 { 400 return this.searchExtension("extension_id", new Integer (extensionId)); 401 } 402 403 private AttachmentExtension searchExtension(String paramName, Object paramValue) throws Exception 404 { 405 String sql = SystemGlobals.getSql("AttachmentModel.selectExtension"); 406 sql = sql.replaceAll("\\$field", paramName); 407 408 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(sql); 409 p.setObject(1, paramValue); 410 411 AttachmentExtension e = new AttachmentExtension(); 412 413 ResultSet rs = p.executeQuery(); 414 if (rs.next()) { 415 e = this.getExtension(rs); 416 } 417 else { 418 e.setUnknown(true); 419 } 420 421 rs.close(); 422 p.close(); 423 424 return e; 425 } 426 427 protected AttachmentExtension getExtension(ResultSet rs) throws Exception 428 { 429 AttachmentExtension e = new AttachmentExtension(); 430 e.setAllow(rs.getInt("allow") == 1); 431 e.setComment(rs.getString("description")); 432 e.setExtension(rs.getString("extension")); 433 e.setExtensionGroupId(rs.getInt("extension_group_id")); 434 e.setId(rs.getInt("extension_id")); 435 436 String icon = rs.getString("upload_icon"); 437 if (icon == null || icon.equals("")) { 438 icon = rs.getString("group_icon"); 439 } 440 441 e.setUploadIcon(icon); 442 443 return e; 444 } 445 446 449 public void addAttachment(Attachment a) throws Exception 450 { 451 PreparedStatement p = this.getStatementForAutoKeys("AttachmentModel.addAttachment"); 452 p.setInt(1, a.getPostId()); 453 p.setInt(2, a.getPrivmsgsId()); 454 p.setInt(3, a.getUserId()); 455 456 this.setAutoGeneratedKeysQuery(SystemGlobals.getSql("AttachmentModel.lastGeneratedAttachmentId")); 457 int id = this.executeAutoKeysQuery(p); 458 p.close(); 459 460 p = JForumExecutionContext.getConnection().prepareStatement( 461 SystemGlobals.getSql("AttachmentModel.addAttachmentInfo")); 462 p.setInt(1, id); 463 p.setString(2, a.getInfo().getPhysicalFilename()); 464 p.setString(3, a.getInfo().getRealFilename()); 465 p.setString(4, a.getInfo().getComment()); 466 p.setString(5, a.getInfo().getMimetype()); 467 p.setLong(6, a.getInfo().getFilesize()); 468 p.setTimestamp(7, new Timestamp (a.getInfo().getUploadTimeInMillis())); 469 p.setInt(8, 0); 470 p.setInt(9, a.getInfo().getExtension().getId()); 471 p.executeUpdate(); 472 p.close(); 473 474 this.updatePost(a.getPostId(), 1); 475 } 476 477 protected void updatePost(int postId, int count) throws Exception 478 { 479 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement( 480 SystemGlobals.getSql("AttachmentModel.updatePost")); 481 p.setInt(1, count); 482 p.setInt(2, postId); 483 p.executeUpdate(); 484 p.close(); 485 } 486 487 490 public void removeAttachment(int id, int postId) throws Exception 491 { 492 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement( 493 SystemGlobals.getSql("AttachmentModel.removeAttachmentInfo")); 494 p.setInt(1, id); 495 p.executeUpdate(); 496 p.close(); 497 498 p = JForumExecutionContext.getConnection().prepareStatement( 499 SystemGlobals.getSql("AttachmentModel.removeAttachment")); 500 p.setInt(1, id); 501 p.executeUpdate(); 502 p.close(); 503 504 p = JForumExecutionContext.getConnection().prepareStatement( 505 SystemGlobals.getSql("AttachmentModel.countPostAttachments")); 506 p.setInt(1, postId); 507 508 ResultSet rs = p.executeQuery(); 509 if (rs.next()) { 510 this.updatePost(postId, rs.getInt(1)); 511 } 512 513 rs.close(); 514 p.close(); 515 } 516 517 520 public void updateAttachment(Attachment a) throws Exception 521 { 522 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement( 523 SystemGlobals.getSql("AttachmentModel.updateAttachment")); 524 p.setString(1, a.getInfo().getComment()); 525 p.setInt(2, a.getInfo().getDownloadCount()); 526 p.setInt(3, a.getId()); 527 p.executeUpdate(); 528 p.close(); 529 } 530 531 534 public List selectAttachments(int postId) throws Exception 535 { 536 List l = new ArrayList (); 537 538 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement( 539 SystemGlobals.getSql("AttachmentModel.selectAttachments")); 540 p.setInt(1, postId); 541 542 ResultSet rs = p.executeQuery(); 543 while (rs.next()) { 544 l.add(this.getAttachment(rs)); 545 } 546 547 return l; 548 } 549 550 protected Attachment getAttachment(ResultSet rs) throws Exception 551 { 552 Attachment a = new Attachment(); 553 a.setId(rs.getInt("attach_id")); 554 a.setPostId(rs.getInt("post_id")); 555 a.setPrivmsgsId(rs.getInt("privmsgs_id")); 556 557 AttachmentInfo ai = new AttachmentInfo(); 558 ai.setComment(rs.getString("description")); 559 ai.setDownloadCount(rs.getInt("download_count")); 560 ai.setFilesize(rs.getLong("filesize")); 561 ai.setMimetype(rs.getString("mimetype")); 562 ai.setPhysicalFilename(rs.getString("physical_filename")); 563 ai.setRealFilename(rs.getString("real_filename")); 564 ai.setUploadTime(rs.getTimestamp("upload_time")); 565 ai.setExtension(this.selectExtension(rs.getInt("extension_id"))); 566 567 a.setInfo(ai); 568 569 return a; 570 } 571 572 575 public Attachment selectAttachmentById(int attachId) throws Exception 576 { 577 Attachment e = null; 578 579 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement( 580 SystemGlobals.getSql("AttachmentModel.selectAttachmentById")); 581 p.setInt(1, attachId); 582 583 ResultSet rs = p.executeQuery(); 584 if (rs.next()) { 585 e = this.getAttachment(rs); 586 } 587 588 rs.close(); 589 p.close(); 590 591 return e; 592 } 593 594 public boolean isPhysicalDownloadMode(int extensionGroupId) throws Exception 595 { 596 boolean result = true; 597 598 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement( 599 SystemGlobals.getSql("AttachmentModel.isPhysicalDownloadMode")); 600 601 p.setInt(1, extensionGroupId); 602 603 ResultSet rs = p.executeQuery(); 604 if (rs.next()) 605 { 606 result = (rs.getInt("download_mode") == 2); 607 } 608 609 rs.close(); 610 p.close(); 611 612 return result; 613 } 614 615 } 616 | Popular Tags |