KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dinamica > GetBlob


1 package dinamica;
2
3 /**
4  * Generic Blob reader.<br>
5  * This Transaction publishes a Recordset with the
6  * information required by the BlobOutput module
7  * to print a blob column's content, whether it is an image or
8  * a document. It is meant to be a generic mechanism to send
9  * 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: &gt;attach&lt;true&gt;/attach&lt; 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
33     /* (non-Javadoc)
34      * @see dinamica.GenericTransaction#service(dinamica.Recordset)
35      */

36     public int service(Recordset inputParams) throws Throwable JavaDoc
37     {
38         
39         //reuse superclass code
40
int rc = super.service(inputParams);
41         
42         //create recordset structure
43
Recordset rs = new Recordset();
44         rs.append("sql", java.sql.Types.VARCHAR);
45         rs.append("filename", java.sql.Types.VARCHAR);
46         rs.append("format", java.sql.Types.VARCHAR);
47         rs.addNew();
48         
49         //get image metadata
50
String JavaDoc sql1 = getSQL(getResource("query-info.sql"), inputParams);
51         Recordset rsInfo = getDb().get(sql1);
52         rsInfo.first();
53         
54         //create sql to retrieve blob
55
String JavaDoc sql2 = getSQL(getResource("query-blob.sql"), inputParams);
56         
57         //fill recordset fields
58
rs.setValue("sql", sql2);
59         
60         //content-type of the blob
61
rs.setValue("format", rsInfo.getValue("content_type"));
62
63         //set filename only if attach=true
64
String JavaDoc attach = getConfig().getConfigValue("attach");
65         if (attach!=null && attach.equals("true"))
66         {
67             if (rsInfo.containsField("filename"))
68                 rs.setValue("filename", rsInfo.getValue("filename"));
69             else
70                 throw new Throwable JavaDoc("Cannot attach BLOB output if [filename] column is not present in the [query-info.sql] template.");
71         }
72
73         publish("blobinfo", rs);
74
75         return rc;
76
77     }
78
79 }
80
Popular Tags