forked from agmoyano/node-jasper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
311 lines (284 loc) · 8.51 KB
/
index.js
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
var java=null,
fs = require('fs'),
path = require('path'),
extend = require('extend'),
util = require('util'),
temp = require('temp'),
async = require('async');
var defaults = {reports:{}, drivers:{}, conns:{}};
function walk(dir, done) {
var results = [];
fs.readdir(dir, function(err, list) {
if (err) return done(err);
var pending = list.length;
if (!pending) return done(null, results);
list.forEach(function(file) {
file = dir + '/' + file;
fs.stat(file, function(err, stat) {
if (stat && stat.isDirectory()) {
walk(file, function(err, res) {
results = results.concat(res);
if (!--pending) done(null, results);
});
} else {
results.push(file);
if (!--pending) done(null, results);
}
});
});
});
};
/*
* options: {
* path: , //Path to jasperreports-x.x.x-project directory
* reports: {
* // Report Definition
* "name": {
* jasper: , //Path to jasper file,
* jrxml: , //Path to jrxml file,
* conn: , //Connection name, definition object or false (if false defaultConn won't apply)
* }
* },
* drivers: {
* // Driver Definition
* "name": {
path: , //Path to jdbc driver jar
class: , //Class name of the driver (what you would tipically place in "Class.forName()" in java)
type: //Type of database (mysql, postgres)
}
* },
* conns: {
* // Connection Definition
* "name": {
* host: , //Database hostname or IP
* port: , //Database Port
* dbname: , //Database Name
* user: , //User Name
* pass: , //User Password
* jdbc: , //jdbc connection string
* driver: //name or definition of the driver for this conn
* }
* },
* defaultConn: , //Default Connection name
java: //Array of java options, for example ["-Djava.awt.headless=true"]
* }
*/
function jasper(options) {
if(options.javaInstance) {
java = options.javaInstance
} else {
java = require('java')
}
this.java = java;
if(options.java) {
if(util.isArray(options.java)) {
options.java.forEach(function(javaOption) {
java.options.push(javaOption);
});
}
if(typeof options.java == 'string') {
java.options.push(options.java);
}
}
var self = this;
self.parentPath = path.dirname(module.parent.filename);
var jrPath = path.resolve(self.parentPath, options.path||'.');
async.auto({
jrJars: function(cb) {
if(fs.statSync(path.join(jrPath, 'lib')).isDirectory() && fs.statSync(path.join(jrPath, 'dist')).isDirectory()) {
async.parallel([
function(cb) {
walk(path.join(jrPath, 'dist'), function(err, results) {
cb(err, results);
});
},
function(cb) {
walk(path.join(jrPath, 'lib'), function(err, results) {
cb(err, results);
});
}
], function(err, results) {
if(err) return cb(err);
var r = results.shift();
results.forEach(function(item) {
r = r.concat(item);
});
cb(null, r);
})
} else {
walk(jrPath, function(err, results) {
cb(err, results);
});
}
},
dirverJars: function(cb) {
var results = [];
if(options.drivers) {
for(var i in options.drivers) {
results.push(path.resolve(self.parentPath, options.drivers[i].path));
}
}
cb(null, results);
},
loadJars: ['jrJars', 'dirverJars', function(cb, jars) {
jars.jrJars.concat(jars.dirverJars).forEach(function(file) {
if(path.extname(file) == '.jar') {
java.classpath.push(file)
}
});
cb();
}],
loadClass: ['loadJars', function(cb) {
var cl = java.callStaticMethodSync("java.lang.ClassLoader","getSystemClassLoader")
for(var i in options.drivers) {
cl.loadClassSync(options.drivers[i].class).newInstanceSync();
}
cb();
}],
imports: ['loadClass', function(cb) {
self.dm = java.import('java.sql.DriverManager');
self.jreds = java.import('net.sf.jasperreports.engine.JREmptyDataSource');
self.jrjsonef = java.import('net.sf.jasperreports.engine.data.JsonDataSource');
self.jbais = java.import('java.io.ByteArrayInputStream');
self.jcm = java.import('net.sf.jasperreports.engine.JasperCompileManager');
self.hm = java.import('java.util.HashMap');
self.jfm = java.import('net.sf.jasperreports.engine.JasperFillManager');
self.jem = java.import('net.sf.jasperreports.engine.JasperExportManager');
cb();
}]
}, function() {
if(self.ready) {
self.ready();
}
});
delete options.path;
extend(self, defaults, options);
}
jasper.prototype.ready = function(f) {
var self = this;
self.ready = f;
};
/*
* name = Report Name
* def = Report Definition
*/
jasper.prototype.add = function(name, def) {
this.reports[name] = def;
}
jasper.prototype.pdf = function(report) {
return this.export(report, 'pdf');
}
/*
* report can be of any of the following types:
* _ A string that represents report's name. No data is supplied.. defaultConn will be applied to get data with reports internal query.
* _ An object that represents report's definition. No data is supplied.. defaultConn will be applied to get data with reports internal query.
* _ An object that represents reports, data and properties to override for this specific method call.
*
* {
* report: , //name, definition or an array with any combination of both
* data: {}, //Data to be applied to the report. If there is an array of reports, data will be applied to each.
* override: {} //properties of report to override for this specific method call.
* }
* _ An array with any combination of the three posibilities described before.
* _ A function returning any combination of the four posibilities described before.
*/
jasper.prototype.export = function(report, type) {
var self = this;
if(!type) return;
type = type.charAt(0).toUpperCase()+type.toLowerCase().slice(1);
var processReport = function(report) {
if(typeof report == 'string') {
return [extend({},self.reports[report])];
} else if(util.isArray(report)) {
var ret = [];
report.forEach(function(i) {
ret = ret.concat(processReport(i));
});
return ret;
} else if(typeof report == 'function') {
return processReport(report());
} else if(typeof report == 'object') {
if(report.data||report.override) {
var reps = processReport(report.report);
return reps.map(function(i) {
if(report.override) {
extend(i, report.override);
}
i.data = report.data;
i.dataset = report.dataset;
return i;
})
} else {
return [report];
}
}
};
var processConn = function(conn, item) {
if(conn == 'in_memory_json') {
var jsonString = JSON.stringify(item.dataset);
var byteArray = java.newArray('byte', jsonString.split('').map(function(c, i) {
return java.newByte(jsonString.charCodeAt(i));
}));
return new self.jrjsonef(new self.jbais(byteArray));
}else if(typeof conn == 'string') {
conn = self.conns[conn];
} else if (typeof conn == 'function') {
conn = conn();
} else if(conn !== false && self.defaultConn) {
conn = self.conns[self.defaultConn];
}
if(conn) {
if(typeof conn.driver == 'string') {
conn.driver = self.drivers[conn.driver];
}
var connStr = conn.jdbc?conn.jdbc:'jdbc:'+conn.driver.type+'://'+conn.host+':'+conn.port+'/'+conn.dbname;
return self.dm.getConnectionSync(connStr, conn.user, conn.pass);
} else {
return new self.jreds();
}
};
var reports = processReport(report);
var prints = [];
reports.forEach(function(item) {
if(!item.jasper && item.jrxml) {
var name = path.basename(item.jrxml, '.jrxml');
var file = '/tmp/'+name+'.jasper';
var compiler = java.callStaticMethodSync(
"net.sf.jasperreports.engine.JasperCompileManager",
"compileReportToFile",
path.resolve(self.parentPath,item.jrxml), file
);
item.jasper = file;
}
if(item.jasper) {
var data = null;
if(item.data) {
data = new self.hm();
for(var j in item.data) {
data.putSync(j, item.data[j])
}
}
var conn = processConn(item.conn, item);
var p = self.jfm.fillReportSync(path.resolve(self.parentPath,item.jasper), data, conn);
prints.push(p);
}
});
if(prints.length) {
var master = prints.shift();
prints.forEach(function(p) {
var s = p.getPagesSync().sizeSync();
for(var j = 0; j < s; j++) {
master.addPageSync(p.getPagesSync().getSync(j));
}
});
var tempName = temp.path({suffix: '.pdf'});
self.jem['exportReportTo'+type+'FileSync'](master, tempName);
var exp = fs.readFileSync(tempName);
fs.unlinkSync(tempName);
return exp;
}
return '';
}
module.exports = function(options) {
return new jasper(options)
};