[cvs] eforum commit by ppilgrim: Register in CVS

JCorporate Ltd jcorp at jcorporate.com
Sat Jun 18 10:01:15 UTC 2005


Log Message:
-----------
Register in CVS

Added Files:
-----------
    eforum/src/WEB-INF/src/com/jcorporate/eforum/controller:
        Skinner.java

Revision Data
-------------
--- /dev/null
+++ src/WEB-INF/src/com/jcorporate/eforum/controller/Skinner.java
@@ -0,0 +1,244 @@
+package com.jcorporate.eforum.controller;
+
+import java.io.IOException;
+
+import javax.servlet.RequestDispatcher;
+import javax.servlet.Servlet;
+import javax.servlet.ServletException;
+
+import com.jcorporate.eforum.dbobj.ForumOptions;
+import com.jcorporate.eforum.dbobj.ForumSkinLAFDef;
+import com.jcorporate.eforum.dbobj.ForumSkinProperties;
+import com.jcorporate.expresso.core.controller.ControllerException;
+import com.jcorporate.expresso.core.controller.ControllerRequest;
+import com.jcorporate.expresso.core.controller.ControllerResponse;
+import com.jcorporate.expresso.core.controller.DBController;
+import com.jcorporate.expresso.core.controller.ServletControllerRequest;
+import com.jcorporate.expresso.core.controller.State;
+import com.jcorporate.expresso.core.db.DBException;
+import com.jcorporate.expresso.core.misc.StringUtil;
+
+
+
+/**
+ * An action controller to dynamically switch reprogram the URL from Tiles
+ * Definition. This controller is alternative to Cedric Dumoulin's
+ * SwitchLayoutAction that appears in his tutorial 
+ * <em> Tiles Advanced Features </em>.
+ * 
+ * <p>
+ * 
+ * This action controller has two mode currently. It will dispatch forward
+ * or dispatch include to page within the same web application. 
+ * It reinterprets a URL using tokens. 
+ * 
+ * @author Peter Pilgrim, 04 March 2005
+ * @version $Id: Skinner.java,v 1.1 2005/06/18 10:01:13 ppilgrim Exp $
+ */
+public class Skinner extends DBController {
+    private String thisClass = getClass().getName() + ".";
+
+    /** JSP attribute name for this controller's input */
+    public final static String  CONST_PAGE = "page";
+    
+    /**
+     * A token for URL interpretion substituted for the skin ID
+     */
+    public final static String  TOKEN_SKIN_ID="@SKIN_ID@";
+
+    /**
+     * A token for URL interpretion substituted for the name of skin
+     */
+    public final static String  TOKEN_SKIN_NAME="@SKIN_NAME@";
+
+    /**
+     * A token for URL interpretion substituted for the skin folder
+     */
+    public final static String  TOKEN_SKIN_FOLDER="@SKIN_FOLDER@";
+
+    /**
+     * A token for URL interpretion substituted for the skin description
+     */
+    public final static String  TOKEN_SKIN_DESCRIPTION="@SKIN_DESCRIPTION@";
+
+    /**
+     * A token for URL interpretion substituted for the stored string value of skin
+     */
+    public final static String  TOKEN_SKIN_STRING_VALUE="@SKIN_STRING_VALUE@";
+    
+    /**
+     * A token for URL interpretion substituted for the stored integer value of skin
+     */
+    public final static String  TOKEN_SKIN_INTEGER_VALUE="@SKIN_INTEGER_VALUE@";
+
+    /**
+     * Defaults constructor 
+     */
+    public Skinner() {
+        super();
+
+        State dispatchForward = new State("dispatchForward", "Reinterpret target URL & dispatch forward");
+        addState(dispatchForward);
+        setInitialState("dispatchForward");
+
+        State dispatchInclude = new State("dispatchInclude", "Reinterpret target URL & dispatch include");
+        addState(dispatchInclude);
+
+        setSchema("com.jcorporate.eforum.ForumSchema");
+    } /* SendNotice() */
+
+    /**
+     * Return the title of this Transaction
+     */
+    public String getTitle() {
+        return new String("Skinner");
+    } /* getTitle() */
+
+
+    /**
+     * Given an input URL substitute the tokens in order to reinterpret the target URL
+     * 
+     * @param inputUrl the input url
+     * @param request the controller request
+     * @return the transformed url
+     */
+    public String substituteTokens( String inputUrl, ControllerRequest request ) 
+    throws DBException
+    {
+        String url = inputUrl;
+        
+        // Retrieve the current forum options
+        ForumOptions fo = new ForumOptions();
+        fo.setDataContext( request.getDataContext() );
+        fo.setField( ForumOptions.FLD_UID, request.getRequestingUser().getUid() );
+        
+        if ( !fo.find() ) {
+            fo.checkDefaultValueIntegrity();
+        }
+        int skinId = fo.getFieldInt( ForumOptions.FLD_SKIN_LAF_ID );
+
+        // Retrieve skin look and feel definition
+        ForumSkinLAFDef skinLafDef = new ForumSkinLAFDef();
+        skinLafDef.setDataContext( request.getDataContext() );
+        skinLafDef.setField( ForumSkinLAFDef.FLD_SKIN_ID, skinId );
+        skinLafDef.retrieve();
+        
+        url = StringUtil.replace( url, TOKEN_SKIN_ID, Integer.toString(skinId) );
+
+        String value = skinLafDef.getField( ForumSkinLAFDef.FLD_SKIN_DESCRIP );
+        url = StringUtil.replace( url, TOKEN_SKIN_DESCRIPTION, value );
+        
+        value = skinLafDef.getField( ForumSkinLAFDef.FLD_SKIN_NAME);
+        url = StringUtil.replace( url, TOKEN_SKIN_NAME, value );
+        
+        // Retrieve skin look and feel properties
+        ForumSkinProperties skinProps = new ForumSkinProperties();
+        skinProps.clear();
+        skinProps.setDataContext( request.getDataContext() );
+        skinProps.setField( ForumSkinProperties.FLD_SKIN_ID, skinId );
+        skinProps.setField( ForumSkinProperties.FLD_PROPERTY_NAME, ForumSkinProperties.SKIN_FOLDER_PROPERTY);
+        skinProps.retrieve();
+        value = skinLafDef.getField( ForumSkinProperties.FLD_TEXT_VALUE );
+        url = StringUtil.replace( url, TOKEN_SKIN_FOLDER, value );
+        
+        return url;
+    }
+    
+    /**
+     * A state method that reinterpret a page URL and dispatches forward 
+     * 
+     * @param request The <code>ControllerRequest</code> object
+     * @param response the <code>ControllerResponse</code> objcet
+     * @throws ControllerException
+     * 
+     */
+    protected void runDispatchForwardState(
+            ControllerRequest request,
+            ControllerResponse response)
+        throws ControllerException 
+    {
+        ServletControllerRequest  servReq = (ServletControllerRequest)request;
+
+        String pageBeforeUrl= request.getParameter( CONST_PAGE );
+        String pageAfterUrl = null;
+        
+        try {
+            pageAfterUrl = substituteTokens( pageBeforeUrl, request );
+            
+            // We flag to Expresso Framework that this state method is
+            // about to return its own binary stream content.
+            response.setCustomResponse(true);   /* ! */
+
+            Servlet servlet = servReq.getCallingServlet();
+            // RequestDispatcher rd = servlet.getServletConfig().getServletContext().getRequestDispatcher(pageAfterUrl);
+            RequestDispatcher rd = servReq.getHttpServletRequest().getRequestDispatcher( pageAfterUrl );
+            
+            rd.forward( servReq.getServletRequest(), servReq.getServletResponse() );
+        }
+        catch (IOException ioe) {
+            throw new ControllerException(
+                    "IO Failure: unable to dispatch forward to target URL`"+pageAfterUrl+"' ( "+
+                    "before URL: `"+pageBeforeUrl+"' )", ioe );
+        }
+        catch (ServletException se) {
+            throw new ControllerException(
+                    "Servlet failure: unable to dispatch forward to target URL`"+pageAfterUrl+"' ( "+
+                    "before URL: `"+pageBeforeUrl+"' )", se );
+        }
+        catch (DBException de) {
+            throw new ControllerException(
+                    "Database failure: unable to dispatch forward to target URL`"+pageAfterUrl+"' ( "+
+                    "before URL: `"+pageBeforeUrl+"' )", de );
+        }
+    }
+
+    /**
+     * A state method that reinterpret a page URL and dispatches forward 
+     * 
+     * @param request The <code>ControllerRequest</code> object
+     * @param response the <code>ControllerResponse</code> objcet
+     * @throws ControllerException
+     * 
+     */
+    protected void runDispatchIncludeState(
+            ControllerRequest request,
+            ControllerResponse response)
+        throws ControllerException 
+    {
+        ServletControllerRequest  servReq = (ServletControllerRequest)request;
+
+        String pageBeforeUrl= request.getParameter( CONST_PAGE );
+        String pageAfterUrl = null;
+        
+        try {
+            pageAfterUrl = substituteTokens( pageBeforeUrl, request );
+            
+            // We flag to Expresso Framework that this state method is
+            // about to return its own binary stream content.
+            response.setCustomResponse(true);   /* ! */
+
+            Servlet servlet = servReq.getCallingServlet();
+            // RequestDispatcher rd = servlet.getServletConfig().getServletContext().getRequestDispatcher(pageAfterUrl);
+            RequestDispatcher rd = servReq.getHttpServletRequest().getRequestDispatcher( pageAfterUrl );
+
+            rd.include( servReq.getServletRequest(), servReq.getServletResponse() );
+        }
+        catch (IOException ioe) {
+            throw new ControllerException(
+                    "IO Failure: unable to dispatch include to target URL`"+pageAfterUrl+"' ( "+
+                    "before URL: `"+pageBeforeUrl+"' )", ioe );
+        }
+        catch (ServletException se) {
+            throw new ControllerException(
+                    "Servlet failure: unable to dispatch include to target URL`"+pageAfterUrl+"' ( "+
+                    "before URL: `"+pageBeforeUrl+"' )", se );
+        }
+        catch (DBException de) {
+            throw new ControllerException(
+                    "Database failure: unable to dispatch include to target URL`"+pageAfterUrl+"' ( "+
+                    "before URL: `"+pageBeforeUrl+"' )", de );
+        }
+    }
+}
+
+


More information about the cvs mailing list