KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > services > homepages > JahiaHomepagesPersistance


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
// JahiaHomepagesPersistance
14
//
15
// NK 17.12.2001
16
//
17
package org.jahia.services.homepages;
18
19 import org.jahia.data.JahiaDBDOMObject;
20 import org.jahia.data.JahiaDOMObject;
21 import org.jahia.exceptions.JahiaException;
22 import org.jahia.registries.ServicesRegistry;
23
24 import java.sql.*;
25 import java.util.Hashtable JavaDoc;
26 import java.util.Vector JavaDoc;
27
28 /**
29  * persistance storage of jahia homepages information
30  *
31  * @author Khue ng
32  */

33 class JahiaHomepagesPersistance {
34     private static org.apache.log4j.Logger logger =
35             org.apache.log4j.Logger.getLogger (JahiaHomepagesPersistance.class);
36
37     private static final String JavaDoc CLASS_NAME = JahiaHomepagesPersistance.class.getName ();
38
39     /** unique instance */
40     private static JahiaHomepagesPersistance m_Instance = null;
41
42     /**
43      * constructor
44      */

45     protected JahiaHomepagesPersistance () {
46
47     }
48
49
50     /**
51      * return the singleton instance of this class
52      */

53     public static synchronized JahiaHomepagesPersistance getInstance () {
54
55         if (m_Instance == null) {
56             m_Instance = new JahiaHomepagesPersistance ();
57         }
58         return m_Instance;
59     }
60
61
62     /**
63      * return the list of all homepages
64      *
65      * @return a Vector of JahiaHomepage bean
66      */

67     Vector JavaDoc getHomepages () throws JahiaException {
68
69         Connection dbConn = null;
70         Statement statement = null;
71
72         Vector JavaDoc v = new Vector JavaDoc ();
73         JahiaHomepage hp = null;
74
75         try {
76             String JavaDoc sqlQuery = "SELECT * FROM jahia_homepages";
77
78             dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
79             statement = dbConn.createStatement ();
80             if (statement != null) {
81                 ResultSet rs = statement.executeQuery (sqlQuery);
82                 if (rs != null) {
83                     while (rs.next ()) {
84                         hp = getHomepageFromResultSet (rs);
85                         hp.loadProperties ();
86                         v.add (hp);
87                     }
88                 }
89             }
90
91         } catch (SQLException se) {
92             logger.error (se.getMessage ());
93             throw new JahiaException (CLASS_NAME + ".getHomepages",
94                     "Cannot load homepages from the database"
95                     + se.getMessage (),
96                     JahiaException.DATABASE_ERROR,
97                     JahiaException.CRITICAL_SEVERITY);
98         } finally {
99
100             closeStatement (statement);
101         }
102
103         return v;
104     }
105
106
107     /**
108      * return the JahiaHomepage bean looking at it id
109      *
110      * @param int the JahiaSite id
111      *
112      * @return JahiaSite the JahiaSite bean
113      */

114     JahiaHomepage load (int id) throws JahiaException {
115
116         Connection dbConn = null;
117         Statement statement = null;
118
119         JahiaHomepage hp = null;
120
121         try {
122             String JavaDoc sqlQuery = "SELECT * FROM jahia_homepages where id_jahia_homepages =" + id;
123
124             dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
125             statement = dbConn.createStatement ();
126             if (statement != null) {
127                 ResultSet rs = statement.executeQuery (sqlQuery);
128                 if (rs != null) {
129                     if (rs.next ()) {
130                         hp = getHomepageFromResultSet (rs);
131                         hp.loadProperties ();
132                     }
133                 }
134             }
135         } catch (SQLException se) {
136             logger.error (se.getMessage ());
137             throw new JahiaException (CLASS_NAME + ".load",
138                     "Cannot load homepage from the database"
139                     + se.getMessage (),
140                     JahiaException.DATABASE_ERROR,
141                     JahiaException.CRITICAL_SEVERITY);
142         } finally {
143
144             closeStatement (statement);
145         }
146
147         return hp;
148     }
149
150     /**
151      * save a homepage, create it if not exist
152      *
153      * @param JahiaHomepage the homepage bean
154      */

155     void save (JahiaHomepage hp) throws JahiaException {
156
157         if (hp == null)
158             return;
159
160         Connection dbConn = null;
161         PreparedStatement pstmt = null;
162
163         try {
164
165             if (hp.getID () > 0) {
166                 update (hp);
167                 return;
168             }
169
170             int id = ServicesRegistry.getInstance ()
171                     .getJahiaIncrementorsDBService ()
172                     .autoIncrement ("jahia_homepages");
173
174             if ((hp.getSiteKey () == null) || hp.getSiteKey ().trim ().equals (""))
175                 throw new JahiaException (CLASS_NAME + ".save",
176                         "Invalid sitekey value",
177                         JahiaException.DATA_ERROR,
178                         JahiaException.ERROR_SEVERITY);
179             if ((hp.getName () == null) || hp.getName ().trim ().equals (""))
180                 hp.setName ("no name (" + id + ")");
181             if ((hp.getDescr () == null))
182                 hp.setDescr ("no description");
183
184             dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
185
186             pstmt =
187                     dbConn.prepareStatement ("INSERT INTO jahia_homepages VALUES(?,?,?,?,?,?)");
188             pstmt.setInt (1, id);
189             pstmt.setString (2, hp.getName ());
190             pstmt.setString (3, hp.getDescr ());
191             pstmt.setString (4, hp.getSiteKey ());
192             pstmt.setInt (5, hp.getType ());
193             pstmt.setInt (6, hp.getAclID ());
194
195             pstmt.execute ();
196
197             hp.setID (id);
198
199         } catch (java.sql.SQLException JavaDoc sqle) {
200             String JavaDoc errorMsg = "Error in save(Jahiahomepage) : " + sqle.getMessage ();
201             logger.error (errorMsg);
202             throw new JahiaException ("Error saving a homepage in the database",
203                     errorMsg,
204                     JahiaException.DATABASE_ERROR,
205                     JahiaException.CRITICAL_SEVERITY);
206         } finally {
207
208             closeStatement ((Statement) pstmt);
209         }
210
211     }
212
213
214     /**
215      * remove a homepage
216      *
217      * @param int the home page id
218      */

219     void delete (int id) throws JahiaException {
220
221         try {
222             String JavaDoc sqlQuery = "DELETE FROM jahia_homepages WHERE id_jahia_homepages=" + id;
223             executeQueryNoResultSet (sqlQuery);
224         } catch (JahiaException je) {
225             String JavaDoc errorMsg = "Error in delete(int id) : " + je.getMessage ();
226             logger.error (errorMsg);
227             throw new JahiaException ("Cannot delete homepage in the database",
228                     errorMsg,
229                     JahiaException.DATABASE_ERROR,
230                     JahiaException.CRITICAL_SEVERITY);
231         }
232     }
233
234
235     /**
236      * Update a home page definition
237      *
238      * @param JahiaHomepage the homepage bean
239      */

240     void update (JahiaHomepage hp) throws JahiaException {
241
242         if (hp == null)
243             return;
244
245         if ((hp.getSiteKey () == null) || hp.getSiteKey ().trim ().equals (""))
246             throw new JahiaException (CLASS_NAME + ".save",
247                     "Invalid sitekey value",
248                     JahiaException.DATA_ERROR,
249                     JahiaException.ERROR_SEVERITY);
250         if ((hp.getName () == null) || hp.getName ().trim ().equals (""))
251             hp.setName ("no name (" + hp.getID () + ")");
252         if ((hp.getDescr () == null))
253             hp.setDescr ("no description");
254
255         Connection dbConn = null;
256         PreparedStatement pstmt = null;
257
258         try {
259
260             dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
261
262             pstmt =
263                     dbConn.prepareStatement (
264                             "UPDATE jahia_homepages SET name_jahia_homepages=?, descr_jahia_homepages=?, sitekey_jahia_homepages=?, type_jahia_homepages=?, rights_jahia_homepages=? WHERE id_jahia_homepages=?");
265             pstmt.setString (1, hp.getName ());
266             pstmt.setString (2, hp.getDescr ());
267             pstmt.setString (3, hp.getSiteKey ());
268             pstmt.setInt (4, hp.getType ());
269             pstmt.setInt (5, hp.getAclID ());
270             pstmt.setInt (6, hp.getID ());
271
272             pstmt.execute ();
273
274         } catch (java.sql.SQLException JavaDoc sqle) {
275             String JavaDoc errorMsg = "Error in update : " + sqle.getMessage ();
276             logger.error (errorMsg);
277             throw new JahiaException ("Cannot update homepage in the database",
278                     errorMsg, JahiaException.DATABASE_ERROR,
279                     JahiaException.CRITICAL_SEVERITY);
280         } finally {
281
282             closeStatement ((Statement) pstmt);
283         }
284
285     }
286
287     /**
288      * Returns a properties as a String.
289      *
290      * @param JahiaHomepage hp , the homepage bean
291      * @param String the property name
292      */

293     String JavaDoc getProperty (JahiaHomepage hp, String JavaDoc key)
294             throws JahiaException {
295
296         Connection dbConn = null;
297         PreparedStatement pstmt = null;
298         String JavaDoc value = null;
299
300         if ((hp == null) || (key == null) || (key.trim ().equals ("")))
301             return null;
302
303         try {
304
305             dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
306
307             pstmt =
308                     dbConn.prepareStatement (
309                             "SELECT value_homepages_prop FROM jahia_homepages_prop WHERE id_jahia_homepages=? AND name_homepages_prop=?");
310             pstmt.setInt (1, hp.getID ());
311             pstmt.setString (2, key);
312             ResultSet rs = pstmt.executeQuery ();
313
314             if (rs != null) {
315                 if (rs.next ()) {
316                     value = rs.getString ("value_homepages_prop");
317                 }
318             }
319         } catch (SQLException se) {
320             logger.error (se.getMessage ());
321             throw new JahiaException (CLASS_NAME + ".getProperty(hp,key)",
322                     "Cannot load homepage prop from the database"
323                     + se.getMessage (),
324                     JahiaException.DATABASE_ERROR,
325                     JahiaException.CRITICAL_SEVERITY);
326         } finally {
327
328             closeStatement ((Statement) pstmt);
329         }
330
331         return value;
332
333     }
334
335     /**
336      * add a property of a home page. Do not check if there is alrady a same
337      * property in db.You must remove it first.
338      *
339      * @param JahiaHomepage hp , the homepage bean
340      * @param String the property name
341      * @param String the property value
342      */

343     void addProperty (JahiaHomepage hp, String JavaDoc key, String JavaDoc value) throws JahiaException {
344
345
346         if ((hp == null) || (hp.getSiteKey () == null) || hp.getSiteKey ().trim ().equals ("")
347                 || (key == null) || (key.trim ().equals ("")))
348             return;
349
350         Connection dbConn = null;
351         PreparedStatement pstmt = null;
352
353         try {
354
355             dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
356
357             pstmt =
358                     dbConn.prepareStatement (
359                             "INSERT INTO jahia_homepages_prop VALUES (?,?,?,?)");
360             pstmt.setInt (1, hp.getID ());
361             pstmt.setString (2, hp.getSiteKey ());
362             pstmt.setString (3, key);
363             pstmt.setString (4, value);
364             pstmt.execute ();
365
366         } catch (SQLException se) {
367             String JavaDoc errorMsg = "Error in addProperty(JahiaHomepage hp, String key, String value) : "
368                     + se.getMessage ();
369             logger.error (errorMsg);
370             throw new JahiaException ("Cannot add a homepage property in the database",
371                     errorMsg,
372                     JahiaException.DATABASE_ERROR,
373                     JahiaException.CRITICAL_SEVERITY);
374         } finally {
375
376             closeStatement ((Statement) pstmt);
377         }
378     }
379
380
381     /**
382      * delete all properties of a homepage
383      *
384      * @param JahiaHomepage hp, the homepage bean
385      */

386     void deleteProperties (JahiaHomepage hp) throws JahiaException {
387
388         if (hp == null)
389             return;
390
391         try {
392             StringBuffer JavaDoc buff =
393                     new StringBuffer JavaDoc (
394                             "DELETE FROM jahia_homepages_prop WHERE id_jahia_homepages=");
395             buff.append (hp.getID ());
396             executeQueryNoResultSet (buff.toString ());
397         } catch (JahiaException je) {
398             String JavaDoc errorMsg = "Error in deleteProperties(JahiaHomepage) : "
399                     + je.getMessage ();
400             logger.error (errorMsg);
401             throw new JahiaException ("Cannot delete all homepage properties in the database",
402                     errorMsg,
403                     JahiaException.DATABASE_ERROR,
404                     JahiaException.CRITICAL_SEVERITY);
405         }
406     }
407
408     /**
409      * remove a property of a home page
410      *
411      * @param int the home page id
412      * @param String the property name
413      */

414     void deleteProperty (int id, String JavaDoc key) throws JahiaException {
415
416         if ((key == null) || (key.trim ().equals ("")))
417             return;
418
419         Connection dbConn = null;
420         PreparedStatement pstmt = null;
421
422         try {
423
424             dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
425
426             pstmt =
427                     dbConn.prepareStatement (
428                             "DELETE FROM jahia_homepages_prop WHERE id_jahia_homepages=? AND name_homepages_prop=? ");
429             pstmt.setInt (1, id);
430             pstmt.setString (2, key);
431
432         } catch (SQLException se) {
433             String JavaDoc errorMsg = "Error in deleteProperty(int id, String key) : "
434                     + se.getMessage ();
435             logger.error (errorMsg);
436             throw new JahiaException ("Cannot delete a homepage property in the database",
437                     errorMsg,
438                     JahiaException.DATABASE_ERROR,
439                     JahiaException.CRITICAL_SEVERITY);
440         } finally {
441
442             closeStatement ((Statement) pstmt);
443         }
444     }
445
446
447     /**
448      * Build a JahiaHomepage bean from a result set
449      *
450      * @return JahiaHomepage the bean
451      */

452     protected JahiaHomepage getHomepageFromResultSet (ResultSet rs)
453             throws JahiaException {
454
455         if (rs == null)
456             throw new JahiaException (CLASS_NAME + ".getHomepageFromResultSet",
457                     "Resultset is null",
458                     JahiaException.DATABASE_ERROR,
459                     JahiaException.ERROR_SEVERITY);
460
461         JahiaHomepagesService hps = ServicesRegistry.getInstance ()
462                 .getJahiaHomepagesService ();
463
464         if (hps == null)
465             throw new JahiaException (CLASS_NAME + ".getHomepageFromResultSet",
466                     "JahiaHomepageService is not available",
467                     JahiaException.SERVICE_ERROR,
468                     JahiaException.ERROR_SEVERITY);
469
470         int id = 0;
471         String JavaDoc name = "";
472         String JavaDoc descr = "";
473         String JavaDoc siteKey = "";
474         int type = 0;
475         int aclID = 0;
476
477         try {
478
479             id = rs.getInt ("id_jahia_homepages");
480             name = rs.getString ("name_jahia_homepages");
481             descr = rs.getString ("descr_jahia_homepages");
482             siteKey = rs.getString ("sitekey_jahia_homepages");
483             type = rs.getInt ("type_jahia_homepages");
484             aclID = rs.getInt ("rights_jahia_homepages");
485
486         } catch (SQLException se) {
487             String JavaDoc errorMsg = "DB Error : " + se.getMessage ();
488             logger.error (errorMsg);
489             throw new JahiaException (CLASS_NAME + ".getHomepageFromResultSet",
490                     errorMsg, JahiaException.DATABASE_ERROR,
491                     JahiaException.CRITICAL_SEVERITY);
492         }
493
494         JahiaHomepage hp = hps.createHomepage (name,
495                 descr,
496                 siteKey,
497                 type,
498                 new Hashtable JavaDoc (),
499                 aclID);
500         hp.setID (id);
501         return hp;
502     }
503
504
505     /**
506      * return a DOM representation of hompage def of a site
507      *
508      * @param String the site keys
509      *
510      * @return JahiaDOMObjet a DOM representation of homepage def of a site
511      *
512      * @author NK
513      */

514     public JahiaDOMObject getHomepageDefsAsDOM (String JavaDoc siteKey) throws JahiaException {
515
516         Connection dbConn = null;
517         Statement statement = null;
518
519         String JavaDoc output = null;
520         JahiaDBDOMObject dom = null;
521
522         try {
523             String JavaDoc sqlQuery = "SELECT * FROM jahia_homepages where sitekey_jahia_homepages='" + siteKey + "'";
524
525             dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
526             statement = dbConn.createStatement ();
527             if (statement != null) {
528                 ResultSet rs = statement.executeQuery (sqlQuery);
529                 if (rs != null) {
530                     dom = new JahiaDBDOMObject ();
531                     dom.addTable ("jahia_homepages", rs);
532                     return dom;
533                 }
534             }
535         } catch (SQLException se) {
536             String JavaDoc errorMsg = "Error in getHomepageDefsAsDOM() : " + se.getMessage ();
537             logger.error (errorMsg);
538             throw new JahiaException ("Cannot load data from the database",
539                     errorMsg, JahiaException.DATABASE_ERROR,
540                     JahiaException.CRITICAL_SEVERITY);
541         } finally {
542
543             closeStatement (statement);
544         }
545
546         return dom;
547     }
548
549
550     /**
551      * return a DOM representation of a site's homepage properties
552      *
553      * @param String siteKey the site key
554      *
555      * @return JahiaDOMObjet a DOM representation of a site's homepage def props
556      *
557      * @author NK
558      */

559     public JahiaDOMObject getHomepageDefPropsAsDOM (String JavaDoc siteKey) throws JahiaException {
560
561         Connection dbConn = null;
562         Statement statement = null;
563
564         String JavaDoc output = null;
565         JahiaDBDOMObject dom = null;
566
567         try {
568             String JavaDoc sqlQuery = "SELECT * FROM jahia_homepages_prop where sitekey_homepages_prop='" + siteKey + "'";
569
570             dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
571             statement = dbConn.createStatement ();
572             if (statement != null) {
573                 ResultSet rs = statement.executeQuery (sqlQuery);
574                 if (rs != null) {
575                     dom = new JahiaDBDOMObject ();
576                     dom.addTable ("jahia_homepages_prop", rs);
577                     return dom;
578                 }
579             }
580         } catch (SQLException se) {
581             String JavaDoc errorMsg = "Error in getHomepageDefPropsAsDOM() : " + se.getMessage ();
582             logger.error (errorMsg);
583             throw new JahiaException ("Cannot load data from the database",
584                     errorMsg, JahiaException.DATABASE_ERROR,
585                     JahiaException.CRITICAL_SEVERITY);
586         } finally {
587
588             closeStatement (statement);
589         }
590
591         return dom;
592     }
593
594     /**
595      * gets all home page acl ids for a given site
596      *
597      * @return a Vector of all acl id
598      */

599     public Vector JavaDoc db_get_all_acl_id (String JavaDoc siteKey)
600             throws JahiaException {
601         Connection dbConn = null;
602         Statement stmt = null;
603         ResultSet rs = null;
604         Vector JavaDoc theIDs = new Vector JavaDoc ();
605         try {
606             String JavaDoc sqlQuery = "SELECT DISTINCT rights_jahia_homepages FROM jahia_homepages "
607                     + "WHERE sitekey_jahia_homepages='" + siteKey + "'";
608
609             dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
610             stmt = dbConn.createStatement ();
611             rs = stmt.executeQuery (sqlQuery);
612
613             while (rs.next ()) {
614                 theIDs.add (new Integer JavaDoc (rs.getInt ("rights_jahia_homepages")));
615             }
616         } catch (SQLException se) {
617             String JavaDoc errorMsg = "Error in db_get_all_acl_id : " + se.getMessage ();
618             logger.error (errorMsg);
619             throw new JahiaException ("Cannot load acl id from the database",
620                     errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL_SEVERITY);
621         } finally {
622
623             closeStatement (stmt);
624         }
625         return theIDs;
626
627     }
628
629
630     private void executeQueryNoResultSet (String JavaDoc queryStr) throws JahiaException {
631
632         Connection dbConn = null;
633         Statement statement = null;
634
635         try {
636             dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
637             statement = dbConn.createStatement ();
638             if (statement != null) {
639                 statement.executeUpdate (queryStr);
640             }
641         } catch (SQLException se) {
642             String JavaDoc errorMsg = "Error in executeQueryNoResultSet(String queryStr) : " + se.getMessage ();
643             logger.error (errorMsg);
644             throw new JahiaException ("Cannot execute query" + queryStr,
645                     errorMsg,
646                     JahiaException.DATABASE_ERROR,
647                     JahiaException.CRITICAL_SEVERITY);
648         } finally {
649
650             closeStatement (statement);
651         }
652
653     }
654
655
656     private void closeStatement (Statement statement) {
657         // Close the opened statement
658
try {
659             if (statement != null) {
660                 statement.close ();
661                 statement = null;
662             }
663
664         } catch (SQLException sqlEx) {
665             logger.error ("Cannot close a statement", sqlEx);
666         }
667     }
668
669 }
670
671
Popular Tags