KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > knowgate > hipergate > FileHandler


1 /*
2   Copyright (C) 2003 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.hipergate;
34
35 import java.lang.System JavaDoc;
36
37 import java.io.File JavaDoc;
38
39 import java.sql.SQLException JavaDoc;
40 import java.sql.Connection JavaDoc;
41 import java.sql.PreparedStatement JavaDoc;
42
43 import com.knowgate.dataobjs.DB;
44
45 /**
46  * Class used for creating references to files at k_prod_locats database table
47  * @author Sergio Montoro Ten
48  * @version 1.0
49  */

50 public class FileHandler {
51
52   public FileHandler() {
53     sFileSeparator = System.getProperty("file.separator");
54   }
55
56   // ----------------------------------------------------------
57

58   private String JavaDoc getExtension(String JavaDoc sFile) {
59     int iLast = sFile.lastIndexOf(".");
60
61     if (iLast>0)
62       return sFile.substring(++iLast);
63     else
64       return "";
65   } // getExtension()
66

67   // ----------------------------------------------------------
68

69   public void upload(String JavaDoc sPath, String JavaDoc sFile, Connection JavaDoc oConn, String JavaDoc sLocationId) throws SQLException JavaDoc {
70     int iFileLen;
71     File JavaDoc oOldFile;
72     String JavaDoc sExt;
73     String JavaDoc sUniqueName;
74     PreparedStatement JavaDoc oStmt;
75
76     if (!sPath.endsWith(sFileSeparator)) sPath = sPath + sFileSeparator;
77
78     sExt = getExtension(sFile);
79
80     sUniqueName = (sExt.length()==0 ? sLocationId : sLocationId + "." + sExt);
81
82     oOldFile = new File JavaDoc(sPath + sFile);
83     iFileLen = new Long JavaDoc(oOldFile.length()).intValue();
84     oOldFile.renameTo(new File JavaDoc(sPath + sUniqueName));
85     oOldFile = null;
86
87     if (null!=oConn && null!=sLocationId) {
88       oStmt = oConn.prepareStatement("UPDATE " + DB.k_prod_locats + " SET " + DB.xfile + "=?," + DB.len_file + "=? WHERE " + DB.gu_location + "=?");
89       oStmt.setString(1, sUniqueName);
90       oStmt.setInt(2, iFileLen);
91       oStmt.setString(3, sLocationId);
92       oStmt.executeUpdate();
93       oStmt.close();
94       oStmt = null;
95     } // fi (oConn && sLocationId)
96
} // upload()
97

98   // ----------------------------------------------------------
99

100   public void delete(String JavaDoc sPath, String JavaDoc sFile, Connection JavaDoc oConn, String JavaDoc sLocationId) throws SQLException JavaDoc {
101     File JavaDoc oFile;
102     String JavaDoc sPathFile;
103     PreparedStatement JavaDoc oStmt;
104
105     if (null!=sPath && null!=sFile) {
106       if (sPath.endsWith(sFileSeparator))
107         sPathFile = sPath + sFile;
108       else
109         sPathFile = sPath + sFileSeparator + sFile;
110
111       oFile = new File JavaDoc(sPathFile);
112       oFile.delete();
113       oFile = null;
114     } // fi (sPath && sFile)
115

116     if (null!=oConn && null!=sLocationId) {
117       oStmt = oConn.prepareStatement("DELETE " + DB.k_prod_locats + " WHERE " + DB.gu_location + "=?");
118       oStmt.setString(1, sLocationId);
119       oStmt.executeUpdate();
120       oStmt.close();
121       oStmt = null;
122     } // fi (oConn && sLocationId)
123
} // delete()
124

125   // ----------------------------------------------------------
126

127   private String JavaDoc sFileSeparator;
128 }
Popular Tags