myfaces-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From haz...@apache.org
Subject svn commit: r751188 - in /myfaces/commons/trunk/myfaces-commons-components/src/main/java/org/apache/myfaces/commons/exporter/util: ExcelExporterUtil.java PDFExporterUtil.java
Date Sat, 07 Mar 2009 03:30:41 GMT
Author: hazems
Date: Sat Mar  7 03:30:38 2009
New Revision: 751188

URL: http://svn.apache.org/viewvc?rev=751188&view=rev
Log:
Fixing https://issues.apache.org/jira/browse/MFCOMMONS-6

Modified:
    myfaces/commons/trunk/myfaces-commons-components/src/main/java/org/apache/myfaces/commons/exporter/util/ExcelExporterUtil.java
    myfaces/commons/trunk/myfaces-commons-components/src/main/java/org/apache/myfaces/commons/exporter/util/PDFExporterUtil.java

Modified: myfaces/commons/trunk/myfaces-commons-components/src/main/java/org/apache/myfaces/commons/exporter/util/ExcelExporterUtil.java
URL: http://svn.apache.org/viewvc/myfaces/commons/trunk/myfaces-commons-components/src/main/java/org/apache/myfaces/commons/exporter/util/ExcelExporterUtil.java?rev=751188&r1=751187&r2=751188&view=diff
==============================================================================
--- myfaces/commons/trunk/myfaces-commons-components/src/main/java/org/apache/myfaces/commons/exporter/util/ExcelExporterUtil.java
(original)
+++ myfaces/commons/trunk/myfaces-commons-components/src/main/java/org/apache/myfaces/commons/exporter/util/ExcelExporterUtil.java
Sat Mar  7 03:30:38 2009
@@ -38,22 +38,57 @@
 /**
  * This class is a utility class for serving excel exporting.
  */
-public class ExcelExporterUtil {
+public class ExcelExporterUtil 
+{
     
-    private static void addColumnHeaders(HSSFSheet sheet, List columns) {
+    /**
+     * This utility method is used for generating the excel 
+     * table to the HttpServletResponse object. 
+     * @param workBook
+     * @param response
+     * @param fileName
+     * @throws IOException
+     */
+    public static void generateEXCEL(FacesContext facesContext,
+            HttpServletResponse response, String fileName,
+            HtmlDataTable dataTable) throws IOException
+    {
+
+        /*
+         * By default if the fileName is not specified, then use the
+         * table id.
+         */
+        if (fileName == null)
+        {
+            fileName = dataTable.getId();
+        }
+
+        /* generate the excel model */
+        HSSFWorkbook generatedExcel = ExcelExporterUtil
+                .generateExcelTableModel(facesContext, dataTable);
+
+        writeExcelToResponse(response, generatedExcel, fileName);
+    }    
+
+    private static void addColumnHeaders(HSSFSheet sheet, List columns)
+    {
         HSSFRow rowHeader = sheet.createRow(0);
-        
-        for (int i = 0; i < columns.size(); i++) {
+
+        for (int i = 0; i < columns.size(); i++)
+        {
             UIColumn column = (UIColumn) columns.get(i);
             addColumnValue(rowHeader, column.getHeader(), i);
         }
     }
 
-    private static List getColumns(HtmlDataTable table) {
+    private static List getColumns(HtmlDataTable table)
+    {
         List columns = new ArrayList();
-        for (int i = 0; i < table.getChildCount(); i++) {
+        for (int i = 0; i < table.getChildCount(); i++)
+        {
             UIComponent child = (UIComponent) table.getChildren().get(i);
-            if (child instanceof UIColumn) {
+            if (child instanceof UIColumn)
+            {
                 columns.add(child);
             }
         }
@@ -61,47 +96,70 @@
     }
 
     private static void addColumnValue(HSSFRow rowHeader,
-            UIComponent component, int index) {
+            UIComponent component, int index)
+    {
         HSSFCell cell = rowHeader.createCell((short) index);
         cell.setEncoding(HSSFCell.ENCODING_UTF_16);
-        if (component instanceof ValueHolder) {
+        if (component instanceof ValueHolder)
+        {
             String stringValue = ComponentUtils.getStringValue(FacesContext
                     .getCurrentInstance(), component);
             cell.setCellValue(stringValue);
         }
     }
-    
+
+    private static void addColumnValue(HSSFRow rowHeader, String value,
+            int index)
+    {
+        HSSFCell cell = rowHeader.createCell((short) index);
+        cell.setEncoding(HSSFCell.ENCODING_UTF_16);
+        cell.setCellValue(value);
+    }
+
     /*
      * This method is used for adding the columns values to the HSSFSheet.
      */
     private static void generateTableContent(FacesContext facesContext,
-            HSSFSheet sheet, List columns, HtmlDataTable dataTable) {
-        
+            HSSFSheet sheet, List columns, HtmlDataTable dataTable)
+    {
+
         int numberOfColumns = columns.size();
-        int numberOfRows = dataTable.getRowCount();        
+        int numberOfRows = dataTable.getRowCount();
         int startFrom = 0;
         int currentIndex = 1;
-        int endAt = numberOfRows;         
-    
+        int endAt = numberOfRows;
+
         /* fill the table with the data. */
-        for (int i = startFrom; i < endAt; ++i) {
+        for (int i = startFrom; i < endAt; ++i)
+        {
             dataTable.setRowIndex(i);
             HSSFRow row = sheet.createRow(currentIndex++);
-            for (int j = 0; j < numberOfColumns; ++j) {
+            for (int j = 0; j < numberOfColumns; ++j)
+            {
                 UIColumn column = (UIColumn) columns.get(j);
-                addColumnValue(row, (UIComponent) column.getChildren().get(0),
-                        j);
+                String cellValue = "";
+
+                for (int k = 0; k < column.getChildren().size(); ++k)
+                {
+                    if (column.getChildren().get(k) instanceof ValueHolder)
+                    {
+                        cellValue += ((ValueHolder) column.getChildren().get(k))
+                                .getValue();
+                    }
+                }
+                addColumnValue(row, cellValue, j);
             }
         }
-    }    
-    
+    }
+
     /*
      * This utility method is used for writing the excelModel (generatedExcel)
-     * to the response (response) and uses the (fileName) as the generated file 
+     * to the response (response) and uses the (fileName) as the generated file
      * name.
      */
     private static void writeExcelToResponse(HttpServletResponse response,
-            HSSFWorkbook generatedExcel, String fileName) throws IOException {
+            HSSFWorkbook generatedExcel, String fileName) throws IOException
+    {
 
         /* write the model to the stream */
         response.setContentType("application/vnd.ms-excel");
@@ -114,7 +172,7 @@
 
         generatedExcel.write(response.getOutputStream());
     }
-    
+
     /*
      * This utility method is used for generating the (HSSFWorkbook) 
      * excel table model from the passed (HtmlDataTable).
@@ -123,9 +181,10 @@
      * @return the (HSSFWorkbook) object. 
      */
     private static HSSFWorkbook generateExcelTableModel(
-            FacesContext facesContext, HtmlDataTable dataTable) {
-  
-        HSSFWorkbook workbook = new HSSFWorkbook();       
+            FacesContext facesContext, HtmlDataTable dataTable)
+    {
+
+        HSSFWorkbook workbook = new HSSFWorkbook();
         HSSFSheet sheet = workbook.createSheet(dataTable.getId());
         List columns = getColumns(dataTable);
         int currentRowIndex = dataTable.getRowIndex();
@@ -135,33 +194,5 @@
 
         dataTable.setRowIndex(currentRowIndex);
         return workbook;
-    }    
-
-    /**
-     * This utility method is used for generating the excel 
-     * table to the HttpServletResponse object. 
-     * @param workBook
-     * @param response
-     * @param fileName
-     * @throws IOException
-     */    
-    public static void generateEXCEL(FacesContext facesContext,
-            HttpServletResponse response, String fileName, HtmlDataTable dataTable)
-            throws IOException {
-
-        /*
-         * By default if the fileName is not specified, then use the
-         * table id.
-         */
-        if (fileName == null) 
-        {
-            fileName = dataTable.getId();
-        }
-
-        /* generate the excel model */
-        HSSFWorkbook generatedExcel = ExcelExporterUtil
-                .generateExcelTableModel(facesContext, dataTable);
-
-        writeExcelToResponse(response, generatedExcel, fileName);
     }
 }

Modified: myfaces/commons/trunk/myfaces-commons-components/src/main/java/org/apache/myfaces/commons/exporter/util/PDFExporterUtil.java
URL: http://svn.apache.org/viewvc/myfaces/commons/trunk/myfaces-commons-components/src/main/java/org/apache/myfaces/commons/exporter/util/PDFExporterUtil.java?rev=751188&r1=751187&r2=751188&view=diff
==============================================================================
--- myfaces/commons/trunk/myfaces-commons-components/src/main/java/org/apache/myfaces/commons/exporter/util/PDFExporterUtil.java
(original)
+++ myfaces/commons/trunk/myfaces-commons-components/src/main/java/org/apache/myfaces/commons/exporter/util/PDFExporterUtil.java
Sat Mar  7 03:30:38 2009
@@ -39,14 +39,16 @@
 /**
  * This class is a utility class for serving PDF exporting.
  */
-public class PDFExporterUtil {
+public class PDFExporterUtil
+{
 
     /*
      * This method is used for setting the response headers of the pdf.
      */
     private static void setPDFResponseHeaders(HttpServletResponse response,
             ByteArrayOutputStream byteArrayStream, String fileName)
-            throws IOException {
+            throws IOException
+    {
 
         // setting response headers.
         response.setHeader("Expires", "0");
@@ -72,12 +74,14 @@
     /*
      * This method is used for adding the columns headers to the pdfTable.
      */
-    private static void generateTableHeader(PdfPTable pdfTable, List columns) {
-
-        for (int i = 0; i < columns.size(); i++) {
+    private static void generateTableHeader(PdfPTable pdfTable, List columns)
+    {
+        for (int i = 0; i < columns.size(); i++)
+        {
             UIColumn column = (UIColumn) columns.get(i);
             UIComponent columnHeaderCell = column.getHeader();
-            if (columnHeaderCell instanceof ValueHolder) {
+            if (columnHeaderCell instanceof ValueHolder)
+            {
                 String cellValue = ComponentUtils.getStringValue(FacesContext
                         .getCurrentInstance(), columnHeaderCell);
                 pdfTable.addCell(cellValue);
@@ -89,24 +93,32 @@
      * This method is used for adding the columns values to the pdfTable.
      */
     private static void generateTableContent(FacesContext facesContext,
-            PdfPTable pdfTable, List columns, HtmlDataTable dataTable) {
-
+            PdfPTable pdfTable, List columns, HtmlDataTable dataTable)
+    {
         int numberOfColumns = columns.size();
         int numberOfRows = dataTable.getRowCount();
         int startFrom = 0;
         int endAt = numberOfRows;
-        
+
         /* fill the table with the data. */
-        for (int i = startFrom; i < endAt; ++i) {
+        for (int i = startFrom; i < endAt; ++i)
+        {
             dataTable.setRowIndex(i);
-            for (int j = 0; j < numberOfColumns; ++j) {
-                UIComponent valueHolder = (UIComponent) ((UIColumn) columns
-                        .get(j)).getChildren().get(0);
-                if (valueHolder instanceof ValueHolder) {
-                    String cellValue = ComponentUtils.getStringValue(
-                            FacesContext.getCurrentInstance(), valueHolder);
-                    pdfTable.addCell(cellValue);
+            for (int j = 0; j < numberOfColumns; ++j)
+            {
+                String cellValue = "";
+                UIColumn currentColumn = (UIColumn) columns.get(j);
+
+                for (int k = 0; k < currentColumn.getChildren().size(); ++k)
+                {
+                    if (currentColumn.getChildren().get(k) instanceof ValueHolder)
+                    {
+                        cellValue += ((ValueHolder) currentColumn.getChildren()
+                                .get(k)).getValue();
+                    }
                 }
+
+                pdfTable.addCell(cellValue);
             }
         }
     }
@@ -115,20 +127,21 @@
      * This method is used for creating the PDFTable model.
      */
     public static PdfPTable generatePDFTableModel(FacesContext facesContext,
-            HtmlDataTable dataTable) {
-
+            HtmlDataTable dataTable)
+    {
         int numberOfColumns;
         List columns = null;
         PdfPTable pdfTable = null;
 
-
         /* getting the HTMLDataTable Columns */
         columns = ComponentUtils.getHTMLDataTableColumns(dataTable);
 
-        if (columns.size() == 0) {
+        if (columns.size() == 0)
+        {
             return null;
         }
-        else {
+        else
+        {
             numberOfColumns = columns.size();
         }
 
@@ -137,14 +150,14 @@
 
         generateTableHeader(pdfTable, columns);
 
-        generateTableContent(facesContext, pdfTable, columns,
-                dataTable);
+        generateTableContent(facesContext, pdfTable, columns, dataTable);
 
         return pdfTable;
     }
-    
+
     /**
      * This method is responsible for writing the PDF to the response stream.
+     * 
      * @param facesContext
      * @param response
      * @param fileName
@@ -152,22 +165,21 @@
      */
     public static void generatePDF(FacesContext facesContext,
             HttpServletResponse response, String fileName,
-            HtmlDataTable dataTable) throws Exception {
-
+            HtmlDataTable dataTable) throws Exception
+    {
         int currentRowIndex;
         Document document = new Document();
         ByteArrayOutputStream byteArrayStream = new ByteArrayOutputStream();
         PdfWriter.getInstance(document, byteArrayStream);
         PdfPTable pdfTable = null;
-        
+
         /*
-         * By default if the fileName is not specified, then use the
-         * table id.
+         * By default if the fileName is not specified, then use the table id.
          */
-        if (fileName == null) 
+        if (fileName == null)
         {
             fileName = dataTable.getId();
-        }        
+        }
 
         currentRowIndex = dataTable.getRowIndex();
 



Mime
View raw message