KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > services > pages > JahiaPageDefinitionPropDB


1 package org.jahia.services.pages;
2
3 import org.jahia.data.JahiaDBDOMObject;
4 import org.jahia.data.JahiaDOMObject;
5 import org.jahia.exceptions.JahiaException;
6
7 import java.sql.*;
8 import java.util.Enumeration JavaDoc;
9 import java.util.Properties JavaDoc;
10 import java.util.Vector JavaDoc;
11
12 /**
13  * <p>Title: Database serialization of page defintion properties.</p>
14  * <p>Description: This object manages all the operations of serialization
15  * of page definition properties in the database.</p>
16  * <p>Copyright: Copyright (c) 2002</p>
17  * <p>Company: Jahia Ltd</p>
18  *
19  * @author Khue Nguyen
20  * @version 1.0
21  */

22
23 class JahiaPageDefinitionPropDB {
24
25     private static org.apache.log4j.Logger logger =
26             org.apache.log4j.Logger.getLogger (JahiaPageDefinitionPropDB.class);
27
28     private static final String JavaDoc CLASS_NAME = JahiaPageDefinitionPropDB.class.getName ();
29
30
31     /**
32      * Retrieves all the properties for a given page definition.
33      *
34      * @param id the page definition id
35      *
36      * @return a Properties object that contains all the properties
37      *
38      * @throws JahiaException generated if there were problems executing the
39      * query or communicating with the database.
40      */

41     public static Properties JavaDoc getProperties (int id)
42             throws JahiaException {
43         Properties JavaDoc result = new Properties JavaDoc ();
44         Connection dbConn = null;
45         Statement stmt = null;
46         ResultSet rs = null;
47         try {
48             String JavaDoc sqlQuery = "SELECT * FROM jahia_pages_def_prop";
49             sqlQuery += " WHERE id_jahia_pages_def_prop=" + id;
50
51             dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
52             stmt = dbConn.createStatement ();
53             rs = stmt.executeQuery (sqlQuery);
54
55             while (rs.next ()) {
56                 id = rs.getInt ("id_jahia_pages_def_prop");
57                 int siteID = rs.getInt ("jahiaid_pages_def_prop");
58                 String JavaDoc name = rs.getString ("name_pages_def_prop");
59                 String JavaDoc value = rs.getString ("value_pages_def_prop");
60                 result.setProperty (name, value);
61             }
62
63         } catch (SQLException se) {
64             String JavaDoc errorMsg = "Error in " + CLASS_NAME + ".getProperties() : " +
65                     se.getMessage ();
66             logger.error (errorMsg);
67             throw new JahiaException ("Cannot load page def properties from the database",
68                     errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL_SEVERITY);
69
70         } finally {
71             closeStatement (stmt);
72         }
73         return result;
74
75     }
76
77
78     /**
79      * Saves a whole set of properties in the database for the specified
80      * containerList. WARNING : the way this is implemented (for speed reasons)
81      * is that first all the existing properties for a containerList are
82      * DELETED from the database. Remember to always load the full set of
83      * properties before calling this method or this will result in dataloss.
84      * Also if the operation with the database fails during the delete operation
85      * or the insertion of the new elements this will also occur in data loss.
86      * A safer way would be to test the existence of each property in the database
87      * and then INSERT or UPDATE the value, but that would take forever.
88      *
89      * @param pageDef the page definition of the page def whose properties
90      * we are serializing in the database.
91      * @param props the Properties object that contains all
92      * the properties to save in the database. Only what is passed here will
93      * exist in the database. If the database contained properties that aren't
94      * in this hashtable they will be deleted.
95      *
96      * @throws JahiaException generated if there were problems executing the
97      * query or communicating with the database.
98      */

99     public static void setProperties (JahiaPageDefinition pageDef, Properties JavaDoc props)
100             throws JahiaException {
101
102         if (pageDef == null)
103             return;
104
105         // First we clear all the existing properties in the database...
106
// Warning this is dangerous if the operation is interrupted in the
107
// middle.
108
removeProperties (pageDef.getID ());
109
110         Connection dbConn = null;
111         PreparedStatement pstmt = null;
112
113         try {
114             // opens connection
115
dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
116
117             Enumeration JavaDoc propNames = props.keys ();
118             while (propNames.hasMoreElements ()) {
119                 String JavaDoc curPropName = (String JavaDoc) propNames.nextElement ();
120                 String JavaDoc curPropValue = props.getProperty (curPropName);
121
122                 pstmt =
123                         dbConn.prepareStatement (
124                                 "INSERT INTO jahia_pages_def_prop VALUES(?,?,?,?)");
125                 pstmt.setInt (1, pageDef.getID ());
126                 pstmt.setInt (2, pageDef.getJahiaID ());
127                 pstmt.setString (3, curPropName);
128                 pstmt.setString (4, curPropValue);
129                 pstmt.execute ();
130             }
131
132         } catch (SQLException se) {
133             String JavaDoc errorMsg = "Error in " + CLASS_NAME + ".setProperties : " + se.getMessage ();
134             logger.error (errorMsg);
135             throw new JahiaException ("Cannot create page def properties in the database",
136                     errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL_SEVERITY);
137         } finally {
138             closeStatement ((Statement) pstmt);
139         }
140     }
141
142
143     /**
144      * Removes all the properties for the specified page def id.
145      *
146      * @param id idenfitifer of the page def to delete all the properties
147      *
148      * @throws JahiaException generated if there were problems executing the
149      * query or communicating with the database.
150      */

151     public static void removeProperties (int id)
152             throws JahiaException {
153         Connection dbConn = null;
154         Statement stmt = null;
155         try {
156
157             // composes the query
158
String JavaDoc sqlQuery = "DELETE FROM jahia_pages_def_prop ";
159             sqlQuery += "WHERE id_jahia_pages_def_prop = " + id;
160
161             // executes the query
162
dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
163             stmt = dbConn.createStatement ();
164             stmt.executeUpdate (sqlQuery);
165
166         }
167                 // catches error if cannot execute update query
168
catch (SQLException se) {
169             String JavaDoc errorMsg = "Error in " + CLASS_NAME + ".removeProperties : " + se.getMessage ();
170             logger.error (errorMsg);
171             throw new JahiaException ("Cannot delete page def properties in the database",
172                     errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL_SEVERITY);
173         } finally {
174             closeStatement (stmt);
175         }
176     }
177
178
179     /**
180      * Retrieves the page def property from the database. This method may
181      * also be used as a test for existence of a property in the database.
182      *
183      * @param id identifier of the page def
184      * @param name name of the property to retrieve
185      *
186      * @return a String containing the value of the property, or null if the
187      * property doesn't have a value in the database (ie if it doesn't exist)
188      *
189      * @throws JahiaException generated if there were problems executing the
190      * query or communicating with the database.
191      */

192     public static String JavaDoc getProperty (int id, String JavaDoc name)
193             throws JahiaException {
194         String JavaDoc result = null;
195         Connection dbConn = null;
196         PreparedStatement pstmt = null;
197         ResultSet rs = null;
198         try {
199
200             dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
201
202             String JavaDoc sqlQuery = "SELECT value_pages_def_prop FROM jahia_pages_def_prop";
203             sqlQuery += " WHERE id_jahia_pages_def_prop=? AND name_pages_def_prop=?";
204
205             pstmt = dbConn.prepareStatement (sqlQuery);
206
207             pstmt.setInt (1, id);
208             pstmt.setString (2, name);
209
210             rs = pstmt.executeQuery ();
211
212             if (rs.next ()) {
213                 String JavaDoc value = rs.getString ("value_pages_def_prop");
214                 result = value;
215             }
216
217         } catch (SQLException se) {
218             String JavaDoc errorMsg = "Error in " + CLASS_NAME + ".getProperty : " + se.getMessage ();
219             logger.error (errorMsg);
220             throw new JahiaException ("Cannot load page def property from the database",
221                     errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL_SEVERITY);
222         } finally {
223             closeStatement ((Statement) pstmt);
224         }
225         return result;
226     }
227
228
229     /**
230      * Saves a single property in the database for a given page def. This
231      * operation starts by deleting any existing entry and then inserting a
232      * new value.
233      *
234      * @param pageDef page definition instance
235      * @param name name of the property to add in the database
236      * @param value value of the property to add in the database
237      *
238      * @throws JahiaException generated if there were problems executing the
239      * query or communicating with the database.
240      */

241     public static void setProperty (JahiaPageDefinition pageDef, String JavaDoc name, String JavaDoc value)
242             throws JahiaException {
243
244         if (pageDef == null)
245             return;
246
247         removeProperty (pageDef.getID (), name);
248
249         Connection dbConn = null;
250         PreparedStatement pstmt = null;
251         try {
252             // opens connection
253
dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
254             String JavaDoc sqlQuery = "INSERT INTO jahia_pages_def_prop VALUES(?,?,?,?)";
255
256             pstmt = dbConn.prepareStatement (sqlQuery);
257             pstmt.setInt (1, pageDef.getID ());
258             pstmt.setInt (2, pageDef.getJahiaID ());
259             pstmt.setString (3, name);
260             pstmt.setString (4, value);
261
262             // executes the query, then closes the connection
263
pstmt.execute (sqlQuery);
264
265
266             closeStatement ((Statement) pstmt);
267
268         } catch (SQLException se) {
269             String JavaDoc errorMsg = "Error in " + CLASS_NAME + ".setProperty : " + se.getMessage ();
270             logger.error (errorMsg);
271             throw new JahiaException ("Cannot create page def property in the database",
272                     errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL_SEVERITY);
273         } finally {
274             closeStatement ((Statement) pstmt);
275         }
276     }
277
278
279     /**
280      * Removes a single property for the given page def.
281      *
282      * @param id the id
283      * @param name name of the property to be deleted.
284      *
285      * @throws JahiaException generated if there were problems executing the
286      * query or communicating with the database.
287      */

288     public static void removeProperty (int id, String JavaDoc name)
289             throws JahiaException {
290         Connection dbConn = null;
291         PreparedStatement pstmt = null;
292         try {
293
294             // composes the query
295
String JavaDoc sqlQuery = "DELETE FROM jahia_pages_def_prop WHERE id_jahia_pages_def_prop = ? AND name_pages_def_prop = ?";
296
297             // executes the query
298
dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
299             pstmt = dbConn.prepareStatement (sqlQuery);
300             pstmt.setInt (1, id);
301             pstmt.setString (2, name);
302             pstmt.executeUpdate ();
303
304         }
305                 // catches error if cannot execute update query
306
catch (SQLException se) {
307             String JavaDoc errorMsg = "Error in " + CLASS_NAME + ".removeProperty : " + se.getMessage ();
308             logger.error (errorMsg);
309             throw new JahiaException ("Cannot delete page def property in the database",
310                     errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL_SEVERITY);
311         } finally {
312             closeStatement ((Statement) pstmt);
313         }
314     }
315
316
317     /**
318      * return a DOM document of all the properties of all the page def for
319      * a given site
320      *
321      * @param siteID the site identification number
322      *
323      * @return JahiaDOMObject a DOM representation of the properties of the
324      * containerList
325      *
326      * @throws JahiaException generated if there were problems executing the
327      * query or communicating with the database.
328      */

329     public static JahiaDOMObject getPageDefPropsAsDOM (int siteID)
330             throws JahiaException {
331
332         Connection dbConn = null;
333         Statement statement = null;
334
335         JahiaDBDOMObject dom = null;
336
337         try {
338
339             String JavaDoc sqlQuery = "SELECT * FROM jahia_pages_def_prop WHERE jahiaid_pages_def_prop=" + siteID;
340
341             dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
342             statement = dbConn.createStatement ();
343             if (statement != null) {
344                 ResultSet rs = statement.executeQuery (sqlQuery);
345                 if (rs != null) {
346                     dom = new JahiaDBDOMObject ();
347                     dom.addTable ("jahia_pages_def_prop", rs);
348                     return dom;
349                 }
350             }
351         } catch (SQLException se) {
352             String JavaDoc errorMsg = "Error in " + CLASS_NAME + ".getPropertiesAsDOM : " + se.getMessage ();
353             logger.error (errorMsg);
354             throw new JahiaException ("Error loading pages def properties from the database",
355                     errorMsg, JahiaException.DATABASE_ERROR,
356                     JahiaException.CRITICAL_SEVERITY);
357         } finally {
358             closeStatement (statement);
359         }
360
361         return dom;
362     }
363
364     /**
365      * gets all acl ids of a site's page def ACL ids
366      *
367      * @return a Vector of all acl id
368      */

369     public static Vector JavaDoc db_get_all_acl_id (int siteID)
370             throws JahiaException {
371         Connection dbConn = null;
372         Statement stmt = null;
373         ResultSet rs = null;
374         Vector JavaDoc theIDs = new Vector JavaDoc ();
375         try {
376             String JavaDoc sqlQuery = "SELECT DISTINCT value_pages_def_prop FROM jahia_pages_def_prop "
377                     + "WHERE jahiaid_pages_def_prop=" + siteID + " AND name_pages_def_prop='" + JahiaPageDefinition.ACLID_PROP + "'";
378
379             dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
380             stmt = dbConn.createStatement ();
381             rs = stmt.executeQuery (sqlQuery);
382
383             String JavaDoc value = null;
384             while (rs.next ()) {
385                 value = rs.getString ("value_pages_def_prop");
386                 if (value != null) {
387                     try {
388                         theIDs.add (new Integer JavaDoc (value));
389                     } catch (Throwable JavaDoc t) {
390                         t.printStackTrace ();
391                     }
392                 }
393             }
394         } catch (SQLException se) {
395             String JavaDoc errorMsg = "Error in db_get_all_acl_id : " + se.getMessage ();
396             logger.error (errorMsg);
397             throw new JahiaException ("Cannot load acl id from the database",
398                     errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL_SEVERITY);
399         } finally {
400             closeStatement (stmt);
401         }
402         return theIDs;
403
404     }
405
406
407     private static void closeStatement (Statement statement) {
408         try {
409             if (statement != null) {
410                 statement.close ();
411             }
412         } catch (SQLException sqlEx) {
413             logger.warn("Cannot close a statement", sqlEx);
414         }
415     }
416
417 }
Popular Tags