1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
|
package com.aspose.cells.demos;
import java.io.*;
import java.net.URL;
import java.sql.*;
import com.aspose.cells.Workbook;
import com.aspose.cells.WorkbookDesigner;
import com.aspose.cells.License;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.faces.context.FacesContext;
import java.text.SimpleDateFormat;
import com.icesoft.faces.context.ByteArrayResource;
import com.icesoft.faces.context.Resource;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.Locale;
import java.lang.reflect.*;
public abstract class Demo
{
private static final String DB_URL_P1 = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=";
private static final String DB_URL_P2 = "/WEB-INF/Database/Northwind.mdb";
protected String TEMPLATE_FILE_PATH_PART = "/WEB-INF/Designer/Northwind.xls";
protected Connection connection = null;
public Demo() {
FacesContext context = FacesContext.getCurrentInstance();
HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
try {
TEMPLATE_FILE_PATH_PART = getFileName(request, TEMPLATE_FILE_PATH_PART);
initLicense(new License());
}catch(Exception e) {
e.printStackTrace();
}
}
private Connection createConnection()
{
try
{
if (null!=connection) return connection;
FacesContext context = FacesContext.getCurrentInstance();
HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
String dbName = request.getRealPath(DB_URL_P2).replace("\\", "/");
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String connectionString = "jdbc:odbc:DRIVER={Microsoft Access Driver (*.mdb)};DBQ=" + dbName;
connection = DriverManager.getConnection(connectionString, "", "");
} catch (Exception e) {
System.out.println("openDBConnection: " + e.toString());
}
return connection;
}
protected static String getDate(Integer year, Integer month, Integer day) {
return "#" + day.toString() + "/" + month.toString() + "/" + year.toString() + "#";
}
protected static String getQuarter(String fieldName) {
return " FORMAT(" + fieldName + ", 'yyyy/Q') ";
}
protected static String getBool(boolean flag) {
return flag ? "Yes" : "No";
}
protected static String getDistinct() {
return "DISTINCTROW";
}
protected Statement createStatement()
{
return createStatement(true);
}
protected Statement createStatement(boolean Scrollable)
{
try
{
Connection conn = createConnection();
Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
return stmt;
} catch (Exception e) {
System.out.println("createStatement: " + e.toString());
}
return null;
}
protected PreparedStatement prepareStatement(String stmt)
{
try
{
Connection conn = createConnection();
PreparedStatement pstmt = conn.prepareStatement(stmt
,ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
return pstmt;
} catch (Exception e) {
System.out.println("prepareStatement: " + e.toString());
}
return null;
}
protected void closeConnection() throws SQLException
{
if (null!=connection) {
connection.close();
connection = null;
}
}
protected String getFileName(HttpServletRequest request, String absoluteName)
throws java.io.FileNotFoundException
{
URL url = getClass().getClassLoader().getResource(absoluteName);
if (null==url && null!=request)
{
String path =
request.getRealPath(absoluteName).replace("\\", "/");
return path;
}
String path = new File(url.getPath()).getAbsolutePath();
return path;
}
protected InputStream getFile(HttpServletRequest request, String absoluteName)
throws java.io.FileNotFoundException
{
InputStream is = getClass().getClassLoader().getResourceAsStream(absoluteName);
if (null==is && null!=request)
{
String path =
request.getRealPath(absoluteName).replace("\\", "/");
System.err.println("Failed to load " + absoluteName);
System.err.println("Loading file from " + path);
is = new FileInputStream(path);
}
return is;
}
public abstract void execute() throws Exception;
protected final String FILE_NAME = "result.xls";
public Resource getGeneratedfile() {
byte[] res = getResult();
if (null==res || res.length<1) {
return null;
}
return new ByteArrayResource(res);
}
private String fileName = FILE_NAME;
public String getFilename() {
return fileName;
}
private String respType = "application/vnd.ms-excel";
public String getResptype() {
return respType;
}
public void setResptype(String v) {
respType = v;
}
public void setFilename(String v) {
this.fileName = v;
}
public byte[] getResult()
{
return resp;
}
protected void setResponse(String fName, ByteArrayOutputStream out) throws IOException {
setFilename(fName);
resp = out.toByteArray();
out.close();
}
protected void setResponse(String fName,byte[] out) throws IOException {
setFilename(fName);
resp = out;
}
protected byte[] resp;
protected void setResponse(String fileName, Workbook w) throws IOException
{
ByteArrayOutputStream out = new ByteArrayOutputStream();
w.save(out);
setResponse(fileName, out);
}
protected void setResponse(String fileName, WorkbookDesigner w) throws IOException
{
ByteArrayOutputStream out = new ByteArrayOutputStream();
w.save(out);
setResponse(fileName, out);
}
public static void initLicense(Object obj) {
try {
Class myclass = Class.forName("com.aspose.demos.Common");
if (null==myclass) {
System.out.println("initLicense not found");
return ;
}
Method[] methods = myclass.getMethods();
Object object = myclass.newInstance();
Class partypes[] = new Class[1];
partypes[0] = Object.class;
Method meth = myclass.getMethod("initLicense", partypes);
if (null!=meth) {
Object arglist[] = new Object[1];
arglist[0] = obj;
meth.invoke(object, arglist);
}
} catch (ClassNotFoundException cnfe) {
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
|