01: package dinamica;
02:
03: import java.io.*;
04:
05: /**
06: * Generic transaction class that saves uploaded file
07: * into blob column in database table via JDBC prepared statements.
08: * The SQL will be preprocessed to set all
09: * the required static values, the BLOB data will
10: * be sent to the server using a prepared statement.<br>
11: * Plase consult the Dinamica BLOB How-to (cat-blob.pdf)
12: * to learn about required table structure and available
13: * blob management facilities. This class assumes that you are
14: * using the generic Upload component provided with Dinamica
15: * since v2.0.9 (look into /dinamica/extra folder).<br>
16: * NOTE: the file control in the HTML form must be named "file",
17: * future versions of this class may use config.xml to read the
18: * name of the parameter.
19: *
20: * @author Martin Cordova
21: * */
22: public class SaveBlob extends GenericTableManager {
23:
24: /* (non-Javadoc)
25: * @see dinamica.GenericTransaction#service(dinamica.Recordset)
26: */
27: public int service(Recordset inputParams) throws Throwable {
28: //reuse superclass code
29: int rc = super .service(inputParams);
30:
31: //patch 2007-10-09 - archivo nulo sera considerado un error
32: if (inputParams.isNull("file.filename"))
33: throw new Throwable(
34: "¡Por favor indique una ruta válida de archivo!");
35:
36: //get temp file
37: String path = (String) inputParams.getValue("file");
38: File f = new File(path);
39:
40: //get file size
41: Integer size = new Integer((int) f.length());
42: inputParams.setValue("image_size", size);
43:
44: if (size.intValue() == 0)
45: throw new Throwable("¡No se puede cargar un archivo vacío!");
46:
47: //prepare sql template (replace static values)
48: String sql = getResource("query.sql");
49: sql = getSQL(sql, inputParams);
50:
51: //get db object and save blob using prepared statement
52: Db db = getDb();
53: db.saveBlob(sql, path);
54:
55: //delete temp file
56: f.delete();
57:
58: return rc;
59:
60: }
61:
62: }
|