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>Duty Attachment</p>
050: * This class represents objects stored as blobs at k_duties_attach table
051: * @author Sergio Montoro Ten
052: * @version 3.0
053: */
054: public class DutyAttachment {
055:
056: private int iSize;
057: private String sFileName;
058: private String sDutyId;
059:
060: // ---------------------------------------------------------------------------
061:
062: /**
063: * Default constructor
064: */
065: public DutyAttachment() {
066: iSize = 0;
067: sFileName = sDutyId = null;
068: }
069:
070: // ---------------------------------------------------------------------------
071:
072: /**
073: * Constructor
074: * @param sIdDuty String GUID of Duty
075: * @param sNameFile String File Name
076: * @param iSizeFile int File size in bytes
077: */
078: public DutyAttachment(String sIdDuty, String sNameFile,
079: int iSizeFile) {
080: iSize = iSizeFile;
081: sDutyId = sIdDuty;
082: sFileName = sNameFile;
083: }
084:
085: // ---------------------------------------------------------------------------
086:
087: /**
088: *
089: * @return int File size in bytes
090: */
091: public int size() {
092: return iSize;
093: }
094:
095: // ---------------------------------------------------------------------------
096:
097: /**
098: *
099: * @return String File Name
100: */
101: public String fileName() {
102: return sFileName;
103: }
104:
105: // ---------------------------------------------------------------------------
106:
107: /**
108: *
109: * @return String Duty GUID
110: */
111: public String dutyId() {
112: return sDutyId;
113: }
114:
115: // ---------------------------------------------------------------------------
116:
117: /**
118: * Get stored attachment as byte array
119: * @param oConn JDCConnection
120: * @return byte[]
121: * @throws SQLException
122: * @throws IOException
123: */
124: public byte[] getBytes(JDCConnection oConn) throws SQLException,
125: IOException {
126: byte[] aBytes;
127: PreparedStatement oStmt = oConn
128: .prepareStatement("SELECT " + DB.len_file + ","
129: + DB.bin_file + " FROM " + DB.k_duties_attach
130: + " WHERE " + DB.gu_duty + "=? AND "
131: + DB.tx_file + "=?",
132: ResultSet.TYPE_FORWARD_ONLY,
133: ResultSet.CONCUR_READ_ONLY);
134: oStmt.setString(1, sDutyId);
135: oStmt.setString(2, sFileName);
136: ResultSet oRSet = oStmt.executeQuery();
137: if (oRSet.next()) {
138: aBytes = new byte[oRSet.getInt(1)];
139: oRSet.getBinaryStream(2).read(aBytes);
140: } else {
141: aBytes = null;
142: }
143: oRSet.close();
144: oStmt.close();
145: return aBytes;
146: } // getBytes
147:
148: // ---------------------------------------------------------------------------
149:
150: /**
151: * Remove attachment from k_duties_attach table
152: * @param oConn JDCConnection
153: * @throws SQLException
154: */
155: public void delete(JDCConnection oConn) throws SQLException {
156: DutyAttachment.delete(oConn, sDutyId, sFileName);
157: }
158:
159: // ---------------------------------------------------------------------------
160:
161: /**
162: * Remove attachment from k_dutys_attach table
163: * @param oConn JDCConnection
164: * @param sDutyId String
165: * @param sFileName String
166: * @throws SQLException
167: */
168: public static void delete(JDCConnection oConn, String sDutyId,
169: String sFileName) throws SQLException {
170:
171: if (DebugFile.trace) {
172: DebugFile.writeln("Begin Duty.delete([JDCConnection],"
173: + sDutyId + "," + sFileName + ")");
174: DebugFile.incIdent();
175: }
176: PreparedStatement oDlte = oConn.prepareStatement("DELETE FROM "
177: + DB.k_duties_attach + " WHERE " + DB.gu_duty
178: + "=? AND " + DB.tx_file + "=?");
179: oDlte.setString(1, sDutyId);
180: oDlte.setString(2, sFileName);
181: oDlte.executeUpdate();
182: oDlte.close();
183:
184: if (DebugFile.trace) {
185: DebugFile.decIdent();
186: DebugFile.writeln("End Duty.delete()");
187: }
188: } // delete
189:
190: // ---------------------------------------------------------------------------
191:
192: /**
193: * Insert attachment into k_duties_attach table
194: * @param oConn JDCConnection
195: * @param sDutyId String GUID of Duty to which attachment belongs
196: * @param sFilePath String Full path to local file
197: * @throws SQLException
198: * @throws FileNotFoundException
199: * @throws IOException
200: * @throws NullPointerException
201: */
202: public static void createFromFile(JDCConnection oConn,
203: String sDutyId, String sFilePath) throws SQLException,
204: FileNotFoundException, IOException, NullPointerException {
205:
206: if (DebugFile.trace) {
207: DebugFile
208: .writeln("Begin DutyAttachment.createFromFile([JDCConnection],"
209: + sDutyId + "," + sFilePath + ")");
210: DebugFile.incIdent();
211: }
212:
213: File oFile = new File(sFilePath);
214:
215: if (oFile == null) {
216: if (DebugFile.trace)
217: DebugFile.decIdent();
218: throw new FileNotFoundException(sFilePath);
219: }
220: if (!oFile.exists()) {
221: if (DebugFile.trace)
222: DebugFile.decIdent();
223: throw new FileNotFoundException(sFilePath);
224: }
225:
226: PreparedStatement oStmt = oConn.prepareStatement("INSERT INTO "
227: + DB.k_duties_attach + "(" + DB.gu_duty + ","
228: + DB.tx_file + "," + DB.len_file + "," + DB.bin_file
229: + ") VALUES (?,?,?,?)");
230:
231: int iFileLen = new Long(oFile.length()).intValue();
232:
233: FileInputStream oFileStream = new FileInputStream(oFile);
234: oStmt.setString(1, sDutyId);
235: oStmt.setString(2, oFile.getName());
236: oStmt.setInt(3, iFileLen);
237: oStmt.setBinaryStream(4, oFileStream, iFileLen);
238: oStmt.executeUpdate();
239: oStmt.close();
240: oFileStream.close();
241: oFileStream = null;
242:
243: if (DebugFile.trace) {
244: DebugFile.decIdent();
245: DebugFile.writeln("End DutyAttachment.createFromFile()");
246: }
247: } // createFromFile
248:
249: // **********************************************************
250: // Constantes Publicas
251:
252: public static final short ClassId = 85;
253:
254: }
|