KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright (c) 2003, 2004 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  * Created on Jan 17, 2005 4:36:30 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.sql.Timestamp JavaDoc;
48 import java.util.ArrayList JavaDoc;
49 import java.util.HashMap JavaDoc;
50 import java.util.List JavaDoc;
51 import java.util.Map JavaDoc;
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 /**
63  * @author Rafael Steil
64  * @version $Id: GenericAttachmentDAO.java,v 1.6 2006/01/29 15:06:23 rafaelsteil Exp $
65  */

66 public class GenericAttachmentDAO extends AutoKeys implements net.jforum.dao.AttachmentDAO
67 {
68     /**
69      * @see net.jforum.dao.AttachmentDAO#addQuotaLimit(net.jforum.entities.QuotaLimit)
70      */

71     public void addQuotaLimit(QuotaLimit limit) throws Exception JavaDoc
72     {
73         PreparedStatement JavaDoc 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     /**
83      * @see net.jforum.dao.AttachmentDAO#updateQuotaLimit(net.jforum.entities.QuotaLimit)
84      */

85     public void updateQuotaLimit(QuotaLimit limit) throws Exception JavaDoc
86     {
87         PreparedStatement JavaDoc 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     /**
98      * @see net.jforum.dao.AttachmentDAO#cleanGroupQuota()
99      */

100     public void cleanGroupQuota() throws Exception JavaDoc
101     {
102         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(
103                 SystemGlobals.getSql("AttachmentModel.deleteGroupQuota"));
104         p.executeUpdate();
105         p.close();
106     }
107     
108     /**
109      * @see net.jforum.dao.AttachmentDAO#setGroupQuota(int, int)
110      */

111     public void setGroupQuota(int groupId, int quotaId) throws Exception JavaDoc
112     {
113         PreparedStatement JavaDoc 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     /**
122      * @see net.jforum.dao.AttachmentDAO#removeQuotaLimit(int)
123      */

124     public void removeQuotaLimit(int id) throws Exception JavaDoc
125     {
126         this.removeQuotaLimit(new String JavaDoc[] { Integer.toString(id) });
127     }
128     
129     /**
130      * @see net.jforum.dao.AttachmentDAO#removeQuotaLimit(java.lang.String[])
131      */

132     public void removeQuotaLimit(String JavaDoc[] ids) throws Exception JavaDoc
133     {
134         PreparedStatement JavaDoc 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     /**
146      * @see net.jforum.dao.AttachmentDAO#selectQuotaLimit()
147      */

148     public List JavaDoc selectQuotaLimit() throws Exception JavaDoc
149     {
150         List JavaDoc l = new ArrayList JavaDoc();
151         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(
152                 SystemGlobals.getSql("AttachmentModel.selectQuotaLimit"));
153         
154         ResultSet JavaDoc 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     /**
166      * @see net.jforum.dao.AttachmentDAO#selectQuotaLimit()
167      */

168     public QuotaLimit selectQuotaLimitByGroup(int groupId) throws Exception JavaDoc
169     {
170         QuotaLimit ql = null;
171         
172         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(
173                 SystemGlobals.getSql("AttachmentModel.selectQuotaLimitByGroup"));
174         p.setInt(1, groupId);
175         
176         ResultSet JavaDoc 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     /**
188      * @see net.jforum.dao.AttachmentDAO#selectGroupsQuotaLimits()
189      */

190     public Map JavaDoc selectGroupsQuotaLimits() throws Exception JavaDoc
191     {
192         Map JavaDoc m = new HashMap JavaDoc();
193         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(
194                 SystemGlobals.getSql("AttachmentModel.selectGroupsQuotaLimits"));
195         
196         ResultSet JavaDoc rs = p.executeQuery();
197         while (rs.next()) {
198             m.put(new Integer JavaDoc(rs.getInt("group_id")), new Integer JavaDoc(rs.getInt("quota_limit_id")));
199         }
200         
201         return m;
202     }
203     
204     protected QuotaLimit getQuotaLimit(ResultSet JavaDoc rs) throws Exception JavaDoc
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     /**
216      * @see net.jforum.dao.AttachmentDAO#addExtensionGroup(net.jforum.entities.AttachmentExtensionGroup)
217      */

218     public void addExtensionGroup(AttachmentExtensionGroup g) throws Exception JavaDoc
219     {
220         PreparedStatement JavaDoc 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     /**
231      * @see net.jforum.dao.AttachmentDAO#removeExtensionGroups(java.lang.String[])
232      */

233     public void removeExtensionGroups(String JavaDoc[] ids) throws Exception JavaDoc
234     {
235         PreparedStatement JavaDoc 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     /**
247      * @see net.jforum.dao.AttachmentDAO#selectExtensionGroups()
248      */

249     public List JavaDoc selectExtensionGroups() throws Exception JavaDoc
250     {
251         List JavaDoc l = new ArrayList JavaDoc();
252         
253         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(
254                 SystemGlobals.getSql("AttachmentModel.selectExtensionGroups"));
255         
256         ResultSet JavaDoc 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     /**
268      * @see net.jforum.dao.AttachmentDAO#extensionsForSecurity()
269      */

270     public Map JavaDoc extensionsForSecurity() throws Exception JavaDoc
271     {
272         Map JavaDoc m = new HashMap JavaDoc();
273         
274         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(
275                 SystemGlobals.getSql("AttachmentModel.extensionsForSecurity"));
276         
277         ResultSet JavaDoc 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 JavaDoc(allow == 1));
285         }
286         
287         rs.close();
288         p.close();
289         
290         return m;
291     }
292     
293     /**
294      * @see net.jforum.dao.AttachmentDAO#updateExtensionGroup(net.jforum.entities.AttachmentExtensionGroup)
295      */

296     public void updateExtensionGroup(AttachmentExtensionGroup g) throws Exception JavaDoc
297     {
298         PreparedStatement JavaDoc 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 JavaDoc rs) throws Exception JavaDoc
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     /**
322      * @see net.jforum.dao.AttachmentDAO#addExtension(net.jforum.entities.AttachmentExtension)
323      */

324     public void addExtension(AttachmentExtension e) throws Exception JavaDoc
325     {
326         PreparedStatement JavaDoc 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     /**
338      * @see net.jforum.dao.AttachmentDAO#removeExtensions(java.lang.String[])
339      */

340     public void removeExtensions(String JavaDoc[] ids) throws Exception JavaDoc
341     {
342         PreparedStatement JavaDoc 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     /**
352      * @see net.jforum.dao.AttachmentDAO#selectExtensions()
353      */

354     public List JavaDoc selectExtensions() throws Exception JavaDoc
355     {
356         List JavaDoc l = new ArrayList JavaDoc();
357         
358         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(
359                 SystemGlobals.getSql("AttachmentModel.selectExtensions"));
360         
361         ResultSet JavaDoc 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     /**
373      * @see net.jforum.dao.AttachmentDAO#updateExtension(net.jforum.entities.AttachmentExtension)
374      */

375     public void updateExtension(AttachmentExtension e) throws Exception JavaDoc
376     {
377         PreparedStatement JavaDoc 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     /**
390      * @see net.jforum.dao.AttachmentDAO#selectExtension(java.lang.String)
391      */

392     public AttachmentExtension selectExtension(String JavaDoc extension) throws Exception JavaDoc
393     {
394         return this.searchExtension(SystemGlobals.getValue(ConfigKeys.EXTENSION_FIELD),
395                 extension);
396     }
397     
398     private AttachmentExtension selectExtension(int extensionId) throws Exception JavaDoc
399     {
400         return this.searchExtension("extension_id", new Integer JavaDoc(extensionId));
401     }
402     
403     private AttachmentExtension searchExtension(String JavaDoc paramName, Object JavaDoc paramValue) throws Exception JavaDoc
404     {
405         String JavaDoc sql = SystemGlobals.getSql("AttachmentModel.selectExtension");
406         sql = sql.replaceAll("\\$field", paramName);
407         
408         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(sql);
409         p.setObject(1, paramValue);
410         
411         AttachmentExtension e = new AttachmentExtension();
412         
413         ResultSet JavaDoc 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 JavaDoc rs) throws Exception JavaDoc
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 JavaDoc 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     /**
447      * @see net.jforum.dao.AttachmentDAO#addAttachment(net.jforum.entities.Attachment)
448      */

449     public void addAttachment(Attachment a) throws Exception JavaDoc
450     {
451         PreparedStatement JavaDoc 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 JavaDoc(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 JavaDoc
478     {
479         PreparedStatement JavaDoc 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     /**
488      * @see net.jforum.dao.AttachmentDAO#removeAttachment(int, int)
489      */

490     public void removeAttachment(int id, int postId) throws Exception JavaDoc
491     {
492         PreparedStatement JavaDoc 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 JavaDoc 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     /**
518      * @see net.jforum.dao.AttachmentDAO#updateAttachment(net.jforum.entities.Attachment)
519      */

520     public void updateAttachment(Attachment a) throws Exception JavaDoc
521     {
522         PreparedStatement JavaDoc 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     /**
532      * @see net.jforum.dao.AttachmentDAO#selectAttachments(int)
533      */

534     public List JavaDoc selectAttachments(int postId) throws Exception JavaDoc
535     {
536         List JavaDoc l = new ArrayList JavaDoc();
537         
538         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(
539                 SystemGlobals.getSql("AttachmentModel.selectAttachments"));
540         p.setInt(1, postId);
541         
542         ResultSet JavaDoc 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 JavaDoc rs) throws Exception JavaDoc
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     /**
573      * @see net.jforum.dao.AttachmentDAO#selectAttachmentById(int)
574      */

575     public Attachment selectAttachmentById(int attachId) throws Exception JavaDoc
576     {
577         Attachment e = null;
578         
579         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(
580                 SystemGlobals.getSql("AttachmentModel.selectAttachmentById"));
581         p.setInt(1, attachId);
582         
583         ResultSet JavaDoc 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 JavaDoc
595     {
596         boolean result = true;
597         
598         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(
599                 SystemGlobals.getSql("AttachmentModel.isPhysicalDownloadMode"));
600         
601         p.setInt(1, extensionGroupId);
602         
603         ResultSet JavaDoc 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