KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mvnforum > common > AttachmentUtil


1 /*
2  * $Header: /cvsroot/mvnforum/mvnforum/src/com/mvnforum/common/AttachmentUtil.java,v 1.10 2006/04/14 17:05:26 minhnn Exp $
3  * $Author: minhnn $
4  * $Revision: 1.10 $
5  * $Date: 2006/04/14 17:05:26 $
6  *
7  * ====================================================================
8  *
9  * Copyright (C) 2002-2006 by MyVietnam.net
10  *
11  * All copyright notices regarding mvnForum MUST remain
12  * intact in the scripts and in the outputted HTML.
13  * The "powered by" text/logo with a link back to
14  * http://www.mvnForum.com and http://www.MyVietnam.net in
15  * the footer of the pages MUST remain visible when the pages
16  * are viewed on the internet or intranet.
17  *
18  * This program is free software; you can redistribute it and/or modify
19  * it under the terms of the GNU General Public License as published by
20  * the Free Software Foundation; either version 2 of the License, or
21  * any later version.
22  *
23  * This program is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26  * GNU General Public License for more details.
27  *
28  * You should have received a copy of the GNU General Public License
29  * along with this program; if not, write to the Free Software
30  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31  *
32  * Support can be obtained from support forums at:
33  * http://www.mvnForum.com/mvnforum/index
34  *
35  * Correspondence and Marketing Questions can be sent to:
36  * info at MyVietnam net
37  *
38  * @author: Minh Nguyen
39  * @author: Mai Nguyen
40  */

41 package com.mvnforum.common;
42
43 import java.io.File JavaDoc;
44 import java.io.IOException JavaDoc;
45 import java.util.Collection JavaDoc;
46 import java.util.Iterator JavaDoc;
47
48 import com.mvnforum.MVNForumConfig;
49 import com.mvnforum.ManagerFactory;
50 import com.mvnforum.db.AttachmentBean;
51 import com.mvnforum.db.DAOFactory;
52 import net.myvietnam.mvncore.exception.DatabaseException;
53 import net.myvietnam.mvncore.service.BinaryStorage;
54 import net.myvietnam.mvncore.util.FileUtil;
55 import org.apache.commons.logging.Log;
56 import org.apache.commons.logging.LogFactory;
57
58 public class AttachmentUtil {
59
60     private static Log log = LogFactory.getLog(AttachmentUtil.class);
61
62     private AttachmentUtil() {
63     }
64
65     public static String JavaDoc getAttachFilenameOnDisk(int attachID) {
66         String JavaDoc filename = MVNForumConfig.getAttachmentDir() + File.separatorChar + attachID + ".mvn";
67         return filename;
68     }
69
70     /*
71     // remove, now use BinaryStorage
72     public static void deleteAttachFilenameOnDisk(int attachID) {
73         String filename = getAttachFilenameOnDisk(attachID);
74         try {
75             log.info("About to delete post attachment = " + filename);
76             FileUtil.deleteFile(filename);
77         } catch (Exception ex) {
78             log.warn("Cannot delete post attachment file " + filename, ex);
79             //@todo schedule to delete it later
80         }
81     }*/

82
83     public static String JavaDoc getPmAttachFilenameOnDisk(int attachID) {
84         String JavaDoc filename = MVNForumConfig.getPmAttachmentDir() + File.separatorChar + attachID + ".mvn";
85         return filename;
86     }
87
88     /*public static void deletePmAttachFilenameOnDisk(int pmAttachID) {
89         String filename = getPmAttachFilenameOnDisk(pmAttachID);
90         try {
91             log.info("About to delete message attachment = " + filename);
92             FileUtil.deleteFile(filename);
93         } catch (Exception ex) {
94             log.warn("Cannot delete message attachment file " + filename, ex);
95             //@todo schedule to delete it later
96         }
97     }*/

98
99     /**
100      * NOTE: This method should be called before any attemp to delete a forum
101      * because it require the forum is exited
102      * After calling this method, go ahead and delete the forum
103      *
104      * NOTE: For Admin only
105      */

106     public static void deleteAttachments_inForum(int forumID) throws DatabaseException {
107
108         BinaryStorage binaryStorage = ManagerFactory.getBinaryStorage();
109
110         // First, try to delete attachment in database
111
Collection JavaDoc attachmentBeans = DAOFactory.getAttachmentDAO().getAttachments_inForum(forumID);
112
113         log.info("Delete forum = " + forumID + " attachment count = " + attachmentBeans.size());
114
115         //now delete files on disk
116
for (Iterator JavaDoc iter = attachmentBeans.iterator(); iter.hasNext(); ) {
117             AttachmentBean attachmentBean = (AttachmentBean)iter.next();
118             int attachID = attachmentBean.getAttachID();
119
120             //this method already catch the exception
121
//AttachmentUtil.deleteAttachFilenameOnDisk(attachID);
122
try {
123                 binaryStorage.deleteData(BinaryStorage.CATEGORY_POST_ATTACHMENT, String.valueOf(attachID), null);
124             } catch (IOException JavaDoc ex) {
125                 log.error("Cannot delete file", ex);
126                 // actually this exception is never existed
127
}
128
129             try {
130                 DAOFactory.getAttachmentDAO().delete(attachID);
131             } catch (Exception JavaDoc ex) {
132                 log.warn("Cannot delete attachment in database", ex);
133             }
134         }
135     }
136
137 }
138
Popular Tags