Author: ghuber
Date: Thu Feb  2 16:49:26 2012
New Revision: 1239719

URL: http://svn.apache.org/viewvc?rev=1239719&view=rev
Log:
Template testing/mods.....

Modified:
    
roller/trunk/weblogger-business/src/main/java/org/apache/roller/weblogger/business/themes/ThemeManagerImpl.java
    
roller/trunk/weblogger-web/src/main/java/org/apache/roller/weblogger/ui/struts2/editor/TemplateRemove.java
    
roller/trunk/weblogger-web/src/main/java/org/apache/roller/weblogger/ui/struts2/editor/Templates.java
    
roller/trunk/weblogger-web/src/main/java/org/apache/roller/weblogger/ui/struts2/editor/TemplatesRemove.java
    
roller/trunk/weblogger-web/src/main/resources/ApplicationResources.properties
    
roller/trunk/weblogger-webapp/src/main/webapp/WEB-INF/jsps/editor/TemplateRemove.jsp

Modified: 
roller/trunk/weblogger-business/src/main/java/org/apache/roller/weblogger/business/themes/ThemeManagerImpl.java
URL: 
http://svn.apache.org/viewvc/roller/trunk/weblogger-business/src/main/java/org/apache/roller/weblogger/business/themes/ThemeManagerImpl.java?rev=1239719&r1=1239718&r2=1239719&view=diff
==============================================================================
--- 
roller/trunk/weblogger-business/src/main/java/org/apache/roller/weblogger/business/themes/ThemeManagerImpl.java
 (original)
+++ 
roller/trunk/weblogger-business/src/main/java/org/apache/roller/weblogger/business/themes/ThemeManagerImpl.java
 Thu Feb  2 16:49:26 2012
@@ -57,409 +57,438 @@ import org.apache.roller.weblogger.util.
 
 /**
  * Base implementation of a ThemeManager.
- *
- * This particular implementation reads theme data off the filesystem
- * and assumes that those themes are not changable at runtime.
+ * 
+ * This particular implementation reads theme data off the filesystem and
+ * assumes that those themes are not changable at runtime.
  */
 @com.google.inject.Singleton
 public class ThemeManagerImpl implements ThemeManager {
 
-   static FileTypeMap map = null;
-   static {
-         // TODO: figure out why PNG is missing from Java MIME types
-        map = FileTypeMap.getDefaultFileTypeMap();
-        if (map instanceof MimetypesFileTypeMap) {
-            try {
-                ((MimetypesFileTypeMap)map).addMimeTypes("image/png png PNG");
-            } catch (Exception ignored) {}
-        }
-    }
-
-    private static Log log = LogFactory.getLog(ThemeManagerImpl.class);
-    private final Weblogger roller;
-    // directory where themes are kept
-    private String themeDir = null;
-    // the Map contains ... (theme id, Theme)
-    private Map themes = null;
-
-    // list of available types for templates
-    private static ArrayList<String> typeList = new ArrayList<String>();
-
-    @com.google.inject.Inject
-    protected ThemeManagerImpl(Weblogger roller) {
-
-        this.roller = roller;
-
-        //set the available types that can be used for templates
-        this.addAvailableTypes();
-
-        // get theme directory from config and verify it
-        this.themeDir = WebloggerConfig.getProperty("themes.dir");
-        if (themeDir == null || themeDir.trim().length() < 1) {
-            throw new RuntimeException("couldn't get themes directory from 
config");
-        } else {
-            // chop off trailing slash if it exists
-            if (themeDir.endsWith("/")) {
-                themeDir = themeDir.substring(0, themeDir.length() - 1);
-            }
-
-            // make sure it exists and is readable
-            File themeDirFile = new File(themeDir);
-            if (!themeDirFile.exists()
-                    || !themeDirFile.isDirectory()
-                    || !themeDirFile.canRead()) {
-                throw new RuntimeException("couldn't access theme dir [" + 
themeDir + "]");
-            }
-        }
-    }
-
-     public void initialize() throws InitializationException {
-
-        log.debug("Initializing Theme Manager");
-
-        if (themeDir != null) {
-            // rather than be lazy we are going to load all themes from
-            // the disk preemptive and cache them
-            this.themes = loadAllThemesFromDisk();
-
-            log.info("Loaded " + this.themes.size() + " themes from disk.");
-        }
-    }
-
-    /**
-     * @see 
org.apache.roller.weblogger.model.ThemeManager#getTheme(java.lang.String)
-     */
-    public SharedTheme getTheme(String id)
-            throws ThemeNotFoundException, WebloggerException {
-
-        // try to lookup theme from library
-        SharedTheme theme = (SharedTheme) this.themes.get(id);
-
-        // no theme?  throw exception.
-        if (theme == null) {
-            throw new ThemeNotFoundException("Couldn't find theme [" + id + 
"]");
-        }
-
-        return theme;
-    }
-
-    /**
-     * @see org.apache.roller.weblogger.model.ThemeManager#getTheme(weblog)
-     */
-    public WeblogTheme getTheme(Weblog weblog) throws WebloggerException {
-
-        if (weblog == null) {
-            return null;
-        }
-
-        WeblogTheme weblogTheme = null;
-
-        // if theme is custom or null then return a WeblogCustomTheme
-        if (weblog.getEditorTheme() == null
-                || WeblogTheme.CUSTOM.equals(weblog.getEditorTheme())) {
-            weblogTheme = new WeblogCustomTheme(weblog);
-
-            // otherwise we are returning a WeblogSharedTheme
-        } else {
-            ThemeManager themeMgr = roller.getThemeManager();
-            SharedTheme staticTheme =
-                    (SharedTheme) this.themes.get(weblog.getEditorTheme());
-            if (staticTheme != null) {
-                weblogTheme = new WeblogSharedTheme(weblog, staticTheme);
-            } else {
-                log.warn("Unable to lookup theme " + weblog.getEditorTheme());
-            }
-        }
-
-        // TODO: if somehow the theme is still null should we provide some
-        // kind of fallback option like a default theme?
-
-        return weblogTheme;
-    }
-
-    /**
-     * @see 
org.apache.roller.weblogger.model.ThemeManager#getEnabledThemesList()
-     *
-     * TODO: reimplement enabled vs. disabled logic once we support it
-     */
-    public List getEnabledThemesList() {
-
-        List all_themes = new ArrayList(this.themes.values());
-
-        // sort 'em ... default ordering for themes is by name
-        Collections.sort(all_themes);
-
-        return all_themes;
-    }
-
-    /**
-     * @see 
org.apache.roller.weblogger.model.ThemeManager#importTheme(website, theme)
-     */
-    public void importTheme(Weblog website, SharedTheme theme)
-            throws WebloggerException {
-
-        log.debug("Importing theme [" + theme.getName() + "] to weblog [" + 
website.getName() + "]");
-
-        WeblogManager wmgr = roller.getWeblogManager();
-        MediaFileManager fileMgr = roller.getMediaFileManager();
-
-        MediaFileDirectory root = fileMgr.getMediaFileRootDirectory(website);
-        log.warn("Weblog " + website.getHandle() + " does not have a root 
MediaFile directory");
-
-        Set importedActionTemplates = new HashSet();
-        ThemeTemplate themeTemplate = null;
-        ThemeTemplate stylesheetTemplate = theme.getStylesheet();
-        Iterator iter = theme.getTemplates().iterator();
-        while (iter.hasNext()) {
-            themeTemplate = (ThemeTemplate) iter.next();
-
-            WeblogTemplate template = null;
-
-            // if template is an action, lookup by action
-            if (themeTemplate.getAction() != null
-                    && 
!themeTemplate.getAction().equals(WeblogTemplate.ACTION_CUSTOM)) {
-                importedActionTemplates.add(themeTemplate.getAction());
-                template = wmgr.getPageByAction(website, 
themeTemplate.getAction());
-
-                // otherwise, lookup by name
-            } else {
-                template = wmgr.getPageByName(website, 
themeTemplate.getName());
-            }
-
-            // Weblog does not have this template, so create it.
-            boolean newTmpl = false;
-            if (template == null) {
-                template = new WeblogTemplate();
-                template.setWebsite(website);
-                newTmpl = true;
-            }
-
-            // TODO: fix conflict situation
-            // it's possible that someone has defined a theme template which
-            // matches 2 existing templates, 1 by action, the other by name
-
-            // update template attributes
-            // NOTE: we don't want to copy the template data for an existing 
stylesheet
-            if (newTmpl || !themeTemplate.equals(stylesheetTemplate)) {
-                template.setAction(themeTemplate.getAction());
-                template.setName(themeTemplate.getName());
-                template.setDescription(themeTemplate.getDescription());
-                template.setLink(themeTemplate.getLink());
-                template.setContents(themeTemplate.getContents());
-                template.setHidden(themeTemplate.isHidden());
-                template.setNavbar(themeTemplate.isNavbar());
-                
template.setTemplateLanguage(themeTemplate.getTemplateLanguage());
-                // NOTE: decorators are deprecated starting in 4.0
-                template.setDecoratorName(null);
-                template.setLastModified(new Date());
-
-                // save it
-                wmgr.savePage(template);
-            }
-
-            // create weblog template code objects and save them 
-            for (String type : ThemeManagerImpl.getTypesList()) {
-               WeblogThemeTemplateCode weblogTemplateCode = 
(WeblogThemeTemplateCode) template
-                               .getTemplateCode(type);
-               if (weblogTemplateCode == null) {
-                       TemplateCode templateCode = 
themeTemplate.getTemplateCode(type);
-                       if (templateCode != null) {
-                               weblogTemplateCode = new 
WeblogThemeTemplateCode(template.getId(), type);
-                               weblogTemplateCode.setType(type);
-                               
weblogTemplateCode.setTemplate(templateCode.getTemplate());
-                               
weblogTemplateCode.setTemplateLanguage(templateCode.getTemplateLanguage());
-                               
weblogTemplateCode.setContentType(templateCode.getContentType());
-                               
WebloggerFactory.getWeblogger().getWeblogManager().saveTemplateCode(weblogTemplateCode);
-                       }
-               }
-            }
-        }
-
-        // now, see if the weblog has left over action templates that
-        // need to be deleted because they aren't in their new theme
-        for (int i = 0; i < WeblogTemplate.ACTIONS.length; i++) {
-            String action = WeblogTemplate.ACTIONS[i];
-
-            // if we didn't import this action then see if it should be deleted
-            if (!importedActionTemplates.contains(action)) {
-                WeblogTemplate toDelete = wmgr.getPageByAction(website, 
action);
-                if (toDelete != null) {
-                    log.debug("Removing stale action template " + 
toDelete.getId());
-                    wmgr.removePage(toDelete);
-                }
-            }
-        }
-
-
-        // always update this weblog's theme and customStylesheet, then save
-        website.setEditorTheme(WeblogTheme.CUSTOM);
-        if (theme.getStylesheet() != null) {
-            website.setCustomStylesheetPath(theme.getStylesheet().getLink());
-        }
-        wmgr.saveWeblog(website);
-
-        // now lets import all the theme resources
-        List resources = theme.getResources();
-        Iterator iterat = resources.iterator();
-        ThemeResource resource = null;
-        while (iterat.hasNext()) {
-            resource = (ThemeResource) iterat.next();
-
-            log.debug("Importing resource " + resource.getPath());
-
-            if (resource.isDirectory()) {
-                MediaFileDirectory mdir =
-                    fileMgr.getMediaFileDirectoryByPath(website, 
resource.getPath());
-                if (mdir == null) {
-                    log.debug("    Creating directory: " + resource.getPath());
-                    mdir = fileMgr.createMediaFileDirectory(
-                       fileMgr.getMediaFileRootDirectory(website), 
resource.getPath());
-                    roller.flush();
-                } else {
-                    log.debug("    No action: directory already exists");
-                }
-
-            } else {
-                String resourcePath = resource.getPath();
-
-                MediaFileDirectory mdir = null;
-                String justName = null;
-                String justPath = null;
-
-                if (resourcePath.indexOf("/") == -1) {
-                    mdir = fileMgr.getMediaFileRootDirectory(website);
-                    justPath = "";
-                    justName = resourcePath;
-
-                } else {
-                    justPath = resourcePath.substring(0, 
resourcePath.lastIndexOf("/"));
-                    if (!justPath.startsWith("/")) justPath = "/" + justPath;
-                    justName = 
resourcePath.substring(resourcePath.lastIndexOf("/") + 1);
-                    mdir = fileMgr.getMediaFileDirectoryByPath(website, 
justPath);
-                    if (mdir == null) {
-                        log.debug("    Creating directory: " + justPath);
-                        mdir = fileMgr.createMediaFileDirectoryByPath(website, 
justPath);
-                        roller.flush();
-                    }
-                }
-
-                MediaFile oldmf = fileMgr.getMediaFileByOriginalPath(website, 
justPath + "/" + justName);
-                if (oldmf != null) {
-                    fileMgr.removeMediaFile(website, oldmf);
-                }
-
-                // save file without file-type, quota checks, etc.
-                InputStream is = resource.getInputStream();
-                MediaFile mf = new MediaFile();
-                mf.setDirectory(mdir);
-                mf.setWeblog(website);
-                mf.setName(justName);
-                mf.setOriginalPath(justPath + "/" + justName);
-                mf.setContentType(map.getContentType(justName));
-                mf.setInputStream(is);
-                mf.setLength(resource.getLength());
-
-                log.debug("    Saving file: " + justName);
-                log.debug("    Saviving in directory = " + mf.getDirectory());
-                RollerMessages errors = new RollerMessages();
-                fileMgr.createMediaFile(website, mf, errors);
-                try {
-                    resource.getInputStream().close();
-                } catch (IOException ex) {
-                    errors.addError("error.closingStream");
-                    log.debug("ERROR closing inputstream");
-                }
-                if (errors.getErrorCount() > 0) {
-                    throw new WebloggerException(errors.toString());
-                }
-                roller.flush();
-            }
-        }
-    }
-
-    /**
-     * This is a convenience method which loads all the theme data from
-     * themes stored on the filesystem in the roller webapp /themes/ directory.
-     */
-    private Map loadAllThemesFromDisk() {
-
-        Map themeMap = new HashMap();
-
-        // first, get a list of the themes available
-        File themesdir = new File(this.themeDir);
-        FilenameFilter filter = new FilenameFilter() {
-
-            public boolean accept(File dir, String name) {
-                File file =
-                        new File(dir.getAbsolutePath() + File.separator + 
name);
-                return file.isDirectory() && !file.getName().startsWith(".");
-            }
-        };
-        String[] themenames = themesdir.list(filter);
-
-        if (themenames == null) {
-            log.warn("No themes loaded!  Perhaps you specified the wrong "
-                    + "location for your themes directory?");
-        }
-
-        // now go through each theme and load it into a Theme object
-        for (int i = 0; i < themenames.length; i++) {
-            try {
-                Theme theme = new SharedThemeFromDir(this.themeDir + 
File.separator + themenames[i]);
-                if (theme != null) {
-                    themeMap.put(theme.getId(), theme);
-                }
-            } catch (Throwable unexpected) {
-                // shouldn't happen, so let's learn why it did
-                log.error("Problem reading theme " + themenames[i], 
unexpected);
-            }
-        }
-
-        return themeMap;
-    }
-
-    /**
-     * @see ThemeManager#reLoadThemeFromDisk(String)
-     */
-    public boolean reLoadThemeFromDisk(String reloadTheme) {
-
-        boolean reloaded = false;
-
-        try {
-
-            Theme theme = new SharedThemeFromDir(this.themeDir + File.separator
-                    + reloadTheme);
-
-            if (theme != null) {
-
-                Theme loadedTheme = (Theme) themes.get(theme.getId());
-
-                if (loadedTheme != null
-                        && 
theme.getLastModified().after(loadedTheme.getLastModified())) {
-                    themes.remove(theme.getId());
-                    themes.put(theme.getId(), theme);
-                    reloaded = true;
-                }
-
-            }
-
-        } catch (Throwable unexpected) {
-            // shouldn't happen, so let's learn why it did
-            log.error("Problem reloading theme " + reloadTheme, unexpected);
-        }
-
-        return reloaded;
-
-    }
-
-    private void addAvailableTypes(){
-        this.getTypesList().add("standard");
-        this.getTypesList().add("mobile");
-    }
-
-    public static ArrayList<String> getTypesList() {
-          return typeList;
-      }
-
+       static FileTypeMap map = null;
+       static {
+               // TODO: figure out why PNG is missing from Java MIME types
+               map = FileTypeMap.getDefaultFileTypeMap();
+               if (map instanceof MimetypesFileTypeMap) {
+                       try {
+                               ((MimetypesFileTypeMap) 
map).addMimeTypes("image/png png PNG");
+                       } catch (Exception ignored) {
+                       }
+               }
+       }
+
+       private static Log log = LogFactory.getLog(ThemeManagerImpl.class);
+       private final Weblogger roller;
+       // directory where themes are kept
+       private String themeDir = null;
+       // the Map contains ... (theme id, Theme)
+       private Map themes = null;
+
+       // list of available types for templates
+       private static ArrayList<String> typeList = new ArrayList<String>();
+
+       @com.google.inject.Inject
+       protected ThemeManagerImpl(Weblogger roller) {
+
+               this.roller = roller;
+
+               // set the available types that can be used for templates
+               this.addAvailableTypes();
+
+               // get theme directory from config and verify it
+               this.themeDir = WebloggerConfig.getProperty("themes.dir");
+               if (themeDir == null || themeDir.trim().length() < 1) {
+                       throw new RuntimeException(
+                                       "couldn't get themes directory from 
config");
+               } else {
+                       // chop off trailing slash if it exists
+                       if (themeDir.endsWith("/")) {
+                               themeDir = themeDir.substring(0, 
themeDir.length() - 1);
+                       }
+
+                       // make sure it exists and is readable
+                       File themeDirFile = new File(themeDir);
+                       if (!themeDirFile.exists() || 
!themeDirFile.isDirectory()
+                                       || !themeDirFile.canRead()) {
+                               throw new RuntimeException("couldn't access 
theme dir ["
+                                               + themeDir + "]");
+                       }
+               }
+       }
+
+       public void initialize() throws InitializationException {
+
+               log.debug("Initializing Theme Manager");
+
+               if (themeDir != null) {
+                       // rather than be lazy we are going to load all themes 
from
+                       // the disk preemptive and cache them
+                       this.themes = loadAllThemesFromDisk();
+
+                       log.info("Loaded " + this.themes.size() + " themes from 
disk.");
+               }
+       }
+
+       /**
+        * @see 
org.apache.roller.weblogger.model.ThemeManager#getTheme(java.lang.String)
+        */
+       public SharedTheme getTheme(String id) throws ThemeNotFoundException,
+                       WebloggerException {
+
+               // try to lookup theme from library
+               SharedTheme theme = (SharedTheme) this.themes.get(id);
+
+               // no theme? throw exception.
+               if (theme == null) {
+                       throw new ThemeNotFoundException("Couldn't find theme 
[" + id + "]");
+               }
+
+               return theme;
+       }
+
+       /**
+        * @see org.apache.roller.weblogger.model.ThemeManager#getTheme(weblog)
+        */
+       public WeblogTheme getTheme(Weblog weblog) throws WebloggerException {
+
+               if (weblog == null) {
+                       return null;
+               }
+
+               WeblogTheme weblogTheme = null;
+
+               // if theme is custom or null then return a WeblogCustomTheme
+               if (weblog.getEditorTheme() == null
+                               || 
WeblogTheme.CUSTOM.equals(weblog.getEditorTheme())) {
+                       weblogTheme = new WeblogCustomTheme(weblog);
+
+                       // otherwise we are returning a WeblogSharedTheme
+               } else {
+                       ThemeManager themeMgr = roller.getThemeManager();
+                       SharedTheme staticTheme = (SharedTheme) 
this.themes.get(weblog
+                                       .getEditorTheme());
+                       if (staticTheme != null) {
+                               weblogTheme = new WeblogSharedTheme(weblog, 
staticTheme);
+                       } else {
+                               log.warn("Unable to lookup theme " + 
weblog.getEditorTheme());
+                       }
+               }
+
+               // TODO: if somehow the theme is still null should we provide 
some
+               // kind of fallback option like a default theme?
+
+               return weblogTheme;
+       }
+
+       /**
+        * @see 
org.apache.roller.weblogger.model.ThemeManager#getEnabledThemesList()
+        * 
+        *      TODO: reimplement enabled vs. disabled logic once we support it
+        */
+       public List getEnabledThemesList() {
+
+               List all_themes = new ArrayList(this.themes.values());
+
+               // sort 'em ... default ordering for themes is by name
+               Collections.sort(all_themes);
+
+               return all_themes;
+       }
+
+       /**
+        * @see 
org.apache.roller.weblogger.model.ThemeManager#importTheme(website,
+        *      theme)
+        */
+       public void importTheme(Weblog website, SharedTheme theme)
+                       throws WebloggerException {
+
+               log.debug("Importing theme [" + theme.getName() + "] to weblog 
["
+                               + website.getName() + "]");
+
+               WeblogManager wmgr = roller.getWeblogManager();
+               MediaFileManager fileMgr = roller.getMediaFileManager();
+
+               MediaFileDirectory root = 
fileMgr.getMediaFileRootDirectory(website);
+               log.warn("Weblog " + website.getHandle()
+                               + " does not have a root MediaFile directory");
+
+               Set importedActionTemplates = new HashSet();
+               ThemeTemplate themeTemplate = null;
+               ThemeTemplate stylesheetTemplate = theme.getStylesheet();
+               Iterator iter = theme.getTemplates().iterator();
+               while (iter.hasNext()) {
+                       themeTemplate = (ThemeTemplate) iter.next();
+
+                       WeblogTemplate template = null;
+
+                       // if template is an action, lookup by action
+                       if (themeTemplate.getAction() != null
+                                       && !themeTemplate.getAction().equals(
+                                                       
WeblogTemplate.ACTION_CUSTOM)) {
+                               
importedActionTemplates.add(themeTemplate.getAction());
+                               template = wmgr.getPageByAction(website,
+                                               themeTemplate.getAction());
+
+                               // otherwise, lookup by name
+                       } else {
+                               template = wmgr.getPageByName(website, 
themeTemplate.getName());
+                       }
+
+                       // Weblog does not have this template, so create it.
+                       boolean newTmpl = false;
+                       if (template == null) {
+                               template = new WeblogTemplate();
+                               template.setWebsite(website);
+                               newTmpl = true;
+                       }
+
+                       // TODO: fix conflict situation
+                       // it's possible that someone has defined a theme 
template which
+                       // matches 2 existing templates, 1 by action, the other 
by name
+
+                       // update template attributes
+                       // NOTE: we don't want to copy the template data for an 
existing
+                       // stylesheet
+                       if (newTmpl || 
!themeTemplate.equals(stylesheetTemplate)) {
+                               template.setAction(themeTemplate.getAction());
+                               template.setName(themeTemplate.getName());
+                               
template.setDescription(themeTemplate.getDescription());
+                               template.setLink(themeTemplate.getLink());
+                               
template.setContents(themeTemplate.getContents());
+                               template.setHidden(themeTemplate.isHidden());
+                               template.setNavbar(themeTemplate.isNavbar());
+                               template.setTemplateLanguage(themeTemplate
+                                               .getTemplateLanguage());
+                               // NOTE: decorators are deprecated starting in 
4.0
+                               template.setDecoratorName(null);
+                               template.setLastModified(new Date());
+
+                               // save it
+                               wmgr.savePage(template);
+                       }
+
+                       // create weblog template code objects and save them
+                       for (String type : ThemeManagerImpl.getTypesList()) {
+
+                               // See if we already have some code for this 
template already (eg previous theme)
+                               WeblogThemeTemplateCode weblogTemplateCode = 
(WeblogThemeTemplateCode) template
+                                               .getTemplateCode(type);
+
+                               // Get the template for the new theme
+                               TemplateCode templateCode = 
themeTemplate.getTemplateCode(type);
+                               if (templateCode != null) {
+                                       
+                                       // Check for existing template
+                                       if (weblogTemplateCode == null) {
+                                               // Does not exist so create a 
new one
+                                               weblogTemplateCode = new 
WeblogThemeTemplateCode(
+                                                               
template.getId(), type);
+                                       }
+                                       weblogTemplateCode.setType(type);
+                                       
weblogTemplateCode.setTemplate(templateCode.getTemplate());
+                                       
weblogTemplateCode.setTemplateLanguage(templateCode
+                                                       .getTemplateLanguage());
+                                       
weblogTemplateCode.setContentType(templateCode
+                                                       .getContentType());
+                                       
WebloggerFactory.getWeblogger().getWeblogManager()
+                                                       
.saveTemplateCode(weblogTemplateCode);
+                               }
+
+                       }
+               }
+
+               // now, see if the weblog has left over action templates that
+               // need to be deleted because they aren't in their new theme
+               for (int i = 0; i < WeblogTemplate.ACTIONS.length; i++) {
+                       String action = WeblogTemplate.ACTIONS[i];
+
+                       // if we didn't import this action then see if it 
should be deleted
+                       if (!importedActionTemplates.contains(action)) {
+                               WeblogTemplate toDelete = 
wmgr.getPageByAction(website, action);
+                               if (toDelete != null) {
+                                       log.debug("Removing stale action 
template "
+                                                       + toDelete.getId());
+                                       wmgr.removePage(toDelete);
+                               }
+                       }
+               }
+
+               // always update this weblog's theme and customStylesheet, then 
save
+               website.setEditorTheme(WeblogTheme.CUSTOM);
+               if (theme.getStylesheet() != null) {
+                       
website.setCustomStylesheetPath(theme.getStylesheet().getLink());
+               }
+               wmgr.saveWeblog(website);
+
+               // now lets import all the theme resources
+               List resources = theme.getResources();
+               Iterator iterat = resources.iterator();
+               ThemeResource resource = null;
+               while (iterat.hasNext()) {
+                       resource = (ThemeResource) iterat.next();
+
+                       log.debug("Importing resource " + resource.getPath());
+
+                       if (resource.isDirectory()) {
+                               MediaFileDirectory mdir = 
fileMgr.getMediaFileDirectoryByPath(
+                                               website, resource.getPath());
+                               if (mdir == null) {
+                                       log.debug("    Creating directory: " + 
resource.getPath());
+                                       mdir = fileMgr.createMediaFileDirectory(
+                                                       
fileMgr.getMediaFileRootDirectory(website),
+                                                       resource.getPath());
+                                       roller.flush();
+                               } else {
+                                       log.debug("    No action: directory 
already exists");
+                               }
+
+                       } else {
+                               String resourcePath = resource.getPath();
+
+                               MediaFileDirectory mdir = null;
+                               String justName = null;
+                               String justPath = null;
+
+                               if (resourcePath.indexOf("/") == -1) {
+                                       mdir = 
fileMgr.getMediaFileRootDirectory(website);
+                                       justPath = "";
+                                       justName = resourcePath;
+
+                               } else {
+                                       justPath = resourcePath.substring(0,
+                                                       
resourcePath.lastIndexOf("/"));
+                                       if (!justPath.startsWith("/"))
+                                               justPath = "/" + justPath;
+                                       justName = 
resourcePath.substring(resourcePath
+                                                       .lastIndexOf("/") + 1);
+                                       mdir = 
fileMgr.getMediaFileDirectoryByPath(website,
+                                                       justPath);
+                                       if (mdir == null) {
+                                               log.debug("    Creating 
directory: " + justPath);
+                                               mdir = 
fileMgr.createMediaFileDirectoryByPath(website,
+                                                               justPath);
+                                               roller.flush();
+                                       }
+                               }
+
+                               MediaFile oldmf = 
fileMgr.getMediaFileByOriginalPath(website,
+                                               justPath + "/" + justName);
+                               if (oldmf != null) {
+                                       fileMgr.removeMediaFile(website, oldmf);
+                               }
+
+                               // save file without file-type, quota checks, 
etc.
+                               InputStream is = resource.getInputStream();
+                               MediaFile mf = new MediaFile();
+                               mf.setDirectory(mdir);
+                               mf.setWeblog(website);
+                               mf.setName(justName);
+                               mf.setOriginalPath(justPath + "/" + justName);
+                               mf.setContentType(map.getContentType(justName));
+                               mf.setInputStream(is);
+                               mf.setLength(resource.getLength());
+
+                               log.debug("    Saving file: " + justName);
+                               log.debug("    Saviving in directory = " + 
mf.getDirectory());
+                               RollerMessages errors = new RollerMessages();
+                               fileMgr.createMediaFile(website, mf, errors);
+                               try {
+                                       resource.getInputStream().close();
+                               } catch (IOException ex) {
+                                       errors.addError("error.closingStream");
+                                       log.debug("ERROR closing inputstream");
+                               }
+                               if (errors.getErrorCount() > 0) {
+                                       throw new 
WebloggerException(errors.toString());
+                               }
+                               roller.flush();
+                       }
+               }
+       }
+
+       /**
+        * This is a convenience method which loads all the theme data from 
themes
+        * stored on the filesystem in the roller webapp /themes/ directory.
+        */
+       private Map loadAllThemesFromDisk() {
+
+               Map themeMap = new HashMap();
+
+               // first, get a list of the themes available
+               File themesdir = new File(this.themeDir);
+               FilenameFilter filter = new FilenameFilter() {
+
+                       public boolean accept(File dir, String name) {
+                               File file = new File(dir.getAbsolutePath() + 
File.separator
+                                               + name);
+                               return file.isDirectory() && 
!file.getName().startsWith(".");
+                       }
+               };
+               String[] themenames = themesdir.list(filter);
+
+               if (themenames == null) {
+                       log.warn("No themes loaded!  Perhaps you specified the 
wrong "
+                                       + "location for your themes 
directory?");
+               }
+
+               // now go through each theme and load it into a Theme object
+               for (int i = 0; i < themenames.length; i++) {
+                       try {
+                               Theme theme = new 
SharedThemeFromDir(this.themeDir
+                                               + File.separator + 
themenames[i]);
+                               if (theme != null) {
+                                       themeMap.put(theme.getId(), theme);
+                               }
+                       } catch (Throwable unexpected) {
+                               // shouldn't happen, so let's learn why it did
+                               log.error("Problem reading theme " + 
themenames[i], unexpected);
+                       }
+               }
+
+               return themeMap;
+       }
+
+       /**
+        * @see ThemeManager#reLoadThemeFromDisk(String)
+        */
+       public boolean reLoadThemeFromDisk(String reloadTheme) {
+
+               boolean reloaded = false;
+
+               try {
+
+                       Theme theme = new SharedThemeFromDir(this.themeDir + 
File.separator
+                                       + reloadTheme);
+
+                       if (theme != null) {
+
+                               Theme loadedTheme = (Theme) 
themes.get(theme.getId());
+
+                               if (loadedTheme != null
+                                               && 
theme.getLastModified().after(
+                                                               
loadedTheme.getLastModified())) {
+                                       themes.remove(theme.getId());
+                                       themes.put(theme.getId(), theme);
+                                       reloaded = true;
+                               }
+
+                       }
+
+               } catch (Throwable unexpected) {
+                       // shouldn't happen, so let's learn why it did
+                       log.error("Problem reloading theme " + reloadTheme, 
unexpected);
+               }
+
+               return reloaded;
+
+       }
+
+       private void addAvailableTypes() {
+               this.getTypesList().add("standard");
+               this.getTypesList().add("mobile");
+       }
+
+       public static ArrayList<String> getTypesList() {
+               return typeList;
+       }
 
 }

Modified: 
roller/trunk/weblogger-web/src/main/java/org/apache/roller/weblogger/ui/struts2/editor/TemplateRemove.java
URL: 
http://svn.apache.org/viewvc/roller/trunk/weblogger-web/src/main/java/org/apache/roller/weblogger/ui/struts2/editor/TemplateRemove.java?rev=1239719&r1=1239718&r2=1239719&view=diff
==============================================================================
--- 
roller/trunk/weblogger-web/src/main/java/org/apache/roller/weblogger/ui/struts2/editor/TemplateRemove.java
 (original)
+++ 
roller/trunk/weblogger-web/src/main/java/org/apache/roller/weblogger/ui/struts2/editor/TemplateRemove.java
 Thu Feb  2 16:49:26 2012
@@ -24,104 +24,138 @@ import java.util.List;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.roller.weblogger.WebloggerException;
+import org.apache.roller.weblogger.business.WeblogManager;
 import org.apache.roller.weblogger.business.WebloggerFactory;
+import org.apache.roller.weblogger.pojos.ThemeTemplate;
+import org.apache.roller.weblogger.pojos.Weblog;
 import org.apache.roller.weblogger.pojos.WeblogPermission;
 import org.apache.roller.weblogger.pojos.WeblogTemplate;
 import org.apache.roller.weblogger.pojos.WeblogTheme;
 import org.apache.roller.weblogger.ui.struts2.util.UIAction;
 import org.apache.roller.weblogger.util.cache.CacheManager;
 
-
 /**
  * Remove a template.
  */
 public class TemplateRemove extends UIAction {
-    
-    private static Log log = LogFactory.getLog(TemplateRemove.class);
-    
-    // id of template to remove
-    private String removeId = null;
-    
-    // template object that we will remove
-    private WeblogTemplate template = null;
-    
-    
-    public TemplateRemove() {
-        this.actionName = "templateRemove";
-        this.desiredMenu = "editor";
-        this.pageTitle = "editPages.title.removeOK";
-    }
-    
-    
-    // must be a weblog admin to use this action
-    public List<String> requiredWeblogPermissionActions() {
-        return Collections.singletonList(WeblogPermission.ADMIN);
-    }
-    
-    
-    public void myPrepare() {
-        if(getRemoveId() != null) try {
-            
setTemplate(WebloggerFactory.getWeblogger().getWeblogManager().getPage(getRemoveId()));
-        } catch (WebloggerException ex) {
-            log.error("Error looking up template by id - "+getRemoveId(), ex);
-            // TODO: i18n
-            addError("Could not find template to remove - "+getRemoveId());
-        }
-    }
-    
-    
-    /**
-     * Display the remove template confirmation.
-     */
-    public String execute() {
-        return "confirm";
-    }
-    
-    
-    /**
-     * Remove a new template.
-     */
-    public String remove() {
-        
-        if(getTemplate() != null) try {
-            if(!getTemplate().isRequired() || 
!WeblogTheme.CUSTOM.equals(getActionWeblog().getEditorTheme())) {
-
-                // notify cache
-                CacheManager.invalidate(getTemplate());
-                
-                
WebloggerFactory.getWeblogger().getWeblogManager().removePage(getTemplate());
-                WebloggerFactory.getWeblogger().flush();
-                
-                return SUCCESS;
-            } else {
-                // TODO: i18n
-                addError("Cannot remove required template");
-            }
-            
-        } catch(Exception ex) {
-            log.error("Error removing page - "+getRemoveId(), ex);
-            // TODO: i18n
-            addError("Error removing page");
-        }
-        
-        return "confirm";
-    }
-
-    
-    public String getRemoveId() {
-        return removeId;
-    }
-
-    public void setRemoveId(String removeId) {
-        this.removeId = removeId;
-    }
-
-    public WeblogTemplate getTemplate() {
-        return template;
-    }
-
-    public void setTemplate(WeblogTemplate template) {
-        this.template = template;
-    }
-    
+
+       private static Log log = LogFactory.getLog(TemplateRemove.class);
+
+       // id of template to remove
+       private String removeId = null;
+
+       // template object that we will remove
+       private WeblogTemplate template = null;
+
+       public TemplateRemove() {
+               this.actionName = "templateRemove";
+               this.desiredMenu = "editor";
+               this.pageTitle = "editPages.title.removeOK";
+       }
+
+       // must be a weblog admin to use this action
+       public List<String> requiredWeblogPermissionActions() {
+               return Collections.singletonList(WeblogPermission.ADMIN);
+       }
+
+       public void myPrepare() {
+               if (getRemoveId() != null)
+                       try {
+                               
setTemplate(WebloggerFactory.getWeblogger().getWeblogManager()
+                                               .getPage(getRemoveId()));
+                       } catch (WebloggerException ex) {
+                               log.error("Error looking up template by id - " 
+ getRemoveId(),
+                                               ex);
+                               // TODO: i18n
+                               addError("Could not find template to remove - " 
+ getRemoveId());
+                       }
+       }
+
+       /**
+        * Display the remove template confirmation.
+        */
+       public String execute() {
+               return "confirm";
+       }
+
+       /**
+        * Remove a new template.
+        */
+       public String remove() {
+
+               if (getTemplate() != null)
+                       try {
+                               if (!getTemplate().isRequired()
+                                               || 
!WeblogTheme.CUSTOM.equals(getActionWeblog()
+                                                               
.getEditorTheme())) {
+
+                                       WeblogManager mgr = 
WebloggerFactory.getWeblogger()
+                                                       .getWeblogManager();
+
+                                       // if weblog template remove custom 
style sheet also
+                                       if (getTemplate().getName().equals(
+                                                       
WeblogTemplate.DEFAULT_PAGE)) {
+
+                                               Weblog weblog = 
getActionWeblog();
+
+                                               ThemeTemplate stylesheet = 
getActionWeblog().getTheme()
+                                                               
.getStylesheet();
+
+                                               // Delete style sheet if the 
same name
+                                               if (stylesheet != null
+                                                               && 
getActionWeblog().getTheme().getStylesheet() != null
+                                                               && 
stylesheet.getLink().equals(
+                                                                               
getActionWeblog().getTheme()
+                                                                               
                .getStylesheet().getLink())) {
+                                                       // Same so OK to delete
+                                                       WeblogTemplate css = 
mgr.getPageByLink(
+                                                                       
getActionWeblog(), stylesheet.getLink());
+
+                                                       if (css != null) {
+                                                               
mgr.removePage(css);
+                                                       }
+                                               }
+
+                                               // Clear for next custom theme
+                                               
weblog.setCustomStylesheetPath(null);
+                                               weblog.setDefaultPageId(null);
+
+                                       }
+
+                                       // notify cache
+                                       CacheManager.invalidate(getTemplate());
+                                       mgr.removePage(getTemplate());
+                                       WebloggerFactory.getWeblogger().flush();
+
+                                       return SUCCESS;
+                               } else {
+                                       // TODO: i18n
+                                       addError("Cannot remove required 
template");
+                               }
+
+                       } catch (Exception ex) {
+                               log.error("Error removing page - " + 
getRemoveId(), ex);
+                               // TODO: i18n
+                               addError("Error removing page");
+                       }
+
+               return "confirm";
+       }
+
+       public String getRemoveId() {
+               return removeId;
+       }
+
+       public void setRemoveId(String removeId) {
+               this.removeId = removeId;
+       }
+
+       public WeblogTemplate getTemplate() {
+               return template;
+       }
+
+       public void setTemplate(WeblogTemplate template) {
+               this.template = template;
+       }
+
 }

Modified: 
roller/trunk/weblogger-web/src/main/java/org/apache/roller/weblogger/ui/struts2/editor/Templates.java
URL: 
http://svn.apache.org/viewvc/roller/trunk/weblogger-web/src/main/java/org/apache/roller/weblogger/ui/struts2/editor/Templates.java?rev=1239719&r1=1239718&r2=1239719&view=diff
==============================================================================
--- 
roller/trunk/weblogger-web/src/main/java/org/apache/roller/weblogger/ui/struts2/editor/Templates.java
 (original)
+++ 
roller/trunk/weblogger-web/src/main/java/org/apache/roller/weblogger/ui/struts2/editor/Templates.java
 Thu Feb  2 16:49:26 2012
@@ -34,223 +34,254 @@ import java.util.Collections;
 import java.util.Date;
 import java.util.List;
 
-
 /**
  * Templates listing page.
  */
 public class Templates extends UIAction {
-    
-    private static Log log = LogFactory.getLog(Templates.class);
-    
-    // list of templates to display
-    private List<WeblogTemplate> templates = Collections.EMPTY_LIST;
-    
-    // list of template action types user is allowed to create
-    private List availableActions = Collections.EMPTY_LIST;
-    
-    // name and action of new template if we are adding a template
-    private String newTmplName = null;
-    private String newTmplAction = null;
-    private String type = null;
-    
-    public Templates() {
-        this.actionName = "templates";
-        this.desiredMenu = "editor";
-        this.pageTitle = "pagesForm.title";
-    }
-    
-    
-    public List<String> requiredWeblogPermissionActions() {
-        return Collections.singletonList(WeblogPermission.ADMIN);
-    }
-    
-    
-    public String execute() {
-        
-        // query for templates list
-        try {
-            
-             // get current list of templates, minus custom stylesheet
-            List<WeblogTemplate> raw = 
WebloggerFactory.getWeblogger().getWeblogManager().getPages(getActionWeblog());
-            List<WeblogTemplate> pages = new ArrayList<WeblogTemplate>();
-            pages.addAll(raw);
-            if(getActionWeblog().getTheme().getStylesheet() != null) {
-                
pages.remove(WebloggerFactory.getWeblogger().getWeblogManager().getPageByLink(getActionWeblog(),
-                        
getActionWeblog().getTheme().getStylesheet().getLink()));
-            }
-            setTemplates(pages);
-            
-            // build list of action types that may be added
-            List availableActions = new ArrayList();
-            availableActions.add(WeblogTemplate.ACTION_CUSTOM);
-            
-            if(WeblogTheme.CUSTOM.equals(getActionWeblog().getEditorTheme())) {
-                // if the weblog is using a custom theme then determine which
-                // action templates are still available to be created
-                availableActions.add(WeblogTemplate.ACTION_PERMALINK);
-                availableActions.add(WeblogTemplate.ACTION_SEARCH);
-                availableActions.add(WeblogTemplate.ACTION_WEBLOG);
-                availableActions.add(WeblogTemplate.ACTION_TAGSINDEX);
-                
-                for(WeblogTemplate tmpPage : getTemplates()) {
-                    
if(!WeblogTemplate.ACTION_CUSTOM.equals(tmpPage.getAction())) {
-                        availableActions.remove(tmpPage.getAction());
-                    }
-                }
-            } else if (pages.isEmpty()) {
-                availableActions.add(WeblogTemplate.ACTION_WEBLOG);
-            }
-            setAvailableActions(availableActions);
-
-        } catch (WebloggerException ex) {
-            log.error("Error getting templates for weblog - 
"+getActionWeblog().getHandle(), ex);
-            // TODO: i18n
-            addError("Error getting template list");
-        }
-        
-        return LIST;
-    }
-    
-    
-    /**
-     * Save a new template.
-     */
-    public String add() {
-        
-        // validation
-        myValidate();
-        
-        if(!hasActionErrors()) try {
-            
-            WeblogTemplate newTemplate = new WeblogTemplate();
-            newTemplate.setWebsite(getActionWeblog());
-            newTemplate.setAction(getNewTmplAction());
-            newTemplate.setName(getNewTmplName());
-            newTemplate.setDescription(newTemplate.getName());
-            newTemplate.setContents(getText("pageForm.newTemplateContent"));
-            newTemplate.setHidden(false);
-            newTemplate.setNavbar(false);
-            newTemplate.setLastModified( new Date() );
-
-            if(WeblogTemplate.ACTION_CUSTOM.equals(getNewTmplAction())){
-                newTemplate.setLink(getNewTmplName());
-            }
-            
-            // all templates start out as velocity templates
-            newTemplate.setTemplateLanguage("velocity");
-            
-            // for now, all templates just use _decorator
-            if(!"_decorator".equals(newTemplate.getName())) {
-                newTemplate.setDecoratorName("_decorator");
-            }
-            
-            // save the new Template
-            WebloggerFactory.getWeblogger().getWeblogManager().savePage( 
newTemplate );
-
-            //Create weblog template codes for available types.
-            WeblogThemeTemplateCode standardTemplCode = new 
WeblogThemeTemplateCode(newTemplate.getId(),"standard");
-            WeblogThemeTemplateCode mobileTemplCode = new 
WeblogThemeTemplateCode(newTemplate.getId(),"mobile");
-
-            standardTemplCode.setTemplate(newTemplate.getContents());
-            standardTemplCode.setTemplateLanguage("velocity");
-
-            mobileTemplCode.setTemplate(newTemplate.getContents());
-            mobileTemplCode.setTemplateLanguage("velocity");
-
-            
WebloggerFactory.getWeblogger().getWeblogManager().saveTemplateCode(standardTemplCode);
-            
WebloggerFactory.getWeblogger().getWeblogManager().saveTemplateCode(mobileTemplCode);
-
-            // if this person happened to create a Weblog template from
-            // scratch then make sure and set the defaultPageId
-            if(WeblogTemplate.DEFAULT_PAGE.equals(newTemplate.getName())) {
-                getActionWeblog().setDefaultPageId(newTemplate.getId());
-                
WebloggerFactory.getWeblogger().getWeblogManager().saveWeblog(getActionWeblog());
-            }
-            
-            // flush results to db
-            WebloggerFactory.getWeblogger().flush();
-            
-            // reset form fields
-            setNewTmplName(null);
-            setNewTmplAction(null);
-            
-        } catch (WebloggerException ex) {
-            log.error("Error adding new template for weblog - 
"+getActionWeblog().getHandle(), ex);
-            // TODO: i18n
-            addError("Error adding new template");
-        }
-        
-        return execute();
-    }
-
-    
-    // validation when adding a new template
-    private void myValidate() {
-        
-        // make sure name is non-null and within proper size
-        if(StringUtils.isEmpty(getNewTmplName())) {
-            addError("Template.error.nameNull");
-        } else if(getNewTmplName().length() > 255) {
-            addError("Template.error.nameSize");
-        }
-        
-        // make sure action is a valid
-        if(StringUtils.isEmpty(getNewTmplAction())) {
-            addError("Template.error.actionNull");
-        }
-        
-        // check if template by that name already exists
-        try {
-            WeblogTemplate existingPage = 
WebloggerFactory.getWeblogger().getWeblogManager().getPageByName(getActionWeblog(),
 getNewTmplName());
-            if(existingPage != null) {
-                addError("pagesForm.error.alreadyExists", getNewTmplName());
-            }
-        } catch (WebloggerException ex) {
-            log.error("Error checking for existing template", ex);
-        }
-
-
-    }
-    
-    /**
-     * Checks if is custom theme.
-     *
-     * @return true, if is custom theme
-     */
-    public boolean isCustomTheme() {
-        return (WeblogTheme.CUSTOM.equals(getActionWeblog().getEditorTheme()));
-    }
-    
-    public List<WeblogTemplate> getTemplates() {
-        return templates;
-    }
-
-    public void setTemplates(List<WeblogTemplate> templates) {
-        this.templates = templates;
-    }
-
-    public List getAvailableActions() {
-        return availableActions;
-    }
-
-    public void setAvailableActions(List availableActions) {
-        this.availableActions = availableActions;
-    }
-    
-    public String getNewTmplName() {
-        return newTmplName;
-    }
-
-    public void setNewTmplName(String newTmplName) {
-        this.newTmplName = newTmplName;
-    }
-
-    public String getNewTmplAction() {
-        return newTmplAction;
-    }
-
-    public void setNewTmplAction(String newTmplAction) {
-        this.newTmplAction = newTmplAction;
-    }
+
+       private static Log log = LogFactory.getLog(Templates.class);
+
+       // list of templates to display
+       private List<WeblogTemplate> templates = Collections.EMPTY_LIST;
+
+       // list of template action types user is allowed to create
+       private List availableActions = Collections.EMPTY_LIST;
+
+       // name and action of new template if we are adding a template
+       private String newTmplName = null;
+       private String newTmplAction = null;
+       private String type = null;
+
+       public Templates() {
+               this.actionName = "templates";
+               this.desiredMenu = "editor";
+               this.pageTitle = "pagesForm.title";
+       }
+
+       public List<String> requiredWeblogPermissionActions() {
+               return Collections.singletonList(WeblogPermission.ADMIN);
+       }
+
+       public String execute() {
+
+               // query for templates list
+               try {
+
+                       // get current list of templates, minus custom 
stylesheet
+                       List<WeblogTemplate> raw = 
WebloggerFactory.getWeblogger()
+                                       
.getWeblogManager().getPages(getActionWeblog());
+                       List<WeblogTemplate> pages = new 
ArrayList<WeblogTemplate>();
+                       pages.addAll(raw);
+                       // Remove style sheet from list so not to show when 
theme is
+                       // selected in shared theme mode
+                       if (getActionWeblog().getTheme().getStylesheet() != 
null) {
+                               pages.remove(WebloggerFactory
+                                               .getWeblogger()
+                                               .getWeblogManager()
+                                               .getPageByLink(
+                                                               
getActionWeblog(),
+                                                               
getActionWeblog().getTheme().getStylesheet()
+                                                                               
.getLink()));
+                       }
+                       setTemplates(pages);
+
+                       // build list of action types that may be added
+                       List availableActions = new ArrayList();
+                       availableActions.add(WeblogTemplate.ACTION_CUSTOM);
+
+                       if 
(WeblogTheme.CUSTOM.equals(getActionWeblog().getEditorTheme())) {
+                               // if the weblog is using a custom theme then 
determine which
+                               // action templates are still available to be 
created
+                               
availableActions.add(WeblogTemplate.ACTION_PERMALINK);
+                               
availableActions.add(WeblogTemplate.ACTION_SEARCH);
+                               
availableActions.add(WeblogTemplate.ACTION_WEBLOG);
+                               
availableActions.add(WeblogTemplate.ACTION_TAGSINDEX);
+
+                               for (WeblogTemplate tmpPage : getTemplates()) {
+                                       if 
(!WeblogTemplate.ACTION_CUSTOM.equals(tmpPage
+                                                       .getAction())) {
+                                               
availableActions.remove(tmpPage.getAction());
+                                       }
+                               }
+                       } else {
+                               // Make sure we have an option for the default 
web page
+                               
availableActions.add(WeblogTemplate.ACTION_WEBLOG);
+                               setNewTmplAction(WeblogTemplate.ACTION_WEBLOG);
+                               for (WeblogTemplate tmpPage : getTemplates()) {
+                                       if (WeblogTemplate.ACTION_WEBLOG
+                                                       
.equals(tmpPage.getAction())) {
+                                               
availableActions.remove(WeblogTemplate.ACTION_WEBLOG);
+                                               setNewTmplAction(null);
+                                               break;
+                                       }
+                               }
+                       }
+                       setAvailableActions(availableActions);
+
+               } catch (WebloggerException ex) {
+                       log.error("Error getting templates for weblog - "
+                                       + getActionWeblog().getHandle(), ex);
+                       // TODO: i18n
+                       addError("Error getting template list");
+               }
+
+               return LIST;
+       }
+
+       /**
+        * Save a new template.
+        */
+       public String add() {
+
+               // validation
+               myValidate();
+
+               if (!hasActionErrors())
+                       try {
+
+                               WeblogTemplate newTemplate = new 
WeblogTemplate();
+                               newTemplate.setWebsite(getActionWeblog());
+                               newTemplate.setAction(getNewTmplAction());
+                               newTemplate.setName(getNewTmplName());
+                               
newTemplate.setDescription(newTemplate.getName());
+                               
newTemplate.setContents(getText("pageForm.newTemplateContent"));
+                               newTemplate.setHidden(false);
+                               newTemplate.setNavbar(false);
+                               newTemplate.setLastModified(new Date());
+
+                               if 
(WeblogTemplate.ACTION_CUSTOM.equals(getNewTmplAction())) {
+                                       newTemplate.setLink(getNewTmplName());
+                               }
+
+                               // Make sure we have always have a Weblog main 
page. Stops
+                               // deleting main page in custom theme mode also.
+                               if 
(WeblogTemplate.ACTION_WEBLOG.equals(getNewTmplAction())) {
+                                       
newTemplate.setName(WeblogTemplate.DEFAULT_PAGE);
+                               }
+
+                               // all templates start out as velocity templates
+                               newTemplate.setTemplateLanguage("velocity");
+
+                               // for now, all templates just use _decorator
+                               if 
(!"_decorator".equals(newTemplate.getName())) {
+                                       
newTemplate.setDecoratorName("_decorator");
+                               }
+
+                               // save the new Template
+                               
WebloggerFactory.getWeblogger().getWeblogManager()
+                                               .savePage(newTemplate);
+
+                               // Create weblog template codes for available 
types.
+                               WeblogThemeTemplateCode standardTemplCode = new 
WeblogThemeTemplateCode(
+                                               newTemplate.getId(), 
"standard");
+                               WeblogThemeTemplateCode mobileTemplCode = new 
WeblogThemeTemplateCode(
+                                               newTemplate.getId(), "mobile");
+
+                               
standardTemplCode.setTemplate(newTemplate.getContents());
+                               
standardTemplCode.setTemplateLanguage("velocity");
+
+                               
mobileTemplCode.setTemplate(newTemplate.getContents());
+                               mobileTemplCode.setTemplateLanguage("velocity");
+
+                               
WebloggerFactory.getWeblogger().getWeblogManager()
+                                               
.saveTemplateCode(standardTemplCode);
+                               
WebloggerFactory.getWeblogger().getWeblogManager()
+                                               
.saveTemplateCode(mobileTemplCode);
+
+                               // if this person happened to create a Weblog 
template from
+                               // scratch then make sure and set the 
defaultPageId. What does
+                               // this do????
+                               if 
(WeblogTemplate.DEFAULT_PAGE.equals(newTemplate.getName())) {
+                                       
getActionWeblog().setDefaultPageId(newTemplate.getId());
+                                       
WebloggerFactory.getWeblogger().getWeblogManager()
+                                                       
.saveWeblog(getActionWeblog());
+                               }
+
+                               // flush results to db
+                               WebloggerFactory.getWeblogger().flush();
+
+                               // reset form fields
+                               setNewTmplName(null);
+                               setNewTmplAction(null);
+
+                       } catch (WebloggerException ex) {
+                               log.error("Error adding new template for weblog 
- "
+                                               + 
getActionWeblog().getHandle(), ex);
+                               // TODO: i18n
+                               addError("Error adding new template");
+                       }
+
+               return execute();
+       }
+
+       // validation when adding a new template
+       private void myValidate() {
+
+               // make sure name is non-null and within proper size
+               if (StringUtils.isEmpty(getNewTmplName())) {
+                       addError("Template.error.nameNull");
+               } else if (getNewTmplName().length() > 255) {
+                       addError("Template.error.nameSize");
+               }
+
+               // make sure action is a valid
+               if (StringUtils.isEmpty(getNewTmplAction())) {
+                       addError("Template.error.actionNull");
+               }
+
+               // check if template by that name already exists
+               try {
+                       WeblogTemplate existingPage = 
WebloggerFactory.getWeblogger()
+                                       .getWeblogManager()
+                                       .getPageByName(getActionWeblog(), 
getNewTmplName());
+                       if (existingPage != null) {
+                               addError("pagesForm.error.alreadyExists", 
getNewTmplName());
+                       }
+               } catch (WebloggerException ex) {
+                       log.error("Error checking for existing template", ex);
+               }
+
+       }
+
+       /**
+        * Checks if is custom theme.
+        * 
+        * @return true, if is custom theme
+        */
+       public boolean isCustomTheme() {
+               return 
(WeblogTheme.CUSTOM.equals(getActionWeblog().getEditorTheme()));
+       }
+
+       public List<WeblogTemplate> getTemplates() {
+               return templates;
+       }
+
+       public void setTemplates(List<WeblogTemplate> templates) {
+               this.templates = templates;
+       }
+
+       public List getAvailableActions() {
+               return availableActions;
+       }
+
+       public void setAvailableActions(List availableActions) {
+               this.availableActions = availableActions;
+       }
+
+       public String getNewTmplName() {
+               return newTmplName;
+       }
+
+       public void setNewTmplName(String newTmplName) {
+               this.newTmplName = newTmplName;
+       }
+
+       public String getNewTmplAction() {
+               return newTmplAction;
+       }
+
+       public void setNewTmplAction(String newTmplAction) {
+               this.newTmplAction = newTmplAction;
+       }
 
 }

Modified: 
roller/trunk/weblogger-web/src/main/java/org/apache/roller/weblogger/ui/struts2/editor/TemplatesRemove.java
URL: 
http://svn.apache.org/viewvc/roller/trunk/weblogger-web/src/main/java/org/apache/roller/weblogger/ui/struts2/editor/TemplatesRemove.java?rev=1239719&r1=1239718&r2=1239719&view=diff
==============================================================================
--- 
roller/trunk/weblogger-web/src/main/java/org/apache/roller/weblogger/ui/struts2/editor/TemplatesRemove.java
 (original)
+++ 
roller/trunk/weblogger-web/src/main/java/org/apache/roller/weblogger/ui/struts2/editor/TemplatesRemove.java
 Thu Feb  2 16:49:26 2012
@@ -128,13 +128,13 @@ public class TemplatesRemove extends UIA
 
                        try {
 
-                               WeblogManager mgr = 
WebloggerFactory.getWeblogger()
-                                               .getWeblogManager();
-
                                String[] idsToDelete = 
Utilities.stringToStringArray(getIds(),
                                                ",");
                                if (idsToDelete != null && idsToDelete.length > 
0) {
 
+                                       WeblogManager mgr = 
WebloggerFactory.getWeblogger()
+                                                       .getWeblogManager();
+
                                        Weblog weblog = getActionWeblog();
                                        WeblogTemplate template = null;
 
@@ -147,14 +147,15 @@ public class TemplatesRemove extends UIA
                                                                                
        .equals(getActionWeblog()
                                                                                
                        .getEditorTheme())) {
 
-                                                               // if weblog 
template remove custom style sheet also
+                                                               // if weblog 
template remove custom style sheet
+                                                               // also
                                                                if 
(template.getName().equals(
                                                                                
WeblogTemplate.DEFAULT_PAGE)) {
 
                                                                        
ThemeTemplate stylesheet = getActionWeblog()
                                                                                
        .getTheme().getStylesheet();
 
-                                                                       // 
Delete style sheet if the same if found
+                                                                       // 
Delete style sheet if the same name
                                                                        if 
(stylesheet != null
                                                                                
        && getActionWeblog().getTheme()
                                                                                
                        .getStylesheet() != null
@@ -175,6 +176,7 @@ public class TemplatesRemove extends UIA
 
                                                                        // 
Clear for next custom theme
                                                                        
weblog.setCustomStylesheetPath(null);
+                                                                       
weblog.setDefaultPageId(null);
 
                                                                }
 
@@ -186,14 +188,12 @@ public class TemplatesRemove extends UIA
 
                                        // Save for changes
                                        mgr.saveWeblog(weblog);
-                                       
+
                                        WebloggerFactory.getWeblogger().flush();
 
                                        // notify caches
                                        
CacheManager.invalidate(getActionWeblog());
 
-                                       addMessage("referers.deletedTemplates");
-
                                }
 
                                return SUCCESS;

Modified: 
roller/trunk/weblogger-web/src/main/resources/ApplicationResources.properties
URL: 
http://svn.apache.org/viewvc/roller/trunk/weblogger-web/src/main/resources/ApplicationResources.properties?rev=1239719&r1=1239718&r2=1239719&view=diff
==============================================================================
--- 
roller/trunk/weblogger-web/src/main/resources/ApplicationResources.properties 
(original)
+++ 
roller/trunk/weblogger-web/src/main/resources/ApplicationResources.properties 
Thu Feb  2 16:49:26 2012
@@ -1341,7 +1341,7 @@ pageRemoves.subtitle=Confirm removal of 
 pageRemove.youSure=Are you sure you want to remove this page?
 pageRemoves.youSure=Are you sure you want to remove these pages?
 pageRemoves.youSureWarning=<b>WARNING</b>: this will may remove custom 
html/css design and is <b>NOT REVERSIBLE</b>. \
-You might want to backup your stylesheet if you are not sure.
+You might want to <b>backup your stylesheet</b> if you are not sure.
 pageRemove.pageId=Page ID
 pageRemove.pageName=Page Name
 

Modified: 
roller/trunk/weblogger-webapp/src/main/webapp/WEB-INF/jsps/editor/TemplateRemove.jsp
URL: 
http://svn.apache.org/viewvc/roller/trunk/weblogger-webapp/src/main/webapp/WEB-INF/jsps/editor/TemplateRemove.jsp?rev=1239719&r1=1239718&r2=1239719&view=diff
==============================================================================
--- 
roller/trunk/weblogger-webapp/src/main/webapp/WEB-INF/jsps/editor/TemplateRemove.jsp
 (original)
+++ 
roller/trunk/weblogger-webapp/src/main/webapp/WEB-INF/jsps/editor/TemplateRemove.jsp
 Thu Feb  2 16:49:26 2012
@@ -25,6 +25,11 @@
     <s:text name="pageRemove.youSure"> 
         <s:param value="template.name" />
     </s:text>
+    <br/>
+       <br/>
+       <span class="warning">
+               <s:text name="pageRemoves.youSureWarning" />
+       </span>
 </p>
 
 <p>


Reply via email to