1 5 package com.tdsecurities.itracker.converter; 6 7 import java.io.BufferedOutputStream ; 8 import java.io.File ; 9 import java.io.FileOutputStream ; 10 import java.io.FilenameFilter ; 11 import java.io.InputStream ; 12 import java.sql.Blob ; 13 import java.sql.PreparedStatement ; 14 import java.sql.ResultSet ; 15 import java.sql.Timestamp ; 16 import java.util.Date ; 17 18 import org.apache.log4j.Logger; 19 20 import com.tdsecurities.itracker.common.DataSourceManager; 21 22 26 public class AttachmentConverter extends BasicConverter 27 { 28 private static final String ATTACHMENTS_DIR = "/jboss-3.2.3/itracker/attachments/"; 29 private static final String 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 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 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 getSourceQuery() 37 { 38 return SOURCE_SQL; 39 } 40 41 protected String getTargetQuery() 42 { 43 return TARGET_SQL; 44 } 45 46 protected String getIdStoreName() 47 { 48 return "issueattachment"; 49 } 50 51 protected String getLastIdQuery() 52 { 53 return LAST_ID_SQL; 54 } 55 56 protected int prepareTargetStatement(PreparedStatement targetStmt, ResultSet rs) throws Exception 57 { 58 long now = (new Date ()).getTime(); 59 int col = 1; 60 String 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 (now)); 73 targetStmt.setTimestamp(col++, new Timestamp (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 filename, Blob data) throws Exception 80 { 81 BufferedOutputStream out = new BufferedOutputStream (new FileOutputStream (ATTACHMENTS_DIR + filename)); 82 InputStream 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 f = new File (ATTACHMENTS_DIR); 115 File [] attachments = f.listFiles( new FilenameFilter () 116 { 117 public boolean accept(File dir, String 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 |