1 32 33 package com.knowgate.projtrack; 34 35 import java.io.File ; 36 import java.io.FileInputStream ; 37 import java.io.FileNotFoundException ; 38 import java.io.IOException ; 39 40 import java.sql.SQLException ; 41 import java.sql.PreparedStatement ; 42 import java.sql.ResultSet ; 43 44 import com.knowgate.jdc.JDCConnection; 45 import com.knowgate.debug.DebugFile; 46 import com.knowgate.dataobjs.DB; 47 48 54 public class BugAttachment { 55 56 private int iSize; 57 private String sFileName; 58 private String sBugId; 59 60 62 public BugAttachment() { 63 iSize = 0; 64 sFileName = sBugId = null; 65 } 66 67 69 public BugAttachment(String sIdBug, String sNameFile, int iSizeFile) { 70 iSize = iSizeFile; 71 sBugId = sIdBug; 72 sFileName = sNameFile; 73 } 74 75 77 public int size() { return iSize; } 78 79 81 public String fileName() { return sFileName; } 82 83 85 public String bugId() { return sBugId; } 86 87 89 public byte[] getBytes(JDCConnection oConn) 90 throws SQLException , IOException { 91 byte[] aBytes; 92 PreparedStatement oStmt = oConn.prepareStatement( 93 "SELECT "+DB.len_file+","+DB.bin_file+" FROM "+DB.k_bugs_attach+ " WHERE "+DB.gu_bug+"=? AND "+DB.tx_file+"=?", 94 ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); 95 oStmt.setString(1, sBugId); 96 oStmt.setString(2, sFileName); 97 ResultSet oRSet = oStmt.executeQuery(); 98 if (oRSet.next()) { 99 aBytes = new byte[oRSet.getInt(1)]; 100 oRSet.getBinaryStream(2).read(aBytes); 101 } else { 102 aBytes = null; 103 } 104 oRSet.close(); 105 oStmt.close(); 106 return aBytes; 107 } 109 111 116 public void delete (JDCConnection oConn) throws SQLException { 117 BugAttachment.delete(oConn, sBugId, sFileName); 118 } 119 120 122 129 public static void delete (JDCConnection oConn, String sBugId, String sFileName) 130 throws SQLException { 131 132 if (DebugFile.trace) { 133 DebugFile.writeln("Begin Bug.delete([JDCConnection],"+sBugId+","+sFileName+")"); 134 DebugFile.incIdent(); 135 } 136 PreparedStatement oDlte = oConn.prepareStatement("DELETE FROM " + DB.k_bugs_attach + " WHERE " + DB.gu_bug + "=? AND " + DB.tx_file + "=?"); 137 oDlte.setString(1, sBugId); 138 oDlte.setString(2, sFileName); 139 oDlte.executeUpdate(); 140 oDlte.close(); 141 142 if (DebugFile.trace) { 143 DebugFile.decIdent(); 144 DebugFile.writeln("End Bug.delete()"); 145 } 146 } 148 150 160 public static void createFromFile(JDCConnection oConn, String sBugId, String sFilePath) 161 throws SQLException , FileNotFoundException , IOException , NullPointerException { 162 163 if (DebugFile.trace) { 164 DebugFile.writeln("Begin BugAttachment.createFromFile([JDCConnection],"+sBugId+","+sFilePath+")"); 165 DebugFile.incIdent(); 166 } 167 168 File oFile = new File (sFilePath); 169 170 if (oFile==null) { 171 if (DebugFile.trace) DebugFile.decIdent(); 172 throw new FileNotFoundException (sFilePath); 173 } 174 if (!oFile.exists()) { 175 if (DebugFile.trace) DebugFile.decIdent(); 176 throw new FileNotFoundException (sFilePath); 177 } 178 179 PreparedStatement oStmt = oConn.prepareStatement("INSERT INTO " + DB.k_bugs_attach + "(" + DB.gu_bug + "," + DB.tx_file + "," + DB.len_file + "," + DB.bin_file + ") VALUES (?,?,?,?)"); 180 181 int iFileLen = new Long (oFile.length()).intValue(); 182 183 FileInputStream oFileStream = new FileInputStream (oFile); 184 oStmt.setString(1, sBugId); 185 oStmt.setString(2, oFile.getName()); 186 oStmt.setInt(3, iFileLen); 187 oStmt.setBinaryStream(4, oFileStream, iFileLen); 188 oStmt.executeUpdate(); 189 oStmt.close(); 190 oFileStream.close(); 191 oFileStream = null; 192 193 if (DebugFile.trace) { 194 DebugFile.decIdent(); 195 DebugFile.writeln("End BugAttachment.createFromFile()"); 196 } 197 } 199 202 public static final short ClassId = 84; 203 204 } 205 | Popular Tags |