roller-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From agillil...@apache.org
Subject svn commit: r543612 - in /roller/trunk/apps/weblogger: src/java/org/apache/roller/weblogger/ui/struts2/editor/ web/WEB-INF/classes/ web/WEB-INF/jsps/editor/
Date Fri, 01 Jun 2007 20:56:00 GMT
Author: agilliland
Date: Fri Jun  1 13:55:59 2007
New Revision: 543612

URL: http://svn.apache.org/viewvc?view=rev&rev=543612
Log:
removing templateAdd action and moving its method into the templates action.  this is basically
needed because we can't do proper validation in templateAdd because the way it works is to
always dispatch to the templates action for display.  however, struts2 validation works by
simply detecting action errors, even if they were set on a different action, and thus the
action chain gets messed up when the templateAdd action would report any errors.  this approach
is the easiest solution.


Removed:
    roller/trunk/apps/weblogger/src/java/org/apache/roller/weblogger/ui/struts2/editor/TemplateAdd.java
Modified:
    roller/trunk/apps/weblogger/src/java/org/apache/roller/weblogger/ui/struts2/editor/Templates.java
    roller/trunk/apps/weblogger/web/WEB-INF/classes/struts.xml
    roller/trunk/apps/weblogger/web/WEB-INF/jsps/editor/TemplatesSidebar.jsp

Modified: roller/trunk/apps/weblogger/src/java/org/apache/roller/weblogger/ui/struts2/editor/Templates.java
URL: http://svn.apache.org/viewvc/roller/trunk/apps/weblogger/src/java/org/apache/roller/weblogger/ui/struts2/editor/Templates.java?view=diff&rev=543612&r1=543611&r2=543612
==============================================================================
--- roller/trunk/apps/weblogger/src/java/org/apache/roller/weblogger/ui/struts2/editor/Templates.java
(original)
+++ roller/trunk/apps/weblogger/src/java/org/apache/roller/weblogger/ui/struts2/editor/Templates.java
Fri Jun  1 13:55:59 2007
@@ -20,7 +20,9 @@
 
 import java.util.ArrayList;
 import java.util.Collections;
+import java.util.Date;
 import java.util.List;
+import org.apache.commons.lang.StringUtils;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.roller.RollerException;
@@ -45,6 +47,10 @@
     // 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;
+    
     
     public Templates() {
         this.actionName = "templates";
@@ -53,7 +59,6 @@
     }
     
     
-    // must be a weblog admin to use this action
     public short requiredWeblogPermissions() {
         return WeblogPermission.ADMIN;
     }
@@ -97,6 +102,90 @@
     }
     
     
+    /**
+     * 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() );
+            
+            // 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
+            UserManager mgr = RollerFactory.getRoller().getUserManager();
+            mgr.savePage( newTemplate );
+            
+            // 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());
+                mgr.saveWebsite(getActionWeblog());
+            }
+            
+            // flush results to db
+            RollerFactory.getRoller().flush();
+            
+            // reset form fields
+            setNewTmplName(null);
+            setNewTmplAction(null);
+            
+        } catch (RollerException 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("TemplateEdit.error.nameNull");
+        } else if(getNewTmplName().length() > 255) {
+            addError("TemplateEdit.error.nameSize");
+        }
+        
+        // make sure action is a valid
+        if(StringUtils.isEmpty(getNewTmplAction())) {
+            addError("TemplateEdit.error.actionNull");
+        }
+        
+        // check if template by that name already exists
+        try {
+            UserManager umgr = RollerFactory.getRoller().getUserManager();
+            WeblogTemplate existingPage = umgr.getPageByName(getActionWeblog(), getNewTmplName());
+            if(existingPage != null) {
+                addError("pagesForm.error.alreadyExists", getNewTmplName());
+            }
+        } catch (RollerException ex) {
+            log.error("Error checking for existing template", ex);
+        }
+    }
+    
+    
     public List getTemplates() {
         return templates;
     }
@@ -111,6 +200,22 @@
 
     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/apps/weblogger/web/WEB-INF/classes/struts.xml
URL: http://svn.apache.org/viewvc/roller/trunk/apps/weblogger/web/WEB-INF/classes/struts.xml?view=diff&rev=543612&r1=543611&r2=543612
==============================================================================
--- roller/trunk/apps/weblogger/web/WEB-INF/classes/struts.xml (original)
+++ roller/trunk/apps/weblogger/web/WEB-INF/classes/struts.xml Fri Jun  1 13:55:59 2007
@@ -334,11 +334,6 @@
             <result name="list" type="tiles">.Templates</result>
         </action>
         
-        <action name="templateAdd!*" method="{1}"
-                class="org.apache.roller.weblogger.ui.struts2.editor.TemplateAdd">
-            <result name="success" type="chain">templates</result>
-        </action>
-        
         <action name="templateEdit!*" method="{1}"
                 class="org.apache.roller.weblogger.ui.struts2.editor.TemplateEdit">
             <result name="list" type="chain">templates</result>

Modified: roller/trunk/apps/weblogger/web/WEB-INF/jsps/editor/TemplatesSidebar.jsp
URL: http://svn.apache.org/viewvc/roller/trunk/apps/weblogger/web/WEB-INF/jsps/editor/TemplatesSidebar.jsp?view=diff&rev=543612&r1=543611&r2=543612
==============================================================================
--- roller/trunk/apps/weblogger/web/WEB-INF/jsps/editor/TemplatesSidebar.jsp (original)
+++ roller/trunk/apps/weblogger/web/WEB-INF/jsps/editor/TemplatesSidebar.jsp Fri Jun  1 13:55:59
2007
@@ -25,7 +25,7 @@
                 <h3><s:text name="pagesForm.addNewPage" /></h3>
                 <hr size="1" noshade="noshade" />
                 
-                <s:form action="templateAdd!save">
+                <s:form action="templates!add">
                     <s:hidden name="weblog" />
                     
                     <table cellpadding="0" cellspacing="6">



Mime
View raw message