KickJava   Java API By Example, From Geeks To Geeks.

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


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 Developer of the Shared Modifications is Jahia Solution Sarl.
30  * Portions created by the Initial Developer are Copyright (C) 2002 by the
31  * Initial Developer. All Rights Reserved.
32  *
33  * Contributor(s):
34  * DD-MMM-YYYY, <company name>, <author name> [: <small comment>]
35  *
36  * ----- END LICENSE BLOCK -----
37  */

38
39 /** Holds all the methods enabling container definitions load, update and delete.
40  *
41  * @author Eric Vassalli
42  *
43  * @todo This is a huge mess... Table names are completely misleading. From what
44  * I can understand jahia_ctn_def_properties is a table used to figure out which
45  * Jahia pages are using which definition, so that we may for example update
46  * or destroy these pages once the definition is changed or deleted.
47  */

48
49
50 package org.jahia.services.containers;
51
52 import org.jahia.data.JahiaDBDOMObject;
53 import org.jahia.data.JahiaDOMObject;
54 import org.jahia.data.containers.JahiaContainerDefinition;
55 import org.jahia.data.containers.JahiaContainerSubDefinition;
56 import org.jahia.exceptions.JahiaException;
57 import org.jahia.registries.ServicesRegistry;
58 import org.jahia.services.database.ConnectionDispenser;
59 import org.jahia.utils.JahiaTools;
60
61 import java.sql.Connection JavaDoc;
62 import java.sql.PreparedStatement JavaDoc;
63 import java.sql.ResultSet JavaDoc;
64 import java.sql.SQLException JavaDoc;
65 import java.sql.Statement JavaDoc;
66 import java.util.Enumeration JavaDoc;
67 import java.util.Hashtable JavaDoc;
68 import java.util.Vector JavaDoc;
69
70
71 public class JahiaContainerDefinitionsDB {
72     final private static org.apache.log4j.Logger logger =
73             org.apache.log4j.Logger.getLogger (JahiaContainerDefinitionsDB.class);
74
75     private JahiaContainerStructuresDB c_struct;
76
77
78     /**
79      * Default constructor, creates a new <code></code> object instance.
80      */

81     public JahiaContainerDefinitionsDB () {
82         c_struct = new JahiaContainerStructuresDB ();
83     }
84
85
86     /**
87      * load a container definition by its id
88      *
89      * @return a JahiaContainerDefinition object
90      *
91      * @throws org.jahia.exceptions.JahiaException
92      * when a data failure occured, or when the allocated resources could not be freed.
93      * @see org.jahia.data.containers.JahiaContainerDefinition
94      */

95     public JahiaContainerDefinition db_load_container_definition (int defID)
96             throws JahiaException {
97         Connection JavaDoc dbConn = null;
98         Statement JavaDoc stmt = null;
99         Statement JavaDoc stmt2 = null;
100         ResultSet JavaDoc rs = null;
101         ResultSet JavaDoc rs2 = null;
102         JahiaContainerDefinition theDef = null;
103         try {
104
105             // creates connexion
106
dbConn = ConnectionDispenser.getConnection ();
107             stmt = dbConn.createStatement ();
108             stmt2 = dbConn.createStatement ();
109
110             // prepares sqlQuery
111
String JavaDoc sqlQuery = "SELECT * FROM jahia_ctn_def WHERE id_jahia_ctn_def = " + defID;
112             rs = stmt.executeQuery (sqlQuery);
113             if (rs.next ()) {
114                 int ID = rs.getInt ("id_jahia_ctn_def");
115                 int jahiaID = rs.getInt ("jahiaid_jahia_ctn_def");
116                 String JavaDoc name = rs.getString ("name_jahia_ctn_def");
117
118                 sqlQuery = "SELECT * FROM jahia_ctn_def_properties ";
119                 sqlQuery += "WHERE ctndefid_jahia_ctn_def_prop=" + ID;
120                 rs2 = stmt2.executeQuery (sqlQuery);
121
122                 Hashtable JavaDoc subDefs = new Hashtable JavaDoc ();
123
124                 while (rs2.next ()) {
125                     int subDefID = rs2.getInt ("id_jahia_ctn_def_properties");
126                     int templateID = rs2.getInt ("pagedefid_jahia_ctn_def_prop");
127                     String JavaDoc title = rs2.getString ("title_jahia_ctn_def_properties");
128                     Vector JavaDoc structure = c_struct.db_load_container_structure (subDefID);
129                     subDefs.put (new Integer JavaDoc (templateID),
130                             new JahiaContainerSubDefinition (subDefID, templateID, title,
131                                     structure));
132                 }
133
134                 theDef = new JahiaContainerDefinition (ID, jahiaID, name, subDefs);
135             }
136
137         } catch (SQLException JavaDoc se) {
138             String JavaDoc errorMsg = "Error in db_load_container_definition : " + se.getMessage () + " -> BAILING OUT";
139             logger.error (errorMsg);
140             throw new JahiaException ("Cannot load fields from the database",
141                     errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL_SEVERITY);
142         } finally {
143             closeStatement(stmt);
144             closeStatement(stmt2);
145         }
146         return theDef;
147     }
148
149
150     /**
151      * load a container definition by its name and site id
152      *
153      * @return a JahiaContainerDefinition object
154      *
155      * @throws org.jahia.exceptions.JahiaException
156      * when a data failure occured, or when the allocated resources could not be freed.
157      * @see org.jahia.data.containers.JahiaContainerDefinition
158      */

159     public JahiaContainerDefinition db_load_container_definition (int siteID, String JavaDoc defName)
160             throws JahiaException {
161         Connection JavaDoc dbConn = null;
162         Statement JavaDoc stmt = null;
163         Statement JavaDoc stmt2 = null;
164         ResultSet JavaDoc rs = null;
165         ResultSet JavaDoc rs2 = null;
166         JahiaContainerDefinition theDef = null;
167         try {
168
169             // creates connexion
170
dbConn = ConnectionDispenser.getConnection ();
171             stmt = dbConn.createStatement ();
172             stmt2 = dbConn.createStatement ();
173
174             // prepares sqlQuery
175
String JavaDoc sqlQuery = "SELECT * FROM jahia_ctn_def WHERE jahiaid_jahia_ctn_def = " +
176                     siteID + " and name_jahia_ctn_def='" + defName + "'";
177             rs = stmt.executeQuery (sqlQuery);
178             if (rs.next ()) {
179                 int ID = rs.getInt ("id_jahia_ctn_def");
180                 int jahiaID = rs.getInt ("jahiaid_jahia_ctn_def");
181                 String JavaDoc name = rs.getString ("name_jahia_ctn_def");
182
183                 sqlQuery = "SELECT * FROM jahia_ctn_def_properties ";
184                 sqlQuery += "WHERE ctndefid_jahia_ctn_def_prop=" + ID;
185                 rs2 = stmt2.executeQuery (sqlQuery);
186
187                 Hashtable JavaDoc subDefs = new Hashtable JavaDoc ();
188
189                 while (rs2.next ()) {
190                     int subDefID = rs2.getInt ("id_jahia_ctn_def_properties");
191                     int templateID = rs2.getInt ("pagedefid_jahia_ctn_def_prop");
192                     String JavaDoc title = rs2.getString ("title_jahia_ctn_def_properties");
193                     Vector JavaDoc structure = c_struct.db_load_container_structure (subDefID);
194                     subDefs.put (new Integer JavaDoc (templateID),
195                             new JahiaContainerSubDefinition (subDefID, templateID, title,
196                                     structure));
197                 }
198
199                 theDef = new JahiaContainerDefinition (ID, jahiaID, name, subDefs);
200             }
201
202         } catch (SQLException JavaDoc se) {
203             String JavaDoc errorMsg = "Error in db_load_container_definition : " + se.getMessage () + " -> BAILING OUT";
204             throw new JahiaException ("Cannot load fields from the database",
205                     errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL_SEVERITY,
206                     se);
207         } finally {
208             closeStatement(stmt);
209             closeStatement(stmt2);
210         }
211         return theDef;
212     }
213
214
215     /**
216      * Creates a container definition.
217      *
218      * @param theDef the JahiaContainerDefinition object
219      *
220      * @throws org.jahia.exceptions.JahiaException
221      * when a data failure occured, or when the allocated resources could not be freed.
222      * @see org.jahia.data.containers.JahiaContainerDefinition
223      */

224     public void db_create_container_definition (JahiaContainerDefinition theDef)
225             throws JahiaException {
226         Connection JavaDoc dbConn = null;
227         PreparedStatement JavaDoc stmt = null;
228         try {
229             // gets the field id
230
int theDefID = ServicesRegistry.getInstance ().getJahiaIncrementorsDBService (
231             ).autoIncrement ("jahia_ctn_def");
232             theDef.setID (theDefID);
233             // saves definition base
234
String JavaDoc sqlQuery =
235                     "INSERT INTO jahia_ctn_def (id_jahia_ctn_def,jahiaid_jahia_ctn_def,name_jahia_ctn_def) VALUES(?,?,?)";
236
237             // opens connection
238
dbConn = ConnectionDispenser.getConnection ();
239             stmt = dbConn.prepareStatement (sqlQuery);
240             stmt.setInt (1, theDef.getID ());
241             stmt.setInt (2, theDef.getJahiaID ());
242             stmt.setString (3, theDef.getName ());
243             stmt.executeUpdate ();
244
245             // enters values with update_container_definition
246
db_update_container_defprop (theDef);
247         }
248                 // catches error if cannot execute insert query
249
catch (SQLException JavaDoc se) {
250             String JavaDoc errorMsg = "Error in db_create_container_definition : " + se.getMessage ();
251             logger.error (errorMsg);
252             throw new JahiaException (
253                     "Cannot insert new container definitions in the database",
254                     errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL_SEVERITY);
255
256         } finally {
257             closeStatement(stmt);
258         }
259     }
260     
261     /**
262      * creates a container definition
263      *
264      * @param theDef the JahiaContainerDefinition object
265      *
266      * @throws org.jahia.exceptions.JahiaException
267      * when a data failure occured, or when the allocated resources could not be freed.
268      * @see org.jahia.data.containers.JahiaContainerDefinition
269      */

270     public void db_create_container_defprop(JahiaContainerDefinition theDef) throws JahiaException {
271         Connection JavaDoc dbConn = null;
272         PreparedStatement JavaDoc stmt = null;
273         try {
274             dbConn = ConnectionDispenser.getConnection ();
275             String JavaDoc sqlQuery = "INSERT INTO jahia_ctn_def_properties (id_jahia_ctn_def_properties,ctndefid_jahia_ctn_def_prop,pagedefid_jahia_ctn_def_prop,title_jahia_ctn_def_properties) VALUES(?,?,?,?)";
276            
277             stmt = dbConn.prepareStatement (sqlQuery);
278             // saves all sub definitions
279
Hashtable JavaDoc subDefs = theDef.getSubDefs();
280             Enumeration JavaDoc subKeys = subDefs.keys();
281             while (subKeys.hasMoreElements()) {
282                 int pageDefID = ((Integer JavaDoc) subKeys.nextElement()).intValue();
283                 JahiaContainerSubDefinition theSubDef = (JahiaContainerSubDefinition) subDefs
284                         .get(new Integer JavaDoc(pageDefID));
285
286                 // sets the subdef id
287
if (theSubDef.getID() == 0) {
288                     int theSubDefID = ServicesRegistry.getInstance().getJahiaIncrementorsDBService().autoIncrement(
289                             "jahia_ctn_def_properties");
290                     theSubDef.setID(theSubDefID);
291                 }
292
293                 if (logger.isDebugEnabled()) {
294                     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc("Updating sub container def [");
295                     buffer.append(theSubDef.getTitle());
296                     buffer.append(",");
297                     buffer.append(theSubDef.getID());
298                     buffer.append("] for container def ");
299                     buffer.append(theDef.getID());
300                     logger.debug(buffer.toString());
301                 }
302
303                 if (!theSubDef.getTitle().equals("")) {
304                     // composes the query
305
stmt.setInt(1, theSubDef.getID());
306                     stmt.setInt(2, theDef.getID());
307                     stmt.setInt(3, pageDefID);
308                     stmt.setString(4, JahiaTools.quote(theSubDef.getTitle()));
309
310                     stmt.executeUpdate();
311                 }
312
313                 // creates the container structure
314
c_struct.db_update_container_structure(theSubDef);
315             }
316         } catch (SQLException JavaDoc se) {
317             String JavaDoc errorMsg = "Error in db_update_container_definition : " + se.getMessage();
318             logger.error(errorMsg);
319             throw new JahiaException("Cannot update container definitions in the database", errorMsg,
320                     JahiaException.DATABASE_ERROR, JahiaException.CRITICAL_SEVERITY);
321         } finally {
322             closeStatement(stmt);
323         }
324
325     }
326         
327     /**
328      * updates a container definition
329      *
330      * @param theDef the JahiaContainerDefinition object
331      *
332      * @throws org.jahia.exceptions.JahiaException
333      * when a data failure occured, or when the allocated resources could not be freed.
334      * @see org.jahia.data.containers.JahiaContainerDefinition
335      */

336     public void db_update_container_defprop (JahiaContainerDefinition theDef)
337             throws JahiaException {
338         Connection JavaDoc dbConn = null;
339         PreparedStatement JavaDoc stmt = null;
340         String JavaDoc sqlQuery = "";
341         try {
342             // creates the connection
343
dbConn = ConnectionDispenser.getConnection ();
344             sqlQuery = "DELETE FROM jahia_ctn_def_properties WHERE ctndefid_jahia_ctn_def_prop=?";
345            
346             stmt = dbConn.prepareStatement (sqlQuery);
347             stmt.setInt(1,theDef.getID());
348
349             // deletes all precedent sub definitions
350
stmt.executeUpdate ();
351
352             db_create_container_defprop(theDef);
353         }
354                 // catches error if cannot execute update query
355
catch (SQLException JavaDoc se) {
356             String JavaDoc errorMsg = "Error in db_update_container_definition : " + se.getMessage ();
357             logger.error (errorMsg);
358             throw new JahiaException ("Cannot update container definitions in the database",
359                     errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL_SEVERITY);
360         } finally {
361             closeStatement(stmt);
362         }
363     }
364
365
366     /**
367      * deletes a container definition and all it's subdef
368      *
369      * @param theDefID the container definition id
370      *
371      * @throws org.jahia.exceptions.JahiaException
372      * when a data failure occured, or when the allocated resources could not be freed.
373      */

374     public void db_delete_container_definition (int theDefID)
375             throws JahiaException {
376         Connection JavaDoc dbConn = null;
377         Statement JavaDoc stmt = null;
378         try {
379             // composes the query
380
String JavaDoc sqlQuery = "DELETE FROM jahia_ctn_def WHERE id_jahia_ctn_def=" + theDefID;
381
382             // executes the query
383
dbConn = ConnectionDispenser.getConnection ();
384             stmt = dbConn.createStatement ();
385             stmt.executeUpdate (sqlQuery);
386
387
388         }
389                 // catches error if cannot execute update query
390
catch (SQLException JavaDoc se) {
391             String JavaDoc errorMsg = "Error in db_delete_container_definition : " + se.getMessage ();
392             logger.error (errorMsg);
393             throw new JahiaException ("Cannot delete container definitions in the database",
394                     errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL_SEVERITY);
395         } finally {
396             closeStatement(stmt);
397         }
398     }
399
400
401     /**
402      * deletes a container sub definitions of a ctn definition ID
403      *
404      * @param ctnDefID the container definition ID
405      *
406      * @throws org.jahia.exceptions.JahiaException
407      * when a data failure occured, or when the allocated resources could not be freed.
408      */

409     public void db_delete_container_sub_definition (int ctnDefID)
410             throws JahiaException {
411         Connection JavaDoc dbConn = null;
412         Statement JavaDoc stmt = null;
413         try {
414             // composes the query
415
String JavaDoc sqlQuery =
416                     "DELETE FROM jahia_ctn_def_properties WHERE ctndefid_jahia_ctn_def_prop=" +
417                     ctnDefID;
418
419             // executes the query
420
dbConn = ConnectionDispenser.getConnection ();
421             stmt = dbConn.createStatement ();
422             stmt.executeUpdate (sqlQuery);
423         }
424                 // catches error if cannot execute update query
425
catch (SQLException JavaDoc se) {
426             String JavaDoc errorMsg = "Error in db_delete_container_sub_definition : " + se.getMessage ();
427             logger.error (errorMsg);
428             throw new JahiaException (
429                     "Cannot delete container sub definitions in the database",
430                     errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL_SEVERITY);
431         } finally {
432             closeStatement(stmt);
433         }
434     }
435
436
437     /**
438      * return a DOM document of all container def of a site
439      *
440      * @param siteID the site id
441      *
442      * @return JahiaDOMObject a DOM representation of this object
443      */

444     public JahiaDOMObject getContainerDefsAsDOM (int siteID)
445             throws JahiaException {
446
447         Connection JavaDoc dbConn = null;
448         Statement JavaDoc statement = null;
449         JahiaDBDOMObject dom = null;
450
451         try {
452             String JavaDoc sqlQuery = "SELECT * FROM jahia_ctn_def where jahiaid_jahia_ctn_def=" + siteID;
453
454             dbConn = ConnectionDispenser.getConnection ();
455             statement = dbConn.createStatement ();
456             if (statement != null) {
457                 ResultSet JavaDoc rs = statement.executeQuery (sqlQuery);
458                 if (rs != null) {
459                     dom = new JahiaDBDOMObject ();
460                     dom.addTable ("jahia_ctn_def", rs);
461                     return dom;
462                 }
463             }
464         } catch (SQLException JavaDoc se) {
465             String JavaDoc errorMsg = "Error in getContainerDefsAsDOM(int siteID) : " + se.getMessage ();
466             logger.error (errorMsg);
467             throw new JahiaException ("Cannot load container defs from the database",
468                     errorMsg, JahiaException.DATABASE_ERROR,
469                     JahiaException.CRITICAL_SEVERITY);
470         } finally {
471
472             closeStatement (statement);
473         }
474
475         return dom;
476     }
477
478
479     /**
480      * return a DOM document of all container def prop of a site
481      *
482      * @param siteID the site id
483      *
484      * @return JahiaDOMObject a DOM representation of this object
485      */

486     public JahiaDOMObject getContainerDefPropsAsDOM (int siteID)
487             throws JahiaException {
488
489         Connection JavaDoc dbConn = null;
490         Statement JavaDoc statement = null;
491
492         JahiaDBDOMObject dom = null;
493
494         try {
495
496             String JavaDoc sqlQuery = "SELECT DISTINCT jahia_ctn_def_properties.id_jahia_ctn_def_properties,jahia_ctn_def_properties.ctndefid_jahia_ctn_def_prop,jahia_ctn_def_properties.pagedefid_jahia_ctn_def_prop,jahia_ctn_def_properties.title_jahia_ctn_def_properties FROM jahia_ctn_def_properties,jahia_ctn_def where jahia_ctn_def_properties.ctndefid_jahia_ctn_def_prop=jahia_ctn_def.id_jahia_ctn_def AND jahia_ctn_def.jahiaid_jahia_ctn_def=" +
497                     siteID;
498
499             dbConn = ConnectionDispenser.getConnection ();
500             statement = dbConn.createStatement ();
501             if (statement != null) {
502                 ResultSet JavaDoc rs = statement.executeQuery (sqlQuery);
503                 if (rs != null) {
504                     dom = new JahiaDBDOMObject ();
505                     dom.addTable ("jahia_ctn_def_properties", rs);
506                     return dom;
507                 }
508             }
509         } catch (SQLException JavaDoc se) {
510             String JavaDoc errorMsg = "Error in getContainerDefPropsAsDOM(int siteID) : " + se.getMessage ();
511             logger.error (errorMsg);
512             throw new JahiaException ("Cannot load container defs prop from the database",
513                     errorMsg, JahiaException.DATABASE_ERROR,
514                     JahiaException.CRITICAL_SEVERITY);
515         } finally {
516
517             closeStatement (statement);
518         }
519
520         return dom;
521     }
522
523
524     private void closeStatement (Statement JavaDoc statement) {
525         // Close the opened statement
526
try {
527             if (statement != null) {
528                 statement.close ();
529             }
530
531         } catch (SQLException JavaDoc sqlEx) {
532             logger.warn ("Cannot close a statement", sqlEx);
533         }
534     }
535
536 }
537
Popular Tags