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 package org.imixs.workflow.jaxrs.plugins;
29
30 import java.io.ByteArrayOutputStream;
31 import java.io.FileOutputStream;
32 import java.io.StringWriter;
33 import java.util.Collection;
34 import java.util.StringTokenizer;
35 import java.util.Vector;
36 import java.util.logging.Logger;
37
38 import javax.naming.Context;
39 import javax.naming.InitialContext;
40 import javax.naming.NamingException;
41 import javax.xml.bind.JAXBContext;
42 import javax.xml.bind.Marshaller;
43
44 import org.imixs.workflow.ItemCollection;
45 import org.imixs.workflow.Plugin;
46 import org.imixs.workflow.WorkflowContext;
47 import org.imixs.workflow.jee.ejb.EntityService;
48 import org.imixs.workflow.jee.ejb.ReportService;
49 import org.imixs.workflow.xml.EntityCollection;
50 import org.imixs.workflow.xml.XMLItemCollectionAdapter;
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73 public class ReportPlugin extends org.imixs.workflow.plugins.AbstractPlugin {
74
75 private EntityService entityServiceEJB = null;
76 private ReportService reportServiceEJB = null;
77
78 private String reportName = null;
79 private String reportFilePath = null;
80 private String reportTarget = null;
81 private String sEQL;
82 private String sXSL;
83 private String sContentType;
84 private String sEncoding;
85 private ItemCollection blobWorkitem = null;
86
87 private static Logger logger = Logger.getLogger("org.imixs.workflow");
88
89 public void init(WorkflowContext actx) throws Exception {
90 super.init(actx);
91 }
92
93 public EntityService getEntityService() throws Exception {
94 if (entityServiceEJB == null)
95 lookupEJBs();
96
97 return entityServiceEJB;
98 }
99
100 public ReportService getReportService() throws Exception {
101 if (reportServiceEJB == null)
102 lookupEJBs();
103
104 return reportServiceEJB;
105 }
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122 public int run(ItemCollection adocumentContext,
123 ItemCollection adocumentActivity) throws Exception {
124 try {
125
126 reportName = adocumentActivity.getItemValueString("txtReportName");
127 reportFilePath = adocumentActivity
128 .getItemValueString("txtReportFilePath");
129 if ("".equals(reportFilePath))
130 reportFilePath = reportName;
131
132
133 reportFilePath = this.replaceDynamicValues(reportFilePath,
134 adocumentContext);
135
136 reportTarget = adocumentActivity
137 .getItemValueString("txtReportTarget");
138
139 if ("".equals(reportName))
140 return PLUGIN_OK;
141
142
143 if (!reportName.endsWith(".ixr"))
144 reportName = reportName + ".ixr";
145
146 ItemCollection itemCol = getReportService().getReport(reportName);
147
148
149 sEQL = itemCol.getItemValueString("txtquery");
150
151
152 String sParamString = adocumentActivity
153 .getItemValueString("txtReportParams");
154
155 sParamString = this.replaceDynamicValues(sParamString,
156 adocumentContext);
157
158
159 sEQL = computeEQLParams(sEQL, sParamString);
160 logger.info("ReportPlugin JPQL=" + sEQL);
161 sXSL = itemCol.getItemValueString("txtXSL").trim();
162
163 if ("".equals(sXSL))
164 return PLUGIN_OK;
165
166 sContentType = itemCol.getItemValueString("txtcontenttype");
167 if ("".equals(sContentType))
168 sContentType = "text/html";
169
170 sEncoding = itemCol.getItemValueString("txtencoding");
171
172
173 if ("".equals(sEncoding))
174 sEncoding = "UTF-8";
175
176
177 Collection<ItemCollection> col = getEntityService().findAllEntities(sEQL, 0, -1);
178
179
180
181 String sUnqiueID = adocumentContext.getItemValueString("$uniqueID");
182 if (!"".equals(sUnqiueID)) {
183 Collection<ItemCollection> colNew=new Vector<ItemCollection>();
184 for (ItemCollection aitemCol : col) {
185 if (sUnqiueID.equals(aitemCol
186 .getItemValueString("$uniqueid"))) {
187
188
189 ItemCollection itemTemp = new ItemCollection(
190 adocumentContext.getAllItems());
191 itemTemp.replaceItemValue("$temp", "true");
192 colNew.add(itemTemp);
193 logger.info(" ReportPlugin - relaced deprecated workitem from collection" );
194 } else
195 colNew.add(aitemCol);
196 }
197 col=colNew;
198 } else {
199
200
201 ItemCollection itemTemp = new ItemCollection(
202 adocumentContext.getAllItems());
203 itemTemp.replaceItemValue("$temp", "true");
204 col.add(itemTemp);
205 logger.info(" ReportPlugin - add current workitem into collection" );
206
207 }
208
209
210 EntityCollection xmlCol = XMLItemCollectionAdapter
211 .putCollection(col);
212
213 StringWriter xmlWriter = new StringWriter();
214
215 JAXBContext context = JAXBContext
216 .newInstance(EntityCollection.class);
217
218 Marshaller m = context.createMarshaller();
219 m.setProperty("jaxb.encoding", sEncoding);
220 m.marshal(xmlCol, xmlWriter);
221
222
223 ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
224
225 try {
226 if ("application/pdf".equals(sContentType.toLowerCase()))
227 org.imixs.workflow.jaxrs.ReportRestService
228 .fopTranformation(xmlWriter.toString(), sXSL,
229 sEncoding, outputStream);
230 else
231 org.imixs.workflow.jaxrs.ReportRestService
232 .xslTranformation(xmlWriter.toString(), sXSL,
233 sEncoding, outputStream);
234 } finally {
235 outputStream.close();
236 }
237
238
239 if ("0".equals(reportTarget)) {
240
241 adocumentContext.addFile(outputStream.toByteArray(),
242 reportFilePath, sContentType);
243 }
244
245 if ("1".equals(reportTarget)) {
246 load(adocumentContext);
247
248 blobWorkitem.addFile(outputStream.toByteArray(),
249 reportFilePath, sContentType);
250 save(adocumentContext);
251 }
252
253 if ("2".equals(reportTarget)) {
254 FileOutputStream fos = null;
255 try {
256 fos = new FileOutputStream(reportFilePath);
257 fos.write(outputStream.toByteArray());
258 fos.flush();
259 } finally {
260 if (fos != null) {
261 fos.close();
262 }
263 }
264 }
265
266 return Plugin.PLUGIN_OK;
267 } catch (Exception e) {
268 e.printStackTrace();
269 }
270
271 return Plugin.PLUGIN_ERROR;
272 }
273
274 public void close(int status) throws Exception {
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 private void lookupEJBs() throws Exception {
303
304 if (entityServiceEJB == null || reportServiceEJB == null) {
305 String jndiName = null;
306
307 InitialContext ictx = new InitialContext();
308 Context ctx = (Context) ictx.lookup("java:comp/env");
309 jndiName = "ejb/EntityServiceBean";
310 entityServiceEJB = (org.imixs.workflow.jee.ejb.EntityService) ctx
311 .lookup(jndiName);
312
313 jndiName = "ejb/ReportServiceBean";
314 reportServiceEJB = (ReportService) ctx.lookup(jndiName);
315
316 }
317
318 if (entityServiceEJB == null)
319 throw new Exception(
320 "Error - unable to load EJB ejb/EntityServiceBean....");
321
322 if (reportServiceEJB == null)
323 throw new Exception(
324 "Error - unable to load EJB ejb/ReportServiceBean....");
325
326 }
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342 private String computeEQLParams(String aQuery, String sParamString) {
343
344
345 if (sParamString.startsWith("?") || sParamString.startsWith("&"))
346 sParamString = sParamString.substring(1);
347
348
349 StringTokenizer tokenizer = new StringTokenizer(sParamString, "&");
350
351 while (tokenizer.hasMoreTokens()) {
352
353 String aToken = tokenizer.nextToken();
354
355 if (aToken.indexOf('=') == -1)
356 continue;
357
358 String sKeyName = aToken.substring(0, aToken.indexOf('='));
359 String sParamValue = aToken.substring(aToken.indexOf('=') + 1);
360
361 if (aQuery.indexOf("?" + sKeyName) > -1)
362 aQuery = aQuery.replace("?" + sKeyName, sParamValue);
363
364 }
365 return aQuery;
366 }
367
368
369
370
371
372
373
374
375
376
377 private void load(ItemCollection itemCol) throws Exception {
378
379 String sUniqueID = itemCol.getItemValueString("$uniqueid");
380
381
382 String sQuery = " SELECT lobitem FROM Entity as lobitem"
383 + " join lobitem.textItems as t1"
384 + " join lobitem.textItems as t2"
385 + " WHERE t1.itemName = 'type'"
386 + " AND t1.itemValue = 'workitemlob'"
387 + " AND t2.itemName = '$uniqueidref'" + " AND t2.itemValue = '"
388 + sUniqueID + "'";
389
390 Collection<ItemCollection> itemcol = getEntityService()
391 .findAllEntities(sQuery, 0, 1);
392 if (itemcol != null && itemcol.size() > 0) {
393
394 blobWorkitem = itemcol.iterator().next();
395 } else {
396 blobWorkitem = new ItemCollection();
397 blobWorkitem.replaceItemValue("$uniqueidRef", sUniqueID);
398 blobWorkitem.replaceItemValue("type", "workitemlob");
399 }
400
401 }
402
403
404
405
406
407
408
409
410
411
412
413 private void save(ItemCollection parentWorkitem) throws Exception {
414
415 if (blobWorkitem != null && parentWorkitem != null) {
416
417
418 Vector vAccess = parentWorkitem.getItemValue("$ReadAccess");
419 blobWorkitem.replaceItemValue("$ReadAccess", vAccess);
420
421 vAccess = parentWorkitem.getItemValue("$WriteAccess");
422 blobWorkitem.replaceItemValue("$WriteAccess", vAccess);
423
424 blobWorkitem.replaceItemValue("$uniqueidRef",
425 parentWorkitem.getItemValueString("$uniqueID"));
426 blobWorkitem.replaceItemValue("type", "workitemlob");
427
428 blobWorkitem = getEntityService().save(blobWorkitem);
429
430 }
431 }
432
433 }