KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tdsecurities > itracker > converter > AttachmentConverter


1 /**
2  * Copyright (c) 2003 TD Securities
3  * Created on Dec 31, 2003
4  */

5 package com.tdsecurities.itracker.converter;
6
7 import java.io.BufferedOutputStream JavaDoc;
8 import java.io.File JavaDoc;
9 import java.io.FileOutputStream JavaDoc;
10 import java.io.FilenameFilter JavaDoc;
11 import java.io.InputStream JavaDoc;
12 import java.sql.Blob JavaDoc;
13 import java.sql.PreparedStatement JavaDoc;
14 import java.sql.ResultSet JavaDoc;
15 import java.sql.Timestamp JavaDoc;
16 import java.util.Date JavaDoc;
17
18 import org.apache.log4j.Logger;
19
20 import com.tdsecurities.itracker.common.DataSourceManager;
21
22 /**
23  * @author pardec2
24  * @version $Id$
25  */

26 public class AttachmentConverter extends BasicConverter
27 {
28     private static final String JavaDoc ATTACHMENTS_DIR = "/jboss-3.2.3/itracker/attachments/";
29     private static final String JavaDoc SOURCE_SQL = "select a.bug_id, a.file_name, b.countorder, c.group_id, c.submitted_by, b.attachment from bug_attachment a, bug_split_attachment b, bug c where a.attachment_id = b.attachment_id and a.bug_id = c.bug_id";
30     private static final String JavaDoc TARGET_SQL = "insert into issueattachmentbean (id,orig_file_name,type,file_name,description,file_size,create_date,last_modified,issue_id,user_id) values (?,?,?,?,?,?,?,?,?,?)";;
31     private static final String JavaDoc LAST_ID_SQL = "select max(id) from issueattachmentbean";
32
33     private int attachmentId = 1;
34     private Logger log = Logger.getLogger(AttachmentConverter.class);
35     
36     protected String JavaDoc getSourceQuery()
37     {
38         return SOURCE_SQL;
39     }
40     
41     protected String JavaDoc getTargetQuery()
42     {
43         return TARGET_SQL;
44     }
45
46     protected String JavaDoc getIdStoreName()
47     {
48         return "issueattachment";
49     }
50     
51     protected String JavaDoc getLastIdQuery()
52     {
53         return LAST_ID_SQL;
54     }
55
56     protected int prepareTargetStatement(PreparedStatement JavaDoc targetStmt, ResultSet JavaDoc rs) throws Exception JavaDoc
57     {
58         long now = (new Date JavaDoc()).getTime();
59         int col = 1;
60         String JavaDoc filename = "proj" + rs.getInt("group_id") +
61                           "_issue" + rs.getInt("bug_id") +
62                           "_attachment" + (rs.getInt("countorder") + 1);
63
64         long size = writeFile( filename, rs.getBlob("attachment"));
65         
66         targetStmt.setInt(col++, attachmentId++);
67         targetStmt.setString(col++, rs.getString("file_name"));
68         targetStmt.setString(col++, "application/octet-stream");
69         targetStmt.setString(col++, filename);
70         targetStmt.setString(col++, rs.getString("file_name"));
71         targetStmt.setLong(col++, size);
72         targetStmt.setTimestamp(col++, new Timestamp JavaDoc(now));
73         targetStmt.setTimestamp(col++, new Timestamp JavaDoc(now));
74         targetStmt.setInt(col++, rs.getInt("bug_id"));
75         targetStmt.setInt(col++, rs.getInt("submitted_by"));
76         return BasicConverter.OK;
77     }
78
79     protected long writeFile(String JavaDoc filename, Blob JavaDoc data) throws Exception JavaDoc
80     {
81         BufferedOutputStream JavaDoc out = new BufferedOutputStream JavaDoc(new FileOutputStream JavaDoc(ATTACHMENTS_DIR + filename));
82         InputStream JavaDoc in = null;
83         long size;
84
85         try
86         {
87             size = data.length();
88             if (size > 0)
89             {
90                 byte[] buff = new byte[1024];
91                 in = data.getBinaryStream();
92                 int i;
93                 while ((i = in.read(buff)) > 0)
94                 {
95                     out.write(buff, 0, i);
96                 }
97                 out.flush();
98             }
99         }
100         finally
101         {
102             if( in != null)
103                 in.close();
104             if( out != null)
105                 out.close();
106         }
107         return size;
108     }
109
110     protected void preConversionProcessing()
111     {
112         log.info( "Converting attachments...");
113         executeUpdate(DataSourceManager.ITRACKER, "delete from issueattachmentbean");
114         File JavaDoc f = new File JavaDoc(ATTACHMENTS_DIR);
115         File JavaDoc[] attachments = f.listFiles( new FilenameFilter JavaDoc()
116         {
117             public boolean accept(File JavaDoc dir, String JavaDoc name)
118             {
119                 if(name.startsWith("proj"))
120                     return true;
121                 return false;
122             }
123         });
124         for (int i = 0; i < attachments.length; i++)
125         {
126             attachments[i].delete();
127         }
128     }
129 }
130
Popular Tags