KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > knowgate > projtrack > BugAttachment


1 /*
2   Copyright (C) 2003-2006 Know Gate S.L. All rights reserved.
3                            C/Oņa, 107 1š2 28050 Madrid (Spain)
4
5   Redistribution and use in source and binary forms, with or without
6   modification, are permitted provided that the following conditions
7   are met:
8
9   1. Redistributions of source code must retain the above copyright
10      notice, this list of conditions and the following disclaimer.
11
12   2. The end-user documentation included with the redistribution,
13      if any, must include the following acknowledgment:
14      "This product includes software parts from hipergate
15      (http://www.hipergate.org/)."
16      Alternately, this acknowledgment may appear in the software itself,
17      if and wherever such third-party acknowledgments normally appear.
18
19   3. The name hipergate must not be used to endorse or promote products
20      derived from this software without prior written permission.
21      Products derived from this software may not be called hipergate,
22      nor may hipergate appear in their name, without prior written
23      permission.
24
25   This library is distributed in the hope that it will be useful,
26   but WITHOUT ANY WARRANTY; without even the implied warranty of
27   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
28
29   You should have received a copy of hipergate License with this code;
30   if not, visit http://www.hipergate.org or mail to info@hipergate.org
31 */

32
33 package com.knowgate.projtrack;
34
35 import java.io.File JavaDoc;
36 import java.io.FileInputStream JavaDoc;
37 import java.io.FileNotFoundException JavaDoc;
38 import java.io.IOException JavaDoc;
39
40 import java.sql.SQLException JavaDoc;
41 import java.sql.PreparedStatement JavaDoc;
42 import java.sql.ResultSet JavaDoc;
43
44 import com.knowgate.jdc.JDCConnection;
45 import com.knowgate.debug.DebugFile;
46 import com.knowgate.dataobjs.DB;
47
48 /**
49  * <p>Bug Attachment</p>
50  * This class represents objects stored as blobs at k_bugs_attach table
51  * @author Sergio Montoro Ten
52  * @version 3.0
53  */

54 public class BugAttachment {
55
56   private int iSize;
57   private String JavaDoc sFileName;
58   private String JavaDoc sBugId;
59
60   // ---------------------------------------------------------------------------
61

62   public BugAttachment() {
63     iSize = 0;
64     sFileName = sBugId = null;
65   }
66
67   // ---------------------------------------------------------------------------
68

69   public BugAttachment(String JavaDoc sIdBug, String JavaDoc sNameFile, int iSizeFile) {
70     iSize = iSizeFile;
71     sBugId = sIdBug;
72     sFileName = sNameFile;
73   }
74
75   // ---------------------------------------------------------------------------
76

77   public int size() { return iSize; }
78
79   // ---------------------------------------------------------------------------
80

81   public String JavaDoc fileName() { return sFileName; }
82
83   // ---------------------------------------------------------------------------
84

85   public String JavaDoc bugId() { return sBugId; }
86
87   // ---------------------------------------------------------------------------
88

89   public byte[] getBytes(JDCConnection oConn)
90     throws SQLException JavaDoc, IOException JavaDoc {
91     byte[] aBytes;
92     PreparedStatement JavaDoc 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 JavaDoc 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   } // getBytes
108

109   // ---------------------------------------------------------------------------
110

111   /**
112    * Remove attachment from k_bugs_attach table
113    * @param oConn JDCConnection
114    * @throws SQLException
115    */

116   public void delete (JDCConnection oConn) throws SQLException JavaDoc {
117     BugAttachment.delete(oConn, sBugId, sFileName);
118   }
119
120   // ---------------------------------------------------------------------------
121

122   /**
123    * Remove attachment from k_bugs_attach table
124    * @param oConn JDCConnection
125    * @param sBugId String
126    * @param sFileName String
127    * @throws SQLException
128    */

129   public static void delete (JDCConnection oConn, String JavaDoc sBugId, String JavaDoc sFileName)
130     throws SQLException JavaDoc {
131
132     if (DebugFile.trace) {
133       DebugFile.writeln("Begin Bug.delete([JDCConnection],"+sBugId+","+sFileName+")");
134       DebugFile.incIdent();
135     }
136     PreparedStatement JavaDoc 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   } // delete
147

148   // ---------------------------------------------------------------------------
149

150   /**
151    * Insert attachment into k_bugs_attach table
152    * @param oConn JDCConnection
153    * @param sBugId String GUID of Bug to which attachment belongs
154    * @param sFilePath String Full path to local file
155    * @throws SQLException
156    * @throws FileNotFoundException
157    * @throws IOException
158    * @throws NullPointerException
159    */

160   public static void createFromFile(JDCConnection oConn, String JavaDoc sBugId, String JavaDoc sFilePath)
161     throws SQLException JavaDoc, FileNotFoundException JavaDoc, IOException JavaDoc, NullPointerException JavaDoc {
162
163     if (DebugFile.trace) {
164       DebugFile.writeln("Begin BugAttachment.createFromFile([JDCConnection],"+sBugId+","+sFilePath+")");
165       DebugFile.incIdent();
166     }
167
168     File JavaDoc oFile = new File JavaDoc(sFilePath);
169
170     if (oFile==null) {
171       if (DebugFile.trace) DebugFile.decIdent();
172       throw new FileNotFoundException JavaDoc(sFilePath);
173     }
174     if (!oFile.exists()) {
175       if (DebugFile.trace) DebugFile.decIdent();
176       throw new FileNotFoundException JavaDoc(sFilePath);
177     }
178
179     PreparedStatement JavaDoc 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 JavaDoc(oFile.length()).intValue();
182
183     FileInputStream JavaDoc oFileStream = new FileInputStream JavaDoc (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   } // createFromFile
198

199   // **********************************************************
200
// Constantes Publicas
201

202   public static final short ClassId = 84;
203
204 }
205
Popular Tags