You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@roller.apache.org by ag...@apache.org on 2006/10/03 20:11:38 UTC

svn commit: r452587 - in /incubator/roller/trunk/src/org/apache/roller: business/ThemeManagerImpl.java model/ThemeManager.java ui/authoring/struts/actions/ThemeEditorAction.java

Author: agilliland
Date: Tue Oct  3 11:11:37 2006
New Revision: 452587

URL: http://svn.apache.org/viewvc?view=rev&rev=452587
Log:
Second commit for Roller 3.1 Theme Encapsulation work ...

http://rollerweblogger.org/wiki/Wiki.jsp?page=Proposal_ThemeEncapsulation

1. Updated ThemeManager to import resource files as well as templates.

  * renamed saveThemePages() to importTheme()
  * added ability to import static resources.

NOTE: this work only partially implements the overall functionality of the proposal.  there will be further commits to complete the work.


Modified:
    incubator/roller/trunk/src/org/apache/roller/business/ThemeManagerImpl.java
    incubator/roller/trunk/src/org/apache/roller/model/ThemeManager.java
    incubator/roller/trunk/src/org/apache/roller/ui/authoring/struts/actions/ThemeEditorAction.java

Modified: incubator/roller/trunk/src/org/apache/roller/business/ThemeManagerImpl.java
URL: http://svn.apache.org/viewvc/incubator/roller/trunk/src/org/apache/roller/business/ThemeManagerImpl.java?view=diff&rev=452587&r1=452586&r2=452587
==============================================================================
--- incubator/roller/trunk/src/org/apache/roller/business/ThemeManagerImpl.java (original)
+++ incubator/roller/trunk/src/org/apache/roller/business/ThemeManagerImpl.java Tue Oct  3 11:11:37 2006
@@ -21,9 +21,7 @@
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
-import java.io.FileReader;
 import java.io.FilenameFilter;
-import java.io.IOException;
 import java.io.InputStreamReader;
 import java.util.ArrayList;
 import java.util.Collection;
@@ -38,6 +36,9 @@
 import org.apache.roller.RollerException;
 import org.apache.roller.ThemeNotFoundException;
 import org.apache.roller.config.RollerConfig;
+import org.apache.roller.model.FileIOException;
+import org.apache.roller.model.FileManager;
+import org.apache.roller.model.FilePathException;
 import org.apache.roller.model.RollerFactory;
 import org.apache.roller.model.ThemeManager;
 import org.apache.roller.model.UserManager;
@@ -134,6 +135,130 @@
     
     
     /**
+     * @see org.apache.roller.model.ThemeManager#importTheme(website, theme)
+     */
+    public void importTheme(WebsiteData website, Theme theme)
+        throws RollerException {
+        
+        log.debug("Setting custom templates for website: "+website.getName());
+        
+        try {
+            UserManager userMgr = RollerFactory.getRoller().getUserManager();
+            
+            Collection templates = theme.getTemplates();
+            Iterator iter = templates.iterator();
+            ThemeTemplate theme_template = null;
+            while ( iter.hasNext() ) {
+                theme_template = (ThemeTemplate) iter.next();
+                
+                WeblogTemplate template = null;
+                
+                if(theme_template.getName().equals(WeblogTemplate.DEFAULT_PAGE)) {
+                    // this is the main Weblog template
+                    try {
+                        template = userMgr.getPage(website.getDefaultPageId());
+                    } catch(Exception e) {
+                        // user may not have a default page yet
+                    }
+                } else {
+                    // any other template
+                    template = userMgr.getPageByName(website, theme_template.getName());
+                }
+                
+                
+                if (template != null) {
+                    // User already has page by that name, so overwrite it.
+                    template.setContents(theme_template.getContents());
+                    template.setLink(theme_template.getLink());
+                    
+                } else {
+                    // User does not have page by that name, so create new page.
+                    template = new WeblogTemplate(
+                            null,                               // id
+                            website,                            // website
+                            theme_template.getName(),           // name
+                            theme_template.getDescription(),    // description
+                            theme_template.getLink(),           // link
+                            theme_template.getContents(),       // contents
+                            new Date(),                         // last mod
+                            theme_template.getTemplateLanguage(), // temp lang
+                            theme_template.isHidden(),          // hidden
+                            theme_template.isNavbar(),          // navbar
+                            theme_template.getDecoratorName()   // decorator
+                            );
+                    userMgr.savePage( template );
+                }
+            }
+            
+            // now update this website's theme to custom
+            website.setEditorTheme(Theme.CUSTOM);
+            
+            // if this is the first time someone is customizing a theme then
+            // we need to set a default page
+            if(website.getDefaultPageId() == null ||
+                    website.getDefaultPageId().trim().equals("") ||
+                    website.getDefaultPageId().equals("dummy")) {
+                // we have to go back to the db to figure out the id
+                WeblogTemplate template = userMgr.getPageByName(website, "Weblog");
+                if(template != null) {
+                    log.debug("Setting default page to "+template.getId());
+                    website.setDefaultPageId(template.getId());
+                }
+            }
+            
+            // we also want to set the weblogdayid
+            WeblogTemplate dayTemplate = userMgr.getPageByName(website, "_day");
+            if(dayTemplate != null) {
+                log.debug("Setting default day page to "+dayTemplate.getId());
+                website.setWeblogDayPageId(dayTemplate.getId());
+            }
+            
+            // save our updated website
+            userMgr.saveWebsite(website);
+            
+            // now lets import all the theme resources
+            FileManager fileMgr = RollerFactory.getRoller().getFileManager();
+            
+            /* NOTE: we make the assumption that the Collection returned by
+             * theme.getResources() will include both the Files for directories
+             * as well as files, and in the correct order so that we can simply
+             * save them all one after the other.  If that were not to be the
+             * case then we can expect errors.
+             *
+             * TODO: we should probably make this more robust so that it wouldn't
+             * fail if the resources were not returned as described above.
+             */
+            Collection resources = theme.getResources();
+            Iterator iterat = resources.iterator();
+            File resourceFile = null;
+            while ( iterat.hasNext() ) {
+                resourceFile = (File) iterat.next();
+                
+                String path = resourceFile.getAbsolutePath().substring(
+                        this.themeDir.length()+theme.getName().length()+1);
+                
+                log.debug("Importing resource "+resourceFile.getAbsolutePath()+" to "+path);
+                
+                try {
+                    if(resourceFile.isDirectory()) {
+                        fileMgr.createDirectory(website.getHandle(), path);
+                    } else {
+                        fileMgr.saveFile(website.getHandle(), path, "text/plain", 
+                                resourceFile.length(), new FileInputStream(resourceFile));
+                    }
+                } catch (Exception ex) {
+                    log.info(ex);
+                }
+            }
+            
+        } catch (Exception e) {
+            log.error("ERROR in action",e);
+            throw new RollerException( e );
+        }
+    }
+    
+    
+    /**
      * This is a convenience method which loads all the theme data from
      * themes stored on the filesystem in the roller webapp /themes/ directory.
      */
@@ -320,97 +445,4 @@
         }
     }
     
-    
-    /**
-     * Helper method that copies down the pages from a given theme into a
-     * users weblog templates.
-     *
-     * @param rreq Request wrapper.
-     * @param theme Name of theme to save.
-     * @throws RollerException
-     */
-    public void saveThemePages(WebsiteData website, Theme theme)
-        throws RollerException {
-        
-        log.debug("Setting custom templates for website: "+website.getName());
-        
-        try {
-            UserManager userMgr = RollerFactory.getRoller().getUserManager();
-            
-            Collection templates = theme.getTemplates();
-            Iterator iter = templates.iterator();
-            ThemeTemplate theme_template = null;
-            while ( iter.hasNext() ) {
-                theme_template = (ThemeTemplate) iter.next();
-                
-                WeblogTemplate template = null;
-                
-                if(theme_template.getName().equals(WeblogTemplate.DEFAULT_PAGE)) {
-                    // this is the main Weblog template
-                    try {
-                        template = userMgr.getPage(website.getDefaultPageId());
-                    } catch(Exception e) {
-                        // user may not have a default page yet
-                    }
-                } else {
-                    // any other template
-                    template = userMgr.getPageByName(website, theme_template.getName());
-                }
-                
-                
-                if (template != null) {
-                    // User already has page by that name, so overwrite it.
-                    template.setContents(theme_template.getContents());
-                    template.setLink(theme_template.getLink());
-                    
-                } else {
-                    // User does not have page by that name, so create new page.
-                    template = new WeblogTemplate(
-                            null,                               // id
-                            website,                            // website
-                            theme_template.getName(),           // name
-                            theme_template.getDescription(),    // description
-                            theme_template.getLink(),           // link
-                            theme_template.getContents(),       // contents
-                            new Date(),                         // last mod
-                            theme_template.getTemplateLanguage(), // temp lang
-                            theme_template.isHidden(),          // hidden
-                            theme_template.isNavbar(),          // navbar
-                            theme_template.getDecoratorName()   // decorator
-                            );
-                    userMgr.savePage( template );
-                }
-            }
-            
-            // now update this website's theme to custom
-            website.setEditorTheme(Theme.CUSTOM);
-            
-            // if this is the first time someone is customizing a theme then
-            // we need to set a default page
-            if(website.getDefaultPageId() == null ||
-                    website.getDefaultPageId().trim().equals("") ||
-                    website.getDefaultPageId().equals("dummy")) {
-                // we have to go back to the db to figure out the id
-                WeblogTemplate template = userMgr.getPageByName(website, "Weblog");
-                if(template != null) {
-                    log.debug("Setting default page to "+template.getId());
-                    website.setDefaultPageId(template.getId());
-                }
-            }
-            
-            // we also want to set the weblogdayid
-            WeblogTemplate dayTemplate = userMgr.getPageByName(website, "_day");
-            if(dayTemplate != null) {
-                log.debug("Setting default day page to "+dayTemplate.getId());
-                website.setWeblogDayPageId(dayTemplate.getId());
-            }
-            
-            // save our updated website
-            userMgr.saveWebsite(website);
-            
-        } catch (Exception e) {
-            log.error("ERROR in action",e);
-            throw new RollerException( e );
-        }       
-    }
 }

Modified: incubator/roller/trunk/src/org/apache/roller/model/ThemeManager.java
URL: http://svn.apache.org/viewvc/incubator/roller/trunk/src/org/apache/roller/model/ThemeManager.java?view=diff&rev=452587&r1=452586&r2=452587
==============================================================================
--- incubator/roller/trunk/src/org/apache/roller/model/ThemeManager.java (original)
+++ incubator/roller/trunk/src/org/apache/roller/model/ThemeManager.java Tue Oct  3 11:11:37 2006
@@ -52,11 +52,14 @@
     
     
     /**
-     * Save all the templates for a Theme into the given weblog.
+     * Import all the contents for a Theme into a weblog.
+     *
+     * @param weblog The weblog to import the theme into.
+     * @param theme The theme that should be imported.
      *
      * @throws RollerException If there is some kind of error in saving.
      */
-    public void saveThemePages(WebsiteData website, Theme theme) 
+    public void importTheme(WebsiteData website, Theme theme) 
         throws RollerException;
     
 }

Modified: incubator/roller/trunk/src/org/apache/roller/ui/authoring/struts/actions/ThemeEditorAction.java
URL: http://svn.apache.org/viewvc/incubator/roller/trunk/src/org/apache/roller/ui/authoring/struts/actions/ThemeEditorAction.java?view=diff&rev=452587&r1=452586&r2=452587
==============================================================================
--- incubator/roller/trunk/src/org/apache/roller/ui/authoring/struts/actions/ThemeEditorAction.java (original)
+++ incubator/roller/trunk/src/org/apache/roller/ui/authoring/struts/actions/ThemeEditorAction.java Tue Oct  3 11:11:37 2006
@@ -369,7 +369,7 @@
                     // only if custom themes are allowed
                     if(RollerRuntimeConfig.getBooleanProperty("themes.customtheme.allowed")) {
                         try {
-                            themeMgr.saveThemePages(website, usersTheme);
+                            themeMgr.importTheme(website, usersTheme);
                             RollerFactory.getRoller().flush();
                         } catch(RollerException re) {
                             mLogger.error(re);