KickJava   Java API By Example, From Geeks To Geeks.

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


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
// NK 27.02.2002 - added in Jahia
15
//
16

17 package org.jahia.services.containers;
18
19 import org.jahia.data.JahiaDBDOMObject;
20 import org.jahia.data.JahiaDOMObject;
21 import org.jahia.exceptions.JahiaException;
22
23 import java.sql.*;
24 import java.util.Enumeration JavaDoc;
25 import java.util.Properties JavaDoc;
26 import java.util.Vector JavaDoc;
27
28 /**
29  * <p>Title: Database serialization of containerList properties.</p>
30  * <p>Description: This object manages all the operations of serialization
31  * of containerList properties in the database.</p>
32  * <p>Copyright: Copyright (c) 2001</p>
33  * <p>Company: Jahia</p>
34  *
35  * @author Khue Nguyen
36  * @version 1.0
37  */

38
39 public class JahiaContainerListPropDB {
40
41     final private static org.apache.log4j.Logger logger =
42             org.apache.log4j.Logger.getLogger (JahiaContainerListPropDB.class);
43
44
45     /**
46      * Default constructor, not much to say here...
47      */

48     public JahiaContainerListPropDB () {
49     }
50
51     /**
52      * Retrieves all the properties for a given containerList.
53      *
54      * @param containerListID the identifier of the containerList whose properties
55      * we want to retrieve from the database
56      *
57      * @return a Properties object that contains all the properties that are
58      * available for this containerList in the database
59      *
60      * @throws JahiaException generated if there were problems executing the
61      * query or communicating with the database.
62      */

63     public Properties JavaDoc getProperties (int containerListID)
64             throws JahiaException {
65         Properties JavaDoc result = new Properties JavaDoc ();
66         Connection dbConn = null;
67         PreparedStatement stmt = null;
68         ResultSet rs = null;
69         try {
70             dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
71
72             stmt = dbConn.prepareStatement ("SELECT * FROM jahia_ctnlists_prop WHERE ctnlistid_ctnlists_prop=?");
73             stmt.setInt(1, containerListID);
74             rs = stmt.executeQuery ();
75
76             while (rs.next ()) {
77                 int id = rs.getInt ("ctnlistid_ctnlists_prop");
78                 int siteID = rs.getInt ("jahiaid_ctnlists_prop");
79                 String JavaDoc name = rs.getString ("name_ctnlists_prop");
80                 String JavaDoc value = rs.getString ("value_ctnlists_prop");
81                 result.setProperty (name, value);
82             }
83
84             rs.close();
85
86         } catch (SQLException se) {
87             String JavaDoc errorMsg = "Cannot load container list properties from the database";
88             logger.warn (errorMsg, se);
89             throw new JahiaException (errorMsg, errorMsg, JahiaException.DATABASE_ERROR,
90                     JahiaException.CRITICAL_SEVERITY, se);
91
92         } finally {
93             closeStatement (stmt);
94         }
95         return result;
96
97     }
98
99     /**
100      * Saves a whole set of properties in the database for the specified
101      * containerList. WARNING : the way this is implemented (for speed reasons)
102      * is that first all the existing properties for a containerList are
103      * DELETED from the database. Remember to always load the full set of
104      * properties before calling this method or this will result in dataloss.
105      * Also if the operation with the database fails during the delete operation
106      * or the insertion of the new elements this will also occur in data loss.
107      * A safer way would be to test the existence of each property in the database
108      * and then INSERT or UPDATE the value, but that would take forever.
109      *
110      * @param containerListID the containerList whose properties
111      * we are serializing in the database.
112      * @param jahiaID the siteid
113      * @param containerListProperties the Properties object that contains all
114      * the properties to save in the database. Only what is passed here will
115      * exist in the database. If the database contained properties that aren't
116      * in this hashtable they will be deleted.
117      *
118      * @throws JahiaException generated if there were problems executing the
119      * query or communicating with the database.
120      */

121     public void setProperties (int containerListID,
122                                int jahiaID,
123                                Properties JavaDoc containerListProperties)
124             throws JahiaException {
125
126         // First we clear all the existing properties in the database...
127
// Warning this is dangerous if the operation is interrupted in the
128
// middle.
129
removeProperties (containerListID);
130
131         Connection dbConn = null;
132         PreparedStatement pstmt = null;
133         try {
134             // opens connection
135
dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
136
137             Enumeration JavaDoc propNames = containerListProperties.keys ();
138             while (propNames.hasMoreElements ()) {
139                 String JavaDoc curPropName = (String JavaDoc) propNames.nextElement ();
140                 String JavaDoc curPropValue = containerListProperties.getProperty (curPropName);
141
142                 pstmt =
143                         dbConn.prepareStatement (
144                                 "INSERT INTO jahia_ctnlists_prop VALUES(?,?,?,?)");
145                 pstmt.setInt (1, containerListID);
146                 pstmt.setInt (2, jahiaID);
147                 pstmt.setString (3, curPropName);
148                 pstmt.setString (4, curPropValue);
149                 pstmt.execute ();
150             }
151
152         } catch (SQLException se) {
153             String JavaDoc errorMsg = "Cannot set container list properties in the database";
154             logger.warn (errorMsg, se);
155             throw new JahiaException (errorMsg, errorMsg, JahiaException.DATABASE_ERROR,
156                     JahiaException.CRITICAL_SEVERITY, se);
157
158         } finally {
159             closeStatement (pstmt);
160         }
161     }
162
163     /**
164      * Removes all the properties for the specified containerList ID.
165      *
166      * @param containerListID idenfitifer of the containerList to delete all the
167      * properties
168      *
169      * @throws JahiaException generated if there were problems executing the
170      * query or communicating with the database.
171      */

172     public void removeProperties (int containerListID)
173             throws JahiaException {
174         Connection dbConn = null;
175         Statement stmt = null;
176         try {
177
178             // composes the query
179
String JavaDoc sqlQuery =
180                     "DELETE FROM jahia_ctnlists_prop WHERE ctnlistid_ctnlists_prop = " +
181                     containerListID;
182
183             // executes the query
184
dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
185             stmt = dbConn.createStatement ();
186             stmt.executeUpdate (sqlQuery);
187
188         }
189                 // catches error if cannot execute update query
190
catch (SQLException se) {
191             String JavaDoc errorMsg = "Cannot remove container list properties in the database";
192             logger.warn (errorMsg, se);
193             throw new JahiaException (errorMsg, errorMsg, JahiaException.DATABASE_ERROR,
194                     JahiaException.CRITICAL_SEVERITY, se);
195         } finally {
196
197             closeStatement (stmt);
198         }
199     }
200
201     /**
202      * Retrieves the containerList property from the database. This method may
203      * also be used as a test for existence of a property in the database.
204      *
205      * @param containerListID identifier of the containerList
206      * @param propertyName name of the property to retrieve
207      *
208      * @return a String containing the value of the property, or null if the
209      * property doesn't have a value in the database (ie if it doesn't exist)
210      *
211      * @throws JahiaException generated if there were problems executing the
212      * query or communicating with the database.
213      */

214     public String JavaDoc getProperty (int containerListID, String JavaDoc propertyName)
215             throws JahiaException {
216         String JavaDoc result = null;
217         Connection dbConn = null;
218         PreparedStatement pstmt = null;
219         ResultSet rs = null;
220         try {
221             dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
222             String JavaDoc sqlQuery = "SELECT value_ctnlists_prop FROM jahia_ctnlists_prop WHERE ctnlistid_ctnlists_prop=? AND name_ctnlists_prop=?";
223             pstmt = dbConn.prepareStatement (sqlQuery);
224
225             pstmt.setInt (1, containerListID);
226             pstmt.setString (2, propertyName);
227
228             rs = pstmt.executeQuery ();
229
230             if (rs.next ()) {
231                 String JavaDoc value = rs.getString ("value_ctnlists_prop");
232                 result = value;
233             }
234
235         } catch (SQLException se) {
236             String JavaDoc errorMsg = "Cannot load container list properties in the database";
237             logger.warn (errorMsg, se);
238             throw new JahiaException (errorMsg, errorMsg, JahiaException.DATABASE_ERROR,
239                     JahiaException.CRITICAL_SEVERITY, se);
240
241         } finally {
242             closeStatement (pstmt);
243         }
244         return result;
245     }
246
247     /**
248      * Saves a single property in the database for a given containerList. This
249      * operation starts by deleting any existing entry and then inserting a
250      * new value.
251      *
252      * @param containerListID identifier of the containerList
253      * @param jahiaID , the site id
254      * @param propertyName name of the property to add in the database
255      * @param propertyValue name of the property to add in the database
256      *
257      * @throws JahiaException generated if there were problems executing the
258      * query or communicating with the database.
259      */

260     public void setProperty (int containerListID,
261                              int jahiaID,
262                              String JavaDoc propertyName,
263                              String JavaDoc propertyValue)
264             throws JahiaException {
265
266         removeProperty (containerListID, propertyName);
267
268         Connection dbConn = null;
269         PreparedStatement pstmt = null;
270         try {
271             // opens connection
272
dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
273
274             pstmt =
275                     dbConn.prepareStatement ("INSERT INTO jahia_ctnlists_prop VALUES(?,?,?,?)");
276             pstmt.setInt (1, containerListID);
277             pstmt.setInt (2, jahiaID);
278             pstmt.setString (3, propertyName);
279             pstmt.setString (4, propertyValue);
280             pstmt.execute ();
281
282         } catch (SQLException se) {
283             String JavaDoc errorMsg = "Cannot load container list properties in the database";
284             logger.warn (errorMsg, se);
285             throw new JahiaException (errorMsg, errorMsg, JahiaException.DATABASE_ERROR,
286                     JahiaException.CRITICAL_SEVERITY, se);
287
288         } finally {
289             closeStatement (pstmt);
290         }
291     }
292
293     /**
294      * Removes a single property for the given containerList.
295      *
296      * @param containerListID identifer of the containerList
297      * @param propertyName name of the property to be deleted.
298      *
299      * @throws JahiaException generated if there were problems executing the
300      * query or communicating with the database.
301      */

302     public void removeProperty (int containerListID,
303                                 String JavaDoc propertyName)
304             throws JahiaException {
305         Connection dbConn = null;
306         PreparedStatement pstmt = null;
307         try {
308
309             dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
310
311             // composes the query
312
String JavaDoc sqlQuery = "DELETE FROM jahia_ctnlists_prop WHERE ctnlistid_ctnlists_prop = ? AND name_jahia_ctnlists_prop=?";
313
314             // executes the query
315
pstmt = dbConn.prepareStatement (sqlQuery);
316             pstmt.setInt (1, containerListID);
317             pstmt.setString (2, propertyName);
318             pstmt.executeUpdate ();
319         }
320                 // catches error if cannot execute update query
321
catch (SQLException se) {
322             String JavaDoc errorMsg = "Cannot delete container list properties in the database";
323             logger.warn (errorMsg, se);
324             throw new JahiaException (errorMsg, errorMsg, JahiaException.DATABASE_ERROR,
325                     JahiaException.CRITICAL_SEVERITY, se);
326
327         } finally {
328             closeStatement (pstmt);
329         }
330     }
331
332
333     /**
334      * return a DOM document of all the properties of all the containers for
335      * a given siteID
336      *
337      * @param siteID the identifier for the siteID for which to extract all the
338      * containerList properties
339      *
340      * @return JahiaDOMObject a DOM representation of the properties of the
341      * containerList
342      *
343      * @throws JahiaException generated if there were problems executing the
344      * query or communicating with the database.
345      */

346     public JahiaDOMObject getPropertiesAsDOM (int siteID)
347             throws JahiaException {
348         Connection dbConn = null;
349         Statement statement = null;
350         JahiaDBDOMObject dom = null;
351
352         try {
353             String JavaDoc sqlQuery = "SELECT DISTINCT * FROM jahia_ctnlists_prop WHERE jahiaid_ctnlists_prop=" + siteID;
354
355             dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
356             statement = dbConn.createStatement ();
357             if (statement != null) {
358                 ResultSet rs = statement.executeQuery (sqlQuery);
359                 if (rs != null) {
360                     dom = new JahiaDBDOMObject ();
361                     dom.addTable ("jahia_ctnlists_prop", rs);
362                     return dom;
363                 }
364             }
365         } catch (SQLException se) {
366             String JavaDoc errorMsg = "Cannot load container list properties in the database";
367             logger.warn (errorMsg, se);
368             throw new JahiaException (errorMsg, errorMsg, JahiaException.DATABASE_ERROR,
369                     JahiaException.CRITICAL_SEVERITY, se);
370
371         } finally {
372             closeStatement (statement);
373         }
374
375         return dom;
376     }
377
378
379     /**
380      * return a Vector of all view fields acl ids for a given siteID
381      *
382      * @param siteID the identifier for the siteID for which to extract all the
383      * view fields acl
384      *
385      * @return Vector , the vector of acl ids
386      *
387      * @throws JahiaException generated if there were problems executing the
388      * query or communicating with the database.
389      */

390     public Vector JavaDoc getCtnListFieldACLs (int siteID)
391             throws JahiaException {
392
393         Connection dbConn = null;
394         Statement statement = null;
395         Vector JavaDoc ids = new Vector JavaDoc ();
396
397         try {
398
399             String JavaDoc sqlQuery =
400                     "SELECT DISTINCT name_ctnlists_prop,value_ctnlists_prop FROM jahia_ctnlists_prop WHERE jahiaid_ctnlists_prop=" +
401                     siteID;
402
403             dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
404             statement = dbConn.createStatement ();
405             if (statement != null) {
406                 ResultSet rs = statement.executeQuery (sqlQuery);
407                 String JavaDoc name = "";
408                 String JavaDoc value = "";
409                 while (rs.next ()) {
410                     name = rs.getString ("name_ctnlists_prop");
411                     value = rs.getString ("value_ctnlists_prop");
412                     if ((value != null) && (!value.trim ().equals ("")) && (name != null) && (name.startsWith (
413                             "view_field_acl_"))) {
414                         try {
415                             ids.add (new Integer JavaDoc (value));
416                         } catch (Throwable JavaDoc t) {
417                         }
418                     }
419                 }
420             }
421         } catch (SQLException se) {
422             String JavaDoc errorMsg = "Cannot load container list properties in the database";
423             logger.warn (errorMsg, se);
424             throw new JahiaException (errorMsg, errorMsg, JahiaException.DATABASE_ERROR,
425                     JahiaException.CRITICAL_SEVERITY, se);
426
427         } finally {
428             closeStatement (statement);
429         }
430
431         return ids;
432     }
433
434
435     private void closeStatement (Statement statement) {
436         // Close the opened statement
437
try {
438             if (statement != null) {
439                 statement.close ();
440             }
441         } catch (SQLException sqlEx) {
442             logger.warn ("Cannot close a statement", sqlEx);
443         }
444     }
445
446 }
Popular Tags