View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.commons.exporter;
20  
21  import javax.faces.component.StateHolder;
22  import javax.faces.component.html.HtmlDataTable;
23  import javax.faces.context.FacesContext;
24  import javax.faces.event.ActionEvent;
25  import javax.faces.event.ActionListener;
26  import javax.servlet.http.HttpServletResponse;
27  
28  import org.apache.commons.logging.Log;
29  import org.apache.commons.logging.LogFactory;
30  import org.apache.myfaces.commons.exporter.util.ComponentUtils;
31  import org.apache.myfaces.commons.exporter.util.ExcelExporterUtil;
32  import org.apache.myfaces.commons.exporter.util.ExporterConstants;
33  import org.apache.myfaces.commons.exporter.util.PDFExporterUtil;
34  
35  /**
36   *
37   * This class is acting as the Exporter ActionListener.
38   */
39  public class ExporterActionListener implements ActionListener, StateHolder
40  {
41  
42      private static final Log log = LogFactory
43              .getLog(ExporterActionListener.class);
44  
45      public static final String FILENAME_KEY = "filename";
46  
47      public static final String FILE_TYPE_KEY = "fileType";
48  
49      public static final String FOR_KEY = "for";
50  
51      private String _fileType;
52  
53      private String _fileName;
54  
55      private String _for;
56  
57      public ExporterActionListener() 
58      {
59      }
60  
61      public ExporterActionListener(String _for, String _fileType,
62              String _fileName)
63      {
64          this._fileType = _fileType;
65          this._fileName = _fileName;
66          this._for = _for;
67      }
68  
69      public void processAction(ActionEvent event)
70      {
71          FacesContext facesContext = FacesContext.getCurrentInstance();
72          Object response = facesContext.getExternalContext().getResponse();
73  
74          if (!(response instanceof HttpServletResponse))
75          {
76              log.error("ExporteActionListener requires servlet");
77          }
78          else
79          {
80              try
81              {
82  
83                  /* get the source dataTable component */
84                  HtmlDataTable dataTable = (HtmlDataTable) ComponentUtils
85                          .findComponentById(facesContext, facesContext
86                                  .getViewRoot(), _for);
87  
88                  if (!(dataTable instanceof HtmlDataTable))
89                  {
90                      throw new RuntimeException(
91                              "exporterActionListener for attribute should contain a "
92                                      + "dataTable component id");
93                  }
94  
95                  if (ExporterConstants.EXCEL_FILE_TYPE
96                          .equalsIgnoreCase(_fileType))
97                  {
98  
99                      /*
100                      * Excel case. Generate the XLS to the response stream.
101                      */
102                     Object contextResponse = facesContext.getExternalContext()
103                             .getResponse();
104 
105                     if (contextResponse instanceof HttpServletResponse)
106                     {
107                         ExcelExporterUtil.generateEXCEL(facesContext,
108                                 (HttpServletResponse) contextResponse,
109                                 _fileName, dataTable);
110                     }
111 
112                 }
113                 else
114                 {
115 
116                     /*
117                      * PDF case. Generate the PDF to the response stream.
118                      */
119                     HttpServletResponse httpResponse = (HttpServletResponse) facesContext
120                             .getExternalContext().getResponse();
121 
122                     PDFExporterUtil.generatePDF(facesContext, httpResponse,
123                             _fileName, dataTable);
124                 }
125 
126                 /* save the seralized view and complete the response. */
127                 facesContext.getApplication().getStateManager()
128                         .saveSerializedView(facesContext);
129                 facesContext.responseComplete();
130             }
131             catch (Exception exception)
132             {
133                 throw new RuntimeException(exception);
134             }
135 
136         }
137 
138         facesContext.responseComplete();
139     }
140 
141     public String getFilename()
142     {
143         return _fileName;
144     }
145 
146     public void setFilename(String _filename)
147     {
148         this._fileName = _filename;
149     }
150 
151     public String getFileType()
152     {
153         return _fileType;
154     }
155 
156     public void setFileType(String type)
157     {
158         _fileType = type;
159     }
160 
161     public String getFor()
162     {
163         return _for;
164     }
165 
166     public void setFor(String _for)
167     {
168         this._for = _for;
169     }
170 
171     public void restoreState(FacesContext context, Object state)
172     {
173         String values[] = (String[]) state;
174 
175         _for = values[0];
176         _fileName = values[1];
177         _fileType = values[2];
178     }
179 
180     public Object saveState(FacesContext context)
181     {
182         String values[] = new String[3];
183 
184         values[0] = _for;
185         values[1] = _fileName;
186         values[2] = _fileType;
187         return ((String[]) values);
188     }
189 
190     public boolean isTransient()
191     {
192         return false;
193     }
194 
195     public void setTransient(boolean newTransientValue)
196     {
197         if (newTransientValue == true)
198         {
199             throw new IllegalArgumentException();
200         }
201     }
202 }