001: /*
002: * $Header: /cvsroot/mvnforum/mvnforum/src/com/mvnforum/common/AttachmentUtil.java,v 1.22 2007/10/09 11:09:16 lexuanttkhtn Exp $
003: * $Author: lexuanttkhtn $
004: * $Revision: 1.22 $
005: * $Date: 2007/10/09 11:09:16 $
006: *
007: * ====================================================================
008: *
009: * Copyright (C) 2002-2007 by MyVietnam.net
010: *
011: * All copyright notices regarding mvnForum MUST remain
012: * intact in the scripts and in the outputted HTML.
013: * The "powered by" text/logo with a link back to
014: * http://www.mvnForum.com and http://www.MyVietnam.net in
015: * the footer of the pages MUST remain visible when the pages
016: * are viewed on the internet or intranet.
017: *
018: * This program is free software; you can redistribute it and/or modify
019: * it under the terms of the GNU General Public License as published by
020: * the Free Software Foundation; either version 2 of the License, or
021: * any later version.
022: *
023: * This program is distributed in the hope that it will be useful,
024: * but WITHOUT ANY WARRANTY; without even the implied warranty of
025: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
026: * GNU General Public License for more details.
027: *
028: * You should have received a copy of the GNU General Public License
029: * along with this program; if not, write to the Free Software
030: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
031: *
032: * Support can be obtained from support forums at:
033: * http://www.mvnForum.com/mvnforum/index
034: *
035: * Correspondence and Marketing Questions can be sent to:
036: * info at MyVietnam net
037: *
038: * @author: Minh Nguyen
039: * @author: Mai Nguyen
040: */
041: package com.mvnforum.common;
042:
043: import java.io.File;
044: import java.io.IOException;
045: import java.util.Collection;
046: import java.util.Iterator;
047:
048: import net.myvietnam.mvncore.exception.DatabaseException;
049: import net.myvietnam.mvncore.service.BinaryStorageService;
050: import net.myvietnam.mvncore.service.MvnCoreServiceFactory;
051:
052: import org.apache.commons.logging.Log;
053: import org.apache.commons.logging.LogFactory;
054:
055: import com.mvnforum.MVNForumConfig;
056: import com.mvnforum.db.AttachmentBean;
057: import com.mvnforum.db.DAOFactory;
058:
059: public class AttachmentUtil {
060:
061: private static Log log = LogFactory.getLog(AttachmentUtil.class);
062:
063: private AttachmentUtil() {
064: }
065:
066: public static String getAttachFilenameOnDisk(int attachID) {
067: String filename = MVNForumConfig.getAttachmentDir()
068: + File.separatorChar + attachID + ".mvn";
069: return filename;
070: }
071:
072: public static String getAlbumItemFilenameOnDisk(String memberName,
073: String albumName, int albumItemID) {
074: String filename = MVNForumConfig.getAlbumItemDir()
075: + File.separatorChar + memberName.toLowerCase()
076: + File.separatorChar + albumName.toLowerCase()
077: + File.separatorChar + albumItemID + ".mvn";
078: return filename;
079: }
080:
081: public static String getAlbumItemDirOnDisk(String memberName,
082: String albumName) {
083: String dir = MVNForumConfig.getAlbumItemDir()
084: + File.separatorChar + memberName.toLowerCase()
085: + File.separatorChar + albumName.toLowerCase();
086: return dir;
087: }
088:
089: public static String getAlbumPropertiesFileOnDisk(
090: String memberName, String albumName) {
091: String dir = MVNForumConfig.getAlbumItemDir()
092: + File.separatorChar + memberName.toLowerCase()
093: + File.separatorChar + albumName.toLowerCase()
094: + File.separatorChar + "album.properties";
095: return dir;
096: }
097:
098: public static String getAlbumDirOnDisk(String memberName) {
099: String dir = MVNForumConfig.getAlbumItemDir()
100: + File.separatorChar + memberName.toLowerCase();
101:
102: return dir;
103: }
104:
105: /*
106: // remove, now use BinaryStorage
107: public static void deleteAttachFilenameOnDisk(int attachID) {
108: String filename = getAttachFilenameOnDisk(attachID);
109: try {
110: log.info("About to delete post attachment = " + filename);
111: FileUtil.deleteFile(filename);
112: } catch (Exception ex) {
113: log.warn("Cannot delete post attachment file " + filename, ex);
114: //@todo schedule to delete it later
115: }
116: }*/
117:
118: public static String getPmAttachFilenameOnDisk(int attachID) {
119: String filename = MVNForumConfig.getPmAttachmentDir()
120: + File.separatorChar + attachID + ".mvn";
121: return filename;
122: }
123:
124: /*public static void deletePmAttachFilenameOnDisk(int pmAttachID) {
125: String filename = getPmAttachFilenameOnDisk(pmAttachID);
126: try {
127: log.info("About to delete message attachment = " + filename);
128: FileUtil.deleteFile(filename);
129: } catch (Exception ex) {
130: log.warn("Cannot delete message attachment file " + filename, ex);
131: //@todo schedule to delete it later
132: }
133: }*/
134:
135: /**
136: * NOTE: This method should be called before any attemp to delete a forum
137: * because it require the forum is exited
138: * After calling this method, go ahead and delete the forum
139: *
140: * NOTE: For Admin only
141: */
142: public static void deleteAttachments_inForum(int forumID)
143: throws DatabaseException {
144:
145: BinaryStorageService binaryStorage = MvnCoreServiceFactory
146: .getMvnCoreService().getBinaryStorageService();
147:
148: // First, try to delete attachment in database
149: Collection attachmentBeans = DAOFactory.getAttachmentDAO()
150: .getAttachments_inForum(forumID);
151:
152: log.info("Delete forum = " + forumID + " attachment count = "
153: + attachmentBeans.size());
154:
155: //now delete files on disk
156: for (Iterator iter = attachmentBeans.iterator(); iter.hasNext();) {
157: AttachmentBean attachmentBean = (AttachmentBean) iter
158: .next();
159: int attachID = attachmentBean.getAttachID();
160:
161: //this method already catch the exception
162: //AttachmentUtil.deleteAttachFilenameOnDisk(attachID);
163: try {
164: binaryStorage.deleteData(
165: BinaryStorageService.CATEGORY_POST_ATTACHMENT,
166: String.valueOf(attachID), null);
167: } catch (IOException ex) {
168: log.error("Cannot delete file", ex);
169: // actually this exception is never existed
170: }
171:
172: try {
173: DAOFactory.getAttachmentDAO().delete(attachID);
174: } catch (Exception ex) {
175: log.warn("Cannot delete attachment in database", ex);
176: }
177: }
178: }
179:
180: }
|