You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by ca...@apache.org on 2006/10/10 23:33:10 UTC

svn commit: r462581 - in /myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/excelexport: ./ ExcelExport.java ExcelExportPhaseListener.java ExcelExportRenderer.java ExcelExportTag.java

Author: cagatay
Date: Tue Oct 10 14:33:09 2006
New Revision: 462581

URL: http://svn.apache.org/viewvc?view=rev&rev=462581
Log:
Add new component ExcelExport

Added:
    myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/excelexport/
    myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/excelexport/ExcelExport.java   (with props)
    myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/excelexport/ExcelExportPhaseListener.java   (with props)
    myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/excelexport/ExcelExportRenderer.java   (with props)
    myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/excelexport/ExcelExportTag.java   (with props)

Added: myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/excelexport/ExcelExport.java
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/excelexport/ExcelExport.java?view=auto&rev=462581
==============================================================================
--- myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/excelexport/ExcelExport.java (added)
+++ myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/excelexport/ExcelExport.java Tue Oct 10 14:33:09 2006
@@ -0,0 +1,74 @@
+/*
+ * Copyright 2002,2004 The Apache Software Foundation.
+ *
+ * Licensed 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.excelexport;
+
+import javax.faces.component.UIComponentBase;
+import javax.faces.context.FacesContext;
+import javax.faces.el.ValueBinding;
+
+import org.apache.myfaces.shared_tomahawk.util._ComponentUtils;
+
+/**
+ * @author cagatay (latest modification by $Author$)
+ * @version $Revision$ $Date$
+ */
+public class ExcelExport extends UIComponentBase {
+
+	public static final String COMPONENT_TYPE = "org.apache.myfaces.ExcelExport";
+	public static final String COMPONENT_FAMILY = "org.apache.myfaces.Export";
+	private static final String DEFAULT_RENDERER = "org.apache.myfaces.ExcelExportRenderer";
+
+	private String _for;
+
+	public ExcelExport() {
+		setRendererType(DEFAULT_RENDERER);
+	}
+
+	public String getFamily() {
+		return COMPONENT_FAMILY;
+	}
+	
+	
+	public boolean getRendersChildren() {
+		return true;
+	}
+
+	public String getFor() {
+		if (_for != null)
+			return _for;
+
+		ValueBinding vb = getValueBinding("for");
+		return vb != null ? _ComponentUtils.getStringValue(getFacesContext(), vb) : null;
+	}
+
+	public void setFor(String forValue) {
+		_for = forValue;
+	}
+
+	public Object saveState(FacesContext context) {
+		Object values[] = new Object[2];
+		values[0] = super.saveState(context);
+		values[1] = _for;
+		return ((Object) (values));
+	}
+
+	public void restoreState(FacesContext context, Object state) {
+		Object values[] = (Object[]) state;
+		super.restoreState(context, values[0]);
+		_for = (String) values[1];
+	}
+
+}

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

Added: myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/excelexport/ExcelExportPhaseListener.java
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/excelexport/ExcelExportPhaseListener.java?view=auto&rev=462581
==============================================================================
--- myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/excelexport/ExcelExportPhaseListener.java (added)
+++ myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/excelexport/ExcelExportPhaseListener.java Tue Oct 10 14:33:09 2006
@@ -0,0 +1,140 @@
+/*
+ * Copyright 2002,2004 The Apache Software Foundation.
+ *
+ * Licensed 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.excelexport;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+import javax.faces.component.UIColumn;
+import javax.faces.component.UIComponent;
+import javax.faces.component.ValueHolder;
+import javax.faces.component.html.HtmlDataTable;
+import javax.faces.context.FacesContext;
+import javax.faces.event.PhaseEvent;
+import javax.faces.event.PhaseId;
+import javax.faces.event.PhaseListener;
+import javax.portlet.RenderResponse;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.myfaces.custom.util.ComponentUtils;
+import org.apache.poi.hssf.usermodel.HSSFCell;
+import org.apache.poi.hssf.usermodel.HSSFRow;
+import org.apache.poi.hssf.usermodel.HSSFSheet;
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;
+
+/**
+ * @author cagatay (latest modification by $Author$)
+ * @version $Revision$ $Date$
+ */
+public class ExcelExportPhaseListener implements PhaseListener {
+
+	public void afterPhase(PhaseEvent phaseEvent) {
+		FacesContext facesContext = phaseEvent.getFacesContext(); 
+		Map map = facesContext.getExternalContext().getRequestParameterMap();
+
+		if(map.containsKey("excelExportTableId")) {
+			String tableId = (String) map.get("excelExportTableId");
+			HtmlDataTable dataTable = (HtmlDataTable) ComponentUtils.findComponentById( facesContext, facesContext.getViewRoot(), tableId );
+			HSSFWorkbook generatedExcel = generateExcel( facesContext, dataTable );
+			try {
+				Object contextResponse = facesContext.getExternalContext().getResponse();
+				if(contextResponse instanceof HttpServletResponse)
+					writeExcelOutput(generatedExcel,(HttpServletResponse)contextResponse);
+				else if(contextResponse instanceof RenderResponse)
+					writeExcelOutput(generatedExcel,(RenderResponse)contextResponse);
+				
+				facesContext.getApplication().getStateManager().saveSerializedView(facesContext);
+				facesContext.responseComplete();
+			} catch (Exception e) {
+				throw new RuntimeException( e );
+			}
+		}
+	}
+
+	public void beforePhase(PhaseEvent phaseEvent) {
+		//do nothing
+	}
+
+	public PhaseId getPhaseId() {
+		return PhaseId.RESTORE_VIEW;
+	}
+	
+	private void writeExcelOutput(HSSFWorkbook workBook, HttpServletResponse response) throws IOException{
+		response.setContentType("application/vnd.ms-excel");
+		workBook.write(response.getOutputStream());
+	}
+	
+	private void writeExcelOutput(HSSFWorkbook workBook, RenderResponse response) throws IOException{
+		response.setContentType("application/vnd.ms-excel");
+		workBook.write(response.getPortletOutputStream());
+	}
+	
+	private List getColumns(HtmlDataTable table) {
+		List columns = new ArrayList();
+		for(int i=0; i < table.getChildCount(); i++) {
+			UIComponent child = (UIComponent) table.getChildren().get( i );
+			if(child instanceof UIColumn) {				
+				columns.add( child );
+			}
+		}
+		return columns;
+	}
+	
+	private HSSFWorkbook generateExcel(FacesContext facesContext, HtmlDataTable table) {
+		HSSFWorkbook workbook = new HSSFWorkbook();
+		HSSFSheet sheet = workbook.createSheet(table.getId());
+		List columns = getColumns(table);
+		int currentRowIndex = table.getRowIndex();
+				
+		addColumnHeaders(sheet, columns);
+		addColumnValues(sheet, columns, table);
+
+		table.setRowIndex( currentRowIndex );
+		return workbook;
+	}
+	
+	private void addColumnValue(HSSFRow rowHeader, UIComponent component, int index) {
+		HSSFCell cell = rowHeader.createCell((short)index);
+		cell.setEncoding(HSSFCell.ENCODING_UTF_16);
+		if(component instanceof ValueHolder) {
+			ValueHolder valueHolder = (ValueHolder) component;
+			cell.setCellValue(valueHolder.getValue().toString());
+		}
+	}
+	
+	private void addColumnHeaders(HSSFSheet sheet, List columns) {
+		HSSFRow rowHeader = sheet.createRow(0);
+		
+		for(int i=0; i < columns.size(); i++) {
+			UIColumn column = (UIColumn) columns.get(i);
+			addColumnValue(rowHeader, column.getHeader(), i);
+		}
+	}
+	
+	private void addColumnValues(HSSFSheet sheet, List columns, HtmlDataTable dataTable) {
+		for(int i=0 ; i < dataTable.getRowCount(); i++)  {
+			dataTable.setRowIndex(i);
+			HSSFRow row = sheet.createRow( i + 1 );
+			for (int j=0; j < columns.size(); j++) {
+				UIColumn column = (UIColumn) columns.get(j);
+				addColumnValue(row, (UIComponent) column.getChildren().get(0), j);
+			}
+		}
+	}
+
+}

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

Added: myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/excelexport/ExcelExportRenderer.java
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/excelexport/ExcelExportRenderer.java?view=auto&rev=462581
==============================================================================
--- myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/excelexport/ExcelExportRenderer.java (added)
+++ myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/excelexport/ExcelExportRenderer.java Tue Oct 10 14:33:09 2006
@@ -0,0 +1,69 @@
+/*
+ * Copyright 2002,2004 The Apache Software Foundation.
+ *
+ * Licensed 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.excelexport;
+
+import java.io.IOException;
+
+import javax.faces.component.UIComponent;
+import javax.faces.context.FacesContext;
+
+import org.apache.commons.lang.StringUtils;
+import org.apache.myfaces.shared_tomahawk.renderkit.RendererUtils;
+import org.apache.myfaces.shared_tomahawk.renderkit.html.HtmlRenderer;
+
+/**
+ * @author cagatay (latest modification by $Author$)
+ * @version $Revision$ $Date$
+ */
+public class ExcelExportRenderer extends HtmlRenderer {
+
+	public void encodeChildren(FacesContext facesContext, UIComponent component) throws IOException{
+		if(component.getChildCount() == 0)
+			return ;
+		
+		String tableId = ((ExcelExport) component).getFor();
+		UIComponent child = (UIComponent) component.getChildren().get( 0 );
+		
+		if( !isDecorated(facesContext, child, tableId) )
+			decorateOnClick(facesContext, child, tableId);
+		
+		RendererUtils.renderChildren(facesContext, component);
+	}
+	
+	private boolean isDecorated(FacesContext facesContext, UIComponent child, String tableId) {
+		String onClick = (String) child.getAttributes().get("onclick");
+		String jsCall = getJSCall(facesContext, tableId);
+		
+		if(onClick == null || onClick.indexOf(jsCall) == -1)
+			return false;
+		else
+			return true;
+	}
+	
+	private void decorateOnClick(FacesContext facesContext, UIComponent child, String tableId) {
+		String jsCall = getJSCall(facesContext, tableId);
+		String onclickEvent = (String) child.getAttributes().get("onclick");
+		child.getAttributes().put("onclick", onclickEvent + ";" + jsCall);
+	}
+	
+	private String getJSCall(FacesContext facesContext, String tableId) {
+		String viewId = StringUtils.split( facesContext.getViewRoot().getViewId() , "\\.")[0];
+		String contextPath = facesContext.getExternalContext().getRequestContextPath();
+		return "window.open('" + contextPath + viewId + ".jsf?excelExportTableId=" + tableId + "');return false;";
+	}
+
+	
+}

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

Added: myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/excelexport/ExcelExportTag.java
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/excelexport/ExcelExportTag.java?view=auto&rev=462581
==============================================================================
--- myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/excelexport/ExcelExportTag.java (added)
+++ myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/excelexport/ExcelExportTag.java Tue Oct 10 14:33:09 2006
@@ -0,0 +1,57 @@
+/*
+ * Copyright 2002,2004 The Apache Software Foundation.
+ *
+ * Licensed 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.excelexport;
+
+import javax.faces.component.UIComponent;
+
+import org.apache.myfaces.shared_tomahawk.taglib.UIComponentTagBase;
+
+/**
+ * @author cagatay (latest modification by $Author$)
+ * @version $Revision$ $Date$
+ */
+public class ExcelExportTag extends UIComponentTagBase {
+
+	private String _for;
+
+	public void release() {
+		super.release();
+		_for = null;
+	}
+
+	protected void setProperties(UIComponent component) {
+		super.setProperties(component);
+
+		setStringProperty(component, "for", _for);
+	}
+
+	public String getComponentType() {
+		return ExcelExport.COMPONENT_TYPE;
+	}
+
+	public String getRendererType() {
+		return "org.apache.myfaces.ExcelExportRenderer";
+	}
+
+	public String getFor() {
+		return _for;
+	}
+
+	public void setFor(String aFor) {
+		_for = aFor;
+	}
+
+}

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