KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.Vector JavaDoc;
26
27
28 /**
29  * class FolderDB
30  *
31  * @author khue ng
32  * @version 1.0
33  */

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

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

53     public static FolderDB getInstance () {
54         if (m_instance == null) {
55             m_instance = new FolderDB ();
56         }
57         return m_instance;
58     }
59
60
61     /**
62      * Method insert
63      * Insert a new row in jahia_filemgr_folders table
64      *
65      * @param the folder object to create
66      *
67      * @return true if no exception occurs
68      */

69     public boolean insert (Folder folder) {
70         boolean success = false;
71         Connection JavaDoc dbConn = null;
72         Statement JavaDoc stmt = null;
73
74         try {
75             int id = ServicesRegistry.getInstance ().getJahiaIncrementorsDBService ()
76                     .autoIncrement ("jahia_filemgr_folders");
77             // get a connection to the db
78
dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
79
80
81             // execute the query
82
String JavaDoc query = " insert into jahia_filemgr_folders (id_jahia_fld,filemanagerid_jahia_fld, parent_jahia_fld, fldname_jahia_fld)" +
83                     " values(" +
84                     id + "," +
85                     folder.getFilemanagerID () + "," +
86                     folder.getParentID () + "," +
87                     "'" + filter (folder.getFolderName ()) + "')";
88
89             stmt = dbConn.createStatement ();
90             logger.debug (query);
91             stmt.execute (query);
92             success = true;
93         } catch (SQLException JavaDoc ex) {
94             processSQLException ("FolderDB::insert()", ex);
95         } catch (JahiaException je) {
96             logger.error (je.getMessage ());
97         } finally {
98             try {
99
100                 if (stmt != null) stmt.close ();
101             } catch (SQLException JavaDoc ex) {
102                 processSQLException ("FolderDB:: insert() freeing connection fails", ex);
103             }
104         }
105
106         return success;
107
108     }
109
110
111     /**
112      * Method update
113      * Update a row in jahia_filemgr_folders table
114      *
115      * @param a folder object
116      *
117      * @return true if no exception occurs
118      */

119     public boolean update (Folder folder) {
120         boolean success = false;
121         Connection JavaDoc dbConn = null;
122         Statement JavaDoc stmt = null;
123
124         try {
125
126             // get a connection to the db
127
dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
128
129             // execute the query
130
String JavaDoc query = " update jahia_filemgr_folders set " +
131                     "filemanagerid_jahia_fld=" + folder.getFilemanagerID () + "," +
132                     "parent_jahia_fld=" + folder.getParentID () + "," +
133                     "fldname_jahia_fld='" + filter (folder.getFolderName ()) + "'" +
134                     " where id_jahia_fld=" + folder.getFolderID ();
135
136             stmt = dbConn.createStatement ();
137             logger.debug (query);
138             stmt.executeUpdate (query);
139             success = true;
140         } catch (SQLException JavaDoc ex) {
141             processSQLException ("FolderDB::update()", ex);
142         } finally {
143             try {
144
145                 if (stmt != null) stmt.close ();
146             } catch (SQLException JavaDoc ex) {
147                 processSQLException ("FolderDB:: update() freeing connection fails", ex);
148             }
149         }
150         return success;
151
152     }
153
154
155     /**
156      * Method delete
157      * Delete a row in jahia_filemgr_folders table
158      *
159      * @param folderID
160      *
161      * @return false on any error else true
162      */

163     public boolean delete (int folderID) {
164         boolean success = false;
165         Connection JavaDoc dbConn = null;
166         Statement JavaDoc stmt = null;
167
168         try {
169
170             // get a connection to the db
171
dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
172
173             // execute the query
174
String JavaDoc query = " delete from jahia_filemgr_folders where id_jahia_fld=" + folderID;
175
176             stmt = dbConn.createStatement ();
177             logger.debug (query);
178             stmt.execute (query);
179             success = true;
180         } catch (SQLException JavaDoc ex) {
181             processSQLException ("FolderDB::delete()", ex);
182         } finally {
183             try {
184
185                 if (stmt != null) stmt.close ();
186             } catch (SQLException JavaDoc ex) {
187                 processSQLException ("FolderDB:: delete() freeing connection fails", ex);
188             }
189         }
190         return success;
191
192     }
193
194
195     /**
196      * Method getFolder
197      * Return a folder looking at it's id
198      *
199      * @param id
200      *
201      * @return a folder object else null
202      */

203     public Folder getFolder (int folderID) {
204
205         Folder folder = null;
206         Connection JavaDoc dbConn = null;
207         Statement JavaDoc stmt = null;
208         ResultSet JavaDoc rs = null;
209
210         try {
211
212             // get a connection to the db
213
dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
214
215             // execute the query
216
String JavaDoc query = " select * from jahia_filemgr_folders where id_jahia_fld=" + folderID;
217
218             stmt = dbConn.createStatement ();
219             logger.debug (query);
220             rs = stmt.executeQuery (query);
221             while (rs.next ()) {
222                 int filemanagerID = rs.getInt ("filemanagerid_jahia_fld");
223                 int parentID = rs.getInt ("parent_jahia_fld");
224                 String JavaDoc folderName = rs.getString ("fldname_jahia_fld");
225                 folder = new Folder (filemanagerID,
226                         parentID,
227                         folderName);
228
229                 folder.setFolderID (folderID);
230             }
231         } catch (SQLException JavaDoc ex) {
232             processSQLException ("FolderDB::getFolder(id)", ex);
233         } finally {
234             try {
235
236                 if (stmt != null) stmt.close ();
237                 if (rs != null) rs.close ();
238             } catch (SQLException JavaDoc ex) {
239                 processSQLException ("FolderDB::getFolder(folderID) freeing connection fails",
240                         ex);
241             }
242         }
243         return folder;
244
245     }
246
247
248     /**
249      * Method getFolderByFile
250      * Return a folder looking at a gived file id
251      *
252      * @param file id
253      *
254      * @return a folder object else null
255      */

256     public Folder getFolderByFile (int fileID) {
257
258         Folder folder = null;
259         Connection JavaDoc dbConn = null;
260         Statement JavaDoc stmt = null;
261         ResultSet JavaDoc rs = null;
262         int folderID = -1;
263
264         try {
265
266             // get a connection to the db
267
dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
268
269             // execute the query
270
String JavaDoc query = " select * from jahia_filemgr_files where id_jahia_file=" + fileID;
271
272             stmt = dbConn.createStatement ();
273             logger.debug (query);
274             rs = stmt.executeQuery (query);
275             if (rs.next ()) {
276                 folderID = rs.getInt ("fldid_jahia_file");
277
278             }
279         } catch (SQLException JavaDoc ex) {
280             processSQLException ("FolderDB::getFolder(id)", ex);
281         } finally {
282             try {
283
284                 if (stmt != null) stmt.close ();
285             } catch (SQLException JavaDoc ex) {
286                 processSQLException ("FolderDB::getFolder(folderID) freeing connection fails",
287                         ex);
288             }
289         }
290
291         if (folderID != -1) {
292             folder = FolderDB.getInstance ().getFolder (folderID);
293         }
294         return folder;
295
296     }
297
298
299     /**
300      * Method getFoldersByParent
301      * Return a Vector of folder for a gived parent folderID
302      *
303      * @param parent folder id
304      *
305      * @return a Vector of folder objects else null
306      */

307     public Vector JavaDoc getFoldersByParent (int parentID) {
308
309         Folder folder = null;
310         Vector JavaDoc folders = null;
311         Connection JavaDoc dbConn = null;
312         Statement JavaDoc stmt = null;
313         ResultSet JavaDoc rs = null;
314
315         try {
316
317             // get a connection to the db
318
dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
319
320             // execute the query
321
String JavaDoc query = "select * from jahia_filemgr_folders where bm_parent_id=" + parentID;
322
323             stmt = dbConn.createStatement ();
324             logger.debug (query);
325             rs = stmt.executeQuery (query);
326             while (rs.next ()) {
327                 int filemanagerID = rs.getInt ("filemanagerid_jahia_fld");
328                 int folderID = rs.getInt ("fld_folder_id");
329                 String JavaDoc folderName = rs.getString ("fldname_jahia_fld");
330                 folder = new Folder (filemanagerID,
331                         parentID,
332                         folderName);
333
334                 folder.setFolderID (folderID);
335                 folders.add (folder);
336             }
337         } catch (SQLException JavaDoc ex) {
338             processSQLException ("FolderDB::getFoldersByParent(id)", ex);
339         } finally {
340             try {
341
342                 if (stmt != null) stmt.close ();
343             } catch (SQLException JavaDoc ex) {
344                 processSQLException (
345                         "FolderDB:: getFoldersByParent(parentID) freeing connection fails",
346                         ex);
347             }
348         }
349         return folders;
350
351     }
352
353
354     /**
355      * Method getFolders
356      * Return a Vector of folders depending on a where query
357      *
358      * @param whereSql a sql where statement
359      * @param orderSql a sql order statement
360      *
361      * @return a Vector of folder objects else null
362      */

363     public Vector JavaDoc getFolders (String JavaDoc whereSql, String JavaDoc orderSql) {
364
365         Folder folder = null;
366         Vector JavaDoc folders = new Vector JavaDoc ();
367         Connection JavaDoc dbConn = null;
368         Statement JavaDoc stmt = null;
369         ResultSet JavaDoc rs = null;
370
371         if (whereSql == null) {
372             whereSql = "";
373         }
374
375         if (orderSql == null) {
376             orderSql = "";
377         }
378
379         try {
380
381             // get a connection to the db
382
dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
383
384             // execute the query
385
String JavaDoc query = "select * from jahia_filemgr_folders " + whereSql + " " + orderSql;
386
387             stmt = dbConn.createStatement ();
388             logger.debug (query);
389             rs = stmt.executeQuery (query);
390             while (rs.next ()) {
391                 int folderID = rs.getInt ("id_jahia_fld");
392                 int filemanagerID = rs.getInt ("filemanagerid_jahia_fld");
393                 int parentID = rs.getInt ("parent_jahia_fld");
394                 String JavaDoc folderName = rs.getString ("fldname_jahia_fld");
395                 folder = new Folder (filemanagerID,
396                         parentID,
397                         folderName);
398
399                 folder.setFolderID (folderID);
400                 folders.addElement (folder);
401             }
402         } catch (SQLException JavaDoc ex) {
403             processSQLException ("FolderDB::getFolders( where, order )", ex);
404         } finally {
405             try {
406
407                 if (stmt != null) stmt.close ();
408             } catch (SQLException JavaDoc ex) {
409                 processSQLException (
410                         "FolderDB::getFolders(where, order) freeing connection fails", ex);
411             }
412         }
413
414         return folders;
415
416     }
417
418
419     /**
420      * Method getFoldersByFilemanager
421      * Return a Vector of folder for a gived filemanagerID
422      *
423      * @param filemanager id
424      *
425      * @return a Vector of folder objects else null
426      */

427     public Vector JavaDoc getFoldersByFilemanager (int filemanagerID) {
428
429         Folder folder = null;
430         Vector JavaDoc folders = null;
431         Connection JavaDoc dbConn = null;
432         Statement JavaDoc stmt = null;
433         ResultSet JavaDoc rs = null;
434
435         try {
436
437             // get a connection to the db
438
dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
439
440             // execute the query
441
String JavaDoc query = "select * from jahia_filemgr_folders where filemanagerid_jahia_fld=" + filemanagerID;
442
443             stmt = dbConn.createStatement ();
444             logger.debug (query);
445             rs = stmt.executeQuery (query);
446             while (rs.next ()) {
447                 int folderID = rs.getInt ("fld_folder_id");
448                 int parentID = rs.getInt ("parent_jahia_fld");
449                 String JavaDoc folderName = rs.getString ("fldname_jahia_fld");
450                 folder = new Folder (filemanagerID,
451                         parentID,
452                         folderName);
453
454                 folder.setFolderID (folderID);
455                 folders.add (folder);
456             }
457         } catch (SQLException JavaDoc ex) {
458             processSQLException ("FolderDB::getFoldersByFilemanager(id)", ex);
459         } finally {
460             try {
461
462                 if (stmt != null) stmt.close ();
463             } catch (SQLException JavaDoc ex) {
464                 processSQLException (
465                         "FolderDB::getFoldersByFilemanager(filemanagerID) freeing connection fails",
466                         ex);
467             }
468         }
469
470         return folders;
471
472     }
473
474
475     /**
476      * Method getRootFolder<br>
477      * Return a root folder (parent id = -1) for a gived filemanager
478      *
479      * @param fmngID filemanager id
480      *
481      * @return a folder object else null
482      */

483     public Folder getRootFolder (int fmngID) {
484
485         Folder folder = null;
486         Connection JavaDoc dbConn = null;
487         Statement JavaDoc stmt = null;
488         ResultSet JavaDoc rs = null;
489
490         try {
491
492             // get a connection to the db
493
dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
494
495             // execute the query
496
String JavaDoc query = " select * from jahia_filemgr_folders where parent_jahia_fld=-1 and filemanagerid_jahia_fld=" + fmngID;
497
498             stmt = dbConn.createStatement ();
499             logger.debug (query);
500             rs = stmt.executeQuery (query);
501             while (rs.next ()) {
502                 int folderID = rs.getInt ("id_jahia_fld");
503                 int filemanagerID = rs.getInt ("filemanagerid_jahia_fld");
504                 int parentID = rs.getInt ("parent_jahia_fld");
505                 String JavaDoc folderName = rs.getString ("fldname_jahia_fld");
506                 folder = new Folder (filemanagerID,
507                         parentID,
508                         folderName);
509
510                 folder.setFolderID (folderID);
511             }
512         } catch (SQLException JavaDoc ex) {
513             processSQLException ("FolderDB::getRootFolder()", ex);
514         } finally {
515             try {
516
517                 if (stmt != null) stmt.close ();
518             } catch (SQLException JavaDoc ex) {
519                 processSQLException ("FolderDB::getRootFolder() freeing connection fails", ex);
520             }
521         }
522
523         return folder;
524
525     }
526
527
528     //--------------------------------------------------------------------------
529
/**
530      * return a DOM document of all filemgr folders of a site
531      *
532      * @param int the site id
533      *
534      * @return JahiaDOMObject a DOM representation of this object
535      *
536      * @author NK
537      */

538     public JahiaDOMObject getFileMgrFoldersAsDOM (int siteID)
539             throws JahiaException {
540
541         Connection JavaDoc dbConn = null;
542         Statement JavaDoc statement = null;
543
544         String JavaDoc output = null;
545         JahiaDBDOMObject dom = null;
546
547         try {
548
549             Filemanager fmng = FilemanagerDB.getInstance ().getFilemanagerByOwner (siteID);
550             if (fmng == null) {
551                 dom = new JahiaDBDOMObject ();
552                 dom.addTable ("jahia_filemgr_folders", null);
553                 return dom;
554             }
555
556             String JavaDoc sqlQuery = "SELECT * FROM jahia_filemgr_folders where filemanagerid_jahia_fld=" + fmng.getFilemanagerID ();
557
558             dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
559             statement = dbConn.createStatement ();
560             if (statement != null) {
561                 ResultSet JavaDoc rs = statement.executeQuery (sqlQuery);
562                 if (rs != null) {
563                     dom = new JahiaDBDOMObject ();
564                     dom.addTable ("jahia_filemgr_folders", rs);
565                     return dom;
566                 }
567             }
568         } catch (SQLException JavaDoc se) {
569             String JavaDoc errorMsg = "Error in getFileMgrFoldersAsDOM(int siteID) : " + se.getMessage ();
570             logger.error (errorMsg + " -> BAILING OUT");
571             throw new JahiaException ("Cannot load filemanager folders from the database",
572                     errorMsg, JahiaException.DATABASE_ERROR,
573                     JahiaException.CRITICAL_SEVERITY);
574         } finally {
575
576             closeStatement (statement);
577         }
578
579         return dom;
580     }
581
582
583     //-------------------------------------------------------------------------
584
private void closeStatement (Statement JavaDoc statement) {
585         // Close the opened statement
586
try {
587             if (statement != null) {
588                 statement.close ();
589             }
590         } catch (SQLException JavaDoc sqlEx) {
591             // just create an exception without raising it, just to notify it
592
// in the logs.
593
JahiaException je = new JahiaException ("Cannot close a statement",
594                     "Cannot close a statement", JahiaException.DATABASE_ERROR,
595                     JahiaException.WARNING_SEVERITY, sqlEx);
596             logger.error ("Error:", je);
597         }
598     }
599
600
601     /**
602      * Method executeQuery<br>
603      * Execute any query, doesnt return any resultset
604      *
605      * @param sqlQuery
606      *
607      * @return false if error
608      */

609     public boolean executeQuery (String JavaDoc query) {
610
611         Connection JavaDoc dbConn = null;
612         Statement JavaDoc stmt = null;
613         boolean success = false;
614
615         try {
616
617             // get a connection to the db
618
dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
619
620             stmt = dbConn.createStatement ();
621             logger.debug (query);
622             stmt.executeQuery (query);
623             success = true;
624         } catch (SQLException JavaDoc ex) {
625             processSQLException ("FolderDB::executeQuery()", ex);
626         } finally {
627             try {
628
629                 if (stmt != null) stmt.close ();
630             } catch (SQLException JavaDoc ex) {
631                 processSQLException ("FolderDB:: executeQuery() freeing connection fails", ex);
632             }
633             return success;
634         }
635     }
636
637     /**
638      * Method filter
639      * Filter special caracters
640      *
641      * @param input String to filter
642      *
643      * @return a filtered string
644      */

645     public static String JavaDoc filter (String JavaDoc input) {
646         StringBuffer JavaDoc filtered = new StringBuffer JavaDoc (input.length ());
647         char c;
648         for (int i = 0; i < input.length (); i++) {
649             c = input.charAt (i);
650             if (c == '\'') {
651                 filtered.append ("&#39;");
652             } else if (c == '"') {
653                 filtered.append ("&#34;");
654             } else {
655                 filtered.append (c);
656             }
657         }
658         return (filtered.toString ());
659     }
660
661     /*
662      * Method processSQLException
663      * For debug purpose
664      *
665      * @param func the name of the calling method
666      * @param SQLException
667      */

668     public void processSQLException (String JavaDoc func, SQLException JavaDoc ex) {
669         if (m_debugQuery) {
670
671             while (ex != null) {
672                 logger.debug (
673                         "SQL EXCEPTION in function [" + func + "]: SqlState = " + ex.getSQLState () + " Error code = " + ex.getErrorCode () + " Error msg = " + ex.toString ());
674                 ex = ex.getNextException ();
675             }
676         }
677     }
678
679 }
680
681
Popular Tags