001: /*
002: Copyright (C) 2003-2006 Know Gate S.L. All rights reserved.
003: C/Oņa, 107 1š2 28050 Madrid (Spain)
004:
005: Redistribution and use in source and binary forms, with or without
006: modification, are permitted provided that the following conditions
007: are met:
008:
009: 1. Redistributions of source code must retain the above copyright
010: notice, this list of conditions and the following disclaimer.
011:
012: 2. The end-user documentation included with the redistribution,
013: if any, must include the following acknowledgment:
014: "This product includes software parts from hipergate
015: (http://www.hipergate.org/)."
016: Alternately, this acknowledgment may appear in the software itself,
017: if and wherever such third-party acknowledgments normally appear.
018:
019: 3. The name hipergate must not be used to endorse or promote products
020: derived from this software without prior written permission.
021: Products derived from this software may not be called hipergate,
022: nor may hipergate appear in their name, without prior written
023: permission.
024:
025: This library is distributed in the hope that it will be useful,
026: but WITHOUT ANY WARRANTY; without even the implied warranty of
027: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
028:
029: You should have received a copy of hipergate License with this code;
030: if not, visit http://www.hipergate.org or mail to info@hipergate.org
031: */
032:
033: package com.knowgate.projtrack;
034:
035: import java.io.File;
036: import java.io.FileInputStream;
037: import java.io.FileNotFoundException;
038: import java.io.IOException;
039:
040: import java.sql.SQLException;
041: import java.sql.PreparedStatement;
042: import java.sql.ResultSet;
043:
044: import com.knowgate.jdc.JDCConnection;
045: import com.knowgate.debug.DebugFile;
046: import com.knowgate.dataobjs.DB;
047:
048: /**
049: * <p>Bug Attachment</p>
050: * This class represents objects stored as blobs at k_bugs_attach table
051: * @author Sergio Montoro Ten
052: * @version 3.0
053: */
054: public class BugAttachment {
055:
056: private int iSize;
057: private String sFileName;
058: private String sBugId;
059:
060: // ---------------------------------------------------------------------------
061:
062: public BugAttachment() {
063: iSize = 0;
064: sFileName = sBugId = null;
065: }
066:
067: // ---------------------------------------------------------------------------
068:
069: public BugAttachment(String sIdBug, String sNameFile, int iSizeFile) {
070: iSize = iSizeFile;
071: sBugId = sIdBug;
072: sFileName = sNameFile;
073: }
074:
075: // ---------------------------------------------------------------------------
076:
077: public int size() {
078: return iSize;
079: }
080:
081: // ---------------------------------------------------------------------------
082:
083: public String fileName() {
084: return sFileName;
085: }
086:
087: // ---------------------------------------------------------------------------
088:
089: public String bugId() {
090: return sBugId;
091: }
092:
093: // ---------------------------------------------------------------------------
094:
095: public byte[] getBytes(JDCConnection oConn) throws SQLException,
096: IOException {
097: byte[] aBytes;
098: PreparedStatement oStmt = oConn.prepareStatement("SELECT "
099: + DB.len_file + "," + DB.bin_file + " FROM "
100: + DB.k_bugs_attach + " WHERE " + DB.gu_bug + "=? AND "
101: + DB.tx_file + "=?", ResultSet.TYPE_FORWARD_ONLY,
102: ResultSet.CONCUR_READ_ONLY);
103: oStmt.setString(1, sBugId);
104: oStmt.setString(2, sFileName);
105: ResultSet oRSet = oStmt.executeQuery();
106: if (oRSet.next()) {
107: aBytes = new byte[oRSet.getInt(1)];
108: oRSet.getBinaryStream(2).read(aBytes);
109: } else {
110: aBytes = null;
111: }
112: oRSet.close();
113: oStmt.close();
114: return aBytes;
115: } // getBytes
116:
117: // ---------------------------------------------------------------------------
118:
119: /**
120: * Remove attachment from k_bugs_attach table
121: * @param oConn JDCConnection
122: * @throws SQLException
123: */
124: public void delete(JDCConnection oConn) throws SQLException {
125: BugAttachment.delete(oConn, sBugId, sFileName);
126: }
127:
128: // ---------------------------------------------------------------------------
129:
130: /**
131: * Remove attachment from k_bugs_attach table
132: * @param oConn JDCConnection
133: * @param sBugId String
134: * @param sFileName String
135: * @throws SQLException
136: */
137: public static void delete(JDCConnection oConn, String sBugId,
138: String sFileName) throws SQLException {
139:
140: if (DebugFile.trace) {
141: DebugFile.writeln("Begin Bug.delete([JDCConnection],"
142: + sBugId + "," + sFileName + ")");
143: DebugFile.incIdent();
144: }
145: PreparedStatement oDlte = oConn.prepareStatement("DELETE FROM "
146: + DB.k_bugs_attach + " WHERE " + DB.gu_bug + "=? AND "
147: + DB.tx_file + "=?");
148: oDlte.setString(1, sBugId);
149: oDlte.setString(2, sFileName);
150: oDlte.executeUpdate();
151: oDlte.close();
152:
153: if (DebugFile.trace) {
154: DebugFile.decIdent();
155: DebugFile.writeln("End Bug.delete()");
156: }
157: } // delete
158:
159: // ---------------------------------------------------------------------------
160:
161: /**
162: * Insert attachment into k_bugs_attach table
163: * @param oConn JDCConnection
164: * @param sBugId String GUID of Bug to which attachment belongs
165: * @param sFilePath String Full path to local file
166: * @throws SQLException
167: * @throws FileNotFoundException
168: * @throws IOException
169: * @throws NullPointerException
170: */
171: public static void createFromFile(JDCConnection oConn,
172: String sBugId, String sFilePath) throws SQLException,
173: FileNotFoundException, IOException, NullPointerException {
174:
175: if (DebugFile.trace) {
176: DebugFile
177: .writeln("Begin BugAttachment.createFromFile([JDCConnection],"
178: + sBugId + "," + sFilePath + ")");
179: DebugFile.incIdent();
180: }
181:
182: File oFile = new File(sFilePath);
183:
184: if (oFile == null) {
185: if (DebugFile.trace)
186: DebugFile.decIdent();
187: throw new FileNotFoundException(sFilePath);
188: }
189: if (!oFile.exists()) {
190: if (DebugFile.trace)
191: DebugFile.decIdent();
192: throw new FileNotFoundException(sFilePath);
193: }
194:
195: PreparedStatement oStmt = oConn.prepareStatement("INSERT INTO "
196: + DB.k_bugs_attach + "(" + DB.gu_bug + "," + DB.tx_file
197: + "," + DB.len_file + "," + DB.bin_file
198: + ") VALUES (?,?,?,?)");
199:
200: int iFileLen = new Long(oFile.length()).intValue();
201:
202: FileInputStream oFileStream = new FileInputStream(oFile);
203: oStmt.setString(1, sBugId);
204: oStmt.setString(2, oFile.getName());
205: oStmt.setInt(3, iFileLen);
206: oStmt.setBinaryStream(4, oFileStream, iFileLen);
207: oStmt.executeUpdate();
208: oStmt.close();
209: oFileStream.close();
210: oFileStream = null;
211:
212: if (DebugFile.trace) {
213: DebugFile.decIdent();
214: DebugFile.writeln("End BugAttachment.createFromFile()");
215: }
216: } // createFromFile
217:
218: // **********************************************************
219: // Constantes Publicas
220:
221: public static final short ClassId = 84;
222:
223: }
|