KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: CreateBlogExecAction.java,v 1.13 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
31 import javax.servlet.http.HttpServletRequest JavaDoc;
32 import javax.servlet.http.HttpServletResponse JavaDoc;
33
34 import org.apache.commons.lang.StringUtils;
35 import org.apache.commons.logging.Log;
36 import org.apache.commons.logging.LogFactory;
37 import org.apache.lucene.analysis.standard.StandardAnalyzer;
38
39 import com.j2biz.blogunity.BlogunityManager;
40 import com.j2biz.blogunity.IConstants;
41 import com.j2biz.blogunity.dao.BlogDAO;
42 import com.j2biz.blogunity.dao.CategoryDAO;
43 import com.j2biz.blogunity.dao.UserDAO;
44 import com.j2biz.blogunity.exception.BlogunityException;
45 import com.j2biz.blogunity.i18n.I18N;
46 import com.j2biz.blogunity.i18n.I18NStatusFactory;
47 import com.j2biz.blogunity.pojo.Blog;
48 import com.j2biz.blogunity.pojo.Category;
49 import com.j2biz.blogunity.util.BlogUtils;
50 import com.j2biz.blogunity.util.HibernateUtil;
51 import com.j2biz.blogunity.util.LuceneUtils;
52 import com.j2biz.blogunity.util.ResourceUtils;
53 import com.j2biz.blogunity.web.ActionResultFactory;
54 import com.j2biz.blogunity.web.FormError;
55 import com.j2biz.blogunity.web.FormErrorList;
56 import com.j2biz.blogunity.web.IActionResult;
57
58 /**
59  * @author michelson
60  * @version $$
61  * @since 0.1
62  *
63  *
64  */

65 public class CreateBlogExecAction extends MyAbstractAction {
66     /**
67      * Logger for this class
68      */

69     private static final Log log = LogFactory.getLog(CreateBlogExecAction.class);
70
71     private static final IActionResult BLOG_FORM_FORWARD = ActionResultFactory
72             .buildForward("/jsp/my/createBlogForm.jsp");
73
74     private static final IActionResult CREATE_SUCCESS_FORWARD = ActionResultFactory
75             .buildForward("/jsp/my/createBlogSuccess.jsp");
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         int blogsperuser = BlogUtils.getInstance().getTotalNumberOfBlogsPerUser();
87         if (blogsperuser != -1 && user.getFoundedBlogs().size() >= blogsperuser) { throw new BlogunityException(
88                 I18NStatusFactory.create(I18N.ERRORS.USER_NOT_ALLOWED_TO_CREATE_NEW_BLOG)); }
89
90         FormErrorList errors = new FormErrorList();
91
92         BlogDAO blogDAO = new BlogDAO();
93         CategoryDAO categoryDAO = new CategoryDAO();
94         UserDAO userDAO = new UserDAO();
95
96         String JavaDoc urlname = request.getParameter("urlname");
97         if (StringUtils.isEmpty(urlname)) {
98             errors.add(new FormError("urlname", "Blogname is empty!"));
99         } else if (!BlogUtils.getInstance().isValidBlogname(urlname)) {
100             errors.add(new FormError("urlname", "Blogname is not valid!"));
101         }
102
103         String JavaDoc fullname = request.getParameter("fullname");
104         if (StringUtils.isEmpty(fullname)) fullname = urlname;
105
106         String JavaDoc themeDir = request.getParameter("themeDir");
107         if (StringUtils.isEmpty(themeDir)) {
108             errors.add(new FormError("themeDir", "Theme-Directory is not specified!"));
109         } else {
110             File JavaDoc _themeDir = new File JavaDoc(BlogunityManager.getServletContext().getRealPath(
111                     IConstants.BLOG_THEMES_DIRECTORY + themeDir));
112             if (!_themeDir.isDirectory() || !_themeDir.canRead()) {
113                 errors.add(new FormError("themeDir", "Can not read theme-directory!"));
114             } else if (_themeDir.list().length <= 0) {
115                 errors.add(new FormError("themeDir", "Theme-Directory is empty!"));
116             }
117         }
118
119         String JavaDoc description = request.getParameter("description");
120         String JavaDoc keywords = request.getParameter("keywords");
121         String JavaDoc type = request.getParameter("type");
122         String JavaDoc cType = request.getParameter("communityType");
123         String JavaDoc commenting = request.getParameter("commenting");
124         String JavaDoc anonymousCommenting = request.getParameter("anonymousCommenting");
125         int blogType = Blog.INDIVIDUAL_BLOG;
126         try {
127             blogType = Integer.parseInt(type);
128         } catch (NumberFormatException JavaDoc e) {
129             log.error("Unable to parse blog type!", e);
130         }
131
132         // check, if the max. number of founded blogs is not already arrived
133
if (blogType == Blog.INDIVIDUAL_BLOG) {
134             int individual = BlogUtils.getInstance().getNumberOfIndividualBlogsPerUser();
135             if (individual != -1 && user.getNumberOfIndividualBlogsFounded() >= individual) { throw new BlogunityException(
136                     I18NStatusFactory
137                             .create(I18N.ERRORS.USER_NOT_ALLOWED_TO_CREATE_INDIVIDUAL_BLOGS)); }
138         } else if (blogType == Blog.COMMUNITY_BLOG) {
139             int community = BlogUtils.getInstance().getNumberOfCommunityBlogsPerUser();
140             if (community != -1 && user.getNumberOfCommunityBlogsFounded() >= community) { throw new BlogunityException(
141                     I18NStatusFactory
142                             .create(I18N.ERRORS.USER_NOT_ALLOWED_TO_CREATE_COMMUNITY_BLOGS)); }
143         }
144
145         int communityType = Blog.PUBLIC_COMMUNTIY;
146         try {
147             communityType = Integer.parseInt(cType);
148         } catch (NumberFormatException JavaDoc e) {
149             log.error("Unable to parse community type!", e);
150         }
151
152         Blog b = new Blog();
153         b.setUrlName(StringUtils.isEmpty(urlname) ? null : urlname);
154         b.setFullName(StringUtils.isEmpty(fullname) ? null : fullname);
155         b.setDescription(StringUtils.isEmpty(description) ? null : description);
156         b.setKeywords(StringUtils.isEmpty(keywords) ? null : keywords);
157         b.setType(blogType);
158         b.setCommunityType(communityType);
159
160         request.setAttribute("newBlog", b);
161
162         // check, if blog this given name already exists
163
if (StringUtils.isNotEmpty(urlname)) {
164             Blog b1 = blogDAO.getBlogByUrlName(urlname);
165             if (b1 != null) {
166                 errors.add(new FormError("name", "Blog with the given name already exists!"));
167             }
168         }
169
170         // return, if errors exists
171
if (errors.size() > 0) {
172             request.setAttribute("errors", errors);
173
174             // set theme-directories to request
175
File JavaDoc[] themeDirs = BlogUtils.getInstance().getThemeDirectories();
176             request.setAttribute("themeDirs", themeDirs);
177
178             return BLOG_FORM_FORWARD;
179         }
180
181         b.setFounder(user);
182         user.addFoundedBlog(b);
183
184         // now add global categories to blog
185
Iterator JavaDoc categories = categoryDAO.getGlobalCategories().iterator();
186         while (categories.hasNext()) {
187             Category cat = (Category) categories.next();
188             b.addCategory(cat);
189         }
190
191         blogDAO.createBlog(b);
192         userDAO.updateUser(user);
193
194         try {
195             createBlogDirectory(b, themeDir);
196         } catch (BlogunityException e1) {
197             log.error("Error creating blog directory!", e1);
198             HibernateUtil.rollbackTransaction();
199             throw new BlogunityException(I18NStatusFactory.create(
200                     I18N.ERRORS.BLOG_CREATE_DIRECTORY, e1));
201         }
202
203         return CREATE_SUCCESS_FORWARD;
204     }
205
206     /**
207      *
208      */

209     private void createBlogDirectory(Blog blog, String JavaDoc themeDirName) throws BlogunityException {
210
211         File JavaDoc defaultThemeDir = new File JavaDoc(BlogunityManager.getServletContext().getRealPath(
212                 IConstants.BLOG_THEMES_DIRECTORY + themeDirName));
213
214         if (!defaultThemeDir.exists() || !defaultThemeDir.isDirectory()
215                 || !defaultThemeDir.canRead()) {
216
217         throw new BlogunityException(I18NStatusFactory.create(I18N.ERRORS.FIND_DIRECTORY,
218                 defaultThemeDir.getAbsolutePath()));
219
220         }
221
222         //1. Main directory
223
boolean result = blog.getMainDataDirectory().mkdir();
224         if (!result) { throw new BlogunityException(I18NStatusFactory.create(
225                 I18N.ERRORS.CREATE_DIRECTORY, blog.getMainDataDirectory().getAbsolutePath())); }
226         //2. Theme directory
227
result = blog.getThemeDirectory().mkdir();
228         if (!result) { throw new BlogunityException(I18NStatusFactory.create(
229                 I18N.ERRORS.CREATE_DIRECTORY, blog.getThemeDirectory().getAbsolutePath())); }
230         //3. Images directory
231
result = blog.getImagesDirectory().mkdir();
232         if (!result) { throw new BlogunityException(I18NStatusFactory.create(
233                 I18N.ERRORS.CREATE_DIRECTORY, blog.getImagesDirectory().getAbsolutePath())); }
234         //3. Files directory
235
result = blog.getFilesDirectory().mkdir();
236         if (!result) { throw new BlogunityException(I18NStatusFactory.create(
237                 I18N.ERRORS.CREATE_DIRECTORY, blog.getFilesDirectory().getAbsolutePath())); }
238         //4. Indexes directory
239
result = blog.getIndexesDirectory().mkdir();
240         if (!result) { throw new BlogunityException(I18NStatusFactory.create(
241                 I18N.ERRORS.CREATE_DIRECTORY, blog.getIndexesDirectory().getAbsolutePath())); }
242
243         // create lucene indexes for blog
244
LuceneUtils.createNewIndex(blog.getIndexesDirectory(), new StandardAnalyzer());
245
246         // copy default theme to newly created theme-directory
247
ResourceUtils.copyDirectory(defaultThemeDir, blog.getThemeDirectory());
248
249     }
250
251 }
Popular Tags