KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > blojsom > plugin > admin > EditBlogTemplatesPlugin


1 /**
2  * Copyright (c) 2003-2006, David A. Czarnecki
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * Redistributions of source code must retain the above copyright notice, this list of conditions and the
9  * following disclaimer.
10  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
11  * following disclaimer in the documentation and/or other materials provided with the distribution.
12  * Neither the name of "David A. Czarnecki" and "blojsom" nor the names of its contributors may be used to
13  * endorse or promote products derived from this software without specific prior written permission.
14  * Products derived from this software may not be called "blojsom", nor may "blojsom" appear in their name,
15  * without prior written permission of David A. Czarnecki.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
18  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
19  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
20  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
21  * EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */

31 package org.blojsom.plugin.admin;
32
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35 import org.blojsom.blog.Blog;
36 import org.blojsom.blog.Entry;
37 import org.blojsom.plugin.PluginException;
38 import org.blojsom.util.BlojsomConstants;
39 import org.blojsom.util.BlojsomUtils;
40
41 import javax.servlet.http.HttpServletRequest JavaDoc;
42 import javax.servlet.http.HttpServletResponse JavaDoc;
43 import java.io.*;
44 import java.util.*;
45
46 /**
47  * EditBlogTemplatesPlugin
48  *
49  * @author David Czarnecki
50  * @version $Id: EditBlogTemplatesPlugin.java,v 1.4 2006/05/13 14:08:53 czarneckid Exp $
51  * @since blojsom 3.0
52  */

53 public class EditBlogTemplatesPlugin extends BaseAdminPlugin {
54
55     private Log _logger = LogFactory.getLog(EditBlogTemplatesPlugin.class);
56
57     private static final String JavaDoc DEFAULT_ACCEPTED_TEMPLATE_EXTENSIONS = "vm";
58     private static final String JavaDoc ACCEPTED_TEMPLATE_EXTENSIONS_INIT_PARAM = "accepted-template-extensions";
59
60     // Pages
61
private static final String JavaDoc EDIT_BLOG_TEMPLATES_PAGE = "/org/blojsom/plugin/admin/templates/admin-edit-blog-templates";
62     private static final String JavaDoc EDIT_BLOG_TEMPLATE_PAGE = "/org/blojsom/plugin/admin/templates/admin-edit-blog-template";
63
64     // Constants
65
private static final String JavaDoc BLOJSOM_PLUGIN_EDIT_BLOG_TEMPLATES_TEMPLATE_FILES = "BLOJSOM_PLUGIN_EDIT_BLOG_TEMPLATES_TEMPLATE_FILES";
66     private static final String JavaDoc BLOJSOM_PLUGIN_EDIT_BLOG_TEMPLATES_TEMPLATE_FILE = "BLOJSOM_PLUGIN_EDIT_BLOG_TEMPLATES_TEMPLATE_FILE";
67     private static final String JavaDoc BLOJSOM_PLUGIN_EDIT_BLOG_TEMPLATES_TEMPLATE = "BLOJSOM_PLUGIN_EDIT_BLOG_TEMPLATES_TEMPLATE";
68     private static final String JavaDoc BLOJSOM_PLUGIN_EDIT_BLOG_TEMPLATES_DIRECTORIES = "BLOJSOM_PLUGIN_EDIT_BLOG_TEMPLATES_DIRECTORIES";
69
70     // Localization constants
71
private static final String JavaDoc FAILED_EDIT_TEMPLATES_PERMISSION_KEY = "failed.edit.templates.permission.text";
72     private static final String JavaDoc INVALID_TEMPLATE_PATH_KEY = "invalid.template.path.text";
73     private static final String JavaDoc UNABLE_TO_LOAD_TEMPLATE_KEY = "unable.to.load.template.text";
74     private static final String JavaDoc UNABLE_TO_DELETE_TEMPLATE_KEY = "unable.to.delete.template.text";
75     private static final String JavaDoc UPDATED_TEMPLATE_KEY = "updated.template.text";
76     private static final String JavaDoc NO_TEMPLATE_NAME_KEY = "no.template.name.text";
77     private static final String JavaDoc INVALID_TEMPLATE_EXTENSION_KEY = "invalid.template.extension.text";
78     private static final String JavaDoc TEMPLATE_DIRECTORY_NONEXISTENT_KEY = "template.directory.nonexistent.text";
79     private static final String JavaDoc TEMPLATE_DIRECTORY_NOTSPECIFIED_KEY = "template.directory.notspecified.text";
80     private static final String JavaDoc UNABLE_TO_ADD_TEMPLATE_DIRECTORY_KEY = "unable.to.add.template.directory.text";
81     private static final String JavaDoc ADDED_TEMPLATE_DIRECTORY_KEY = "added.template.directory.text";
82     private static final String JavaDoc CANNOT_REMOVE_TOP_TEMPLATE_DIRECTORY_KEY = "cannot.remove.top.template.directory.text";
83     private static final String JavaDoc UNABLE_TO_DELETE_TEMPLATE_DIRECTORY_KEY = "unable.to.delete.template.directory.text";
84     private static final String JavaDoc REMOVED_TEMPLATE_DIRECTORY_KEY = "removed.template.directory.text";
85     private static final String JavaDoc DELETED_TEMPLATE_KEY = "deleted.template.text";
86
87     // Actions
88
private static final String JavaDoc ADD_BLOG_TEMPLATE_ACTION = "add-blog-template";
89     private static final String JavaDoc DELETE_BLOG_TEMPLATE_ACTION = "delete-blog-template";
90     private static final String JavaDoc EDIT_BLOG_TEMPLATES_ACTION = "edit-blog-template";
91     private static final String JavaDoc UPDATE_BLOG_TEMPLATE_ACTION = "update-blog-template";
92     private static final String JavaDoc ADD_TEMPLATE_DIRECTORY_ACTION = "add-template-directory";
93     private static final String JavaDoc DELETE_TEMPLATE_DIRECTORY_ACTION = "delete-template-directory";
94
95     // Form elements
96
private static final String JavaDoc BLOG_TEMPLATE = "blog-template";
97     private static final String JavaDoc BLOG_TEMPLATE_DATA = "blog-template-data";
98     private static final String JavaDoc BLOG_TEMPLATE_DIRECTORY = "blog-template-directory";
99     private static final String JavaDoc TEMPLATE_DIRECTORY_TO_ADD = "template-directory-to-add";
100
101     // Permissions
102
private static final String JavaDoc EDIT_BLOG_TEMPLATES_PERMISSION = "edit_blog_templates_permission";
103
104     private Map _acceptedTemplateExtensions;
105     private Properties _templateEditProperties;
106     private Properties _blojsomProperties;
107     private String JavaDoc _templatesDirectory;
108     private String JavaDoc _blogsDirectory;
109
110     /**
111      * Default constructor.
112      */

113     public EditBlogTemplatesPlugin() {
114     }
115
116     /**
117      * Set the template editing properties
118      *
119      * @param templateEditProperties Template editing properties
120      */

121     public void setTemplateEditProperties(Properties templateEditProperties) {
122         _templateEditProperties = templateEditProperties;
123     }
124
125     /**
126      * Set the default blojsom properties
127      *
128      * @param blojsomProperties Default blojsom properties
129      */

130     public void setBlojsomProperties(Properties blojsomProperties) {
131         _blojsomProperties = blojsomProperties;
132     }
133
134     /**
135      * Initialize this plugin. This method only called when the plugin is instantiated.
136      *
137      * @throws org.blojsom.plugin.PluginException
138      * If there is an error initializing the plugin
139      */

140     public void init() throws PluginException {
141         super.init();
142
143         String JavaDoc acceptedTemplateExtensions = _templateEditProperties.getProperty(ACCEPTED_TEMPLATE_EXTENSIONS_INIT_PARAM);
144         if (BlojsomUtils.checkNullOrBlank(acceptedTemplateExtensions)) {
145             acceptedTemplateExtensions = DEFAULT_ACCEPTED_TEMPLATE_EXTENSIONS;
146         }
147
148         _acceptedTemplateExtensions = new HashMap();
149         String JavaDoc[] templateExtensions = BlojsomUtils.parseCommaList(acceptedTemplateExtensions);
150         for (int i = 0; i < templateExtensions.length; i++) {
151             String JavaDoc templateExtension = templateExtensions[i];
152             _acceptedTemplateExtensions.put(templateExtension, templateExtension);
153         }
154
155         _templatesDirectory = _blojsomProperties.getProperty(BlojsomConstants.TEMPLATES_DIRECTORY_IP, BlojsomConstants.DEFAULT_TEMPLATES_DIRECTORY);
156         _blogsDirectory = _blojsomProperties.getProperty(BlojsomConstants.BLOGS_DIRECTORY_IP, BlojsomConstants.DEFAULT_BLOGS_DIRECTORY);
157     }
158
159     /**
160      * Sanitize a filename
161      *
162      * @param blogTemplate Blog template filename
163      * @return Sanitized filename or <code>null</code> if error in sanitizing
164      */

165     protected String JavaDoc sanitizeFilename(String JavaDoc blogTemplate) {
166         String JavaDoc templateFilename = new File(blogTemplate).getName();
167         int lastSeparator;
168         blogTemplate = BlojsomUtils.normalize(blogTemplate);
169         lastSeparator = blogTemplate.lastIndexOf(File.separator);
170         if (lastSeparator == -1) {
171             if (templateFilename != null) {
172                 return templateFilename;
173             } else {
174                 return null;
175             }
176         } else {
177             blogTemplate = blogTemplate.substring(0, lastSeparator + 1) + templateFilename;
178         }
179
180         return blogTemplate;
181     }
182
183     /**
184      * Put the list of template files in the context
185      *
186      * @param templatesDirectory Templates directory
187      * @param context Context
188      */

189     protected void putTemplatesInContext(File templatesDirectory, Map context) {
190         List templateFiles = new ArrayList();
191         BlojsomUtils.listFilesInSubdirectories(templatesDirectory, templatesDirectory.getAbsolutePath(), templateFiles);
192         File[] templates = (File[]) templateFiles.toArray(new File[templateFiles.size()]);
193         Arrays.sort(templates);
194
195         context.put(BLOJSOM_PLUGIN_EDIT_BLOG_TEMPLATES_TEMPLATE_FILES, templates);
196     }
197
198     /**
199      * Put the list of template directories in the context
200      *
201      * @param templatesDirectory Templates directory
202      * @param context Context
203      */

204     protected void putTemplateDirectoriesInContext(File templatesDirectory, Map context) {
205         List templateDirectories = new ArrayList();
206         BlojsomUtils.listDirectoriesInSubdirectories(templatesDirectory, templatesDirectory.getAbsolutePath(), templateDirectories);
207         File[] directories = (File[]) templateDirectories.toArray(new File[templateDirectories.size()]);
208         Arrays.sort(directories);
209
210         context.put(BLOJSOM_PLUGIN_EDIT_BLOG_TEMPLATES_DIRECTORIES, directories);
211     }
212
213     /**
214      * Process the blog entries
215      *
216      * @param httpServletRequest Request
217      * @param httpServletResponse Response
218      * @param user {@link org.blojsom.blog.BlogUser} instance
219      * @param context Context
220      * @param entries Blog entries retrieved for the particular request
221      * @return Modified set of blog entries
222      * @throws PluginException If there is an error processing the blog entries
223      */

224     public Entry[] process(HttpServletRequest JavaDoc httpServletRequest, HttpServletResponse JavaDoc httpServletResponse, Blog blog, Map context, Entry[] entries) throws PluginException {
225         if (!authenticateUser(httpServletRequest, httpServletResponse, context, blog)) {
226             httpServletRequest.setAttribute(BlojsomConstants.PAGE_PARAM, ADMIN_LOGIN_PAGE);
227
228             return entries;
229         }
230
231         String JavaDoc username = getUsernameFromSession(httpServletRequest, blog);
232         if (!checkPermission(blog, null, username, EDIT_BLOG_TEMPLATES_PERMISSION)) {
233             httpServletRequest.setAttribute(BlojsomConstants.PAGE_PARAM, ADMIN_ADMINISTRATION_PAGE);
234             addOperationResultMessage(context, getAdminResource(FAILED_EDIT_TEMPLATES_PERMISSION_KEY, FAILED_EDIT_TEMPLATES_PERMISSION_KEY, blog.getBlogAdministrationLocale()));
235
236             return entries;
237         }
238
239         // Add list of templates to context
240
File templatesDirectory = new File(_servletConfig.getServletContext().getRealPath("/") + BlojsomConstants.DEFAULT_CONFIGURATION_BASE_DIRECTORY + _blogsDirectory + blog.getBlogId() + _templatesDirectory);
241         _logger.debug("Looking for templates in directory: " + templatesDirectory.toString());
242
243         putTemplatesInContext(templatesDirectory, context);
244         putTemplateDirectoriesInContext(templatesDirectory, context);
245
246         String JavaDoc action = BlojsomUtils.getRequestValue(ACTION_PARAM, httpServletRequest);
247         if (BlojsomUtils.checkNullOrBlank(action)) {
248             _logger.debug("User did not request edit action");
249             httpServletRequest.setAttribute(BlojsomConstants.PAGE_PARAM, ADMIN_ADMINISTRATION_PAGE);
250         } else if (PAGE_ACTION.equals(action)) {
251             _logger.debug("User requested edit blog templates page");
252
253             httpServletRequest.setAttribute(BlojsomConstants.PAGE_PARAM, EDIT_BLOG_TEMPLATES_PAGE);
254         } else if (EDIT_BLOG_TEMPLATES_ACTION.equals(action)) {
255             _logger.debug("User requested edit blog templates action");
256
257             String JavaDoc blogTemplate = BlojsomUtils.getRequestValue(BLOG_TEMPLATE, httpServletRequest);
258             if (BlojsomUtils.checkNullOrBlank(blogTemplate)) {
259                 httpServletRequest.setAttribute(BlojsomConstants.PAGE_PARAM, EDIT_BLOG_TEMPLATES_PAGE);
260
261                 return entries;
262             }
263
264             blogTemplate = sanitizeFilename(blogTemplate);
265             if (blogTemplate == null) {
266                 addOperationResultMessage(context, getAdminResource(INVALID_TEMPLATE_PATH_KEY, INVALID_TEMPLATE_PATH_KEY, blog.getBlogAdministrationLocale()));
267                 httpServletRequest.setAttribute(BlojsomConstants.PAGE_PARAM, EDIT_BLOG_TEMPLATES_PAGE);
268
269                 return entries;
270             }
271
272             File blogTemplateFile = new File(_servletConfig.getServletContext().getRealPath("/") + BlojsomConstants.DEFAULT_CONFIGURATION_BASE_DIRECTORY + _blogsDirectory + blog.getBlogId() + _templatesDirectory + blogTemplate);
273             _logger.debug("Reading template file: " + blogTemplateFile.toString());
274
275             try {
276                 BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(blogTemplateFile), BlojsomConstants.UTF8));
277                 String JavaDoc input;
278                 StringBuffer JavaDoc template = new StringBuffer JavaDoc();
279
280                 while ((input = br.readLine()) != null) {
281                     template.append(input);
282                     template.append(BlojsomConstants.LINE_SEPARATOR);
283                 }
284
285                 br.close();
286
287                 context.put(BLOJSOM_PLUGIN_EDIT_BLOG_TEMPLATES_TEMPLATE_FILE, blogTemplate);
288                 context.put(BLOJSOM_PLUGIN_EDIT_BLOG_TEMPLATES_TEMPLATE, BlojsomUtils.escapeString(template.toString()));
289                 httpServletRequest.setAttribute(BlojsomConstants.PAGE_PARAM, EDIT_BLOG_TEMPLATE_PAGE);
290             } catch (UnsupportedEncodingException e) {
291                 _logger.error(e);
292                 addOperationResultMessage(context, formatAdminResource(UNABLE_TO_LOAD_TEMPLATE_KEY, UNABLE_TO_LOAD_TEMPLATE_KEY, blog.getBlogAdministrationLocale(), new Object JavaDoc[]{blogTemplate}));
293                 httpServletRequest.setAttribute(BlojsomConstants.PAGE_PARAM, EDIT_BLOG_TEMPLATES_PAGE);
294             } catch (IOException e) {
295                 _logger.error(e);
296                 addOperationResultMessage(context, formatAdminResource(UNABLE_TO_LOAD_TEMPLATE_KEY, UNABLE_TO_LOAD_TEMPLATE_KEY, blog.getBlogAdministrationLocale(), new Object JavaDoc[]{blogTemplate}));
297                 httpServletRequest.setAttribute(BlojsomConstants.PAGE_PARAM, EDIT_BLOG_TEMPLATES_PAGE);
298             }
299         } else if (UPDATE_BLOG_TEMPLATE_ACTION.equals(action)) {
300             _logger.debug("User requested update blog template action");
301
302             String JavaDoc blogTemplate = BlojsomUtils.getRequestValue(BLOG_TEMPLATE, httpServletRequest);
303             if (BlojsomUtils.checkNullOrBlank(blogTemplate)) {
304                 httpServletRequest.setAttribute(BlojsomConstants.PAGE_PARAM, EDIT_BLOG_TEMPLATES_PAGE);
305
306                 return entries;
307             }
308
309             blogTemplate = sanitizeFilename(blogTemplate);
310             if (blogTemplate == null) {
311                 addOperationResultMessage(context, getAdminResource(INVALID_TEMPLATE_PATH_KEY, INVALID_TEMPLATE_PATH_KEY, blog.getBlogAdministrationLocale()));
312                 httpServletRequest.setAttribute(BlojsomConstants.PAGE_PARAM, EDIT_BLOG_TEMPLATES_PAGE);
313
314                 return entries;
315             }
316
317             String JavaDoc blogTemplateData = BlojsomUtils.getRequestValue(BLOG_TEMPLATE_DATA, httpServletRequest);
318             File blogTemplateFile = new File(_servletConfig.getServletContext().getRealPath("/") + BlojsomConstants.DEFAULT_CONFIGURATION_BASE_DIRECTORY + _blogsDirectory + blog.getBlogId() + _templatesDirectory + blogTemplate);
319
320             _logger.debug("Writing template file: " + blogTemplateFile.toString());
321
322             try {
323                 BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(blogTemplateFile), BlojsomConstants.UTF8));
324                 bw.write(blogTemplateData);
325                 bw.close();
326             } catch (UnsupportedEncodingException e) {
327                 _logger.error(e);
328                 addOperationResultMessage(context, formatAdminResource(UNABLE_TO_DELETE_TEMPLATE_KEY, UNABLE_TO_DELETE_TEMPLATE_KEY, blog.getBlogAdministrationLocale(), new Object JavaDoc[]{blogTemplate}));
329             } catch (IOException e) {
330                 _logger.error(e);
331                 addOperationResultMessage(context, formatAdminResource(UNABLE_TO_DELETE_TEMPLATE_KEY, UNABLE_TO_DELETE_TEMPLATE_KEY, blog.getBlogAdministrationLocale(), new Object JavaDoc[]{blogTemplate}));
332             }
333
334             addOperationResultMessage(context, formatAdminResource(UPDATED_TEMPLATE_KEY, UPDATED_TEMPLATE_KEY, blog.getBlogAdministrationLocale(), new Object JavaDoc[]{blogTemplate}));
335
336             context.put(BLOJSOM_PLUGIN_EDIT_BLOG_TEMPLATES_TEMPLATE_FILE, blogTemplate);
337             context.put(BLOJSOM_PLUGIN_EDIT_BLOG_TEMPLATES_TEMPLATE, BlojsomUtils.escapeString(blogTemplateData));
338             httpServletRequest.setAttribute(BlojsomConstants.PAGE_PARAM, EDIT_BLOG_TEMPLATE_PAGE);
339         } else if (ADD_BLOG_TEMPLATE_ACTION.equals(action)) {
340             _logger.debug("User requested add blog template action");
341
342             String JavaDoc blogTemplate = BlojsomUtils.getRequestValue(BLOG_TEMPLATE, httpServletRequest);
343             String JavaDoc blogTemplateDirectory = BlojsomUtils.getRequestValue(BLOG_TEMPLATE_DIRECTORY, httpServletRequest);
344
345             if (BlojsomUtils.checkNullOrBlank(blogTemplate)) {
346                 addOperationResultMessage(context, getAdminResource(NO_TEMPLATE_NAME_KEY, NO_TEMPLATE_NAME_KEY, blog.getBlogAdministrationLocale()));
347                 httpServletRequest.setAttribute(BlojsomConstants.PAGE_PARAM, EDIT_BLOG_TEMPLATES_PAGE);
348
349                 return entries;
350             }
351
352             String JavaDoc templateName = BlojsomUtils.getFilenameFromPath(blogTemplate);
353             String JavaDoc templateExtension = BlojsomUtils.getFileExtension(templateName);
354
355             if (!_acceptedTemplateExtensions.containsKey(templateExtension)) {
356                 addOperationResultMessage(context, formatAdminResource(INVALID_TEMPLATE_EXTENSION_KEY, INVALID_TEMPLATE_EXTENSION_KEY, blog.getBlogAdministrationLocale(), new Object JavaDoc[]{templateExtension}));
357                 httpServletRequest.setAttribute(BlojsomConstants.PAGE_PARAM, EDIT_BLOG_TEMPLATES_PAGE);
358
359                 return entries;
360             } else {
361                 blogTemplateDirectory = BlojsomUtils.normalize(blogTemplateDirectory);
362                 File addedTemplateDirectory = new File(templatesDirectory, blogTemplateDirectory);
363                 if (addedTemplateDirectory.exists()) {
364                     context.put(BLOJSOM_PLUGIN_EDIT_BLOG_TEMPLATES_TEMPLATE_FILE, blogTemplateDirectory + File.separator + templateName);
365                     context.put(BLOJSOM_PLUGIN_EDIT_BLOG_TEMPLATES_TEMPLATE, "");
366
367                     httpServletRequest.setAttribute(BlojsomConstants.PAGE_PARAM, EDIT_BLOG_TEMPLATE_PAGE);
368                 } else {
369                     addOperationResultMessage(context, getAdminResource(TEMPLATE_DIRECTORY_NONEXISTENT_KEY, TEMPLATE_DIRECTORY_NONEXISTENT_KEY, blog.getBlogAdministrationLocale()));
370                 }
371             }
372         } else if (ADD_TEMPLATE_DIRECTORY_ACTION.equals(action)) {
373             _logger.debug("User requested add blog template directory action");
374
375             String JavaDoc templateDirectoryToAdd = BlojsomUtils.getRequestValue(TEMPLATE_DIRECTORY_TO_ADD, httpServletRequest);
376             String JavaDoc blogTemplateDirectory = BlojsomUtils.getRequestValue(BLOG_TEMPLATE_DIRECTORY, httpServletRequest);
377             if (BlojsomUtils.checkNullOrBlank(templateDirectoryToAdd)) {
378                 addOperationResultMessage(context, getAdminResource(TEMPLATE_DIRECTORY_NOTSPECIFIED_KEY, TEMPLATE_DIRECTORY_NOTSPECIFIED_KEY, blog.getBlogAdministrationLocale()));
379             } else {
380                 blogTemplateDirectory = BlojsomUtils.normalize(blogTemplateDirectory);
381                 templateDirectoryToAdd = BlojsomUtils.normalize(templateDirectoryToAdd);
382
383                 File newTemplateDirectory = new File(templatesDirectory, blogTemplateDirectory + File.separator + templateDirectoryToAdd);
384                 _logger.debug("Adding blog template directory: " + newTemplateDirectory.toString());
385
386                 if (!newTemplateDirectory.mkdir()) {
387                     addOperationResultMessage(context, formatAdminResource(UNABLE_TO_ADD_TEMPLATE_DIRECTORY_KEY, UNABLE_TO_ADD_TEMPLATE_DIRECTORY_KEY, blog.getBlogAdministrationLocale(), new Object JavaDoc[]{templateDirectoryToAdd}));
388                 } else {
389                     addOperationResultMessage(context, formatAdminResource(ADDED_TEMPLATE_DIRECTORY_KEY, ADDED_TEMPLATE_DIRECTORY_KEY, blog.getBlogAdministrationLocale(), new Object JavaDoc[]{templateDirectoryToAdd}));
390
391                     putTemplateDirectoriesInContext(templatesDirectory, context);
392                 }
393             }
394
395             httpServletRequest.setAttribute(BlojsomConstants.PAGE_PARAM, EDIT_BLOG_TEMPLATES_PAGE);
396         } else if (DELETE_TEMPLATE_DIRECTORY_ACTION.equals(action)) {
397             _logger.debug("User requested delete blog template directory action");
398
399             String JavaDoc blogTemplateDirectory = BlojsomUtils.getRequestValue(BLOG_TEMPLATE_DIRECTORY, httpServletRequest);
400             if (BlojsomUtils.checkNullOrBlank(blogTemplateDirectory)) {
401                 addOperationResultMessage(context, getAdminResource(CANNOT_REMOVE_TOP_TEMPLATE_DIRECTORY_KEY, CANNOT_REMOVE_TOP_TEMPLATE_DIRECTORY_KEY, blog.getBlogAdministrationLocale()));
402             } else {
403                 blogTemplateDirectory = BlojsomUtils.normalize(blogTemplateDirectory);
404                 _logger.debug("Sanitized template directory: " + blogTemplateDirectory);
405                 File templateDirectoryToDelete = new File(templatesDirectory, blogTemplateDirectory);
406                 _logger.debug("Removing blog template directory: " + templateDirectoryToDelete);
407
408                 if (!BlojsomUtils.deleteDirectory(templateDirectoryToDelete, true)) {
409                     addOperationResultMessage(context, formatAdminResource(UNABLE_TO_DELETE_TEMPLATE_DIRECTORY_KEY, UNABLE_TO_DELETE_TEMPLATE_DIRECTORY_KEY, blog.getBlogAdministrationLocale(), new Object JavaDoc[]{blogTemplateDirectory}));
410                 } else {
411                     addOperationResultMessage(context, formatAdminResource(REMOVED_TEMPLATE_DIRECTORY_KEY, REMOVED_TEMPLATE_DIRECTORY_KEY, blog.getBlogAdministrationLocale(), new Object JavaDoc[]{blogTemplateDirectory}));
412
413                     putTemplateDirectoriesInContext(templatesDirectory, context);
414                 }
415             }
416
417             httpServletRequest.setAttribute(BlojsomConstants.PAGE_PARAM, EDIT_BLOG_TEMPLATES_PAGE);
418         } else if (DELETE_BLOG_TEMPLATE_ACTION.equals(action)) {
419             _logger.debug("User requested delete blog template directory action");
420
421             String JavaDoc blogTemplate = BlojsomUtils.getRequestValue(BLOG_TEMPLATE, httpServletRequest);
422             if (BlojsomUtils.checkNullOrBlank(blogTemplate)) {
423                 addOperationResultMessage(context, getAdminResource(NO_TEMPLATE_NAME_KEY, NO_TEMPLATE_NAME_KEY, blog.getBlogAdministrationLocale()));
424             }
425
426             blogTemplate = sanitizeFilename(blogTemplate);
427             File templateToDelete = new File(templatesDirectory, blogTemplate);
428             _logger.debug("Deleting blog template: " + templateToDelete.toString());
429
430             if (!templateToDelete.delete()) {
431                 addOperationResultMessage(context, formatAdminResource(UNABLE_TO_DELETE_TEMPLATE_KEY, UNABLE_TO_DELETE_TEMPLATE_KEY, blog.getBlogAdministrationLocale(), new Object JavaDoc[]{blogTemplate}));
432             } else {
433                 addOperationResultMessage(context, formatAdminResource(DELETED_TEMPLATE_KEY, DELETED_TEMPLATE_KEY, blog.getBlogAdministrationLocale(), new Object JavaDoc[]{blogTemplate}));
434
435                 putTemplatesInContext(templatesDirectory, context);
436             }
437
438             httpServletRequest.setAttribute(BlojsomConstants.PAGE_PARAM, EDIT_BLOG_TEMPLATES_PAGE);
439         }
440
441         return entries;
442     }
443 }
444
Popular Tags