1 25 26 package com.j2biz.blogunity.web.actions.my; 27 28 import java.io.File ; 29 import java.util.Iterator ; 30 31 import javax.servlet.http.HttpServletRequest ; 32 import javax.servlet.http.HttpServletResponse ; 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 65 public class CreateBlogExecAction extends MyAbstractAction { 66 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 83 public IActionResult execute(HttpServletRequest request, HttpServletResponse 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 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 fullname = request.getParameter("fullname"); 104 if (StringUtils.isEmpty(fullname)) fullname = urlname; 105 106 String themeDir = request.getParameter("themeDir"); 107 if (StringUtils.isEmpty(themeDir)) { 108 errors.add(new FormError("themeDir", "Theme-Directory is not specified!")); 109 } else { 110 File _themeDir = new File (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 description = request.getParameter("description"); 120 String keywords = request.getParameter("keywords"); 121 String type = request.getParameter("type"); 122 String cType = request.getParameter("communityType"); 123 String commenting = request.getParameter("commenting"); 124 String anonymousCommenting = request.getParameter("anonymousCommenting"); 125 int blogType = Blog.INDIVIDUAL_BLOG; 126 try { 127 blogType = Integer.parseInt(type); 128 } catch (NumberFormatException e) { 129 log.error("Unable to parse blog type!", e); 130 } 131 132 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 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 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 if (errors.size() > 0) { 172 request.setAttribute("errors", errors); 173 174 File [] 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 Iterator 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 209 private void createBlogDirectory(Blog blog, String themeDirName) throws BlogunityException { 210 211 File defaultThemeDir = new File (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 boolean result = blog.getMainDataDirectory().mkdir(); 224 if (!result) { throw new BlogunityException(I18NStatusFactory.create( 225 I18N.ERRORS.CREATE_DIRECTORY, blog.getMainDataDirectory().getAbsolutePath())); } 226 result = blog.getThemeDirectory().mkdir(); 228 if (!result) { throw new BlogunityException(I18NStatusFactory.create( 229 I18N.ERRORS.CREATE_DIRECTORY, blog.getThemeDirectory().getAbsolutePath())); } 230 result = blog.getImagesDirectory().mkdir(); 232 if (!result) { throw new BlogunityException(I18NStatusFactory.create( 233 I18N.ERRORS.CREATE_DIRECTORY, blog.getImagesDirectory().getAbsolutePath())); } 234 result = blog.getFilesDirectory().mkdir(); 236 if (!result) { throw new BlogunityException(I18NStatusFactory.create( 237 I18N.ERRORS.CREATE_DIRECTORY, blog.getFilesDirectory().getAbsolutePath())); } 238 result = blog.getIndexesDirectory().mkdir(); 240 if (!result) { throw new BlogunityException(I18NStatusFactory.create( 241 I18N.ERRORS.CREATE_DIRECTORY, blog.getIndexesDirectory().getAbsolutePath())); } 242 243 LuceneUtils.createNewIndex(blog.getIndexesDirectory(), new StandardAnalyzer()); 245 246 ResourceUtils.copyDirectory(defaultThemeDir, blog.getThemeDirectory()); 248 249 } 250 251 } | Popular Tags |