KickJava   Java API By Example, From Geeks To Geeks.

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


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  * JahiaContainersDB
15  * @author Eric Vassalli
16  *
17  * Holds all the methods enabling containers load, update and delete.
18  *
19  */

20
21 //
22
// db_load_container( ctnid )
23
// db_create_container( theContainer )
24
// db_update_container( theContainer )
25
// db_delete_container( thectnid )
26
//
27

28 package org.jahia.services.containers;
29
30 import org.jahia.data.JahiaDBDOMObject;
31 import org.jahia.data.JahiaDOMObject;
32 import org.jahia.data.containers.JahiaContainer;
33 import org.jahia.exceptions.JahiaException;
34 import org.jahia.services.database.ConnectionDispenser;
35 import org.jahia.services.version.EntryLoadRequest;
36 import org.jahia.services.version.JahiaSaveVersion;
37
38 import java.sql.Connection JavaDoc;
39 import java.sql.PreparedStatement JavaDoc;
40 import java.sql.ResultSet JavaDoc;
41 import java.sql.SQLException JavaDoc;
42 import java.sql.Statement JavaDoc;
43 import java.util.Vector JavaDoc;
44
45 public class JahiaContainersDB {
46
47     private static org.apache.log4j.Logger logger =
48             org.apache.log4j.Logger.getLogger (JahiaContainersDB.class);
49
50     /**
51      * constructor
52      */

53     public JahiaContainersDB () {
54     } // end constructor
55

56     /**
57      * loads all the container info from page
58      *
59      * @param cpageid the page id
60      *
61      * @return returns a vector of JahiaContainer
62      *
63      * @throws throws a critical JahiaException if a data access occurs
64      * @throws throws a warning JahiaException if cannot free resources
65      */

66     public Vector JavaDoc db_load_all_active_containers_info_from_page (int pageID) throws
67             JahiaException {
68         Vector JavaDoc result = new Vector JavaDoc ();
69         Connection JavaDoc dbConn = null;
70         PreparedStatement JavaDoc stmt = null;
71         ResultSet JavaDoc rs = null;
72         JahiaContainer theContainer = null;
73         try {
74             String JavaDoc sqlQuery = "SELECT * FROM jahia_ctn_entries WHERE pageid_jahia_ctn_entries=? AND workflow_state=1";
75
76             dbConn = ConnectionDispenser.getConnection ();
77             stmt = dbConn.prepareStatement (sqlQuery);
78             stmt.setInt(1, pageID);
79             rs = stmt.executeQuery ();
80
81             while (rs.next ()) {
82                 // load values from container_entries
83
int ID = rs.getInt ("id_jahia_ctn_entries");
84                 int jahiaID = rs.getInt ("jahiaid_jahia_ctn_entries");
85                 int listID = rs.getInt ("listid_jahia_ctn_entries");
86                 int definitionID = rs.getInt ("ctndefid_jahia_ctn_entries");
87                 int rank = rs.getInt ("rank_jahia_ctn_entries");
88                 int rights = rs.getInt ("rights_jahia_ctn_entries");
89                 int versionID = rs.getInt ("version_id");
90                 int workflowState = rs.getInt ("workflow_state");
91
92                 theContainer = new JahiaContainer (ID, jahiaID, pageID, listID,
93                         rank, rights, definitionID,
94                         versionID, workflowState);
95                 result.add (theContainer);
96             }
97
98         } catch (SQLException JavaDoc se) {
99             logger.debug ("Bailing out because of SQL exception", se);
100             String JavaDoc errorMsg = "Error in db_load_all_active_container : " +
101                     se.getMessage () + " -> BAILING OUT";
102             throw new JahiaException ("Cannot load fields from the database",
103                     errorMsg, JahiaException.DATABASE_ERROR,
104                     JahiaException.CRITICAL_SEVERITY, se);
105         } finally {
106             try {
107                 if (stmt != null) {
108                     stmt.close ();
109                 }
110             } catch (SQLException JavaDoc ex) {
111                 new JahiaException ("Cannot free resources",
112                         "db_load_all_active_container : cannot free resources",
113                         JahiaException.DATABASE_ERROR,
114                         JahiaException.WARNING_SEVERITY);
115             }
116         }
117         return result;
118     } // end db_load_container
119

120     /**
121      * loads a container from its container id
122      *
123      * @param ctnid the entry ID
124      * @param loadVersion specifies the parameters to load the entry
125      * state corresponding, either active, staged, deleted or versioned.
126      *
127      * @return returns a JahiaContainer if ctnid found, null if not
128      *
129      * @throws JahiaException throws a critical JahiaException if a data access
130      * occurs, throws a warning JahiaException if cannot free resources
131      * @see org.jahia.data.containers.JahiaContainer
132      */

133     public JahiaContainer db_load_container (int ctnid,
134                                              EntryLoadRequest loadVersion) throws
135             JahiaException {
136         Connection JavaDoc dbConn = null;
137         PreparedStatement JavaDoc stmt = null;
138         ResultSet JavaDoc rs = null;
139         JahiaContainer theContainer = null;
140         try {
141             // request de la version active
142
String JavaDoc sqlQuery = "SELECT * FROM jahia_ctn_entries WHERE (id_jahia_ctn_entries=? AND workflow_state=1)";
143
144             // if we want to load the staged version
145
if (loadVersion.isStaging ()) {
146                 sqlQuery = "SELECT * FROM jahia_ctn_entries WHERE (id_jahia_ctn_entries=? AND workflow_state>0) ORDER BY workflow_state DESC";
147             } else if (loadVersion.isDeleted ()) {
148                 sqlQuery = "SELECT * FROM jahia_ctn_entries WHERE (id_jahia_ctn_entries=? AND workflow_state=-1) ORDER BY version_id DESC";
149             // if we want to load the version ID
150
} else if (loadVersion.isVersioned ()) {
151                 sqlQuery = "SELECT * FROM jahia_ctn_entries WHERE (id_jahia_ctn_entries=? AND workflow_state<2 AND version_id<=?) ORDER BY version_id DESC";
152             }
153
154             dbConn = ConnectionDispenser.getConnection ();
155             stmt = dbConn.prepareStatement (sqlQuery);
156             stmt.setInt(1, ctnid);
157             if (loadVersion.isVersioned ()){
158                 stmt.setInt(2, loadVersion.getVersionID ());
159             }
160             rs = stmt.executeQuery ();
161
162             if (rs.next ()) {
163                 // load values from container_entries
164
int ID = rs.getInt ("id_jahia_ctn_entries");
165                 int jahiaID = rs.getInt ("jahiaid_jahia_ctn_entries");
166                 int pageID = rs.getInt ("pageid_jahia_ctn_entries");
167                 int listID = rs.getInt ("listid_jahia_ctn_entries");
168                 int definitionID = rs.getInt ("ctndefid_jahia_ctn_entries");
169                 int rank = rs.getInt ("rank_jahia_ctn_entries");
170                 int rights = rs.getInt ("rights_jahia_ctn_entries");
171                 int versionID = rs.getInt ("version_id");
172                 int workflowState = rs.getInt ("workflow_state");
173
174                 if (!loadVersion.isWithDeleted ()
175                         && (workflowState == -1 || versionID == -1)) {
176                     return null;
177                 }
178                 theContainer = new JahiaContainer (ID, jahiaID, pageID, listID,
179                         rank, rights, definitionID,
180                         versionID, workflowState);
181             }
182
183         } catch (SQLException JavaDoc se) {
184             logger.debug ("Bailing out because of SQL exception", se);
185             String JavaDoc errorMsg = "Error in db_load_container : " + se.getMessage () +
186                     " -> BAILING OUT";
187             throw new JahiaException ("Cannot load fields from the database",
188                     errorMsg, JahiaException.DATABASE_ERROR,
189                     JahiaException.CRITICAL_SEVERITY, se);
190         } finally {
191             try {
192                 if (stmt != null) {
193                     stmt.close ();
194                 }
195             } catch (SQLException JavaDoc ex) {
196                 new JahiaException ("Cannot free resources",
197                         "db_load_container : cannot free resources",
198                         JahiaException.DATABASE_ERROR,
199                         JahiaException.WARNING_SEVERITY);
200             }
201         }
202         return theContainer;
203     } // end db_load_container
204

205     /**
206      * creates a new container, and assignes it a new ID
207      *
208      * @param theContainer the JahiaContainer object to save
209      *
210      * @throws throws a critical JahiaException if a data access occurs
211      * @throws throws a warning JahiaException if cannot free resources
212      * @see org.jahia.data.containers.JahiaContainer
213      */

214     public void db_create_container (JahiaContainer theContainer,
215                                      JahiaSaveVersion saveVersion) throws
216             JahiaException {
217         Connection JavaDoc dbConn = null;
218         PreparedStatement JavaDoc stmt = null;
219         try {
220             // opens connection
221
dbConn = ConnectionDispenser.getConnection ();
222             String JavaDoc sqlQuery = "INSERT INTO jahia_ctn_entries(id_jahia_ctn_entries,jahiaid_jahia_ctn_entries,pageid_jahia_ctn_entries,listid_jahia_ctn_entries,ctndefid_jahia_ctn_entries,rank_jahia_ctn_entries,rights_jahia_ctn_entries,workflow_state,version_id) VALUES (?,?,?,?,?,?,?,?,?)";
223             stmt = dbConn.prepareStatement (sqlQuery);
224
225             // creates empty line
226
stmt.setInt(1, theContainer.getID ());
227             stmt.setInt(2, theContainer.getJahiaID ());
228             stmt.setInt(3, theContainer.getPageID ());
229             stmt.setInt(4, theContainer.getListID ());
230             stmt.setInt(5, theContainer.getctndefid ());
231             stmt.setInt(6, theContainer.getRank ());
232             stmt.setInt(7,theContainer.getAclID ());
233             stmt.setInt(8, saveVersion.getWorkflowState ());
234             stmt.setInt(9, saveVersion.getWorkflowState() > 1 ? 0 : saveVersion.getVersionID());
235
236             // executes the query, then closes the connection
237
stmt.execute ();
238
239         } catch (SQLException JavaDoc se) {
240             logger.debug ("Bailing out because of SQL exception", se);
241             String JavaDoc errorMsg = "Error in db_create_container : " + se.getMessage () +
242                     " -> BAILING OUT";
243             throw new JahiaException ("Cannot create containers in the database",
244                     errorMsg, JahiaException.DATABASE_ERROR,
245                     JahiaException.CRITICAL_SEVERITY, se);
246         } finally {
247             try {
248                 if (stmt != null) {
249                     stmt.close ();
250                 }
251             } catch (SQLException JavaDoc ex) {
252                 new JahiaException ("Cannot free resources",
253                         "db_create_container : cannot free resources",
254                         JahiaException.DATABASE_ERROR,
255                         JahiaException.WARNING_SEVERITY);
256             }
257         }
258     } // end db_create_container
259

260     /**
261      * updates a container entry in the database
262      *
263      * @param theContainer a JahiaContainer object
264      *
265      * @throws throws a critical JahiaException if SQL error
266      * @throws throws a warning JahiaException if cannot free resources
267      */

268     public void db_update_container (JahiaContainer theContainer,
269                                      JahiaSaveVersion saveVersion) throws
270             JahiaException {
271         Connection JavaDoc dbConn = null;
272         PreparedStatement JavaDoc stmt = null;
273         try {
274             dbConn = ConnectionDispenser.getConnection ();
275
276             String JavaDoc errorMsg = "";
277             if (theContainer.getDefinition ().getName ().equals ("")) {
278                 errorMsg =
279                         "Error in db_update_container : container name value is an empty string";
280             }
281             if (errorMsg != "") {
282                 logger.debug (errorMsg);
283                 throw new JahiaException (
284                         "Cannot update containers in the database",
285                         errorMsg, JahiaException.DATABASE_ERROR,
286                         JahiaException.CRITICAL_SEVERITY);
287             }
288
289             int ctnID = theContainer.getID ();
290             
291             // composes the query
292
String JavaDoc sqlQuery = "UPDATE jahia_ctn_entries SET jahiaid_jahia_ctn_entries=?,pageid_jahia_ctn_entries=?,listid_jahia_ctn_entries=?,ctndefid_jahia_ctn_entries=?,rank_jahia_ctn_entries=?,rights_jahia_ctn_entries=?,version_id=? WHERE id_jahia_ctn_entries=? AND workflow_state>1";
293             stmt = dbConn.prepareStatement (sqlQuery);
294             stmt.setInt(1, theContainer.getJahiaID ());
295             stmt.setInt(2, theContainer.getPageID ());
296             stmt.setInt(3, theContainer.getListID ());
297             stmt.setInt(4, theContainer.getctndefid ());
298             stmt.setInt(5, theContainer.getRank ());
299             stmt.setInt(6, theContainer.getAclID ());
300             stmt.setInt(7, 0);
301             stmt.setInt(8, theContainer.getID());
302             
303             // update staged version
304
if (saveVersion.isStaging ()) {
305                 // executes the query
306
int rows = stmt.executeUpdate ();
307                 logger.debug ("Updated rows=" + rows);
308                 // the staged version of the field doesn't already exist
309
if (rows == 0) {
310                     logger.debug ("Creating staged container...");
311                     createStagedContainer (dbConn, ctnID, saveVersion);
312                     stmt.executeUpdate ();
313                 }
314             // update active version
315
} else {
316                 if (saveVersion.isVersioned ()) {
317                     backupContainerVersion (dbConn, ctnID);
318                 }
319                 // composes the query
320
stmt.setInt(7, saveVersion.getVersionID ());
321                 
322                 // executes the query
323
stmt.executeUpdate ();
324             }
325         // catches error if cannot execute update query
326
} catch (SQLException JavaDoc se) {
327             logger.debug ("Bailing out because of SQL exception", se);
328             String JavaDoc errorMsg = "Error in db_update_container : " + se.getMessage ();
329             throw new JahiaException ("Cannot update containers in the database",
330                     errorMsg, JahiaException.DATABASE_ERROR,
331                     JahiaException.CRITICAL_SEVERITY, se);
332         } finally {
333             try {
334                 if (stmt != null) {
335                     stmt.close ();
336                 }
337             } catch (SQLException JavaDoc ex) {
338                 new JahiaException ("Cannot free resources",
339                         "db_update_container : cannot free resources",
340                         JahiaException.DATABASE_ERROR,
341                         JahiaException.WARNING_SEVERITY, ex);
342             }
343         }
344     } // end db_update_container
345

346     /**
347      * deletes a container entry in the database
348      *
349      * @param ctnID the JahiaContainer object ID
350      *
351      * @throws throws a critical JahiaException if SQL error
352      * @throws throws a warning JahiaException if cannot free resources
353      */

354     public void db_delete_container (int ctnID, JahiaSaveVersion saveVersion) throws
355             JahiaException {
356         // we try to load the active container to see if we must have a staged
357
// entry for deletion or just remove all the staged entries.
358
JahiaContainer activeContainer = db_load_container (ctnID,
359                 EntryLoadRequest.CURRENT);
360
361         Connection JavaDoc dbConn = null;
362         PreparedStatement JavaDoc stmt = null;
363         String JavaDoc sqlQuery = "";
364         try {
365             dbConn = ConnectionDispenser.getConnection ();
366
367             // the site is in staging mode, we update the staging version
368
if (saveVersion.isStaging ()) {
369                 if (activeContainer != null
370                         && activeContainer.getWorkflowState () == EntryLoadRequest.ACTIVE_WORKFLOW_STATE) {
371                     // we have an active container, let's updated or create
372
// the staged entry.
373

374                     // composes the query
375
sqlQuery = "UPDATE jahia_ctn_entries SET version_id=-1 WHERE id_jahia_ctn_entries=? AND workflow_state>1";
376                     stmt = dbConn.prepareStatement(sqlQuery);
377                     stmt.setInt(1, ctnID);
378                     
379                     int rows = stmt.executeUpdate ();
380                     // the staged version of the container doesn't already exist
381
if (rows == 0) {
382                         createStagedContainer (dbConn, ctnID, saveVersion);
383                         stmt.executeUpdate ();
384                     }
385                 } else {
386                     // we don't have an active entry so we just delete the
387
// staged entry.
388
sqlQuery = "DELETE FROM jahia_ctn_entries WHERE id_jahia_ctn_entries=? AND workflow_state>1";
389                     stmt = dbConn.prepareStatement(sqlQuery);
390                     stmt.setInt(1, ctnID);
391                     stmt.executeUpdate ();
392                 }
393             // Versioning mode ?
394
} else if (saveVersion.isVersioned ()) {
395                 backupContainerVersion (dbConn, ctnID);
396                 sqlQuery = "UPDATE jahia_ctn_entries SET version_id=?,workflow_state=-1 WHERE id_jahia_ctn_entries=? AND workflow_state=1";
397                 stmt = dbConn.prepareStatement(sqlQuery);
398                 stmt.setInt(1, saveVersion.getVersionID ());
399                 stmt.setInt(2, ctnID);
400                 stmt.executeUpdate ();
401             // Normal mode (no staging, no versioning)
402
} else {
403                 sqlQuery = "DELETE FROM jahia_ctn_entries WHERE id_jahia_ctn_entries=? AND workflow_state=1";
404                 stmt = dbConn.prepareStatement(sqlQuery);
405                 stmt.setInt(1, ctnID);
406                 stmt.executeUpdate ();
407             }
408         }
409                 // catches error if cannot execute update query
410
catch (SQLException JavaDoc se) {
411             logger.debug ("Bailing out because of SQL exception", se);
412             String JavaDoc errorMsg = "Error in db_delete_container : " + se.getMessage () + "\n sqlQuery=" + sqlQuery;
413             throw new JahiaException ("Cannot delete containers in the database",
414                     errorMsg, JahiaException.DATABASE_ERROR,
415                     JahiaException.CRITICAL_SEVERITY, se);
416         } finally {
417             try {
418                 if (stmt != null) {
419                     stmt.close ();
420                 }
421             } catch (SQLException JavaDoc ex) {
422                 new JahiaException ("Cannot free resources",
423                         "db_delete_container : cannot free resources",
424                         JahiaException.DATABASE_ERROR,
425                         JahiaException.WARNING_SEVERITY, ex);
426             }
427         }
428     } // end db_delete_container
429

430     /**
431      * Purges all the container entries for a given container ID. This removes
432      * all the entries including the versioning entries from the database.
433      *
434      * @param ctnID the identifier for the container to completely remove
435      * from the database
436      *
437      * @throws JahiaException thrown if there was an error communicating with
438      * the database.
439      */

440     public void purgeContainer (int ctnID) throws JahiaException {
441
442         Connection JavaDoc dbConn = null;
443         PreparedStatement JavaDoc stmt = null;
444         try {
445             dbConn = ConnectionDispenser.getConnection ();
446
447             String JavaDoc sqlQuery ="DELETE FROM jahia_ctn_entries WHERE id_jahia_ctn_entries=?";
448             stmt = dbConn.prepareStatement (sqlQuery);
449             stmt.setInt(1, ctnID);
450             stmt.executeUpdate ();
451         }
452                 // catches error if cannot execute update query
453
catch (SQLException JavaDoc se) {
454             logger.debug ("Bailing out because of SQL exception", se);
455             String JavaDoc errorMsg = "Error in purgeContainer : " + se.getMessage ();
456             throw new JahiaException ("Cannot delete containers in the database",
457                     errorMsg, JahiaException.DATABASE_ERROR,
458                     JahiaException.CRITICAL_SEVERITY, se);
459         } finally {
460             try {
461                 if (stmt != null) {
462                     stmt.close ();
463                 }
464             } catch (SQLException JavaDoc ex) {
465                 new JahiaException ("Cannot free resources",
466                         "purgeContainer : cannot free resources",
467                         JahiaException.DATABASE_ERROR,
468                         JahiaException.WARNING_SEVERITY, ex);
469             }
470         }
471     }
472
473     /**
474      * To be called when the site is in versioning mode, to backup the
475      * old container before changing it.
476      */

477     private void backupContainerVersion(Connection JavaDoc dbConn, int ctnID) throws SQLException JavaDoc {
478         PreparedStatement JavaDoc queryStmt = null;
479         PreparedStatement JavaDoc insertStmt = null;
480         try {
481             String JavaDoc sqlQuery = "SELECT * FROM jahia_ctn_entries WHERE id_jahia_ctn_entries=? AND workflow_state=1";
482
483             queryStmt = dbConn.prepareStatement(sqlQuery);
484             queryStmt.setInt(1, ctnID);
485             ResultSet JavaDoc rs = queryStmt.executeQuery();
486
487             if (rs != null && rs.next()) {
488                 sqlQuery = "INSERT INTO jahia_ctn_entries (id_jahia_ctn_entries,jahiaid_jahia_ctn_entries,pageid_jahia_ctn_entries,listid_jahia_ctn_entries,ctndefid_jahia_ctn_entries,rank_jahia_ctn_entries,rights_jahia_ctn_entries,version_id,workflow_state) VALUES (?,?,?,?,?,?,?,?,?)";
489                 insertStmt = dbConn.prepareStatement(sqlQuery);
490                 insertStmt.setInt(1, ctnID);
491                 insertStmt.setInt(2, rs.getInt("jahiaid_jahia_ctn_entries"));
492                 insertStmt.setInt(3, rs.getInt("pageid_jahia_ctn_entries"));
493                 insertStmt.setInt(4, rs.getInt("listid_jahia_ctn_entries"));
494                 insertStmt.setInt(5, rs.getInt("ctndefid_jahia_ctn_entries"));
495                 insertStmt.setInt(6, rs.getInt("rank_jahia_ctn_entries"));
496                 insertStmt.setInt(7, rs.getInt("rights_jahia_ctn_entries"));
497                 insertStmt.setInt(8, rs.getInt("version_id"));
498                 insertStmt.setInt(9, 0);
499                 insertStmt.executeUpdate();
500             }
501         } finally {
502             if (queryStmt != null) {
503                 queryStmt.close();
504             }
505             if (insertStmt != null) {
506                 insertStmt.close();
507             }
508         }
509     }
510
511     /**
512      * creates a staged version of a container entry , to be called when the
513      * site is in staging mode, and that a container entry only exist in Active
514      * mode and we want to change it...
515      */

516     private void createStagedContainer(Connection JavaDoc dbConn, int ctnID, JahiaSaveVersion saveVersion) throws SQLException JavaDoc {
517         PreparedStatement JavaDoc queryStmt = null;
518         PreparedStatement JavaDoc insertStmt = null;
519         try {
520             String JavaDoc sqlQuery = "SELECT * FROM jahia_ctn_entries WHERE id_jahia_ctn_entries=? AND workflow_state=1";
521
522             queryStmt = dbConn.prepareStatement(sqlQuery);
523             queryStmt.setInt(1, ctnID);
524             ResultSet JavaDoc rs = queryStmt.executeQuery();
525
526             if (rs != null && rs.next()) {
527                 sqlQuery = "INSERT INTO jahia_ctn_entries (id_jahia_ctn_entries,jahiaid_jahia_ctn_entries,pageid_jahia_ctn_entries,listid_jahia_ctn_entries,ctndefid_jahia_ctn_entries,rank_jahia_ctn_entries,rights_jahia_ctn_entries,version_id,workflow_state) VALUES (?,?,?,?,?,?,?,?,?)";
528                 insertStmt = dbConn.prepareStatement(sqlQuery);
529                 insertStmt.setInt(1, ctnID);
530                 insertStmt.setInt(2, rs.getInt("jahiaid_jahia_ctn_entries"));
531                 insertStmt.setInt(3, rs.getInt("pageid_jahia_ctn_entries"));
532                 insertStmt.setInt(4, rs.getInt("listid_jahia_ctn_entries"));
533                 insertStmt.setInt(5, rs.getInt("ctndefid_jahia_ctn_entries"));
534                 insertStmt.setInt(6, rs.getInt("rank_jahia_ctn_entries"));
535                 insertStmt.setInt(7, rs.getInt("rights_jahia_ctn_entries"));
536                 insertStmt.setInt(8, saveVersion.getVersionID());
537                 insertStmt.setInt(9, saveVersion.getWorkflowState());
538                 insertStmt.executeUpdate();
539             }
540         } finally {
541             if (queryStmt != null) {
542                 queryStmt.close();
543             }
544             if (insertStmt != null) {
545                 insertStmt.close();
546             }
547         }
548     }
549
550     /**
551      * Validates the staged container that way: if (versioning):backup
552      * container, update active version with staged version, delete staged
553      * version
554      *
555      * @param saveVersion
556      * with the right versionID, and right isStaging() and
557      * isVersionID()
558      */

559     public void db_validate_staged_container (int ctnID,
560                                               JahiaSaveVersion saveVersion) throws
561             JahiaException {
562         Connection JavaDoc dbConn = null;
563         Statement JavaDoc stmt = null;
564         try {
565             dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
566             stmt = dbConn.createStatement ();
567             String JavaDoc sqlQuery = "SELECT * FROM jahia_ctn_entries WHERE " +
568                     "id_jahia_ctn_entries = " + ctnID +
569                     " AND workflow_state>1";
570             ResultSet JavaDoc rs = stmt.executeQuery (sqlQuery);
571             if (rs != null) {
572                 if (rs.next ()) {
573                     int aclID = rs.getInt ("rights_jahia_ctn_entries");
574                     int ctn_jahiaID = rs.getInt ("jahiaid_jahia_ctn_entries");
575                     int ctn_pageID = rs.getInt ("pageid_jahia_ctn_entries");
576                     int ctn_listID = rs.getInt ("listid_jahia_ctn_entries");
577                     int ctn_ctndefID = rs.getInt ("ctndefid_jahia_ctn_entries");
578                     int ctn_rank = rs.getInt ("rank_jahia_ctn_entries");
579                     int versionID = rs.getInt ("version_id");
580
581                     // the site is in versioning, backup version
582
if (saveVersion.isVersioned ()) {
583                         backupContainerVersion (dbConn, ctnID);
584
585                         // we must delete this container ?
586
}
587                     if (versionID == -1) {
588                         if (saveVersion.isVersioned ()) {
589                             // update staged version into inactive version
590
sqlQuery = "UPDATE jahia_ctn_entries SET "
591                                     + "version_id = " + saveVersion.getVersionID () +
592                                     ","
593                                     + "workflow_state = -1 "
594                                     + "WHERE id_jahia_ctn_entries = " + ctnID + " "
595                                     + "AND workflow_state>1";
596                             stmt.executeUpdate (sqlQuery);
597                         }
598                         // delete active & staged version
599
sqlQuery =
600                                 "DELETE FROM jahia_ctn_entries WHERE id_jahia_ctn_entries=" +
601                                 ctnID +
602                                 " AND workflow_state>=1";
603                         stmt.executeUpdate (sqlQuery);
604                     } else
605                     // no, we must validate it
606
{
607                         // update active version
608
sqlQuery = "UPDATE jahia_ctn_entries SET "
609                                 + "jahiaid_jahia_ctn_entries = " + ctn_jahiaID +
610                                 ","
611                                 + "pageid_jahia_ctn_entries = " + ctn_pageID + ","
612                                 + "listid_jahia_ctn_entries = " + ctn_listID + ","
613                                 + "ctndefid_jahia_ctn_entries = " + ctn_ctndefID +
614                                 ","
615                                 + "rank_jahia_ctn_entries = " + ctn_rank + ","
616                                 + "rights_jahia_ctn_entries = " + aclID + ","
617                                 + "version_id = " + saveVersion.getVersionID () +
618                                 " "
619                                 + "WHERE id_jahia_ctn_entries = " + ctnID + " "
620                                 + "AND workflow_state=1";
621                         int rows = stmt.executeUpdate (sqlQuery);
622                         // this is a new container, we insert it
623
if (rows == 0) {
624                             sqlQuery = "INSERT INTO jahia_ctn_entries (" +
625                                     "id_jahia_ctn_entries," +
626                                     "jahiaid_jahia_ctn_entries," +
627                                     "pageid_jahia_ctn_entries," +
628                                     "listid_jahia_ctn_entries," +
629                                     "ctndefid_jahia_ctn_entries," +
630                                     "rank_jahia_ctn_entries," +
631                                     "rights_jahia_ctn_entries," +
632                                     "version_id," +
633                                     "workflow_state) VALUES (" +
634                                     ctnID + "," +
635                                     ctn_jahiaID + "," +
636                                     ctn_pageID + "," +
637                                     ctn_listID + "," +
638                                     ctn_ctndefID + "," +
639                                     ctn_rank + "," +
640                                     aclID + "," +
641                                     saveVersion.getVersionID () + "," +
642                                     "1)";
643                             stmt.execute (sqlQuery);
644                         }
645
646                         // delete staged version
647
sqlQuery =
648                                 "DELETE FROM jahia_ctn_entries WHERE id_jahia_ctn_entries=" +
649                                 ctnID +
650                                 " AND workflow_state>1";
651                         stmt.executeUpdate (sqlQuery);
652                     }
653                 }
654             }
655         } catch (SQLException JavaDoc se) {
656             logger.debug ("Bailing out because of SQL exception", se);
657             String JavaDoc errorMsg = "Error in db_validate_staged_container : " +
658                     se.getMessage ();
659             throw new JahiaException ("Cannot update containers in the database",
660                     errorMsg, JahiaException.DATABASE_ERROR,
661                     JahiaException.CRITICAL_SEVERITY, se);
662         } finally {
663             try {
664                 if (stmt != null) {
665                     stmt.close ();
666                 }
667             } catch (SQLException JavaDoc ex) {
668                 new JahiaException ("Cannot free resources",
669                         "db_validate_staged_container : cannot free resources",
670                         JahiaException.DATABASE_ERROR,
671                         JahiaException.WARNING_SEVERITY, ex);
672             }
673         }
674     }
675
676     //--------------------------------------------------------------------------
677
/**
678      * return a DOM document of all containers of a site
679      *
680      * @param siteID the site id
681      *
682      * @return JahiaDOMObject a DOM representation of this object
683      *
684      * @author NK
685      */

686     public JahiaDOMObject getContainersAsDOM (int siteID) throws JahiaException {
687
688         Connection JavaDoc dbConn = null;
689         Statement JavaDoc statement = null;
690
691         JahiaDBDOMObject dom = null;
692
693         try {
694             String JavaDoc sqlQuery =
695                     "SELECT * FROM jahia_ctn_entries where jahiaid_jahia_ctn_entries=" +
696                     siteID;
697
698             dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
699             statement = dbConn.createStatement ();
700             if (statement != null) {
701                 ResultSet JavaDoc rs = statement.executeQuery (sqlQuery);
702                 if (rs != null) {
703                     dom = new JahiaDBDOMObject ();
704                     dom.addTable ("jahia_ctn_entries", rs);
705                     return dom;
706                 }
707             }
708         } catch (SQLException JavaDoc se) {
709             logger.debug ("Bailing out because of SQL exception", se);
710             String JavaDoc errorMsg = "Error in getContainersAsDOM(int siteID) : " +
711                     se.getMessage ();
712             throw new JahiaException ("Cannot load containers from the database",
713                     errorMsg, JahiaException.DATABASE_ERROR,
714                     JahiaException.CRITICAL_SEVERITY, se);
715         } finally {
716
717             closeStatement (statement);
718         }
719
720         return dom;
721     }
722
723     //-------------------------------------------------------------------------
724
private void closeStatement (Statement JavaDoc statement) {
725         // Close the opened statement
726
try {
727             if (statement != null) {
728                 statement.close ();
729             }
730         } catch (SQLException JavaDoc sqlEx) {
731             // just create an exception without raising it, just to notify it
732
// in the logs.
733
new JahiaException ("Cannot close a statement",
734                     "Cannot close a statement", JahiaException.DATABASE_ERROR,
735                     JahiaException.WARNING_SEVERITY, sqlEx);
736         }
737     }
738
739 } // end JahiaContainersDB
Popular Tags