KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: DeleteUserpicAction.java,v 1.6 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 import java.util.Iterator JavaDoc;
30 import java.util.List JavaDoc;
31
32 import javax.servlet.http.HttpServletRequest JavaDoc;
33 import javax.servlet.http.HttpServletResponse JavaDoc;
34
35 import org.apache.commons.lang.StringUtils;
36
37 import com.j2biz.blogunity.BlogunityManager;
38 import com.j2biz.blogunity.dao.CommentDAO;
39 import com.j2biz.blogunity.dao.EntryDAO;
40 import com.j2biz.blogunity.dao.UserDAO;
41 import com.j2biz.blogunity.dao.UserpicDAO;
42 import com.j2biz.blogunity.exception.BlogunityException;
43 import com.j2biz.blogunity.i18n.I18N;
44 import com.j2biz.blogunity.i18n.I18NStatusFactory;
45 import com.j2biz.blogunity.pojo.Comment;
46 import com.j2biz.blogunity.pojo.Entry;
47 import com.j2biz.blogunity.pojo.SystemConfiguration;
48 import com.j2biz.blogunity.pojo.Userpic;
49 import com.j2biz.blogunity.util.ResourceUtils;
50 import com.j2biz.blogunity.web.IActionResult;
51
52 /**
53  * @author michelson
54  * @version $$
55  * @since 0.1
56  *
57  *
58  */

59 public class DeleteUserpicAction extends MyAbstractAction {
60
61     // private static final IActionResult USERPICS_FORM_REDIRECT =
62
// ActionResultFactory
63
// .buildRedirect("/my/editUserpicsForm");
64

65     /*
66      * (non-Javadoc)
67      *
68      * @see com.j2biz.blogunity.web.actions.AbstractAction#execute(javax.servlet.http.HttpServletRequest,
69      * javax.servlet.http.HttpServletResponse)
70      */

71     public IActionResult execute(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
72             throws BlogunityException {
73
74         String JavaDoc userpicId = request.getParameter("userpicId");
75
76         if (StringUtils.isEmpty(userpicId)) { throw new BlogunityException(I18NStatusFactory
77                 .create(I18N.ERRORS.ID_NOT_SETTED, new String JavaDoc[]{"Userpic"})); }
78
79         EntryDAO entryDAO = new EntryDAO();
80         UserpicDAO userpicDAO = new UserpicDAO();
81         UserDAO userDAO = new UserDAO();
82         CommentDAO commentDAO = new CommentDAO();
83
84         Userpic pic;
85         try {
86             pic = userpicDAO.getUserpicByID(Long.parseLong(userpicId));
87         } catch (NumberFormatException JavaDoc e) {
88             throw new BlogunityException(I18NStatusFactory.create(I18N.ERRORS.ID_NOT_VALID,
89                     new String JavaDoc[]{"Userpic"}));
90         }
91
92         removeUserpicfile(pic);
93
94         user.getUserpics().remove(pic);
95
96         // FIXME hibernate produces here unperformant sql-queries.
97
// fix this either at the mapping-side or this native sql.
98
List JavaDoc entries = entryDAO.getEntiresByUserpic(pic);
99         // set userpic=null by all blog-entries, that reference this userpic
100
for (Iterator JavaDoc it = entries.iterator(); it.hasNext();) {
101             Entry entry = (Entry) it.next();
102             entry.setUserpic(null);
103             entryDAO.updateEntry(entry);
104         }
105         List JavaDoc comments = commentDAO.getCommentsByUserpic(pic);
106         // set userpic=null by all blog-comments, that reference this userpic
107
for (Iterator JavaDoc it = comments.iterator(); it.hasNext();) {
108             Comment comment = (Comment) it.next();
109             comment.setUserpic(null);
110             commentDAO.updateComment(comment);
111         }
112
113         userpicDAO.deleteUserpic(pic);
114         userDAO.updateUser(user);
115
116         // return USERPICS_FORM_REDIRECT;
117
return navigationStack.peek();
118     }
119
120     /**
121      * Removes userpic-file from user's "userpics"-directory.
122      *
123      * @param pic
124      */

125     private void removeUserpicfile(Userpic pic) throws BlogunityException {
126
127         SystemConfiguration config = BlogunityManager.getSystemConfiguration();
128
129         File JavaDoc file = new File JavaDoc(config.getDataDir(), pic.getUrl());
130
131         ResourceUtils.removeFile(file);
132
133     }
134
135 }
Popular Tags