KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: EditEntryExecAction.java,v 1.9 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.util.StringTokenizer JavaDoc;
29
30 import javax.servlet.http.HttpServletRequest JavaDoc;
31 import javax.servlet.http.HttpServletResponse JavaDoc;
32
33 import org.apache.commons.lang.StringUtils;
34
35 import com.j2biz.blogunity.dao.CategoryDAO;
36 import com.j2biz.blogunity.dao.EntryDAO;
37 import com.j2biz.blogunity.dao.UserpicDAO;
38 import com.j2biz.blogunity.exception.BlogunityException;
39 import com.j2biz.blogunity.i18n.I18N;
40 import com.j2biz.blogunity.i18n.I18NStatusFactory;
41 import com.j2biz.blogunity.pojo.Entry;
42 import com.j2biz.blogunity.pojo.Userpic;
43 import com.j2biz.blogunity.util.BlogUtils;
44 import com.j2biz.blogunity.util.LuceneUtils;
45 import com.j2biz.blogunity.util.RenderUtils;
46 import com.j2biz.blogunity.web.ActionResultFactory;
47 import com.j2biz.blogunity.web.FormError;
48 import com.j2biz.blogunity.web.FormErrorList;
49 import com.j2biz.blogunity.web.IActionResult;
50
51 /**
52  * @author michelson
53  * @version $$
54  * @since 0.1
55  *
56  *
57  */

58 public class EditEntryExecAction extends MyAbstractAction {
59
60     private static final IActionResult SUCCESS_REDIRECT = ActionResultFactory
61             .buildRedirect("/my/editEntrySuccess");
62
63     private static final IActionResult PREVIEW_FORWARD = ActionResultFactory
64             .buildForward("/jsp/my/editEntryForm.jsp");
65
66     private static final String JavaDoc SUBMIT_PREVIEW = "Preview";
67
68     private static final String JavaDoc SUBMIT_POST = "Post to Blog";
69
70     /*
71      * (non-Javadoc)
72      *
73      * @see com.j2biz.blogunity.web.actions.AbstractAction#execute(javax.servlet.http.HttpServletRequest,
74      * javax.servlet.http.HttpServletResponse)
75      */

76     public IActionResult execute(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
77             throws BlogunityException {
78
79         // String blogId = request.getParameter("blogId");
80
// if (StringUtils.isEmpty(blogId)) { throw new BlogException(
81
// "Fatal error editing blog entry! Blog-Id is not specified!"); }
82

83         String JavaDoc entryId = request.getParameter("entryId");
84         if (StringUtils.isEmpty(entryId)) { throw new BlogunityException(I18NStatusFactory
85                 .create(I18N.ERRORS.ID_NOT_SETTED)); }
86
87         FormErrorList errors = new FormErrorList();
88         EntryDAO entryDAO = new EntryDAO();
89
90         String JavaDoc[] catId = request.getParameterValues("catId");
91         // if (catId == null || catId.length == 0) {
92
// errors.add(new FormError("category", "You must specify at least one
93
// category!"));
94
// catId = new String[0];
95
// }
96

97         String JavaDoc title = request.getParameter("title");
98         if (StringUtils.isEmpty(title)) {
99             errors.add(new FormError("title", "Entry title is not specified!"));
100         }
101
102         String JavaDoc body = request.getParameter("body");
103         if (StringUtils.isEmpty(body)) {
104             errors.add(new FormError("body", "Entry body is not specified!"));
105         }
106
107         String JavaDoc excerpt = request.getParameter("excerpt");
108
109         String JavaDoc commenting = request.getParameter("commenting");
110         String JavaDoc trackbacking = request.getParameter("trackbacking");
111         String JavaDoc anonymousCommenting = request.getParameter("anonymousCommenting");
112         String JavaDoc userpicId = request.getParameter("userpicId");
113
114         Entry e = entryDAO.getEntryByID(new Long JavaDoc(entryId));
115         if (user.getId().longValue() != e.getAuthor().getId().longValue()
116                 && !user.isAdministrator())
117                 throw new BlogunityException(I18NStatusFactory
118                         .create(I18N.ERRORS.USER_NOT_AUTHORIZED_FOR_EXECUTION));
119
120         String JavaDoc alias = request.getParameter("alias");
121         if (StringUtils.isNotEmpty(alias)) {
122             //check, if aliasname is valid
123
if (!BlogUtils.getInstance().isValidAliasname(alias)) {
124                 errors.add(new FormError("alias", "Aliasname is not valid!"));
125             } else {
126                 Entry ex = entryDAO.getEntryByAliasname(alias, e.getBlog().getUrlName());
127                 if (ex != null && ex.getId().longValue() != e.getId().longValue()) {
128                     errors.add(new FormError("alias",
129                             "There is already one entry with aliasname you specified!"));
130                 }
131             }
132         }
133
134         e.setAliasname(alias);
135         e.setRawTitle(title);
136         if (excerpt.trim().length() == 0) excerpt = null;
137         e.setRawExcerpt(excerpt);
138         e.setRawBody(body);
139
140         // only set author if not already exists cause we are in the
141
// editing-mode now
142
if (e.getAuthor() == null) {
143             e.setAuthor(user);
144             e.setAuthorIP(request.getRemoteAddr());
145         }
146
147         e.setTrackbackAllowed(("on".equalsIgnoreCase(trackbacking)) ? true : false);
148         e.setCommentingAllowed(("on".equalsIgnoreCase(commenting)) ? true : false);
149         e
150                 .setAnonymousCommentingAllowed(("on".equalsIgnoreCase(anonymousCommenting)) ? true
151                         : false);
152
153         if (catId != null && catId.length > 0) {
154             for (int i = 0; i < catId.length; i++) {
155                 e.addCategory((new CategoryDAO()).getCategoryByID(new Long JavaDoc(catId[i])));
156             }
157         }
158
159         if (StringUtils.isEmpty(userpicId)) {
160             e.setUserpic(null);
161         } else {
162             // get id
163
try {
164                 StringTokenizer JavaDoc t = new StringTokenizer JavaDoc(userpicId, "|");
165                 String JavaDoc _id = t.nextToken();
166                 Userpic pic = (new UserpicDAO()).getUserpicByID(Long.parseLong(_id));
167                 e.setUserpic(pic);
168             } catch (NumberFormatException JavaDoc e1) {
169                 e.setUserpic(null);
170             }
171         }
172
173         //render on-the-fly
174
e = RenderUtils.render(e, request, response);
175
176         if (errors.size() > 0) {
177             request.setAttribute("errors", errors);
178             request.setAttribute("entry", e);
179             return PREVIEW_FORWARD;
180         }
181
182         String JavaDoc submit = request.getParameter("submit");
183
184         if (SUBMIT_POST.equals(submit)) {
185
186             entryDAO.updateEntry(e);
187
188             // re-index updated entry
189
LuceneUtils.remove(e, e.getBlog().getIndexesDirectory());
190             LuceneUtils.index(e, e.getBlog().getIndexesDirectory());
191
192             request.getSession().setAttribute("savedEntry", e);
193             return SUCCESS_REDIRECT;
194
195         } else if (SUBMIT_PREVIEW.equals(submit)) {
196             request.setAttribute("isPreview", Boolean.TRUE);
197             request.setAttribute("entry", e);
198             return PREVIEW_FORWARD;
199         }
200
201         throw new BlogunityException(I18NStatusFactory.create(I18N.ERRORS.UNSUPPORTED_OPERATION));
202
203     }
204 }
Popular Tags