01: package dinamica;
02:
03: /**
04: * Generic Blob reader.<br>
05: * This Transaction publishes a Recordset with the
06: * information required by the BlobOutput module
07: * to print a blob column's content, whether it is an image or
08: * a document. It is meant to be a generic mechanism to send
09: * blobs as response to requests without requiring special coding.<br>
10: * The whole mechanism depends on a certain table structure, that at least
11: * must contain these fields: content_type, image_data and filename (required only if blob will be sent as attachment).
12: * A primary key to retrieve the blob is also required, usually an ID (integer).
13: * The Action that uses this Transaction must include two query files: query-info.sql and query-blob.sql.
14: * They must have those names. Also include a custom element in config.xml: >attach<true>/attach< Set it to "true" if you want
15: * the browser to open a "Save as" dialog box, otherwise it should be "false".<br>
16: * You may include your own validation rules before this Transaction's execute() method
17: * is invoked. The BlobOutput module will read the blob column by itself, which means that it will
18: * open its own database connection using the default application datasource (context parameter in web.xml).<br>
19: * The Recodset must be published with the name "blobinfo". The fields of this recordset are:
20: * format, filename (null if attach=false), size and sql.<br><br>
21: * <b>NOTE:</b> A validator.xml file with the field that represents the blob ID is required. Set validator=true in config.xml.
22: *
23: * <br>
24: * Creation date: 6/jan/2004<br>
25: * Last Update: 20/may/2004<br>
26: * (c) 2003 Martin Cordova<br>
27: * This code is released under the LGPL license<br>
28: * @author Martin Cordova
29: * */
30: public class GetBlob extends GenericTransaction {
31:
32: /* (non-Javadoc)
33: * @see dinamica.GenericTransaction#service(dinamica.Recordset)
34: */
35: public int service(Recordset inputParams) throws Throwable {
36:
37: //reuse superclass code
38: int rc = super .service(inputParams);
39:
40: //create recordset structure
41: Recordset rs = new Recordset();
42: rs.append("sql", java.sql.Types.VARCHAR);
43: rs.append("filename", java.sql.Types.VARCHAR);
44: rs.append("format", java.sql.Types.VARCHAR);
45: rs.addNew();
46:
47: //get image metadata
48: String sql1 = getSQL(getResource("query-info.sql"), inputParams);
49: Recordset rsInfo = getDb().get(sql1);
50: rsInfo.first();
51:
52: //create sql to retrieve blob
53: String sql2 = getSQL(getResource("query-blob.sql"), inputParams);
54:
55: //fill recordset fields
56: rs.setValue("sql", sql2);
57:
58: //content-type of the blob
59: rs.setValue("format", rsInfo.getValue("content_type"));
60:
61: //set filename only if attach=true
62: String attach = getConfig().getConfigValue("attach");
63: if (attach != null && attach.equals("true")) {
64: if (rsInfo.containsField("filename"))
65: rs.setValue("filename", rsInfo.getValue("filename"));
66: else
67: throw new Throwable(
68: "Cannot attach BLOB output if [filename] column is not present in the [query-info.sql] template.");
69: }
70:
71: publish("blobinfo", rs);
72:
73: return rc;
74:
75: }
76:
77: }
|