KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > services > htmlparser > MarkupSettingPersistence


1 package org.jahia.services.htmlparser;
2
3 import java.sql.Connection JavaDoc;
4 import java.sql.ResultSet JavaDoc;
5 import java.sql.PreparedStatement JavaDoc;
6 import java.sql.SQLException JavaDoc;
7
8 import java.util.ArrayList JavaDoc;
9 import java.util.Enumeration JavaDoc;
10 import java.util.Iterator JavaDoc;
11 import java.util.Properties JavaDoc;
12 import java.util.Hashtable JavaDoc;
13
14 import org.jahia.exceptions.JahiaException;
15 import org.jahia.registries.ServicesRegistry;
16
17
18 /**
19  *
20  * <p>Title: DB Persistence for markup settings</p>
21  * <p>Description: DB Persistence for markup settings</p>
22  * <p>Copyright: Copyright (c) 2002</p>
23  * <p>Company: </p>
24  * @author Khue Nguyen
25  * @version 1.0
26  */

27 class MarkupSettingPersistence {
28
29     static private final String JavaDoc LOAD_ALL_MARKUP_SETTING_QUERY = "SELECT * FROM jahia_markup_set";
30     static private final String JavaDoc GET_MARKUP_SETTING_BYID_QUERY = "SELECT * FROM jahia_markup_set WHERE setting_id =?";
31     static private final String JavaDoc UPDATE_MARKUP_SETTING_BYID_QUERY = "UPDATE jahia_markup_set SET setting_type=?, markupdef_id=?, apply_to_all_site=? WHERE setting_id =?";
32     static private final String JavaDoc INSERT_MARKUP_SETTING_QUERY = "INSERT INTO jahia_markup_set (setting_id, setting_type, markupdef_id, apply_to_all_site) VALUES (?, ?, ?, ?)";
33     static private final String JavaDoc REMOVE_MARKUP_SETTING_BYID_QUERY = "DELETE FROM jahia_markup_set WHERE setting_id=?";
34
35     static private final String JavaDoc GET_ALL_MARKUP_SETTING_PROP_BYID_QUERY = "SELECT * FROM jahia_markup_set_prop WHERE setting_id =?";
36     static private final String JavaDoc INSERT_MARKUP_SETTING_PROP_QUERY = "INSERT INTO jahia_markup_set_prop (setting_id, prop_name, prop_value) VALUES (?, ?, ?)";
37     static private final String JavaDoc REMOVE_ALL_MARKUP_SETTING_PROP_BYID_QUERY = "DELETE FROM jahia_markup_set_prop WHERE setting_id=?";
38
39     static private final String JavaDoc GET_ALL_SITE_MARKUP_SETTING_QUERY = "SELECT * FROM jahia_markup_set_site";
40     static private final String JavaDoc LOAD_ALL_SITE_MARKUP_SETTING_BY_SETTINGID_QUERY = "SELECT * FROM jahia_markup_set_site WHERE setting_id=?";
41     static private final String JavaDoc REMOVE_SITE_MARKUP_SETTING_QUERY = "DELETE FROM jahia_markup_set_site WHERE site_id=? AND setting_id=?";
42     static private final String JavaDoc INSERT_SITE_MARKUP_SETTING_QUERY = "INSERT INTO jahia_markup_set_site (setting_id, site_id) VALUES(?,?)";;
43     static private final String JavaDoc REMOVE_ALL_SITE_MARKUP_SETTING_BY_SETTINGID_QUERY = "DELETE FROM jahia_markup_set_site WHERE setting_id=?";
44
45     private MarkupSettingPersistence() {
46     }
47
48     private static org.apache.log4j.Logger logger =
49         org.apache.log4j.Logger.getLogger(MarkupSettingPersistence.class);
50
51     private static MarkupSettingPersistence instance = null;
52
53     /**
54      * @return an instance of this class
55      */

56     public static synchronized MarkupSettingPersistence getInstance () {
57         if (instance == null) {
58             instance = new MarkupSettingPersistence();
59         }
60         return instance;
61     }
62
63     protected Hashtable JavaDoc loadAllMarkupSettings ()
64         throws JahiaException {
65
66         Hashtable JavaDoc result = new Hashtable JavaDoc();
67
68         MarkupSetting mSetting = null;
69         Connection JavaDoc dbConn = null;
70         PreparedStatement JavaDoc stmt = null;
71         ResultSet JavaDoc rs = null;
72
73         try {
74
75             dbConn = org.jahia.services.database.ConnectionDispenser.getConnection();
76             stmt = dbConn.prepareStatement(LOAD_ALL_MARKUP_SETTING_QUERY);
77             rs = stmt.executeQuery();
78
79             while (rs.next()) {
80                 int id = rs.getInt(1);
81                 String JavaDoc settingType = rs.getString(2);
82                 int markupDefId = rs.getInt(3);
83                 int applyToAllSite = rs.getInt(4);
84
85                 mSetting = new MarkupSetting(settingType, markupDefId);
86                 mSetting.setSettingId(id);
87                 mSetting.setApplyToAllSite(applyToAllSite==1);
88                 result.put(new Integer JavaDoc(id),mSetting);
89             }
90
91         } catch (SQLException JavaDoc se) {
92             throw new JahiaException("Error loading markup settings from the database",
93                                      se.getMessage(),
94                                      JahiaException.DATABASE_ERROR,
95                                      JahiaException.ERROR_SEVERITY, se);
96         } finally {
97             try {
98                 if (stmt != null)
99                     stmt.close();
100             } catch (SQLException JavaDoc ex) {
101                 logger.debug("Cannot free resources",ex);
102             }
103         }
104
105         Iterator JavaDoc iterator = result.values().iterator();
106         while ( iterator.hasNext() ){
107             MarkupSetting setting = (MarkupSetting)iterator.next();
108             setting.setProperties(loadMarkupSettingProperties(setting.getSettingId()));
109             setting.getSites().addAll(loadAllSiteMarkupSettingBySettingId(setting.getSettingId()));
110         }
111         return result;
112     }
113
114
115     protected MarkupSetting getMarkupSetting (int id)
116         throws JahiaException {
117
118         MarkupSetting mSetting = null;
119         Connection JavaDoc dbConn = null;
120         PreparedStatement JavaDoc stmt = null;
121         ResultSet JavaDoc rs = null;
122
123         try {
124
125             dbConn = org.jahia.services.database.ConnectionDispenser.getConnection();
126             stmt = dbConn.prepareStatement(GET_MARKUP_SETTING_BYID_QUERY);
127             stmt.setInt(1, id);
128             rs = stmt.executeQuery();
129
130             // is there at least one element in the resultset ?
131
if (rs.next()) {
132                 // yes, we get first value
133
String JavaDoc settingType = rs.getString(2);
134                 int markupDefId = rs.getInt(3);
135                 int applyToAllSite = rs.getInt(4);
136
137                 mSetting = new MarkupSetting(settingType, markupDefId);
138                 mSetting.setSettingId(id);
139                 mSetting.setApplyToAllSite(applyToAllSite==1);
140             }
141
142         } catch (SQLException JavaDoc se) {
143             throw new JahiaException("Cannot load markup setting " + id +
144                                      "from the database",
145                                      se.getMessage(),
146                                      JahiaException.DATABASE_ERROR,
147                                      JahiaException.ERROR_SEVERITY, se);
148         } finally {
149             try {
150                 if (stmt != null)
151                     stmt.close();
152             } catch (SQLException JavaDoc ex) {
153                 logger.debug("Cannot free resources",ex);
154             }
155         }
156
157         mSetting.setProperties(loadMarkupSettingProperties(id));
158         mSetting.getSites().addAll(loadAllSiteMarkupSettingBySettingId(id));
159         return mSetting;
160     }
161
162     protected void createMarkupSetting (MarkupSetting mSetting)
163         throws JahiaException {
164
165         Connection JavaDoc dbConn = null;
166         PreparedStatement JavaDoc stmt = null;
167         ResultSet JavaDoc rs = null;
168
169         try {
170             int id = ServicesRegistry.getInstance().getJahiaIncrementorsDBService()
171                    .autoIncrement("jahia_markup_set");
172             dbConn = org.jahia.services.database.ConnectionDispenser.getConnection();
173             stmt = dbConn.prepareStatement(INSERT_MARKUP_SETTING_QUERY);
174             stmt.setInt(1, id);
175             stmt.setString(2, mSetting.getSettingType());
176             stmt.setInt(3, mSetting.getMarkupDefId());
177             int applyToAllSite = mSetting.applyToAllSite()?1:0;
178             stmt.setInt(4, applyToAllSite);
179             stmt.executeUpdate();
180             mSetting.setSettingId(id);
181         } catch (SQLException JavaDoc se) {
182             throw new JahiaException("Cannot store markup setting in the database",
183                                      se.getMessage(),
184                                      JahiaException.DATABASE_ERROR,
185                                      JahiaException.ERROR_SEVERITY, se);
186         } finally {
187             try {
188                 if (stmt != null)
189                     stmt.close();
190             } catch (SQLException JavaDoc ex) {
191                 logger.debug("Cannot free resources",ex);
192             }
193         }
194         if ( mSetting.getSettingId() != -1 ){
195             saveMarkupSettingProperties(mSetting.getSettingId(),
196                 mSetting.getProperties());
197
198             saveSiteMarkupSetting(mSetting);
199         }
200     }
201
202     protected void updateMarkupSetting (MarkupSetting mSetting)
203         throws JahiaException {
204         Connection JavaDoc dbConn = null;
205         PreparedStatement JavaDoc stmt = null;
206         ResultSet JavaDoc rs = null;
207
208         try {
209             dbConn = org.jahia.services.database.ConnectionDispenser.getConnection();
210             stmt = dbConn.prepareStatement(UPDATE_MARKUP_SETTING_BYID_QUERY);
211             stmt.setString(1, mSetting.getSettingType());
212             stmt.setInt(2, mSetting.getMarkupDefId());
213             int applyToAllSite = mSetting.applyToAllSite()?1:0;
214             stmt.setInt(3, applyToAllSite);
215             stmt.setInt(4, mSetting.getSettingId());
216             stmt.executeUpdate();
217         } catch (SQLException JavaDoc se) {
218             throw new JahiaException("Cannot update markup setting "+mSetting.getSettingId()+" in the database",
219                                      se.getMessage(),
220                                      JahiaException.DATABASE_ERROR,
221                                      JahiaException.ERROR_SEVERITY, se);
222         } finally {
223             try {
224                 if (stmt != null)
225                     stmt.close();
226             } catch (SQLException JavaDoc ex) {
227                 logger.debug("Cannot free resources",ex);
228             }
229         }
230
231         saveMarkupSettingProperties(mSetting.getSettingId(),
232                 mSetting.getProperties());
233
234         saveSiteMarkupSetting(mSetting);
235
236     }
237
238
239     protected void removeMarkupSetting (MarkupSetting mSetting)
240         throws JahiaException {
241
242         Connection JavaDoc dbConn = null;
243         PreparedStatement JavaDoc stmt = null;
244         ResultSet JavaDoc rs = null;
245
246         try {
247             dbConn = org.jahia.services.database.ConnectionDispenser.getConnection();
248             stmt = dbConn.prepareStatement(REMOVE_MARKUP_SETTING_BYID_QUERY);
249             stmt.setInt(1, mSetting.getSettingId());
250             stmt.executeUpdate();
251         } catch (SQLException JavaDoc se) {
252             logger.debug("Cannot free resources",se);
253         } finally {
254             try {
255                 if (stmt != null)
256                     stmt.close();
257             } catch (SQLException JavaDoc ex) {
258                 logger.debug("Cannot free resources",ex);
259             }
260         }
261
262         deleteMarkupSettingProperties(mSetting.getSettingId());
263         removeAllSiteMarkupSettingBySettingId(mSetting.getSettingId());
264         mSetting.getSites().clear();
265     }
266
267     protected Properties JavaDoc loadMarkupSettingProperties(int id)
268     throws JahiaException {
269
270         Properties JavaDoc properties = new Properties JavaDoc();
271         Connection JavaDoc dbConn = null;
272         PreparedStatement JavaDoc stmt = null;
273         ResultSet JavaDoc rs = null;
274
275         try {
276
277             dbConn = org.jahia.services.database.ConnectionDispenser.getConnection();
278             stmt = dbConn.prepareStatement(GET_ALL_MARKUP_SETTING_PROP_BYID_QUERY);
279             stmt.setInt(1, id);
280             rs = stmt.executeQuery();
281
282             while (rs.next()) {
283                 String JavaDoc propName = rs.getString(2);
284                 String JavaDoc propValue = rs.getString(3);
285                 properties.setProperty(propName,propValue);
286             }
287
288         } catch (SQLException JavaDoc se) {
289             throw new JahiaException("Cannot load markup setting prop for " + id +
290                                      "from the database",
291                                      se.getMessage(),
292                                      JahiaException.DATABASE_ERROR,
293                                      JahiaException.ERROR_SEVERITY, se);
294         } finally {
295             try {
296                 if (stmt != null)
297                     stmt.close();
298             } catch (SQLException JavaDoc ex) {
299                 logger.debug("Cannot free resources",ex);
300             }
301         }
302         return properties;
303     }
304
305     /**
306      * Delete All properties form a given id
307      *
308      * @param id
309      * @return
310      * @throws JahiaException
311      */

312     protected void deleteMarkupSettingProperties(int id)
313     throws JahiaException {
314
315         Connection JavaDoc dbConn = null;
316         PreparedStatement JavaDoc stmt = null;
317
318         try {
319
320             dbConn = org.jahia.services.database.ConnectionDispenser.getConnection();
321             stmt = dbConn.prepareStatement(REMOVE_ALL_MARKUP_SETTING_PROP_BYID_QUERY);
322             stmt.setInt(1, id);
323             stmt.executeUpdate();
324
325         } catch (SQLException JavaDoc se) {
326             throw new JahiaException("Cannot delete markup setting props for " + id +
327                                      "from the database",
328                                      se.getMessage(),
329                                      JahiaException.DATABASE_ERROR,
330                                      JahiaException.ERROR_SEVERITY, se);
331         } finally {
332             try {
333                 if (stmt != null)
334                     stmt.close();
335             } catch (SQLException JavaDoc ex) {
336                 throw new JahiaException("Cannot free resources",
337                                          "Cannot free resources",
338                                          JahiaException.DATABASE_ERROR,
339                                          JahiaException.WARNING_SEVERITY, ex);
340             }
341         }
342     }
343
344     protected void insertMarkupSettingProperty(int id, String JavaDoc propName, String JavaDoc propValue)
345     throws JahiaException {
346
347         Connection JavaDoc dbConn = null;
348         PreparedStatement JavaDoc stmt = null;
349
350         try {
351
352             dbConn = org.jahia.services.database.ConnectionDispenser.getConnection();
353             stmt = dbConn.prepareStatement(INSERT_MARKUP_SETTING_PROP_QUERY);
354             stmt.setInt(1, id);
355             stmt.setString(2, propName);
356             stmt.setString(3, propValue);
357             stmt.executeUpdate();
358
359         } catch (SQLException JavaDoc se) {
360             throw new JahiaException("Cannot insert markup setting prop for " + id +
361                                      "from the database",
362                                      se.getMessage(),
363                                      JahiaException.DATABASE_ERROR,
364                                      JahiaException.ERROR_SEVERITY, se);
365         } finally {
366             try {
367                 if (stmt != null)
368                     stmt.close();
369             } catch (SQLException JavaDoc ex) {
370                 logger.debug("Cannot free resources",ex);
371             }
372         }
373     }
374
375     /**
376      * Will remove all properties values for the given id,
377      * before inserting the new ones
378      *
379      * @param properties
380      * @return
381      * @throws JahiaException
382      */

383     protected void saveMarkupSettingProperties(int id, Properties JavaDoc properties)
384     throws JahiaException {
385
386         // first delete all old values
387
deleteMarkupSettingProperties(id);
388
389         Enumeration JavaDoc enumeration = properties.propertyNames();
390         while ( enumeration.hasMoreElements() ){
391             String JavaDoc name = (String JavaDoc)enumeration.nextElement();
392             insertMarkupSettingProperty(id,name,properties.getProperty(name));
393         }
394     }
395
396     protected void insertSiteMarkupSetting(int settingId, int siteId)
397     throws JahiaException {
398
399         Connection JavaDoc dbConn = null;
400         PreparedStatement JavaDoc stmt = null;
401
402         try {
403
404             dbConn = org.jahia.services.database.ConnectionDispenser.getConnection();
405             stmt = dbConn.prepareStatement(INSERT_SITE_MARKUP_SETTING_QUERY);
406             stmt.setInt(1, settingId);
407             stmt.setInt(2, siteId);
408             stmt.executeUpdate();
409
410         } catch (SQLException JavaDoc se) {
411             throw new JahiaException("Cannot insert site markup setting" +
412                                      "from the database",
413                                      se.getMessage(),
414                                      JahiaException.DATABASE_ERROR,
415                                      JahiaException.ERROR_SEVERITY, se);
416         } finally {
417             try {
418                 if (stmt != null)
419                     stmt.close();
420             } catch (SQLException JavaDoc ex) {
421                 logger.debug("Cannot free resources",ex);
422             }
423         }
424     }
425
426     protected void removeSiteMarkupSetting(int settingId, int siteId)
427     throws JahiaException {
428
429         Connection JavaDoc dbConn = null;
430         PreparedStatement JavaDoc stmt = null;
431
432         try {
433
434             dbConn = org.jahia.services.database.ConnectionDispenser.getConnection();
435             stmt = dbConn.prepareStatement(REMOVE_SITE_MARKUP_SETTING_QUERY);
436             stmt.setInt(1, settingId);
437             stmt.setInt(2, siteId);
438             stmt.executeUpdate();
439
440         } catch (SQLException JavaDoc se) {
441             throw new JahiaException("Cannot remove site markup setting" +
442                                      "from the database",
443                                      se.getMessage(),
444                                      JahiaException.DATABASE_ERROR,
445                                      JahiaException.ERROR_SEVERITY, se);
446         } finally {
447             try {
448                 if (stmt != null)
449                     stmt.close();
450             } catch (SQLException JavaDoc ex) {
451                 logger.debug("Cannot free resources",ex);
452             }
453         }
454     }
455
456     protected void removeAllSiteMarkupSettingBySettingId(int settingId)
457     throws JahiaException {
458
459         Connection JavaDoc dbConn = null;
460         PreparedStatement JavaDoc stmt = null;
461
462         try {
463
464             dbConn = org.jahia.services.database.ConnectionDispenser.getConnection();
465             stmt = dbConn.prepareStatement(REMOVE_ALL_SITE_MARKUP_SETTING_BY_SETTINGID_QUERY);
466             stmt.setInt(1, settingId);
467             stmt.executeUpdate();
468
469         } catch (SQLException JavaDoc se) {
470             throw new JahiaException("Cannot remove all site markup setting for markup "
471                                      + settingId + "from the database",
472                                      se.getMessage(),
473                                      JahiaException.DATABASE_ERROR,
474                                      JahiaException.ERROR_SEVERITY, se);
475         } finally {
476             try {
477                 if (stmt != null)
478                     stmt.close();
479             } catch (SQLException JavaDoc ex) {
480                 logger.debug("Cannot free resources",ex);
481             }
482         }
483     }
484
485     protected ArrayList JavaDoc loadAllSiteMarkupSettingBySettingId(int settingId)
486     throws JahiaException {
487
488         ArrayList JavaDoc result = new ArrayList JavaDoc();
489         Connection JavaDoc dbConn = null;
490         PreparedStatement JavaDoc stmt = null;
491         ResultSet JavaDoc rs = null;
492         try {
493
494             dbConn = org.jahia.services.database.ConnectionDispenser.getConnection();
495             stmt = dbConn.prepareStatement(LOAD_ALL_SITE_MARKUP_SETTING_BY_SETTINGID_QUERY);
496             stmt.setInt(1, settingId);
497             rs = stmt.executeQuery();
498
499             while (rs.next()) {
500                 int siteId = rs.getInt(2);
501                 result.add(new Integer JavaDoc(siteId));
502             }
503
504         } catch (SQLException JavaDoc se) {
505             throw new JahiaException("Cannot load site markup setting for markup "
506                                      + settingId + "from the database",
507                                      se.getMessage(),
508                                      JahiaException.DATABASE_ERROR,
509                                      JahiaException.ERROR_SEVERITY, se);
510         } finally {
511             try {
512                 if (stmt != null)
513                     stmt.close();
514             } catch (SQLException JavaDoc ex) {
515                 logger.debug("Cannot free resources",ex);
516             }
517         }
518         return result;
519     }
520
521     protected void saveSiteMarkupSetting(MarkupSetting mSetting)
522     throws JahiaException {
523         this.removeAllSiteMarkupSettingBySettingId(mSetting.getSettingId());
524         Iterator JavaDoc iterator = mSetting.getSites().iterator();
525         while ( iterator.hasNext() ){
526             Integer JavaDoc I = (Integer JavaDoc)iterator.next();
527             this.insertSiteMarkupSetting(I.intValue(),mSetting.getSettingId());
528         }
529     }
530
531 }
Popular Tags