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 DutyAttachment { 55 56 private int iSize; 57 private String sFileName; 58 private String sDutyId; 59 60 62 65 public DutyAttachment() { 66 iSize = 0; 67 sFileName = sDutyId = null; 68 } 69 70 72 78 public DutyAttachment(String sIdDuty, String sNameFile, int iSizeFile) { 79 iSize = iSizeFile; 80 sDutyId = sIdDuty; 81 sFileName = sNameFile; 82 } 83 84 86 90 public int size() { return iSize; } 91 92 94 98 public String fileName() { return sFileName; } 99 100 102 106 public String dutyId() { return sDutyId; } 107 108 110 117 public byte[] getBytes(JDCConnection oConn) 118 throws SQLException , IOException { 119 byte[] aBytes; 120 PreparedStatement oStmt = oConn.prepareStatement( 121 "SELECT "+DB.len_file+","+DB.bin_file+" FROM "+DB.k_duties_attach+ " WHERE "+DB.gu_duty+"=? AND "+DB.tx_file+"=?", 122 ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); 123 oStmt.setString(1, sDutyId); 124 oStmt.setString(2, sFileName); 125 ResultSet oRSet = oStmt.executeQuery(); 126 if (oRSet.next()) { 127 aBytes = new byte[oRSet.getInt(1)]; 128 oRSet.getBinaryStream(2).read(aBytes); 129 } else { 130 aBytes = null; 131 } 132 oRSet.close(); 133 oStmt.close(); 134 return aBytes; 135 } 137 139 144 public void delete (JDCConnection oConn) throws SQLException { 145 DutyAttachment.delete(oConn, sDutyId, sFileName); 146 } 147 148 150 157 public static void delete (JDCConnection oConn, String sDutyId, String sFileName) 158 throws SQLException { 159 160 if (DebugFile.trace) { 161 DebugFile.writeln("Begin Duty.delete([JDCConnection],"+sDutyId+","+sFileName+")"); 162 DebugFile.incIdent(); 163 } 164 PreparedStatement oDlte = oConn.prepareStatement("DELETE FROM " + DB.k_duties_attach + " WHERE " + DB.gu_duty + "=? AND " + DB.tx_file + "=?"); 165 oDlte.setString(1, sDutyId); 166 oDlte.setString(2, sFileName); 167 oDlte.executeUpdate(); 168 oDlte.close(); 169 170 if (DebugFile.trace) { 171 DebugFile.decIdent(); 172 DebugFile.writeln("End Duty.delete()"); 173 } 174 } 176 178 188 public static void createFromFile(JDCConnection oConn, String sDutyId, String sFilePath) 189 throws SQLException , FileNotFoundException , IOException , NullPointerException { 190 191 if (DebugFile.trace) { 192 DebugFile.writeln("Begin DutyAttachment.createFromFile([JDCConnection],"+sDutyId+","+sFilePath+")"); 193 DebugFile.incIdent(); 194 } 195 196 File oFile = new File (sFilePath); 197 198 if (oFile==null) { 199 if (DebugFile.trace) DebugFile.decIdent(); 200 throw new FileNotFoundException (sFilePath); 201 } 202 if (!oFile.exists()) { 203 if (DebugFile.trace) DebugFile.decIdent(); 204 throw new FileNotFoundException (sFilePath); 205 } 206 207 PreparedStatement oStmt = oConn.prepareStatement("INSERT INTO " + DB.k_duties_attach + "(" + DB.gu_duty + "," + DB.tx_file + "," + DB.len_file + "," + DB.bin_file + ") VALUES (?,?,?,?)"); 208 209 int iFileLen = new Long (oFile.length()).intValue(); 210 211 FileInputStream oFileStream = new FileInputStream (oFile); 212 oStmt.setString(1, sDutyId); 213 oStmt.setString(2, oFile.getName()); 214 oStmt.setInt(3, iFileLen); 215 oStmt.setBinaryStream(4, oFileStream, iFileLen); 216 oStmt.executeUpdate(); 217 oStmt.close(); 218 oFileStream.close(); 219 oFileStream = null; 220 221 if (DebugFile.trace) { 222 DebugFile.decIdent(); 223 DebugFile.writeln("End DutyAttachment.createFromFile()"); 224 } 225 } 227 230 public static final short ClassId = 85; 231 232 } 233 | Popular Tags |