KickJava   Java API By Example, From Geeks To Geeks.

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


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.Enumeration JavaDoc;
9 import java.util.Iterator JavaDoc;
10 import java.util.Properties JavaDoc;
11 import java.util.Hashtable JavaDoc;
12
13 import org.jahia.exceptions.JahiaException;
14 import org.jahia.registries.ServicesRegistry;
15
16
17 /**
18  *
19  * <p>Title: DB Persistence for markup definition</p>
20  * <p>Description: DB Persistence for markup definition</p>
21  * <p>Copyright: Copyright (c) 2002</p>
22  * <p>Company: </p>
23  * @author Khue Nguyen
24  * @version 1.0
25  */

26 class MarkupDefinitionPersistence {
27
28     static private final String JavaDoc LOAD_ALL_MARKUP_QUERY = "SELECT * FROM jahia_markupdef";
29     static private final String JavaDoc GET_MARKUP_BYID_QUERY = "SELECT * FROM jahia_markupdef WHERE id_markup =?";
30     static private final String JavaDoc UPDATE_MARKUP_BYID_QUERY = "UPDATE jahia_markupdef SET name_markup=?, case_sensitive=? WHERE id_markup =?";
31     static private final String JavaDoc INSERT_MARKUP_QUERY = "INSERT INTO jahia_markupdef (id_markup, name_markup, case_sensitive) VALUES (?, ?, ?)";
32     static private final String JavaDoc REMOVE_MARKUP_BYID_QUERY = "DELETE FROM jahia_markupdef WHERE id_markup=?";
33
34     static private final String JavaDoc GET_ALL_MARKUP_PROP_BYID_QUERY = "SELECT * FROM jahia_markupdef_prop WHERE id_markup =?";
35     static private final String JavaDoc INSERT_MARKUP_PROP_QUERY = "INSERT INTO jahia_markupdef_prop (id_markup, prop_name, prop_value) VALUES (?, ?, ?)";
36     static private final String JavaDoc REMOVE_ALL_MARKUP_PROP_BYID_QUERY = "DELETE FROM jahia_markupdef_prop WHERE id_markup=?";
37
38     private MarkupDefinitionPersistence() {
39     }
40
41     private static org.apache.log4j.Logger logger =
42         org.apache.log4j.Logger.getLogger(MarkupDefinitionPersistence.class);
43
44     private static MarkupDefinitionPersistence instance = null;
45
46     /**
47      * @return an instance of this class
48      */

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

294     protected void deleteMarkupDefinitionProperties(int id)
295     throws JahiaException {
296
297         Connection JavaDoc dbConn = null;
298         PreparedStatement JavaDoc stmt = null;
299
300         try {
301
302             dbConn = org.jahia.services.database.ConnectionDispenser.getConnection();
303             stmt = dbConn.prepareStatement(REMOVE_ALL_MARKUP_PROP_BYID_QUERY);
304             stmt.setInt(1, id);
305             stmt.executeUpdate();
306
307         } catch (SQLException JavaDoc se) {
308             throw new JahiaException("Cannot delete markup definition props for " + id +
309                                      "from the database",
310                                      se.getMessage(),
311                                      JahiaException.DATABASE_ERROR,
312                                      JahiaException.ERROR_SEVERITY, se);
313         } finally {
314             try {
315                 if (stmt != null)
316                     stmt.close();
317             } catch (SQLException JavaDoc ex) {
318                 throw new JahiaException("Cannot free resources",
319                                          "Cannot free resources",
320                                          JahiaException.DATABASE_ERROR,
321                                          JahiaException.WARNING_SEVERITY, ex);
322             }
323         }
324     }
325
326     protected void insertMarkupDefinitionProperty(int id, String JavaDoc propName, String JavaDoc propValue)
327     throws JahiaException {
328
329         Connection JavaDoc dbConn = null;
330         PreparedStatement JavaDoc stmt = null;
331
332         try {
333
334             dbConn = org.jahia.services.database.ConnectionDispenser.getConnection();
335             stmt = dbConn.prepareStatement(INSERT_MARKUP_PROP_QUERY);
336             stmt.setInt(1, id);
337             stmt.setString(2, propName);
338             stmt.setString(3, propValue);
339             stmt.executeUpdate();
340
341         } catch (SQLException JavaDoc se) {
342             throw new JahiaException("Cannot insert markup definition prop for " + id +
343                                      "from the database",
344                                      se.getMessage(),
345                                      JahiaException.DATABASE_ERROR,
346                                      JahiaException.ERROR_SEVERITY, se);
347         } finally {
348             try {
349                 if (stmt != null)
350                     stmt.close();
351             } catch (SQLException JavaDoc ex) {
352                 logger.debug("Cannot free resources",ex);
353             }
354         }
355     }
356
357     /**
358      * Will remove all properties values for the given id,
359      * before inserting the new ones
360      *
361      * @param properties
362      * @return
363      * @throws JahiaException
364      */

365     protected void saveMarkupDefinitionProperties(int id, Properties JavaDoc properties)
366     throws JahiaException {
367
368         // first delete all old values
369
deleteMarkupDefinitionProperties(id);
370
371         Enumeration JavaDoc enumeration = properties.propertyNames();
372         while ( enumeration.hasMoreElements() ){
373             String JavaDoc name = (String JavaDoc)enumeration.nextElement();
374             insertMarkupDefinitionProperty(id,name,properties.getProperty(name));
375         }
376     }
377
378 }
Popular Tags