KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > services > containers > JahiaContainerDefPropDB


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  *
14  * ----- BEGIN LICENSE BLOCK -----
15  * Version: JCSL 1.0
16  *
17  * The contents of this file are subject to the Jahia Community Source License
18  * 1.0 or later (the "License"); you may not use this file except in
19  * compliance with the License. You may obtain a copy of the License at
20  * http://www.jahia.org/license
21  *
22  * Software distributed under the License is distributed on an "AS IS" basis,
23  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
24  * for the rights, obligations and limitations governing use of the contents
25  * of the file. The Original and Upgraded Code is the Jahia CMS and Portal
26  * Server. The developer of the Original and Upgraded Code is JAHIA Ltd. JAHIA
27  * Ltd. owns the copyrights in the portions it created. All Rights Reserved.
28  *
29  * The Shared Modifications are Jahia View Helper.
30  *
31  * The Developer of the Shared Modifications is Jahia Solution S�rl.
32  * Portions created by the Initial Developer are Copyright (C) 2002 by the
33  * Initial Developer. All Rights Reserved.
34  *
35  * Contributor(s):
36  * 22-AUG-2003, Jahia Solutions Sarl, Fulco Houkes
37  *
38  * ----- END LICENSE BLOCK -----
39  */

40
41 package org.jahia.services.containers;
42
43 import org.jahia.data.JahiaDBDOMObject;
44 import org.jahia.data.JahiaDOMObject;
45 import org.jahia.exceptions.JahiaException;
46
47 import java.sql.Connection JavaDoc;
48 import java.sql.ResultSet JavaDoc;
49 import java.sql.SQLException JavaDoc;
50 import java.sql.Statement JavaDoc;
51 import java.util.Enumeration JavaDoc;
52 import java.util.Properties JavaDoc;
53
54 /**
55  * <p>Title: Database serialization of containerList properties.</p>
56  * <p>Description: This object manages all the operations of serialization
57  * of containerList properties in the database.</p>
58  * <p>Copyright: Copyright (c) 2002</p>
59  * <p>Company: Jahia Ltd</p>
60  *
61  * @author Serge Huber
62  * @version 1.0
63  */

64
65 public class JahiaContainerDefPropDB {
66
67     private static org.apache.log4j.Logger logger =
68             org.apache.log4j.Logger.getLogger (JahiaContainerDefPropDB.class);
69
70     /**
71      * Default constructor, not much to say here...
72      */

73     public JahiaContainerDefPropDB () {
74     }
75
76     /**
77      * Retrieves all the properties for a given containerList.
78      *
79      * @param containerListID the identifier of the containerList whose properties
80      * we want to retrieve from the database
81      *
82      * @return a Properties object that contains all the properties that are
83      * available for this containerList in the database
84      *
85      * @throws JahiaException generated if there were problems executing the
86      * query or communicating with the database.
87      */

88     public Properties JavaDoc getProperties (int containerListID)
89             throws JahiaException {
90         Properties JavaDoc result = new Properties JavaDoc ();
91         Connection JavaDoc dbConn = null;
92         Statement JavaDoc stmt = null;
93         ResultSet JavaDoc rs = null;
94         try {
95             String JavaDoc sqlQuery = "SELECT * FROM jahia_ctndef_prop";
96             sqlQuery += " WHERE id_jahia_ctn_def=" + containerListID;
97
98             dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
99             stmt = dbConn.createStatement ();
100             rs = stmt.executeQuery (sqlQuery);
101
102             while (rs.next ()) {
103                 int id = rs.getInt ("id_jahia_ctn_def");
104                 String JavaDoc name = rs.getString ("name_jahia_ctndef_prop");
105                 String JavaDoc value = rs.getString ("value_jahia_ctndef_prop");
106                 result.setProperty (name, value);
107             }
108
109         } catch (SQLException JavaDoc se) {
110             String JavaDoc errorMsg = "Error in JahiaContainerDefPropDB.getProperties() : " +
111                     se.getMessage () + " -> BAILING OUT";
112             logger.warn (errorMsg);
113             throw new JahiaException ("Cannot load container def properties from the database",
114                     errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL_SEVERITY);
115         } finally {
116             try {
117
118                 if (stmt != null) stmt.close ();
119             } catch (SQLException JavaDoc ex) {
120                 logger.warn ("cannot free resources", ex);
121             }
122         }
123         return result;
124
125     }
126
127     /**
128      * Saves a whole set of properties in the database for the specified
129      * containerList. WARNING : the way this is implemented (for speed reasons)
130      * is that first all the existing properties for a containerList are
131      * DELETED from the database. Remember to always load the full set of
132      * properties before calling this method or this will result in dataloss.
133      * Also if the operation with the database fails during the delete operation
134      * or the insertion of the new elements this will also occur in data loss.
135      * A safer way would be to test the existence of each property in the database
136      * and then INSERT or UPDATE the value, but that would take forever.
137      *
138      * @param containerListID identifier of the containerList whose properties
139      * we are serializing in the database.
140      * @param containerListProperties the Properties object that contains all
141      * the properties to save in the database. Only what is passed here will
142      * exist in the database. If the database contained properties that aren't
143      * in this hashtable they will be deleted.
144      *
145      * @throws JahiaException generated if there were problems executing the
146      * query or communicating with the database.
147      */

148     public void setProperties (int containerListID,
149                                Properties JavaDoc containerListProperties)
150             throws JahiaException {
151
152         // First we clear all the existing properties in the database...
153
// Warning this is dangerous if the operation is interrupted in the
154
// middle.
155
removeProperties (containerListID);
156
157         Connection JavaDoc dbConn = null;
158         Statement JavaDoc stmt = null;
159         try {
160             // opens connection
161
dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
162             stmt = dbConn.createStatement ();
163
164             Enumeration JavaDoc propNames = containerListProperties.keys ();
165             while (propNames.hasMoreElements ()) {
166                 String JavaDoc curPropName = (String JavaDoc) propNames.nextElement ();
167                 String JavaDoc curPropValue = containerListProperties.getProperty (curPropName);
168
169                 String JavaDoc sqlQuery = "INSERT INTO jahia_ctndef_prop(id_jahia_ctn_def, name_jahia_ctndef_prop, value_jahia_ctndef_prop) VALUES(" +
170                         containerListID + "," +
171                         "'" + curPropName + "'," +
172                         "'" + curPropValue + "')";
173
174                 // executes the query, then closes the connection
175
stmt.execute (sqlQuery);
176             }
177
178         } catch (SQLException JavaDoc se) {
179             String JavaDoc errorMsg = "Error in JahiaContainerDefPropDB.setProperties : " + se.getMessage () + " -> BAILING OUT";
180             logger.warn (errorMsg);
181             throw new JahiaException (
182                     "Cannot create container definition properties in the database",
183                     errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL_SEVERITY);
184         } finally {
185             try {
186
187                 if (stmt != null) stmt.close ();
188             } catch (SQLException JavaDoc ex) {
189                 logger.warn ("cannot free resources", ex);
190             }
191         }
192     }
193
194     /**
195      * Removes all the properties for the specified containerList ID.
196      *
197      * @param containerListID idenfitifer of the containerList to delete all the
198      * properties
199      *
200      * @throws JahiaException generated if there were problems executing the
201      * query or communicating with the database.
202      */

203     public void removeProperties (int containerListID)
204             throws JahiaException {
205         Connection JavaDoc dbConn = null;
206         Statement JavaDoc stmt = null;
207         try {
208
209             // composes the query
210
String JavaDoc sqlQuery = "DELETE FROM jahia_ctndef_prop ";
211             sqlQuery += "WHERE id_jahia_ctn_def = " + containerListID;
212
213             // executes the query
214
dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
215             stmt = dbConn.createStatement ();
216             stmt.executeUpdate (sqlQuery);
217
218         }
219                 // catches error if cannot execute update query
220
catch (SQLException JavaDoc se) {
221             String JavaDoc errorMsg = "Error in JahiaContainerDefPropDB.removeProperties : " + se.getMessage ();
222             logger.warn (errorMsg + " -> BAILING OUT");
223             throw new JahiaException (
224                     "Cannot delete container definition properties in the database",
225                     errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL_SEVERITY);
226         } finally {
227             try {
228
229                 if (stmt != null) stmt.close ();
230             } catch (SQLException JavaDoc ex) {
231                 logger.warn ("cannot free resources", ex);
232             }
233         }
234     }
235
236     /**
237      * Retrieves the containerList property from the database. This method may
238      * also be used as a test for existence of a property in the database.
239      *
240      * @param containerListID identifier of the containerList
241      * @param propertyName name of the property to retrieve
242      *
243      * @return a String containing the value of the property, or null if the
244      * property doesn't have a value in the database (ie if it doesn't exist)
245      *
246      * @throws JahiaException generated if there were problems executing the
247      * query or communicating with the database.
248      */

249     public String JavaDoc getProperty (int containerListID,
250                                String JavaDoc propertyName)
251             throws JahiaException {
252         String JavaDoc result = null;
253         Connection JavaDoc dbConn = null;
254         Statement JavaDoc stmt = null;
255         ResultSet JavaDoc rs = null;
256         try {
257             String JavaDoc sqlQuery = "SELECT value_jahia_ctndef_prop FROM jahia_ctndef_prop";
258             sqlQuery += " WHERE id_jahia_ctn_def=" + containerListID;
259             sqlQuery += " AND name_jahia_ctndef_prop='" + propertyName + "'";
260
261             dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
262             stmt = dbConn.createStatement ();
263             rs = stmt.executeQuery (sqlQuery);
264
265             if (rs.next ()) {
266                 String JavaDoc value = rs.getString ("value_jahia_ctndef_prop");
267                 result = value;
268             }
269
270         } catch (SQLException JavaDoc se) {
271             String JavaDoc errorMsg = "Error in JahiaContainerListsPropDB.getProperty : " +
272                     se.getMessage () + " -> BAILING OUT";
273             logger.warn (errorMsg);
274             throw new JahiaException (
275                     "Cannot load container definition property from the database",
276                     errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL_SEVERITY);
277         } finally {
278             try {
279
280                 if (stmt != null) stmt.close ();
281             } catch (SQLException JavaDoc ex) {
282                 logger.warn ("cannot free resources", ex);
283             }
284         }
285         return result;
286     }
287
288     /**
289      * Saves a single property in the database for a given containerList. This
290      * operation starts by deleting any existing entry and then inserting a
291      * new value.
292      *
293      * @param containerListID identifier of the containerList
294      * @param propertyName name of the property to add in the database
295      * @param propertyValue name of the property to add in the database
296      *
297      * @throws JahiaException generated if there were problems executing the
298      * query or communicating with the database.
299      */

300     public void setProperty (int containerListID,
301                              String JavaDoc propertyName,
302                              String JavaDoc propertyValue)
303             throws JahiaException {
304
305         removeProperty (containerListID, propertyName);
306
307         Connection JavaDoc dbConn = null;
308         Statement JavaDoc stmt = null;
309
310         try {
311             // opens connection
312
dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
313             stmt = dbConn.createStatement ();
314
315             String JavaDoc sqlQuery = "INSERT INTO jahia_ctndef_prop(id_jahia_ctn_def, name_jahia_ctndef_prop, value_jahia_ctndef_prop) VALUES(" +
316                     containerListID + "," +
317                     "'" + propertyName + "'," +
318                     "'" + propertyValue + "')";
319
320             // executes the query, then closes the connection
321
stmt.execute (sqlQuery);
322
323             try {
324
325                 if (stmt != null) stmt.close ();
326             } catch (SQLException JavaDoc ex) {
327                 logger.warn ("cannot free resources", ex);
328             }
329
330         } catch (SQLException JavaDoc se) {
331             String JavaDoc errorMsg = "Error in JahiaContainerDefPropDB.setProperty : " + se.getMessage () + " -> BAILING OUT";
332             logger.warn (errorMsg);
333             throw new JahiaException (
334                     "Cannot create container definition property in the database",
335                     errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL_SEVERITY);
336         } finally {
337             try {
338
339                 if (stmt != null) stmt.close ();
340             } catch (SQLException JavaDoc ex) {
341                 logger.warn ("cannot free resources", ex);
342             }
343         }
344     }
345
346     /**
347      * Removes a single property for the given containerList.
348      *
349      * @param containerListID identifer of the containerList
350      * @param propertyName name of the property to be deleted.
351      *
352      * @throws JahiaException generated if there were problems executing the
353      * query or communicating with the database.
354      */

355     public void removeProperty (int containerListID,
356                                 String JavaDoc propertyName)
357             throws JahiaException {
358         Connection JavaDoc dbConn = null;
359         Statement JavaDoc stmt = null;
360         try {
361
362             // composes the query
363
String JavaDoc sqlQuery = "DELETE FROM jahia_ctndef_prop ";
364             sqlQuery += "WHERE id_jahia_ctn_lists = " + containerListID;
365             sqlQuery += " AND name_jahia_ctnlists_prop='" + propertyName + "'";
366
367             // executes the query
368
dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
369             stmt = dbConn.createStatement ();
370             stmt.executeUpdate (sqlQuery);
371
372         }
373                 // catches error if cannot execute update query
374
catch (SQLException JavaDoc se) {
375             String JavaDoc errorMsg = "Error in JahiaContainerDefPropDB.removeProperty : " + se.getMessage ();
376             logger.warn (errorMsg + " -> BAILING OUT");
377             throw new JahiaException (
378                     "Cannot delete container definition property in the database",
379                     errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL_SEVERITY);
380         } finally {
381             try {
382
383                 if (stmt != null) stmt.close ();
384             } catch (SQLException JavaDoc ex) {
385                 logger.warn ("cannot free resources", ex);
386             }
387         }
388     }
389
390     //--------------------------------------------------------------------------
391
/**
392      * return a DOM document of all the properties of all the containers for
393      * a given siteID
394      *
395      * @param siteID the identifier for the siteID for which to extract all the
396      * containerList properties
397      *
398      * @return JahiaDOMObject a DOM representation of the properties of the
399      * containerList
400      *
401      * @throws JahiaException generated if there were problems executing the
402      * query or communicating with the database.
403      */

404     public JahiaDOMObject getPropertiesAsDOM (int siteID)
405             throws JahiaException {
406
407         Connection JavaDoc dbConn = null;
408         Statement JavaDoc statement = null;
409
410         JahiaDBDOMObject dom = null;
411
412         try {
413
414             String JavaDoc sqlQuery = "SELECT DISTINCT jahia_ctndef_prop.id_jahia_ctn_def,"
415                     + "jahia_ctndef_prop.name_jahia_ctndef_prop,jahia_ctndef_prop.value_jahia_ctndef_prop"
416                     + " FROM jahia_ctndef_prop,jahia_ctn_def where "
417                     + "jahia_ctndef_prop.id_jahia_ctn_def=jahia_ctn_def.id_jahia_ctn_def AND "
418                     + "jahia_ctn_def.jahiaid_jahia_ctn_def=" + siteID;
419
420             dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
421             statement = dbConn.createStatement ();
422             if (statement != null) {
423                 ResultSet JavaDoc rs = statement.executeQuery (sqlQuery);
424                 if (rs != null) {
425                     dom = new JahiaDBDOMObject ();
426                     dom.addTable ("jahia_ctndef_prop", rs);
427                     return dom;
428                 }
429             }
430         } catch (SQLException JavaDoc se) {
431             String JavaDoc errorMsg = "Error in JahiaContainerDefPropDB.getPropertiesAsDOM : " + se.getMessage ();
432             logger.warn (errorMsg + " -> BAILING OUT");
433             throw new JahiaException (
434                     "Cannot load container definition properties from the database",
435                     errorMsg, JahiaException.DATABASE_ERROR,
436                     JahiaException.CRITICAL_SEVERITY);
437         } finally {
438
439             closeStatement (statement);
440         }
441
442         return dom;
443     }
444
445
446     //-------------------------------------------------------------------------
447
private void closeStatement (Statement JavaDoc statement) {
448         // Close the opened statement
449
try {
450             if (statement != null) {
451                 statement.close ();
452             }
453         } catch (SQLException JavaDoc sqlEx) {
454             logger.warn ("Cannot close a statement", sqlEx);
455         }
456     }
457
458 }
Popular Tags