KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > services > sites > JahiaSitesPersistance


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
// JahiaSitesPersistance
14
//
15
// NK 12.03.2001
16
//
17
package org.jahia.services.sites;
18
19
20 import org.jahia.data.JahiaDBDOMObject;
21 import org.jahia.data.JahiaDOMObject;
22 import org.jahia.exceptions.JahiaException;
23 import org.jahia.exceptions.database.JahiaDatabaseException;
24 import org.jahia.registries.ServicesRegistry;
25 import org.jahia.services.acl.JahiaBaseACL;
26 import org.jahia.services.database.ConnectionDispenser;
27 import org.jahia.utils.JahiaTools;
28
29 import java.sql.Connection JavaDoc;
30 import java.sql.PreparedStatement JavaDoc;
31 import java.sql.ResultSet JavaDoc;
32 import java.sql.SQLException JavaDoc;
33 import java.sql.Statement JavaDoc;
34 import java.util.Enumeration JavaDoc;
35 import java.util.Properties JavaDoc;
36 import java.util.Vector JavaDoc;
37
38 /**
39  * persistance storage of jahia sites informations
40  *
41  * @author Khue ng
42  */

43 class JahiaSitesPersistance implements SitesDBInterface {
44     private static org.apache.log4j.Logger logger
45             = org.apache.log4j.Logger.getLogger (JahiaSitesPersistance.class);
46
47     private static final String JavaDoc CLASS_NAME = JahiaSitesPersistance.class.getName ();
48     /** unique instance */
49     private static JahiaSitesPersistance instance = null;
50
51     public static final int ORDER_BY_SERVER_NAME = 1;
52     public static final int ORDER_BY_TITLE = 2;
53
54
55     /**
56      * constructor
57      */

58     protected JahiaSitesPersistance () {
59
60     }
61
62
63     /**
64      * return the singleton instance of this class
65      */

66     public static synchronized JahiaSitesPersistance getInstance () {
67
68         if (instance == null) {
69             instance = new JahiaSitesPersistance ();
70         }
71         return instance;
72     }
73
74
75     /**
76      * return the list of all sites
77      *
78      * @return a Vector of JahiaSite bean
79      */

80     public Vector JavaDoc dbGetSites () throws JahiaException {
81
82         //return dbGetSites(ORDER_BY_TITLE);
83
return dbGetSites (ORDER_BY_SERVER_NAME);
84     }
85
86
87     /**
88      * return the list of all sites
89      *
90      * @param orderType the Order attribute
91      *
92      * @return Vector of JahiaSite bean
93      */

94     public Vector JavaDoc dbGetSites (int orderType) throws JahiaException {
95
96         Connection JavaDoc dbConn = null;
97         PreparedStatement JavaDoc statement = null;
98
99         Vector JavaDoc sites = new Vector JavaDoc ();
100         JahiaSite site = null;
101
102         try {
103             String JavaDoc sqlQuery = "SELECT * FROM jahia_sites" + getOrderCond (orderType);
104
105             dbConn = ConnectionDispenser.getConnection ();
106             statement = dbConn.prepareStatement (sqlQuery);
107             ResultSet JavaDoc rs = statement.executeQuery ();
108             if (rs != null) {
109                while (rs.next ()) {
110                    site = getJahiaSiteFromResultSet (rs);
111                    sites.add (site);
112                }
113             }
114
115         } catch (SQLException JavaDoc se) {
116             String JavaDoc errorMsg = "Error in dbGetSites : " + se.getMessage ();
117             logger.debug ("Error retrieving sites : ", se);
118             throw new JahiaException ("Cannot load sites from the database",
119                     errorMsg, JahiaException.DATABASE_ERROR,
120                     JahiaException.CRITICAL_SEVERITY, se);
121         } finally {
122             closeStatement (statement);
123         }
124
125         Enumeration JavaDoc enumeration = sites.elements ();
126         while (enumeration.hasMoreElements ()) {
127             JahiaSite el = (JahiaSite) enumeration.nextElement ();
128             Properties JavaDoc props = this.getSiteProps (el.getID ());
129             el.setSettings (props);
130         }
131
132         return sites;
133     }
134
135
136     /**
137      * return the JahiaSite bean looking at it id
138      *
139      * @param id the JahiaSite id
140      *
141      * @return JahiaSite the JahiaSite bean
142      */

143     public JahiaSite dbGetSite (int id) throws JahiaException {
144
145         Connection JavaDoc dbConn = null;
146         PreparedStatement JavaDoc statement = null;
147
148         JahiaSite site = null;
149
150         try {
151             String JavaDoc sqlQuery = "SELECT * FROM jahia_sites where id_jahia_sites=?";
152
153             dbConn = ConnectionDispenser.getConnection ();
154             statement = dbConn.prepareStatement (sqlQuery);
155             statement.setInt(1, id);
156             ResultSet JavaDoc rs = statement.executeQuery ();
157             if (rs != null && rs.next ()) {
158                 site = getJahiaSiteFromResultSet (rs);
159             }
160         } catch (SQLException JavaDoc se) {
161             String JavaDoc errorMsg = "Error in dbGetSite(int id) : " + se.getMessage ();
162             logger.debug ("Error retrieving site from the database : ", se);
163             throw new JahiaException ("Cannot load site from the database",
164                     errorMsg, JahiaException.DATABASE_ERROR,
165                     JahiaException.CRITICAL_SEVERITY, se);
166         } finally {
167
168             closeStatement (statement);
169         }
170
171         return site;
172     }
173
174
175     /**
176      * return the JahiaSite bean looking at it key
177      *
178      * @param siteKey the JahiaSite key
179      *
180      * @return JahiaSite the JahiaSite bean
181      */

182     public JahiaSite dbGetSiteByKey (String JavaDoc siteKey) throws JahiaException {
183
184         Connection JavaDoc dbConn = null;
185         PreparedStatement JavaDoc statement = null;
186
187         JahiaSite site = null;
188
189         try {
190             String JavaDoc sqlQuery =
191                     "SELECT * FROM jahia_sites where key_jahia_sites=?";
192
193             dbConn = ConnectionDispenser.getConnection ();
194             statement = dbConn.prepareStatement (sqlQuery);
195             statement.setString(1, siteKey);
196
197             ResultSet JavaDoc rs = statement.executeQuery ();
198             if (rs != null && rs.next ()) {
199                 site = getJahiaSiteFromResultSet (rs);
200             }
201         } catch (SQLException JavaDoc se) {
202             String JavaDoc errorMsg = "Error in dbGetSiteByKey(" + siteKey +
203                     ") : " + se.getMessage ();
204             logger.debug ("Error retrieving site by key " + siteKey, se);
205             throw new JahiaException ("Cannot load site from the database",
206                     errorMsg, JahiaException.DATABASE_ERROR,
207                     JahiaException.CRITICAL_SEVERITY,
208                     se);
209         } finally {
210
211             closeStatement (statement);
212         }
213
214         return site;
215     }
216
217     /**
218      * return the JahiaSite bean looking at its server name
219      *
220      * @param serverName the server name to look for
221      *
222      * @return JahiaSite the JahiaSite bean
223      */

224     public JahiaSite dbGetSiteByServerName (String JavaDoc serverName) throws JahiaException {
225
226         Connection JavaDoc dbConn = null;
227         PreparedStatement JavaDoc statement = null;
228
229         JahiaSite site = null;
230
231         try {
232             String JavaDoc sqlQuery =
233                     "SELECT * FROM jahia_sites where servername_jahia_sites=?";
234
235             dbConn = ConnectionDispenser.getConnection ();
236             statement = dbConn.prepareStatement (sqlQuery);
237             statement.setString(1, serverName);
238             ResultSet JavaDoc rs = statement.executeQuery ();
239             if (rs != null && rs.next ()) {
240                 site = getJahiaSiteFromResultSet (rs);
241             }
242         } catch (SQLException JavaDoc se) {
243             String JavaDoc errorMsg = "Error in dbGetSiteByServerName(" + serverName +
244                     ") : " + se.getMessage ();
245             logger.debug ("Error retrieving site by key " + serverName, se);
246             throw new JahiaException ("Cannot load site from the database",
247                     errorMsg, JahiaException.DATABASE_ERROR,
248                     JahiaException.CRITICAL_SEVERITY,
249                     se);
250         } finally {
251
252             closeStatement (statement);
253         }
254
255         return site;
256     }
257
258     /**
259      * return a site looking at it's server name ( www.jahia.org )
260      *
261      * @param name server name ( www.jahia.org )
262      *
263      * @return JahiaSite the JahiaSite bean or null
264      */

265     public JahiaSite dbGetSite (String JavaDoc name) throws JahiaException {
266
267         Connection JavaDoc dbConn = null;
268         PreparedStatement JavaDoc statement = null;
269
270         JahiaSite site = null;
271
272         try {
273             String JavaDoc sqlQuery =
274                     "SELECT * FROM jahia_sites WHERE servername_jahia_sites=?";
275
276             dbConn = ConnectionDispenser.getConnection ();
277             statement = dbConn.prepareStatement (sqlQuery);
278             statement.setString(1, JahiaTools.quote (name));
279             ResultSet JavaDoc rs = statement.executeQuery ();
280             if (rs != null && rs.next ()) {
281                 site = getJahiaSiteFromResultSet (rs);
282             }
283         } catch (SQLException JavaDoc se) {
284             String JavaDoc errorMsg = "Error in dbGetSite(String name) : " + se.getMessage ();
285             logger.debug ("Error while retrieving site by name [" + name + "]", se);
286             throw new JahiaException ("Cannot load site from the database",
287                     errorMsg,
288                     JahiaException.DATABASE_ERROR,
289                     JahiaException.CRITICAL_SEVERITY,
290                     se);
291         } finally {
292
293             closeStatement (statement);
294         }
295
296         return site;
297     }
298
299
300     /**
301      * add a new site
302      *
303      * @param site the JahiaSite bean
304      */

305     public void dbAddSite (JahiaSite site) throws JahiaException {
306
307         try {
308
309             int id = ServicesRegistry.getInstance ().getJahiaIncrementorsDBService ()
310                     .autoIncrement ("jahia_sites");
311             StringBuffer JavaDoc sqlQuery = new StringBuffer JavaDoc ("INSERT INTO jahia_sites VALUES(");
312             sqlQuery.append (id);
313             sqlQuery.append (",'");
314             sqlQuery.append (JahiaTools.quote (site.getTitle ()));
315             sqlQuery.append ("','");
316             sqlQuery.append (JahiaTools.quote (site.getServerName ()));
317             sqlQuery.append ("','");
318             sqlQuery.append (JahiaTools.quote (site.getSiteKey ()));
319             sqlQuery.append ("',");
320
321             if (site.isActive ()) {
322                 sqlQuery.append (1);
323             } else {
324                 sqlQuery.append (0);
325             }
326
327             sqlQuery.append (",");
328             sqlQuery.append (site.getHomePageID ());
329             sqlQuery.append (",");
330             sqlQuery.append (site.getDefaultTemplateID ());
331             sqlQuery.append (",");
332
333             if (site.getTemplatesAutoDeployMode ()) {
334                 sqlQuery.append (1);
335             } else {
336                 sqlQuery.append (0);
337             }
338
339             sqlQuery.append (",");
340             if (site.getWebAppsAutoDeployMode ()) {
341                 sqlQuery.append (1);
342             } else {
343                 sqlQuery.append (0);
344             }
345
346             sqlQuery.append (",");
347             sqlQuery.append (site.getAclID ());
348             sqlQuery.append (",'");
349             sqlQuery.append (JahiaTools.quote (site.getDescr ()));
350             sqlQuery.append ("')");
351
352             executeQueryNoResultSet (sqlQuery.toString ());
353
354             logger.debug ("Adding site query=[" + sqlQuery + "]");
355
356             site.setID (id);
357
358         } catch (JahiaException je) {
359             String JavaDoc errorMsg = "Error in dbAddSite(JahiaSite site) : " + je.getMessage ();
360             logger.debug ("Error while adding site in database : ", je);
361             throw new JahiaException ("Cannot add site in the database",
362                     errorMsg,
363                     JahiaException.DATABASE_ERROR,
364                     JahiaException.CRITICAL_SEVERITY,
365                     je);
366         }
367     }
368
369
370     /**
371      * remove a site
372      *
373      * @param id the site id
374      */

375     public void dbRemoveSite (int id) throws JahiaException {
376
377         try {
378             String JavaDoc sqlQuery = "DELETE FROM jahia_sites WHERE id_jahia_sites=" + id;
379             executeQueryNoResultSet (sqlQuery);
380         } catch (JahiaException je) {
381             String JavaDoc errorMsg = "Error in dbRemoveSite(int id) : " + je.getMessage ();
382             logger.debug ("Error while removing site : ", je);
383             throw new JahiaException ("Cannot remove site in the database",
384                     errorMsg,
385                     JahiaException.DATABASE_ERROR,
386                     JahiaException.CRITICAL_SEVERITY,
387                     je);
388         }
389     }
390
391
392     /**
393      * Update a JahiaSite definition
394      *
395      * @param site the site bean object
396      */

397     public void dbUpdateSite (JahiaSite site) throws JahiaException {
398
399         if ( site == null ){
400             return;
401         }
402         if ( site.getID()<=0 ){
403             throw new JahiaException("Error updating JahiaSite",
404                                      "Invalid site Id[" + site.getID() + "]",
405                                      JahiaException.DATA_ERROR,
406                                      JahiaException.ERROR_SEVERITY);
407         }
408
409         try {
410             StringBuffer JavaDoc sqlQuery = new StringBuffer JavaDoc ("UPDATE jahia_sites SET ");
411             sqlQuery.append ("id_jahia_sites=");
412             sqlQuery.append (site.getID ());
413             sqlQuery.append (", title_jahia_sites='");
414             sqlQuery.append (JahiaTools.quote (site.getTitle ()));
415             sqlQuery.append ("', servername_jahia_sites='");
416             sqlQuery.append (JahiaTools.quote (site.getServerName ()));
417             sqlQuery.append ("', key_jahia_sites='");
418             sqlQuery.append (JahiaTools.quote (site.getSiteKey ()));
419
420             sqlQuery.append ("', active_jahia_sites=");
421
422             if (site.isActive ()) {
423                 sqlQuery.append (1);
424             } else {
425                 sqlQuery.append (0);
426             }
427
428             sqlQuery.append (", defaultpageid_jahia_sites=");
429             sqlQuery.append (site.getHomePageID ());
430             sqlQuery.append (", defaulttemplateid_jahia_sites=");
431             sqlQuery.append (site.getDefaultTemplateID ());
432
433             sqlQuery.append (", tpl_deploymode_jahia_sites=");
434
435             if (site.getTemplatesAutoDeployMode ()) {
436                 sqlQuery.append (1);
437             } else {
438                 sqlQuery.append (0);
439             }
440
441             sqlQuery.append (", webapps_deploymode_jahia_sites=");
442
443             if (site.getWebAppsAutoDeployMode ()) {
444                 sqlQuery.append (1);
445             } else {
446                 sqlQuery.append (0);
447             }
448
449             sqlQuery.append (", rights_jahia_sites=");
450             sqlQuery.append (site.getAclID ());
451             sqlQuery.append (", descr_jahia_sites='");
452             sqlQuery.append (JahiaTools.quote (site.getDescr ()));
453             sqlQuery.append ("' where id_jahia_sites=");
454             sqlQuery.append (site.getID ());
455
456             executeQueryNoResultSet (sqlQuery.toString ());
457
458         } catch (JahiaException je) {
459             String JavaDoc errorMsg = "Error in dbUpdateSite( JahiaSite site ) : " + je.getMessage ();
460             logger.debug ("Error while updating site : ", je);
461             throw new JahiaException ("Cannot update site in the database",
462                     errorMsg,
463                     JahiaException.DATABASE_ERROR,
464                     JahiaException.CRITICAL_SEVERITY,
465                     je);
466         }
467
468         updateSiteProps (site);
469     }
470
471
472     /**
473      * Build a JahiaSite bean from a result set
474      *
475      * @return JahiaSite the site bean
476      */

477     protected JahiaSite getJahiaSiteFromResultSet (ResultSet JavaDoc rs) throws JahiaException {
478
479         JahiaSite site = null;
480
481         if (rs != null) {
482
483             int id = 0;
484             String JavaDoc title = "";
485             String JavaDoc serverName = "";
486             String JavaDoc siteKey = "";
487             boolean isActive = false;
488             int defaultPageID = -1;
489             int defaultTemplateID = -1;
490             boolean templatesAutoDeployMode = true;
491             boolean webAppsAutoDeployMode = true;
492             String JavaDoc descr = "";
493             int aclID = 0;
494
495             try {
496
497                 id = rs.getInt ("id_jahia_sites");
498                 title = rs.getString ("title_jahia_sites");
499                 serverName = rs.getString ("servername_jahia_sites");
500                 siteKey = rs.getString ("key_jahia_sites");
501                 isActive = (rs.getInt ("active_jahia_sites") == 1);
502                 defaultPageID = rs.getInt ("defaultpageid_jahia_sites");
503                 defaultTemplateID = rs.getInt ("defaulttemplateid_jahia_sites");
504                 templatesAutoDeployMode = (rs.getInt ("tpl_deploymode_jahia_sites") == 1);
505                 webAppsAutoDeployMode = (rs.getInt ("webapps_deploymode_jahia_sites") == 1);
506                 aclID = rs.getInt ("rights_jahia_sites");
507                 descr = rs.getString ("descr_jahia_sites");
508
509             } catch (SQLException JavaDoc se) {
510                 String JavaDoc errorMsg = "Error in getJahiaSiteFromResultSet(rs) : " + se.getMessage ();
511                 logger.debug ("Error while converting result set to JahiaSite : ", se);
512                 throw new JahiaException ("Cannot read data from resultset",
513                         errorMsg,
514                         JahiaException.DATABASE_ERROR,
515                         JahiaException.CRITICAL_SEVERITY,
516                         se);
517             }
518
519             JahiaBaseACL acl = null;
520             try {
521                 acl = new JahiaBaseACL ();
522                 acl.load (aclID);
523
524             } catch (JahiaException je) {
525                 logger.debug ("Error while retrieving site ACL : ", je);
526                 acl = null;
527             }
528
529             site = new JahiaSite (
530                     id,
531                     title,
532                     serverName,
533                     siteKey,
534                     isActive,
535                     defaultPageID,
536                     descr,
537                     acl,
538                     null
539             );
540             site.setDefaultTemplateID (defaultTemplateID);
541             site.setTemplatesAutoDeployMode (templatesAutoDeployMode);
542             site.setWebAppsAutoDeployMode (webAppsAutoDeployMode);
543         }
544
545
546         return site;
547     }
548
549
550     /**
551      * generate the order sql condition
552      */

553     private String JavaDoc getOrderCond (int orderType) {
554
555         // default order
556
String JavaDoc orderCond = " ORDER BY title_jahia_sites ";
557
558         switch (orderType) {
559             case ORDER_BY_SERVER_NAME:
560                 orderCond = " ORDER BY servername_jahia_sites ";
561                 break;
562         }
563
564         return orderCond;
565     }
566
567
568     /**
569      * Return the amount of sites in the database.
570      *
571      * @return The amount of sites.
572      */

573     public synchronized int getNbSites ()
574             throws JahiaDatabaseException {
575         int counter = -1;
576
577         Connection JavaDoc dbConn = null;
578         PreparedStatement JavaDoc statement = null;
579
580         try {
581             dbConn = ConnectionDispenser.getConnection ();
582
583             String JavaDoc query = "SELECT COUNT(key_jahia_sites) as nbItems FROM jahia_sites";
584
585             statement = dbConn.prepareStatement (query);
586             ResultSet JavaDoc rs = statement.executeQuery ();
587             if (rs != null && rs.next ()) {
588                 counter = rs.getInt ("nbItems");
589             }
590         } catch (SQLException JavaDoc ex) {
591             throw new JahiaDatabaseException ("Database error.", ex,
592                     JahiaDatabaseException.
593                     ERROR_SEVERITY);
594         } finally {
595             closeStatement (statement);
596         }
597         return counter;
598     }
599
600
601     /**
602      * Returns a properties as a String.
603      *
604      * @param siteID the site identification number
605      * @param key the property name
606      */

607     String JavaDoc getProperty (int siteID, String JavaDoc key)
608             throws JahiaException {
609
610         Connection JavaDoc dbConn = null;
611         PreparedStatement JavaDoc statement = null;
612         String JavaDoc value = null;
613
614         if (key == null || key.trim ().length() == 0)
615             return null;
616
617         try {
618             String JavaDoc query =
619                   "SELECT value_jahia_site_prop FROM jahia_site_prop WHERE id_jahia_site=? AND name_jahia_site_prop=?";
620             
621             dbConn = ConnectionDispenser.getConnection ();
622             statement = dbConn.prepareStatement (query);
623             statement.setInt(1, siteID);
624             statement.setString(2, JahiaTools.quote (key));
625             ResultSet JavaDoc rs = statement.executeQuery ();
626             if (rs != null && rs.next ()) {
627                 value = rs.getString ("value_jahia_site_prop");
628             }
629         } catch (SQLException JavaDoc se) {
630             logger.debug ("Error while retrieving property siteID=" + siteID + " key=" + key,
631                     se);
632             throw new JahiaException (CLASS_NAME + ".getProperty(siteID,key)",
633                     "Cannot load site's prop from the database"
634                     + se.getMessage (),
635                     JahiaException.DATABASE_ERROR,
636                     JahiaException.CRITICAL_SEVERITY,
637                     se);
638         } finally {
639
640             closeStatement (statement);
641         }
642
643         return value;
644
645     }
646
647
648     /**
649      * add a property. Do not check if there is already a same
650      * property in db.You must remove it first.
651      *
652      * @param siteID the site identification number
653      * @param key the property name
654      * @param value the property value
655      */

656     void addProperty (int siteID, String JavaDoc key, String JavaDoc value) throws JahiaException {
657         
658         if (key == null || key.trim().length() == 0)
659             return;
660         
661         Connection JavaDoc dbConn = null;
662         PreparedStatement JavaDoc statement = null;
663         String JavaDoc query =
664             "INSERT INTO jahia_site_prop VALUES (?, ?, ?)";
665         
666         try {
667             dbConn = ConnectionDispenser.getConnection ();
668             statement = dbConn.prepareStatement (query);
669             statement.setInt(1, siteID);
670             statement.setString(2, JahiaTools.quote(key));
671             statement.setString(3, JahiaTools.quote (value));
672
673             statement.executeUpdate ();
674         } catch (SQLException JavaDoc se) {
675             String JavaDoc errorMsg = "Error in addProperty : " + se.getMessage ();
676             logger.debug ("Error while executing update-type query (" + query + ") : ", se);
677             throw new JahiaException ("Cannot execute query" + query,
678                     errorMsg, JahiaException.DATABASE_ERROR,
679                     JahiaException.CRITICAL_SEVERITY, se);
680         } finally {
681
682             closeStatement (statement);
683         }
684     }
685
686     /**
687      * Update a property. If property is not existing, it is inserted.
688      *
689      * @param siteID the site identification number
690      * @param key the property name
691      * @param value the property value
692      */

693     void updateProperty (int siteID, String JavaDoc key, String JavaDoc value) throws JahiaException {
694         
695         if (key == null || key.trim().length() == 0)
696             return;
697         
698         Connection JavaDoc dbConn = null;
699         PreparedStatement JavaDoc statement = null;
700         String JavaDoc query =
701             "UPDATE jahia_site_prop SET value_jahia_site_prop=? WHERE id_jahia_site=? AND name_jahia_site_prop=?";
702         
703         try {
704             dbConn = ConnectionDispenser.getConnection ();
705             statement = dbConn.prepareStatement (query);
706             statement.setString(1, JahiaTools.quote (value));
707             statement.setInt(2, siteID);
708             statement.setString(3, JahiaTools.quote(key));
709
710             int rowCount = statement.executeUpdate ();
711             if (rowCount == 0) {
712                 addProperty(siteID, key, value);
713             }
714         } catch (SQLException JavaDoc se) {
715             String JavaDoc errorMsg = "Error in addProperty : " + se.getMessage ();
716             logger.debug ("Error while executing update-type query (" + query + ") : ", se);
717             throw new JahiaException ("Cannot execute query" + query,
718                     errorMsg, JahiaException.DATABASE_ERROR,
719                     JahiaException.CRITICAL_SEVERITY, se);
720         } finally {
721             closeStatement (statement);
722         }
723     }
724     
725     /**
726      * delete all properties of a site
727      *
728      * @param siteID the site identification number
729      */

730     void deleteProperties (int siteID) throws JahiaException {
731
732         try {
733             StringBuffer JavaDoc buff =
734                     new StringBuffer JavaDoc ("DELETE FROM jahia_site_prop WHERE id_jahia_site=");
735             buff.append (siteID);
736             executeQueryNoResultSet (buff.toString ());
737         } catch (JahiaException je) {
738             String JavaDoc errorMsg = "Error in deleteProperties(siteID) : "
739                     + je.getMessage ();
740             logger.debug ("Error while deleting site properties for siteID=" + siteID, je);
741
742             throw new JahiaException ("Cannot delete all site's properties in the database",
743                     errorMsg,
744                     JahiaException.DATABASE_ERROR,
745                     JahiaException.CRITICAL_SEVERITY, je);
746         }
747     }
748
749
750     /**
751      * remove a property of a site
752      *
753      * @param id the site identification number
754      * @param key the property name
755      */

756     void deleteProperty (int id, String JavaDoc key) throws JahiaException {
757
758         if (key == null || key.trim ().length() == 0)
759             return;
760         Connection JavaDoc dbConn = null;
761         PreparedStatement JavaDoc statement = null;
762         String JavaDoc sqlQuery =
763             "DELETE FROM jahia_site_prop WHERE id_jahia_site=? AND name_jahia_site_prop=?";
764         try {
765             dbConn = ConnectionDispenser.getConnection ();
766             statement = dbConn.prepareStatement (sqlQuery);
767             statement.setInt(1, id);
768             statement.setString(2, JahiaTools.quote (key));
769
770             statement.executeUpdate ();
771         } catch (SQLException JavaDoc se) {
772             String JavaDoc errorMsg = "Error in deleteProperty : " + se.getMessage ();
773             logger.debug ("Error while executing update-type query (" + sqlQuery + ") : ", se);
774             throw new JahiaException ("Cannot execute query" + sqlQuery,
775                     errorMsg, JahiaException.DATABASE_ERROR,
776                     JahiaException.CRITICAL_SEVERITY, se);
777         } finally {
778             closeStatement (statement);
779         }
780     }
781
782     private void executeQueryNoResultSet (String JavaDoc queryStr) throws JahiaException {
783
784         Connection JavaDoc dbConn = null;
785         Statement JavaDoc statement = null;
786
787         try {
788             dbConn = ConnectionDispenser.getConnection ();
789             statement = dbConn.createStatement ();
790             if (statement != null) {
791                 statement.executeUpdate (queryStr);
792             }
793         } catch (SQLException JavaDoc se) {
794             String JavaDoc errorMsg = "Error in executeQueryNoResultSet(String queryStr) : " + se.getMessage ();
795             logger.debug ("Error while executing update-type query (" + queryStr + ") : ", se);
796             throw new JahiaException ("Cannot execute query" + queryStr,
797                     errorMsg, JahiaException.DATABASE_ERROR,
798                     JahiaException.CRITICAL_SEVERITY, se);
799         } finally {
800
801             closeStatement (statement);
802         }
803
804     }
805
806
807     private void closeStatement (Statement JavaDoc statement) {
808         // Close the opened statement
809
try {
810             if (statement != null) {
811                 statement.close ();
812                 statement = null;
813             }
814         } catch (SQLException JavaDoc sqlEx) {
815             logger.warn("Cannot close a statement", sqlEx);
816         }
817     }
818
819
820     /**
821      * return a DOM representation of a site
822      *
823      * @param id the site identification number
824      *
825      * @return JahiaDOMObjet a DOM representation of this object
826      */

827     public JahiaDOMObject getSiteAsDOM (int id) throws JahiaException {
828
829         Connection JavaDoc dbConn = null;
830         Statement JavaDoc statement = null;
831         JahiaDBDOMObject dom = null;
832
833         try {
834             String JavaDoc sqlQuery = "SELECT * FROM jahia_sites where id_jahia_sites=" + id;
835
836             dbConn = ConnectionDispenser.getConnection ();
837             statement = dbConn.createStatement ();
838             if (statement != null) {
839                 ResultSet JavaDoc rs = statement.executeQuery (sqlQuery);
840                 if (rs != null) {
841                     dom = new JahiaDBDOMObject ();
842                     dom.addTable ("jahia_sites", rs);
843                     return dom;
844                 }
845             }
846         } catch (SQLException JavaDoc se) {
847             String JavaDoc errorMsg = "Error in getSiteAsDOM(int id) : " + se.getMessage ();
848             logger.debug ("Error while converting site " + id + " to DOM : ", se);
849             throw new JahiaException ("Cannot load site from the database",
850                     errorMsg, JahiaException.DATABASE_ERROR,
851                     JahiaException.CRITICAL_SEVERITY, se);
852         } finally {
853
854             closeStatement (statement);
855         }
856
857         return dom;
858     }
859
860
861     /**
862      * return a DOM representation of a site's properties
863      *
864      * @param id the site identification number
865      *
866      * @return JahiaDOMObjet a DOM representation of this object
867      */

868     public JahiaDOMObject getSitePropsAsDOM (int id) throws JahiaException {
869
870         Connection JavaDoc dbConn = null;
871         Statement JavaDoc statement = null;
872         JahiaDBDOMObject dom = null;
873
874         try {
875             String JavaDoc sqlQuery = "SELECT * FROM jahia_site_prop where id_jahia_site=" + id;
876
877             dbConn = ConnectionDispenser.getConnection ();
878
879             statement = dbConn.createStatement ();
880             if (statement != null) {
881                 ResultSet JavaDoc rs = statement.executeQuery (sqlQuery);
882                 if (rs != null) {
883                     dom = new JahiaDBDOMObject ();
884                     dom.addTable ("jahia_site_prop", rs);
885                     return dom;
886                 }
887             }
888         } catch (SQLException JavaDoc se) {
889             String JavaDoc errorMsg = "Error in getSitePropsAsDOM(int id) : " + se.getMessage ();
890             logger.debug ("Error while converting site properties to DOM for siteID=" + id, se);
891             throw new JahiaException ("Cannot load data from the database",
892                     errorMsg, JahiaException.DATABASE_ERROR,
893                     JahiaException.CRITICAL_SEVERITY, se);
894         } finally {
895
896             closeStatement (statement);
897         }
898
899         return dom;
900     }
901
902
903     /**
904      * return all properties of a site
905      *
906      * @param id
907      *
908      * @return
909      *
910      * @throws JahiaException
911      */

912     public Properties JavaDoc getSiteProps (int id) throws JahiaException {
913
914         Connection JavaDoc dbConn = null;
915         PreparedStatement JavaDoc statement = null;
916         Properties JavaDoc props = new Properties JavaDoc ();
917
918         try {
919             String JavaDoc sqlQuery = "SELECT * FROM jahia_site_prop where id_jahia_site=?";
920
921             dbConn = ConnectionDispenser.getConnection ();
922
923             statement = dbConn.prepareStatement (sqlQuery);
924             statement.setInt(1, id);
925             ResultSet JavaDoc rs = statement.executeQuery ();
926             while (rs.next ()) {
927                 String JavaDoc propName = rs.getString ("name_jahia_site_prop");
928                 String JavaDoc propValue = rs.getString ("value_jahia_site_prop");
929                 props.setProperty (propName, propValue);
930             }
931
932         } catch (SQLException JavaDoc se) {
933             String JavaDoc errorMsg = "Error in getSitePropsAsDOM(int id) : " + se.getMessage ();
934             logger.debug ("Error while converting site properties to DOM for siteID=" + id, se);
935             throw new JahiaException ("Cannot load data from the database",
936                     errorMsg, JahiaException.DATABASE_ERROR,
937                     JahiaException.CRITICAL_SEVERITY, se);
938
939         } finally {
940             closeStatement (statement);
941         }
942
943         return props;
944     }
945
946
947     /**
948      * Save site settings in db
949      *
950      * @param site
951      *
952      * @throws JahiaException
953      */

954     void updateSiteProps (JahiaSite site) throws JahiaException {
955         Properties JavaDoc props = site.getSettings ();
956         if (props == null) {
957             return;
958         }
959
960         Enumeration JavaDoc enumeration = props.propertyNames ();
961         while (enumeration.hasMoreElements ()) {
962             String JavaDoc name = (String JavaDoc) enumeration.nextElement ();
963             String JavaDoc value = props.getProperty (name);
964             
965             updateProperty (site.getID (), name, value);
966         }
967     }
968
969 }
970
Popular Tags