KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > services > filemanager > FilemanagerDB


1 //
2
// ____.
3
// __/\ ______| |__/\. _______
4
// __ .____| | \ | +----+ \
5
// _______| /--| | | - \ _ | : - \_________
6
// \\______: :---| : : | : | \________>
7
// |__\---\_____________:______: :____|____:_____\
8
// /_____|
9
//
10
// . . . i n j a h i a w e t r u s t . . .
11
//
12

13 package org.jahia.services.filemanager;
14
15
16 import org.jahia.data.JahiaDBDOMObject;
17 import org.jahia.data.JahiaDOMObject;
18 import org.jahia.exceptions.JahiaException;
19 import org.jahia.registries.ServicesRegistry;
20
21 import java.sql.Connection JavaDoc;
22 import java.sql.ResultSet JavaDoc;
23 import java.sql.SQLException JavaDoc;
24 import java.sql.Statement JavaDoc;
25
26
27 /**
28  * class FilemanagerDB
29  *
30  * @author khue ng
31  * @version 1.0
32  */

33 public class FilemanagerDB {
34     private static org.apache.log4j.Logger logger =
35             org.apache.log4j.Logger.getLogger (FilemanagerDB.class);
36
37     private static boolean m_debugQuery = false;
38     private static FilemanagerDB m_instance = null;
39
40     /**
41      * Constructor
42      */

43     protected FilemanagerDB () {
44     }
45
46     /**
47      * Method getInstance
48      * Provide a unique point to get a reference to the unique instance of this class
49      *
50      * @return a reference to the unique instance of this calss
51      */

52     public static FilemanagerDB getInstance () {
53         if (m_instance == null) {
54             m_instance = new FilemanagerDB ();
55         }
56         return m_instance;
57     }
58
59
60     /**
61      * Method insert
62      * Insert a new row in jahia_filemgr table
63      *
64      * @param ownerID the owner of this filemanager
65      *
66      * @return true if no exception occurs
67      */

68     public boolean insert (Filemanager fmng) {
69         boolean success = false;
70         Connection JavaDoc dbConn = null;
71         Statement JavaDoc stmt = null;
72         ResultSet JavaDoc rs = null;
73
74         try {
75             int id = ServicesRegistry.getInstance ().getJahiaIncrementorsDBService ()
76                     .autoIncrement ("jahia_filemgr");
77
78             // get a connection to the db
79
dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
80
81
82             // execute the query
83
String JavaDoc query = " insert into jahia_filemgr (id_jahia_filemgr,storagepath_jahia_filemgr,ownerid_jahia_filemgr)" +
84                     " values("
85                     + id + ",'"
86                     + fmng.getStoragePath () + "',"
87                     + fmng.getOwnerID () + ")";
88
89             stmt = dbConn.createStatement ();
90             logger.debug (query);
91             stmt.execute (query);
92
93             // get the filemanager id for this owner
94
query =
95                     "select * from jahia_filemgr where ownerid_jahia_filemgr=" + fmng.getOwnerID ();
96
97             logger.debug (query);
98             rs = stmt.executeQuery (query);
99             while (rs.next ()) {
100                 int filemanagerID = rs.getInt ("id_jahia_filemgr");
101                 fmng.setFilemanagerID (filemanagerID);
102             }
103             success = true;
104         } catch (SQLException JavaDoc ex) {
105             processSQLException ("FilemanagerDB::insert()", ex);
106         } catch (JahiaException je) {
107             logger.error (je.getMessage ());
108         } finally {
109             try {
110
111                 if (stmt != null) stmt.close ();
112             } catch (SQLException JavaDoc ex) {
113                 processSQLException ("FilemanagerDB:: insert() freeing connection fails", ex);
114             }
115         }
116
117         return success;
118
119     }
120
121     /**
122      * Method update<br>
123      * Update a row in jahia_filemgr table<br>
124      *
125      * @param fmng the filemanager object
126      *
127      * @return true if no exception occurs
128      */

129     public boolean update (Filemanager fmng) {
130         boolean success = false;
131         Connection JavaDoc dbConn = null;
132         Statement JavaDoc stmt = null;
133
134         try {
135
136             // get a connection to the db
137
dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
138
139             // execute the query
140
String JavaDoc query = " update jahia_filemgr set " +
141                     "storagepath_jahia_filemgr='" + fmng.getStoragePath () + "'," +
142                     "ownerid_jahia_filemgr=" + fmng.getOwnerID () +
143                     " where id_jahia_filemgr=" + fmng.getFilemanagerID ();
144
145             stmt = dbConn.createStatement ();
146             logger.debug (query);
147             stmt.executeUpdate (query);
148             success = true;
149         } catch (SQLException JavaDoc ex) {
150             processSQLException ("FilemanagerDB::update()", ex);
151         } finally {
152             try {
153
154                 if (stmt != null) stmt.close ();
155             } catch (SQLException JavaDoc ex) {
156                 processSQLException ("FilemanagerDB:: update() freeing connection fails", ex);
157             }
158             return success;
159         }
160     }
161
162
163     /**
164      * Method delete
165      * Delete a row in jahia_filemgr table
166      *
167      * @param filemanagerID
168      *
169      * @return false on any error else true
170      */

171     public boolean delete (int filemanagerID) {
172         boolean success = false;
173         Connection JavaDoc dbConn = null;
174         Statement JavaDoc stmt = null;
175
176         try {
177
178             // get a connection to the db
179
dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
180
181             // execute the query
182
String JavaDoc query = " delete from jahia_filemgr where id_jahia_filemgr=" + filemanagerID;
183
184             stmt = dbConn.createStatement ();
185             logger.debug (query);
186             stmt.execute (query);
187             success = true;
188         } catch (SQLException JavaDoc ex) {
189             processSQLException ("FilemanagerDB::delete()", ex);
190         } finally {
191             try {
192
193                 if (stmt != null) stmt.close ();
194             } catch (SQLException JavaDoc ex) {
195                 processSQLException ("FilemanagerDB:: update() freeing connection fails", ex);
196             }
197             return success;
198         }
199
200     }
201
202
203     /**
204      * Method getFilemanager<br>
205      * Return a filemanager looking at it's id<br>
206      *
207      * @param id
208      *
209      * @return a filemanager object else null
210      */

211     public Filemanager getFilemanager (int filemanagerID) {
212
213         Filemanager fmng = null;
214         Connection JavaDoc dbConn = null;
215         Statement JavaDoc stmt = null;
216         ResultSet JavaDoc rs = null;
217
218         try {
219
220             // get a connection to the db
221
dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
222
223             // execute the query
224
String JavaDoc query = " select * from jahia_filemgr where id_jahia_filemgr=" + filemanagerID;
225
226             stmt = dbConn.createStatement ();
227             logger.debug (query);
228             rs = stmt.executeQuery (query);
229             while (rs.next ()) {
230                 String JavaDoc storagePath = rs.getString ("storagepath_jahia_filemgr");
231                 int ownerID = rs.getInt ("ownerid_jahia_filemgr");
232                 fmng = new Filemanager ();
233                 fmng.setStoragePath (storagePath);
234                 fmng.setFilemanagerID (filemanagerID);
235                 fmng.setOwnerID (ownerID);
236
237             }
238         } catch (SQLException JavaDoc ex) {
239             processSQLException ("FilemanagerDB::getFilemanager(id)", ex);
240         } finally {
241             try {
242
243                 if (stmt != null) stmt.close ();
244             } catch (SQLException JavaDoc ex) {
245                 processSQLException (
246                         "FilemanagerDB:: getFilemanager(filemanagerID) freeing connection fails",
247                         ex);
248             }
249         }
250
251         return fmng;
252
253     }
254
255
256     /**
257      * Method getFilemanagerByOwner
258      * Return a filemanager looking at it's owner id
259      *
260      * @param id
261      *
262      * @return a filemanager object else null
263      */

264     public Filemanager getFilemanagerByOwner (int ownerID) {
265
266         Filemanager fmng = null;
267         Connection JavaDoc dbConn = null;
268         Statement JavaDoc stmt = null;
269         ResultSet JavaDoc rs = null;
270
271         try {
272
273             // get a connection to the db
274
dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
275
276             // execute the query
277
String JavaDoc query = "select * from jahia_filemgr where ownerid_jahia_filemgr=" + ownerID;
278
279             stmt = dbConn.createStatement ();
280             logger.debug (query);
281             rs = stmt.executeQuery (query);
282             while (rs.next ()) {
283                 int filemanagerID = rs.getInt ("id_jahia_filemgr");
284                 String JavaDoc storagePath = rs.getString ("storagepath_jahia_filemgr");
285                 fmng = new Filemanager ();
286                 fmng.setFilemanagerID (filemanagerID);
287                 fmng.setStoragePath (storagePath);
288                 fmng.setOwnerID (ownerID);
289             }
290         } catch (SQLException JavaDoc ex) {
291             processSQLException ("FilemanagerDB::getFilemanagerByOwner(id)", ex);
292         } finally {
293             try {
294
295                 if (stmt != null) stmt.close ();
296             } catch (SQLException JavaDoc ex) {
297                 processSQLException (
298                         "FilemanagerDB:: getFilemanager(ownerID) freeing connection fails",
299                         ex);
300             }
301
302         }
303
304         return fmng;
305
306     }
307
308
309     //--------------------------------------------------------------------------
310
/**
311      * return a DOM document of the Filemanager of a site
312      *
313      * @param int the site id
314      *
315      * @return JahiaDOMObject a DOM representation of this object
316      *
317      * @author NK
318      */

319     public JahiaDOMObject getFileMgrAsDOM (int siteID)
320             throws JahiaException {
321
322         Connection JavaDoc dbConn = null;
323         Statement JavaDoc statement = null;
324
325         String JavaDoc output = null;
326         JahiaDBDOMObject dom = null;
327
328         try {
329             String JavaDoc sqlQuery = "SELECT * FROM jahia_filemgr where ownerid_jahia_filemgr=" + siteID;
330
331             dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
332             statement = dbConn.createStatement ();
333             if (statement != null) {
334                 ResultSet JavaDoc rs = statement.executeQuery (sqlQuery);
335                 if (rs != null) {
336                     dom = new JahiaDBDOMObject ();
337                     dom.addTable ("jahia_filemgr", rs);
338                     return dom;
339                 }
340             }
341         } catch (SQLException JavaDoc se) {
342             String JavaDoc errorMsg = "Error in getFileMgrAsDOM(int siteID) : " + se.getMessage ();
343             logger.error (errorMsg);
344             throw new JahiaException ("Cannot load filemanager from the database",
345                     errorMsg, JahiaException.DATABASE_ERROR,
346                     JahiaException.CRITICAL_SEVERITY);
347         } finally {
348
349             closeStatement (statement);
350         }
351
352         return dom;
353     }
354
355
356     //--------------------------------------------------------------------------
357
/**
358      * return a DOM document of the Filemanager files of a site
359      *
360      * @param int the site id
361      *
362      * @return JahiaDOMObject a DOM representation of this object
363      *
364      * @author NK
365      */

366     public JahiaDOMObject getFileMgrFilesAsDOM (int siteID)
367             throws JahiaException {
368
369         Connection JavaDoc dbConn = null;
370         Statement JavaDoc statement = null;
371
372         String JavaDoc output = null;
373         JahiaDBDOMObject dom = null;
374
375
376         try {
377
378             Filemanager fmng = FilemanagerDB.getInstance ().getFilemanagerByOwner (siteID);
379             if (fmng == null) {
380                 dom = new JahiaDBDOMObject ();
381                 dom.addTable ("jahia_filemgr_files", null);
382                 return dom;
383             }
384
385             String JavaDoc sqlQuery = "SELECT * FROM jahia_filemgr_files where filemgrid_jahia_file=" + fmng.getFilemanagerID ();
386
387             dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
388             statement = dbConn.createStatement ();
389             if (statement != null) {
390                 ResultSet JavaDoc rs = statement.executeQuery (sqlQuery);
391                 if (rs != null) {
392                     dom = new JahiaDBDOMObject ();
393                     dom.addTable ("jahia_filemgr_files", rs);
394                     return dom;
395                 }
396             }
397         } catch (SQLException JavaDoc se) {
398             String JavaDoc errorMsg = "Error in getFileMgrFilesAsDOM(int siteID) : " + se.getMessage ();
399             logger.error (errorMsg);
400             throw new JahiaException ("Cannot load filemanager files from the database",
401                     errorMsg, JahiaException.DATABASE_ERROR,
402                     JahiaException.CRITICAL_SEVERITY);
403         } finally {
404
405             closeStatement (statement);
406         }
407
408         return dom;
409     }
410
411
412     private void closeStatement (Statement JavaDoc statement) {
413         // Close the opened statement
414
try {
415             if (statement != null) {
416                 statement.close ();
417             }
418         } catch (SQLException JavaDoc sqlEx) {
419             // just create an exception without raising it, just to notify it
420
// in the logs.
421
JahiaException je = new JahiaException ("Cannot close a statement",
422                     "Cannot close a statement", JahiaException.DATABASE_ERROR,
423                     JahiaException.WARNING_SEVERITY, sqlEx);
424             logger.error ("Error:", je);
425         }
426     }
427
428
429     /**
430      * Method filter
431      * Filter special caracters
432      *
433      * @param input String to filter
434      *
435      * @return a filtered string
436      */

437     public static String JavaDoc filter (String JavaDoc input) {
438         StringBuffer JavaDoc filtered = new StringBuffer JavaDoc (input.length ());
439         char c;
440         for (int i = 0; i < input.length (); i++) {
441             c = input.charAt (i);
442             if (c == '\'') {
443                 filtered.append ("&#39;");
444             } else if (c == '"') {
445                 filtered.append ("&#34;");
446             } else {
447                 filtered.append (c);
448             }
449         }
450         return (filtered.toString ());
451     }
452
453
454     /**
455      * Method toConsole
456      * For debug purpose
457      * Write a msg to the console
458      *
459      * @param msg the message to display
460      */

461     public void toConsole (String JavaDoc msg) {
462         if (false) {
463             // System.out.println(msg);
464
}
465     }
466
467
468     /**
469      * Method processSQLException
470      * For debug purpose
471      *
472      * @param func the name of the calling method
473      * @param SQLException
474      */

475     public void processSQLException (String JavaDoc func, SQLException JavaDoc ex) {
476         if (m_debugQuery) {
477
478             while (ex != null) {
479                 logger.debug (
480                         "SQL EXCEPTION in function [" + func + "]: SqlState = " + ex.getSQLState () + " Error code = " + ex.getErrorCode () + " Error msg = " + ex.toString ());
481                 ex = ex.getNextException ();
482             }
483         }
484     }
485
486 }
487
488
Popular Tags