You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by lu...@apache.org on 2008/05/13 00:11:23 UTC

svn commit: r655667 - in /myfaces/tomahawk/trunk/sandbox: core/src/main/java/org/apache/myfaces/custom/exporter/ core/src/main/tld/ examples/src/main/webapp/

Author: lu4242
Date: Mon May 12 15:11:23 2008
New Revision: 655667

URL: http://svn.apache.org/viewvc?rev=655667&view=rev
Log:
TOMAHAWK-1252 The Exporter new syntax (as actionListener)

Added:
    myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/exporter/ExporterActionListener.java   (with props)
    myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/exporter/ExporterActionListenerTag.java   (with props)
Modified:
    myfaces/tomahawk/trunk/sandbox/core/src/main/tld/myfaces_sandbox.tld
    myfaces/tomahawk/trunk/sandbox/examples/src/main/webapp/exporter.jsp

Added: myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/exporter/ExporterActionListener.java
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/exporter/ExporterActionListener.java?rev=655667&view=auto
==============================================================================
--- myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/exporter/ExporterActionListener.java (added)
+++ myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/exporter/ExporterActionListener.java Mon May 12 15:11:23 2008
@@ -0,0 +1,179 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.myfaces.custom.exporter;
+
+import javax.faces.component.StateHolder;
+import javax.faces.component.html.HtmlDataTable;
+import javax.faces.context.FacesContext;
+import javax.faces.event.ActionEvent;
+import javax.faces.event.ActionListener;
+import javax.portlet.RenderResponse;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.myfaces.custom.exporter.util.ExcelExporterUtil;
+import org.apache.myfaces.custom.exporter.util.ExporterConstants;
+import org.apache.myfaces.custom.exporter.util.PDFExporterUtil;
+import org.apache.myfaces.custom.util.ComponentUtils;
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;
+
+/**
+ * This class is acting as the Exporter ActionListener.
+ */
+public class ExporterActionListener implements ActionListener, StateHolder {
+
+    private static final Log log = LogFactory.getLog(ExporterActionListener.class);    
+    public static final String FILENAME_KEY = "filename";
+    public static final String FILE_TYPE_KEY = "fileType";
+    public static final String FOR_KEY = "for"; 
+    
+    private String _fileType;
+    private String _fileName;
+    private String _for;        
+
+    public void processAction(ActionEvent event) {
+
+	FacesContext facesContext = FacesContext.getCurrentInstance();
+	Object response = facesContext.getExternalContext().getResponse();
+	
+	if (!(response instanceof HttpServletResponse)) 
+	{
+	    log.error("ExporteActionListener requires servlet");
+	}
+	else 
+	{
+	    try 
+	    {		
+
+		HtmlDataTable dataTable = (HtmlDataTable) ComponentUtils
+			.findComponentById(facesContext, facesContext
+				.getViewRoot(), _for);		
+		
+		/* By default if the fileName is not specified, then use the table id. */
+		if(_fileName == null) 
+		{
+		    _fileName = dataTable.getId();
+		}
+		
+		if (ExporterConstants.EXCEL_FILE_TYPE.equalsIgnoreCase(_fileType)) 
+		{
+		    
+		    /* 
+		     * Excel case.
+		     * Generate the XLS to the response stream. 
+		     */    
+		    HSSFWorkbook generatedExcel = ExcelExporterUtil
+			    .generateExcelTableModel(facesContext, dataTable);
+		    Object contextResponse = facesContext.getExternalContext()
+			    .getResponse();
+
+		    if (contextResponse instanceof HttpServletResponse)
+		    {
+			ExcelExporterUtil.generateEXCEL(generatedExcel,
+				(HttpServletResponse) contextResponse,
+				_fileName);
+		    }
+		    else if (contextResponse instanceof RenderResponse)
+		    {
+			ExcelExporterUtil.generateEXCEL(generatedExcel,
+				(RenderResponse) contextResponse, _fileName);
+		    }
+
+		}
+		else 
+		{ 
+		    
+		    /* 
+		     * PDF case.
+		     * Generate the PDF to the response stream. 
+		     */
+		    HttpServletResponse httpResponse = (HttpServletResponse) facesContext
+			    .getExternalContext().getResponse();
+
+		    PDFExporterUtil.generatePDF(facesContext, httpResponse,
+			    _fileName, dataTable);
+		}
+
+		/* save the seralized view and complete the response. */
+		facesContext.getApplication().getStateManager()
+			.saveSerializedView(facesContext);
+		facesContext.responseComplete();
+	    }
+	    catch (Exception exception) {
+		throw new RuntimeException(exception);
+	    }
+
+	}
+
+	facesContext.responseComplete();
+    }
+
+    public String getFilename() {
+        return _fileName;
+    }
+
+    public void setFilename(String _filename) {
+        this._fileName = _filename;
+    }
+
+    public String getFileType() {
+        return _fileType;
+    }
+
+    public void setFileType(String type) {
+        _fileType = type;
+    }
+
+    public String getFor() {
+        return _for;
+    }
+
+    public void setFor(String _for) {
+        this._for = _for;
+    }
+
+    public void restoreState(FacesContext context, Object state) {
+	String values[] = (String[])state;
+	
+        _for = values[0];
+        _fileName = values[1];
+        _fileType = values[2];    
+     }
+
+    public Object saveState(FacesContext context) {
+        String values[] = new String[3];
+        
+        values[0] = _for;
+        values[1] = _fileName;
+        values[2] = _fileType;        
+        return ((String[]) values);
+    }
+
+    public boolean isTransient() {
+	return false;
+    }
+
+    public void setTransient(boolean newTransientValue) {
+	if (newTransientValue == true)
+	{
+	   throw new IllegalArgumentException();
+	}
+    }
+}

Propchange: myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/exporter/ExporterActionListener.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/exporter/ExporterActionListener.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Added: myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/exporter/ExporterActionListenerTag.java
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/exporter/ExporterActionListenerTag.java?rev=655667&view=auto
==============================================================================
--- myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/exporter/ExporterActionListenerTag.java (added)
+++ myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/exporter/ExporterActionListenerTag.java Mon May 12 15:11:23 2008
@@ -0,0 +1,113 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.myfaces.custom.exporter;
+
+import javax.faces.component.ActionSource;
+import javax.faces.component.UIComponent;
+import javax.faces.webapp.UIComponentTag;
+import javax.servlet.jsp.JspException;
+import javax.servlet.jsp.tagext.Tag;
+import javax.servlet.jsp.tagext.TagSupport;
+
+/**
+ * This class is acting as the tag handler for the Exporter ActionListener.
+ */
+public class ExporterActionListenerTag extends TagSupport {
+    
+    private String _fileType;
+    private String _fileName;
+    private String _for;
+    
+    public int doStartTag() throws JspException {
+        
+	// check whether the attributes are not null 
+	if (_for == null)
+        {
+            throw new JspException("for attribute not set");
+        }
+        
+        if (_fileType == null)
+        {
+            throw new JspException("fileType attribute not set");
+        }
+
+        // find the parent UIComponentTag which should be an ActionSource.
+        UIComponentTag componentTag = UIComponentTag.getParentUIComponentTag(pageContext);
+        
+        if (componentTag == null)
+        {
+            throw new JspException("ExporterActionListenerTag has no UIComponentTag ancestor");
+        }
+
+        if (componentTag.getCreated())
+        {
+            
+            // if the component was just created, so we add the Listener.
+            UIComponent component = componentTag.getComponentInstance();
+
+            if (component instanceof ActionSource)
+            {
+                ExporterActionListener exporterActionListener = new ExporterActionListener();
+
+                exporterActionListener.setFor(_for);
+                exporterActionListener.setFileType(_fileType);   
+                exporterActionListener.setFilename(_fileName);
+                
+                ((ActionSource)component).addActionListener(exporterActionListener);
+            }
+            else
+            {
+                throw new JspException("Component " + component.getId() + " is no ActionSource");
+            }
+        }
+
+        return Tag.SKIP_BODY;
+    }
+
+    public void release() {
+	super.release();
+	_fileType = null;
+	_fileName = null;
+	_for = null;
+    }
+    
+    public String getFilename() {
+        return _fileName;
+    }
+
+    public void setFilename(String _filename) {
+        this._fileName = _filename;
+    }
+
+    public String getFileType() {
+        return _fileType;
+    }
+
+    public void setFileType(String type) {
+        _fileType = type;
+    }
+
+    public String getFor() {
+        return _for;
+    }
+
+    public void setFor(String _for) {
+        this._for = _for;
+    }    
+}

Propchange: myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/exporter/ExporterActionListenerTag.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/exporter/ExporterActionListenerTag.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Modified: myfaces/tomahawk/trunk/sandbox/core/src/main/tld/myfaces_sandbox.tld
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/sandbox/core/src/main/tld/myfaces_sandbox.tld?rev=655667&r1=655666&r2=655667&view=diff
==============================================================================
--- myfaces/tomahawk/trunk/sandbox/core/src/main/tld/myfaces_sandbox.tld (original)
+++ myfaces/tomahawk/trunk/sandbox/core/src/main/tld/myfaces_sandbox.tld Mon May 12 15:11:23 2008
@@ -1934,5 +1934,12 @@
       </attribute>                  
     </tag>    
 
+    <tag>
+        <name>exporterActionListener</name>
+        <tag-class>org.apache.myfaces.custom.exporter.ExporterActionListenerTag</tag-class>
+        <body-content>JSP</body-content>
+        &ui_component_attributes;        
+        &exporter_attributes;
+    </tag>    
 
 </taglib>

Modified: myfaces/tomahawk/trunk/sandbox/examples/src/main/webapp/exporter.jsp
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/sandbox/examples/src/main/webapp/exporter.jsp?rev=655667&r1=655666&r2=655667&view=diff
==============================================================================
--- myfaces/tomahawk/trunk/sandbox/examples/src/main/webapp/exporter.jsp (original)
+++ myfaces/tomahawk/trunk/sandbox/examples/src/main/webapp/exporter.jsp Mon May 12 15:11:23 2008
@@ -33,8 +33,7 @@
 <f:view>
 
     <h:form>
-    	<p>This component allows to export the datatable contents to an excel file.</p>
-		<p>Note: Currently the component does not support client side state saving.</p>
+    	<p>This component allows to export the datatable contents to an excel or pdf file.</p>
 		
 		<br>
 		
@@ -73,15 +72,15 @@
 
 		<br>
 		
-		<s:exporter for="tbl_cars" fileType="XLS">
-			<h:commandButton action="" value="Export as excel"/>
-		</s:exporter>
+		<h:commandButton action="" value="Export as excel">
+			<s:exporterActionListener for="tbl_cars" fileType="XLS"/>
+		</h:commandButton>
 		
 		<br>
 		
-		<s:exporter for="tbl_cars" fileType="PDF">
-			<h:commandButton action="" value="Export as pdf"/>
-		</s:exporter>
+		<h:commandButton action="" value="Export as pdf">
+			<s:exporterActionListener for="tbl_cars" fileType="PDF"/>
+		</h:commandButton>
 		
     </h:form>
 </f:view>