KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > async > RegisterFile


1 package async;
2
3 import dinamica.*;
4 import java.io.File JavaDoc;
5
6 /**
7  * RegisterFile<br>
8  * Creates a record in the task table
9  * and triggers the background process
10  * that will process the uploaded file.<br>
11  * NOTE: Thisclass requires JDBC transactions activated in config.xml!!!
12  * <br><br>
13  * Creation date: 17/12/2004<br>
14  * http://www.martincordova.com<br>
15  * @author mcordova - dinamica@martincordova.com
16  */

17 public class RegisterFile extends GenericTransaction
18 {
19
20     /* (non-Javadoc)
21      * @see dinamica.GenericTransaction#service(dinamica.Recordset)
22      */

23     public int service(Recordset inputParams) throws Throwable JavaDoc
24     {
25         super.service(inputParams);
26                 
27         Db db = getDb();
28         
29         // insert task record
30
String JavaDoc sql1 = getSQL(getResource("insert.sql"), inputParams);
31         db.exec(sql1);
32         
33         // get generated PK
34
String JavaDoc sql2 = getSQL(getResource("query.sql"), inputParams);
35         Recordset rs = db.get(sql2);
36         rs.next();
37         String JavaDoc taskID = rs.getString("id");
38         
39         //get temp file uploaded
40
java.io.File JavaDoc f = new File JavaDoc(inputParams.getString("file"));
41         
42         //define new temp name
43
String JavaDoc newFileName = getContext().getAttribute("javax.servlet.context.tempdir").toString()
44         + java.io.File.separator + rs.getString("id") + ".dat";
45         
46         //rename file to preserv it (servlet engine will remove it after request)
47
f.renameTo(new File JavaDoc(newFileName));
48
49         //save session attributes
50
getSession().setAttribute("task.id", taskID);
51         
52         //start background process...
53
Thread JavaDoc t = new Thread JavaDoc(new BatchProcess(taskID, newFileName, this.getDataSource(), getResource("p-insert.sql")), "BatchProcess" + taskID);
54         t.start();
55         
56         return 0;
57     }
58
59 }
60
Popular Tags