1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.custom.exporter;
20
21 import javax.faces.component.StateHolder;
22 import javax.faces.context.FacesContext;
23 import javax.faces.event.ActionEvent;
24 import javax.faces.event.ActionListener;
25 import javax.servlet.http.HttpServletResponse;
26
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.apache.myfaces.component.html.ext.HtmlDataTable;
30 import org.apache.myfaces.custom.exporter.util.ExcelExporterUtil;
31 import org.apache.myfaces.custom.exporter.util.ExporterConstants;
32 import org.apache.myfaces.custom.exporter.util.PDFExporterUtil;
33 import org.apache.myfaces.custom.util.ComponentUtils;
34
35
36
37
38
39 public class ExporterActionListener implements ActionListener, StateHolder {
40
41 private static final Log log = LogFactory
42 .getLog(ExporterActionListener.class);
43
44 public static final String FILENAME_KEY = "filename";
45
46 public static final String FILE_TYPE_KEY = "fileType";
47
48 public static final String FOR_KEY = "for";
49
50 private String _fileType;
51
52 private String _fileName;
53
54 private String _for;
55
56 public void processAction(ActionEvent event) {
57
58 FacesContext facesContext = FacesContext.getCurrentInstance();
59 Object response = facesContext.getExternalContext().getResponse();
60
61 if (!(response instanceof HttpServletResponse)) {
62 log.error("ExporteActionListener requires servlet");
63 }
64 else {
65 try {
66
67
68 HtmlDataTable dataTable = (HtmlDataTable) ComponentUtils
69 .findComponentById(facesContext, facesContext
70 .getViewRoot(), _for);
71
72 if (!(dataTable instanceof HtmlDataTable))
73 {
74 throw new RuntimeException(
75 "exporterActionListener for attribute should contain a "
76 + "dataTable component id");
77 }
78
79
80 if (ExporterConstants.EXCEL_FILE_TYPE
81 .equalsIgnoreCase(_fileType))
82 {
83
84
85
86
87 Object contextResponse = facesContext.getExternalContext()
88 .getResponse();
89
90 if (contextResponse instanceof HttpServletResponse)
91 {
92 ExcelExporterUtil.generateEXCEL(facesContext,
93 (HttpServletResponse) contextResponse,
94 _fileName, dataTable);
95 }
96
97 }
98 else
99 {
100
101
102
103
104 HttpServletResponse httpResponse = (HttpServletResponse) facesContext
105 .getExternalContext().getResponse();
106
107
108 PDFExporterUtil.generatePDF(facesContext, httpResponse,
109 _fileName, dataTable);
110 }
111
112
113 facesContext.getApplication().getStateManager()
114 .saveSerializedView(facesContext);
115 facesContext.responseComplete();
116 }
117 catch (Exception exception)
118 {
119 throw new RuntimeException(exception);
120 }
121
122 }
123
124 facesContext.responseComplete();
125 }
126
127 public String getFilename() {
128 return _fileName;
129 }
130
131 public void setFilename(String _filename) {
132 this._fileName = _filename;
133 }
134
135 public String getFileType() {
136 return _fileType;
137 }
138
139 public void setFileType(String type) {
140 _fileType = type;
141 }
142
143 public String getFor() {
144 return _for;
145 }
146
147 public void setFor(String _for) {
148 this._for = _for;
149 }
150
151 public void restoreState(FacesContext context, Object state) {
152 String values[] = (String[]) state;
153
154 _for = values[0];
155 _fileName = values[1];
156 _fileType = values[2];
157 }
158
159 public Object saveState(FacesContext context) {
160 String values[] = new String[3];
161
162 values[0] = _for;
163 values[1] = _fileName;
164 values[2] = _fileType;
165 return ((String[]) values);
166 }
167
168 public boolean isTransient() {
169 return false;
170 }
171
172 public void setTransient(boolean newTransientValue) {
173 if (newTransientValue == true) {
174 throw new IllegalArgumentException();
175 }
176 }
177 }