KickJava   Java API By Example, From Geeks To Geeks.

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


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

63 public class CreateBlogEntryExecAction extends MyAbstractAction {
64
65     private static final IActionResult SUCCESS_REDIRECT = ActionResultFactory
66             .buildRedirect("/my/saveBlogEntrySuccess");
67
68     private static final IActionResult PREVIEW_FORWARD = ActionResultFactory
69             .buildForward("/jsp/my/createBlogEntryForm.jsp");
70
71     private static final String JavaDoc SUBMIT_PREVIEW = "Preview";
72
73     private static final String JavaDoc SUBMIT_POST = "Post to Blog";
74
75     private static final String JavaDoc SUBMIT_DRAFT = "Save as Draft";
76
77     /*
78      * (non-Javadoc)
79      *
80      * @see com.j2biz.blogunity.web.actions.AbstractAction#execute(javax.servlet.http.HttpServletRequest,
81      * javax.servlet.http.HttpServletResponse)
82      */

83     public IActionResult execute(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
84             throws BlogunityException {
85
86         String JavaDoc blogId = request.getParameter("blogId");
87         if (StringUtils.isEmpty(blogId)) { throw new BlogunityException(I18NStatusFactory.create(
88                 I18N.ERRORS.ID_NOT_SETTED, "Blog")); }
89
90         FormErrorList errors = new FormErrorList();
91
92         BlogDAO blogDAO = new BlogDAO();
93         EntryDAO entryDAO = new EntryDAO();
94         CategoryDAO categoryDAO = new CategoryDAO();
95         UserpicDAO userpicDAO = new UserpicDAO();
96
97         String JavaDoc[] catId = request.getParameterValues("catId");
98         // ATTENTION! Category is now not a must-field anymore..
99
// if (catId == null || catId.length == 0) {
100
// errors.add(new FormError("category", "You must specify at least one
101
// category!"));
102
// catId = new String[0];
103
// }
104

105         String JavaDoc title = request.getParameter("title");
106         if (StringUtils.isEmpty(title)) {
107             errors.add(new FormError("title", "Entry title is not specified!"));
108         }
109
110         String JavaDoc body = request.getParameter("body");
111         if (StringUtils.isEmpty(body)) {
112             errors.add(new FormError("body", "Entry body is not specified!"));
113         }
114
115         String JavaDoc excerpt = request.getParameter("excerpt");
116
117         String JavaDoc commenting = request.getParameter("commenting");
118         String JavaDoc trackbacking = request.getParameter("trackbacking");
119         String JavaDoc anonymousCommenting = request.getParameter("anonymousCommenting");
120
121         String JavaDoc userpicId = request.getParameter("userpicId");
122
123         Blog b = blogDAO.getBlogByID(new Long JavaDoc(blogId));
124         if (!user.canPostToBlog(b))
125                 throw new BlogunityException(I18NStatusFactory
126                         .create(I18N.ERRORS.USER_CAN_NOT_POST_TO_BLOG));
127
128         String JavaDoc alias = request.getParameter("alias");
129         if (StringUtils.isNotEmpty(alias)) {
130             //check, if aliasname is valid
131
if (!BlogUtils.getInstance().isValidAliasname(alias)) {
132                 errors.add(new FormError("alias", "Aliasname is not valid!"));
133             } else {
134                 Entry e = entryDAO.getEntryByAliasname(alias, b.getUrlName());
135                 if (e != null) {
136                     errors.add(new FormError("alias",
137                             "There is already one entry with aliasname you specified!"));
138                 }
139             }
140         }
141
142         Entry e = new Entry();
143         e.setBlog(b);
144         e.setAliasname(alias);
145         e.setRawTitle(title);
146
147         e.setTrackbackAllowed(("on".equalsIgnoreCase(trackbacking)) ? true : false);
148         if (excerpt.trim().length() == 0) excerpt = null;
149         e.setRawExcerpt(excerpt);
150         e.setRawBody(body);
151         e.setCreateTime(new Date JavaDoc());
152         e.setAuthor(user);
153         e.setAuthorIP(request.getRemoteAddr());
154         e.setCommentingAllowed(("on".equalsIgnoreCase(commenting)) ? true : false);
155         e
156                 .setAnonymousCommentingAllowed(("on".equalsIgnoreCase(anonymousCommenting)) ? true
157                         : false);
158
159         if (catId != null && catId.length > 0) {
160             for (int i = 0; i < catId.length; i++) {
161                 e.addCategory(categoryDAO.getCategoryByID(new Long JavaDoc(catId[i])));
162             }
163         }
164
165         if (StringUtils.isEmpty(userpicId)) {
166             e.setUserpic(null);
167         } else {
168             // get id
169
try {
170                 StringTokenizer JavaDoc t = new StringTokenizer JavaDoc(userpicId, "|");
171                 String JavaDoc _id = t.nextToken();
172                 Userpic pic = userpicDAO.getUserpicByID(Long.parseLong(_id));
173                 e.setUserpic(pic);
174             } catch (NumberFormatException JavaDoc e1) {
175                 e.setUserpic(null);
176             }
177         }
178
179         //render on-the-fly
180
e = RenderUtils.render(e, request, response);
181
182         if (errors.size() > 0) {
183             request.setAttribute("errors", errors);
184             request.setAttribute("previewEntry", e);
185             request.setAttribute("blog", e.getBlog());
186             return PREVIEW_FORWARD;
187         }
188
189         String JavaDoc submit = request.getParameter("submit");
190
191         if (SUBMIT_POST.equals(submit)) {
192
193             entryDAO.createEntry(e);
194
195             // add entry to lucene index
196
SystemConfiguration config = BlogunityManager.getSystemConfiguration();
197
198             LuceneUtils.index(e, e.getBlog().getIndexesDirectory());
199
200             request.getSession().setAttribute("savedEntry", e);
201             navigationStack.pop();
202             return SUCCESS_REDIRECT;
203
204         } else if (SUBMIT_DRAFT.equals(submit)) {
205
206             e.setType(Entry.DRAFT);
207             entryDAO.createEntry(e);
208             request.getSession().setAttribute("savedEntry", e);
209             navigationStack.pop();
210             return SUCCESS_REDIRECT;
211
212         } else if (SUBMIT_PREVIEW.equals(submit)) {
213             request.setAttribute("isPreview", Boolean.TRUE);
214             request.setAttribute("previewEntry", e);
215             request.setAttribute("blog", e.getBlog());
216             return PREVIEW_FORWARD;
217         }
218
219         throw new BlogunityException(I18NStatusFactory.create(I18N.ERRORS.UNSUPPORTED_OPERATION));
220     }
221
222 }
Popular Tags