KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > j2biz > blogunity > web > actions > my > DeleteBlogExecAction


1 /*
2  * $Id: DeleteBlogExecAction.java,v 1.7 2005/01/17 21:35:45 michelson Exp $
3  *
4  * Copyright (c) 2004 j2biz Group, http://www.j2biz.com
5  * Koeln / Duesseldorf , Germany
6  *
7  * @author Max Kalina
8  *
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23  *
24  */

25
26 package com.j2biz.blogunity.web.actions.my;
27
28 import java.io.File JavaDoc;
29
30 import javax.servlet.http.HttpServletRequest JavaDoc;
31 import javax.servlet.http.HttpServletResponse JavaDoc;
32
33 import com.j2biz.blogunity.BlogunityManager;
34 import com.j2biz.blogunity.dao.BlogDAO;
35 import com.j2biz.blogunity.exception.BlogunityException;
36 import com.j2biz.blogunity.i18n.I18N;
37 import com.j2biz.blogunity.i18n.I18NStatusFactory;
38 import com.j2biz.blogunity.pojo.Blog;
39 import com.j2biz.blogunity.pojo.SystemConfiguration;
40 import com.j2biz.blogunity.util.HibernateUtil;
41 import com.j2biz.blogunity.web.ActionResultFactory;
42 import com.j2biz.blogunity.web.IActionResult;
43
44 /**
45  * @author michelson
46  * @version $$
47  * @since 0.1
48  *
49  *
50  */

51 public class DeleteBlogExecAction extends MyAbstractAction {
52
53     private static final IActionResult FOUNDED_BLOGS_LIST_REDIRECT = ActionResultFactory
54             .buildRedirect("/my/foundedBlogsList");
55
56     /*
57      * (non-Javadoc)
58      *
59      * @see com.j2biz.blogunity.web.actions.AbstractAction#execute(javax.servlet.http.HttpServletRequest,
60      * javax.servlet.http.HttpServletResponse)
61      */

62     public IActionResult execute(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
63             throws BlogunityException {
64
65         String JavaDoc blogId = request.getParameter("id");
66         BlogDAO dao = new BlogDAO();
67         Blog b = dao.getBlogByID(Long.parseLong(blogId));
68         if (user.getId().longValue() != b.getFounder().getId().longValue()
69                 && !user.isAdministrator())
70                 throw new BlogunityException(I18NStatusFactory
71                         .create(I18N.ERRORS.USER_NOT_AUTHORIZED_FOR_EXECUTION));
72
73         dao.deleteBlog(b);
74
75         try {
76             deleteBlogDirectory(b);
77         } catch (BlogunityException e) {
78             HibernateUtil.rollbackTransaction();
79             throw e;
80         }
81
82         return FOUNDED_BLOGS_LIST_REDIRECT;
83     }
84
85     /**
86      * @param b
87      */

88     private void deleteBlogDirectory(Blog b) throws BlogunityException {
89         SystemConfiguration config = BlogunityManager.getSystemConfiguration();
90
91         File JavaDoc blogsDir = config.getBlogsDirectory();
92         File JavaDoc blogDir = new File JavaDoc(blogsDir, b.getUrlName());
93
94         boolean result = deleteDir(blogDir);
95         if (!result)
96                 throw new BlogunityException(I18NStatusFactory.create(I18N.ERRORS.DELETE_DIRECTORY,
97                         blogDir.getAbsolutePath()));
98
99     }
100
101     /**
102      * Deletes all files and subdirectories under dir. Returns true if all
103      * deletions were successful. If a deletion fails, the method stops
104      * attempting to delete and returns false.
105      *
106      * @param dir
107      * @return
108      */

109     public static boolean deleteDir(File JavaDoc dir) {
110         if (dir.isDirectory()) {
111             String JavaDoc[] children = dir.list();
112             for (int i = 0; i < children.length; i++) {
113                 boolean success = deleteDir(new File JavaDoc(dir, children[i]));
114                 if (!success) { return false; }
115             }
116         }
117
118         // The directory is now empty so delete it
119
return dir.delete();
120     }
121 }
Popular Tags