KickJava   Java API By Example, From Geeks To Geeks.

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


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>Duty Attachment</p>
50  * This class represents objects stored as blobs at k_duties_attach table
51  * @author Sergio Montoro Ten
52  * @version 3.0
53  */

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

62   /**
63    * Default constructor
64    */

65   public DutyAttachment() {
66     iSize = 0;
67     sFileName = sDutyId = null;
68   }
69
70   // ---------------------------------------------------------------------------
71

72   /**
73    * Constructor
74    * @param sIdDuty String GUID of Duty
75    * @param sNameFile String File Name
76    * @param iSizeFile int File size in bytes
77    */

78   public DutyAttachment(String JavaDoc sIdDuty, String JavaDoc sNameFile, int iSizeFile) {
79     iSize = iSizeFile;
80     sDutyId = sIdDuty;
81     sFileName = sNameFile;
82   }
83
84   // ---------------------------------------------------------------------------
85

86   /**
87    *
88    * @return int File size in bytes
89    */

90   public int size() { return iSize; }
91
92   // ---------------------------------------------------------------------------
93

94   /**
95    *
96    * @return String File Name
97    */

98   public String JavaDoc fileName() { return sFileName; }
99
100   // ---------------------------------------------------------------------------
101

102   /**
103    *
104    * @return String Duty GUID
105    */

106   public String JavaDoc dutyId() { return sDutyId; }
107
108   // ---------------------------------------------------------------------------
109

110   /**
111    * Get stored attachment as byte array
112    * @param oConn JDCConnection
113    * @return byte[]
114    * @throws SQLException
115    * @throws IOException
116    */

117   public byte[] getBytes(JDCConnection oConn)
118     throws SQLException JavaDoc, IOException JavaDoc {
119     byte[] aBytes;
120     PreparedStatement JavaDoc 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 JavaDoc 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   } // getBytes
136

137   // ---------------------------------------------------------------------------
138

139   /**
140    * Remove attachment from k_duties_attach table
141    * @param oConn JDCConnection
142    * @throws SQLException
143    */

144   public void delete (JDCConnection oConn) throws SQLException JavaDoc {
145     DutyAttachment.delete(oConn, sDutyId, sFileName);
146   }
147
148   // ---------------------------------------------------------------------------
149

150   /**
151    * Remove attachment from k_dutys_attach table
152    * @param oConn JDCConnection
153    * @param sDutyId String
154    * @param sFileName String
155    * @throws SQLException
156    */

157   public static void delete (JDCConnection oConn, String JavaDoc sDutyId, String JavaDoc sFileName)
158     throws SQLException JavaDoc {
159
160     if (DebugFile.trace) {
161       DebugFile.writeln("Begin Duty.delete([JDCConnection],"+sDutyId+","+sFileName+")");
162       DebugFile.incIdent();
163     }
164     PreparedStatement JavaDoc 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   } // delete
175

176   // ---------------------------------------------------------------------------
177

178   /**
179    * Insert attachment into k_duties_attach table
180    * @param oConn JDCConnection
181    * @param sDutyId String GUID of Duty to which attachment belongs
182    * @param sFilePath String Full path to local file
183    * @throws SQLException
184    * @throws FileNotFoundException
185    * @throws IOException
186    * @throws NullPointerException
187    */

188   public static void createFromFile(JDCConnection oConn, String JavaDoc sDutyId, String JavaDoc sFilePath)
189     throws SQLException JavaDoc, FileNotFoundException JavaDoc, IOException JavaDoc, NullPointerException JavaDoc {
190
191     if (DebugFile.trace) {
192       DebugFile.writeln("Begin DutyAttachment.createFromFile([JDCConnection],"+sDutyId+","+sFilePath+")");
193       DebugFile.incIdent();
194     }
195
196     File JavaDoc oFile = new File JavaDoc(sFilePath);
197
198     if (oFile==null) {
199       if (DebugFile.trace) DebugFile.decIdent();
200       throw new FileNotFoundException JavaDoc(sFilePath);
201     }
202     if (!oFile.exists()) {
203       if (DebugFile.trace) DebugFile.decIdent();
204       throw new FileNotFoundException JavaDoc(sFilePath);
205     }
206
207     PreparedStatement JavaDoc 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 JavaDoc(oFile.length()).intValue();
210
211     FileInputStream JavaDoc oFileStream = new FileInputStream JavaDoc (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   } // createFromFile
226

227   // **********************************************************
228
// Constantes Publicas
229

230   public static final short ClassId = 85;
231
232 }
233
Popular Tags