KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.jahia.services.pages;
14
15 import org.jahia.data.JahiaDBDOMObject;
16 import org.jahia.data.JahiaDOMObject;
17 import org.jahia.data.fields.FieldTypes;
18 import org.jahia.exceptions.JahiaException;
19 import org.jahia.exceptions.JahiaPageNotFoundException;
20 import org.jahia.exceptions.database.JahiaDatabaseException;
21 import org.jahia.services.version.EntryLoadRequest;
22
23 import java.sql.*;
24 import java.util.Set JavaDoc;
25 import java.util.TreeSet JavaDoc;
26 import java.util.Vector JavaDoc;
27 import java.util.ArrayList JavaDoc;
28
29 /**
30  * <p>Title: Jahia page database utilities</p>
31  * <p>Description:
32  * This class offer primitive acces to the pages and page fields DB.
33  * </p>
34  * <p>Copyright: MAP (Jahia Solutions S�rl 2002)</p>
35  * <p>Company: Jahia Solutions S�rl</p>
36  *
37  * @author EV
38  * @version 1.0
39  */

40 class JahiaPageUtilsDB {
41
42     private static org.apache.log4j.Logger logger =
43             org.apache.log4j.Logger.getLogger(JahiaPageUtilsDB.class);
44
45     private static JahiaPageUtilsDB mObject = null;
46
47
48     /**
49      *
50      */

51     private JahiaPageUtilsDB () {
52     }
53
54
55     public static JahiaPageUtilsDB getInstance () {
56         if (mObject == null) {
57             mObject = new JahiaPageUtilsDB ();
58         }
59         return mObject;
60     }
61
62
63     /**
64      * gets all the page ids in a site
65      *
66      * @param jahiaID the jahia site id
67      *
68      * @return a Vector of pages ids
69      */

70     public Vector JavaDoc getPageIDsInSite (int jahiaID) throws JahiaException {
71         Connection dbConn = null;
72         Statement stmt = null;
73         ResultSet rs = null;
74         Vector JavaDoc theList = new Vector JavaDoc ();
75         try {
76             String JavaDoc sqlQuery = "SELECT DISTINCT id_jahia_pages_data FROM jahia_pages_data ";
77             sqlQuery += "WHERE jahiaid_jahia_pages_data=" + jahiaID;
78
79             dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
80             stmt = dbConn.createStatement ();
81             rs = stmt.executeQuery (sqlQuery);
82
83             while (rs.next ()) {
84                 theList.add (new Integer JavaDoc (rs.getInt ("id_jahia_pages_data")));
85             }
86
87         } catch (SQLException se) {
88             String JavaDoc errorMsg = "Error in db_get_page_ids_in_site : " + se.getMessage ();
89             logger.debug (errorMsg + " -> BAILING OUT");
90             throw new JahiaException ("Cannot load page data from the database",
91                     errorMsg, JahiaException.DATABASE_ERROR,
92                     JahiaException.CRITICAL_SEVERITY);
93         } finally {
94             closeStatement(stmt);
95         }
96         return theList;
97     }
98
99     public ArrayList JavaDoc getFirstNPageIDs (int nbToLoad) throws JahiaException {
100         Connection dbConn = null;
101         PreparedStatement stmt = null;
102         ResultSet rs = null;
103         ArrayList JavaDoc result = new ArrayList JavaDoc();
104         try {
105             dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
106             stmt = dbConn.prepareStatement("SELECT DISTINCT id_jahia_pages_data FROM jahia_pages_data ORDER BY id_jahia_pages_data");
107             rs = stmt.executeQuery ();
108
109             int count = 0;
110             while ((rs.next ()) && (count < nbToLoad)) {
111                 result.add (new Integer JavaDoc (rs.getInt ("id_jahia_pages_data")));
112                 count++;
113             }
114             rs.close();
115
116         } catch (SQLException se) {
117             String JavaDoc errorMsg = "Error in getFirstNPageIDs : " + se.getMessage ();
118             logger.debug (errorMsg + " -> BAILING OUT");
119             throw new JahiaException ("Cannot load page data from the database",
120                     errorMsg, JahiaException.DATABASE_ERROR,
121                     JahiaException.CRITICAL_SEVERITY, se);
122         } finally {
123             closeStatement(stmt);
124         }
125         return result;
126     }
127
128
129     /**
130      * Get the jahia page id in a site corresponding to a specified link type.
131      *
132      * @param jahiaID the jahia site id
133      * @param linkType One of these TYPE_DIRECT, TYPE_LINK, TYPE_URL links.
134      *
135      * @return a Vector of pages ids
136      */

137     public Vector JavaDoc getPageIDsInSite (int jahiaID, int linkType)
138             throws JahiaException {
139         Vector JavaDoc theList = new Vector JavaDoc ();
140         PreparedStatement pStmt = null;
141         Connection dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
142         if (dbConn == null) {
143             return theList;
144         }
145         try {
146             StringBuffer JavaDoc sqlQuery =
147                     new StringBuffer JavaDoc ("SELECT DISTINCT id_jahia_pages_data");
148             sqlQuery.append (" FROM jahia_pages_data");
149             sqlQuery.append (" WHERE jahiaid_jahia_pages_data = ? AND workflow_state >= ?");
150             sqlQuery.append (" AND pagetype_jahia_pages_data = ?");
151             pStmt = dbConn.prepareStatement (sqlQuery.toString ());
152             pStmt.setInt (1, jahiaID);
153             pStmt.setInt (2, org.jahia.services.version.EntryLoadRequest.ACTIVE_WORKFLOW_STATE);
154             pStmt.setInt (3, linkType);
155             ResultSet rs = pStmt.executeQuery ();
156             while (rs.next ()) {
157                 theList.add (new Integer JavaDoc (rs.getInt ("id_jahia_pages_data")));
158             }
159
160         } catch (SQLException se) {
161             String JavaDoc errorMsg = "Error in db_get_page_ids_in_site : " + se.getMessage ();
162             logger.debug (errorMsg + " -> BAILING OUT");
163             throw new JahiaException ("Cannot load page data from the database",
164                     errorMsg, JahiaException.DATABASE_ERROR,
165                     JahiaException.CRITICAL_SEVERITY);
166         } finally {
167
168             closeStatement (pStmt);
169         }
170         return theList;
171     }
172
173     /**
174      * Returns a list of all the pageIDs that are using a specific template ID.
175      *
176      * @param templateID the identifier of the template
177      *
178      * @throws JahiaException if an SQL exception is raised or if there is a
179      * problem freeing the ressources...
180      * @return a Vector containing Integer object that correspond to pageIDs.
181      */

182     public Vector JavaDoc getPageIDsWithTemplate (int templateID)
183             throws JahiaException {
184         Connection dbConn = null;
185         Statement stmt = null;
186         ResultSet rs = null;
187         Vector JavaDoc theList = new Vector JavaDoc ();
188         try {
189             String JavaDoc sqlQuery = "SELECT DISTINCT id_jahia_pages_data FROM jahia_pages_data ";
190             sqlQuery += "WHERE pagedefid_jahia_pages_data=" + templateID
191                     + " AND (pagetype_jahia_pages_data=0 OR pagetype_jahia_pages_data=1)";
192
193             dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
194             stmt = dbConn.createStatement ();
195             rs = stmt.executeQuery (sqlQuery);
196
197             while (rs.next ()) {
198                 theList.add (new Integer JavaDoc (rs.getInt ("id_jahia_pages_data")));
199             }
200
201         } catch (SQLException se) {
202             String JavaDoc errorMsg = "Error : " + se.getMessage ();
203             logger.debug (errorMsg + " -> BAILING OUT");
204             throw new JahiaException ("Cannot load page data from the database",
205                     errorMsg, JahiaException.DATABASE_ERROR,
206                     JahiaException.CRITICAL_SEVERITY, se);
207         } finally {
208             closeStatement (stmt);
209         }
210         return theList;
211     }
212
213
214     /**
215      * gets all the fields linking on a specific page
216      *
217      * @param pageID the page id
218      *
219      * @return a Vector of page ids
220      */

221     public Vector JavaDoc getPageIDsPointingOnPage (int pageID, EntryLoadRequest entryLoadRequest) throws JahiaException {
222         Connection dbConn = null;
223         PreparedStatement stmt = null;
224         ResultSet rs = null;
225         Vector JavaDoc pageList = new Vector JavaDoc ();
226         try {
227
228             // creates the connection
229
String JavaDoc sqlQuery = "";
230             dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
231
232             if (entryLoadRequest.isVersioned()) {
233                 sqlQuery = "SELECT DISTINCT id_jahia_pages_data FROM jahia_pages_data WHERE (pagetype_jahia_pages_data=1) AND (pagelinkid_jahia_pages_data=?)";
234             } else {
235                 sqlQuery = "SELECT DISTINCT id_jahia_pages_data FROM jahia_pages_data WHERE (pagetype_jahia_pages_data=1) AND (pagelinkid_jahia_pages_data=?) AND workflow_state>0";
236             }
237
238             stmt = dbConn.prepareStatement (sqlQuery);
239
240             // gets the pages ids
241
stmt.setInt(1, pageID);
242             rs = stmt.executeQuery ();
243             while (rs.next ()) {
244                 pageList.add (new Integer JavaDoc (rs.getInt (1)));
245             }
246
247         } catch (SQLException se) {
248             String JavaDoc errorMsg = "Error in db_get_page_ids_pointing_on_page : " + se.getMessage ();
249             logger.debug (errorMsg + " -> BAILING OUT");
250             throw new JahiaException ("Cannot load page data from the database",
251                     errorMsg, JahiaException.DATABASE_ERROR,
252                     JahiaException.CRITICAL_SEVERITY);
253         } finally {
254             closeStatement (stmt);
255         }
256         return pageList;
257     }
258
259     public int getPageFieldID (int pageID, EntryLoadRequest loadRequest)
260             throws JahiaException {
261
262         int pageFieldID = -1;
263
264         if (loadRequest == null) {
265             pageFieldID = getAnyPageFieldID (pageID);
266         } else if (loadRequest.isCurrent ()) {
267             pageFieldID = getActivePageFieldID (pageID);
268         } else if (loadRequest.isStaging ()) {
269             // here we must check the version ID and make sure it's not -1
270
// because we only retrieve the parent field for the new position,
271
// not all the old positions
272
pageFieldID = getStagedPageFieldID(pageID);
273
274             if ( pageFieldID == -1 ){
275                 // return the active if the staging not found
276
pageFieldID = getActivePageFieldID (pageID);
277             }
278         } else if (loadRequest.isVersioned()) {
279             try {
280                 ContentPage contentPage = ContentPage.getPage(pageID, false);
281                 int parentPageID = contentPage.getParentID(loadRequest);
282                 pageFieldID = getVersionedPageFieldID(pageID, parentPageID,
283                     loadRequest.getVersionID(), true);
284             } catch ( JahiaPageNotFoundException pnfe ){
285                 logger.debug("ContentPage not found pid[" + pageID + "] returning -1 ");
286             }
287         }
288
289         return pageFieldID;
290
291     }
292
293     private int getAnyPageFieldID (int pageID)
294             throws JahiaException {
295
296         // get the DB connection
297
Connection dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
298         int result = -1;
299         if (dbConn == null) {
300             return result;
301         }
302         PreparedStatement pStmt = null;
303         try {
304             // gets the pages ids
305
StringBuffer JavaDoc sqlQuery = new StringBuffer JavaDoc ("SELECT id_jahia_fields_data");
306             sqlQuery.append (" FROM jahia_fields_data");
307             sqlQuery.append (" WHERE value_jahia_fields_data = ?");
308             sqlQuery.append (" AND type_jahia_fields_data = ?");
309             pStmt = dbConn.prepareStatement (sqlQuery.toString ());
310             pStmt.setString (1, Integer.toString (pageID));
311             pStmt.setInt (2, FieldTypes.PAGE);
312             ResultSet rs = pStmt.executeQuery ();
313             if (rs != null) {
314                 int fieldID = -1;
315                 while (rs.next ()) {
316                     fieldID = rs.getInt ("id_jahia_fields_data");
317                     result = fieldID;
318                     continue;
319                 }
320             }
321         } catch (SQLException se) {
322             String JavaDoc errorMsg = "Error in getStagingPageFieldIDs : " + se.getMessage ();
323             logger.debug (errorMsg + " -> BAILING OUT");
324             throw new JahiaException ("Cannot load page data from the database",
325                     errorMsg, JahiaException.DATABASE_ERROR,
326                     JahiaException.CRITICAL_SEVERITY, se);
327         } finally {
328
329             closeStatement (pStmt);
330         }
331         return result;
332     }
333
334     public int getActivePageFieldID (int pageID)
335             throws JahiaException {
336
337         // get the DB connection
338
Connection dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
339         int result = -1;
340         if (dbConn == null) {
341             return result;
342         }
343         PreparedStatement pStmt = null;
344         try {
345             // gets the pages ids
346
StringBuffer JavaDoc sqlQuery = new StringBuffer JavaDoc (
347                     "SELECT id_jahia_fields_data, workflow_state");
348             sqlQuery.append (" FROM jahia_fields_data");
349             sqlQuery.append (" WHERE value_jahia_fields_data = ?");
350             sqlQuery.append (" AND type_jahia_fields_data = ?");
351             sqlQuery.append (" AND workflow_state = ?");
352             pStmt = dbConn.prepareStatement (sqlQuery.toString ());
353             pStmt.setString (1, Integer.toString (pageID));
354             pStmt.setInt (2, FieldTypes.PAGE);
355             pStmt.setInt (3, org.jahia.services.version.EntryLoadRequest.ACTIVE_WORKFLOW_STATE);
356             ResultSet rs = pStmt.executeQuery ();
357             if (rs != null) {
358                 int fieldID = -1;
359                 while (rs.next ()) {
360                     fieldID = rs.getInt ("id_jahia_fields_data");
361                     result = fieldID;
362                 }
363             }
364         } catch (SQLException se) {
365             String JavaDoc errorMsg = "Error in getStagingPageFieldIDs : " + se.getMessage ();
366             logger.debug (errorMsg + " -> BAILING OUT");
367             throw new JahiaException ("Cannot load page data from the database",
368                     errorMsg, JahiaException.DATABASE_ERROR,
369                     JahiaException.CRITICAL_SEVERITY, se);
370         } finally {
371
372             closeStatement (pStmt);
373         }
374         return result;
375     }
376
377     public int getStagedPageFieldID (int pageID)
378             throws JahiaException {
379
380         // get the DB connection
381
Connection dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
382         int result = -1;
383         if (dbConn == null) {
384             return result;
385         }
386         PreparedStatement pStmt = null;
387         try {
388             // gets the pages ids
389
StringBuffer JavaDoc sqlQuery = new StringBuffer JavaDoc (
390                     "SELECT id_jahia_fields_data, workflow_state");
391             sqlQuery.append (" FROM jahia_fields_data");
392             sqlQuery.append (" WHERE value_jahia_fields_data = ?");
393             sqlQuery.append (" AND type_jahia_fields_data = ?");
394             sqlQuery.append (" AND workflow_state > ?");
395             sqlQuery.append (" AND version_id=0");
396             pStmt = dbConn.prepareStatement (sqlQuery.toString ());
397             pStmt.setString (1, Integer.toString (pageID));
398             pStmt.setInt (2, FieldTypes.PAGE);
399             pStmt.setInt (3, org.jahia.services.version.EntryLoadRequest.ACTIVE_WORKFLOW_STATE);
400             ResultSet rs = pStmt.executeQuery ();
401             if (rs != null) {
402                 int fieldID = -1;
403                 while (rs.next ()) {
404                     fieldID = rs.getInt ("id_jahia_fields_data");
405                     result = fieldID;
406                 }
407             }
408         } catch (SQLException se) {
409             String JavaDoc errorMsg = "Error in getStagingPageFieldIDs : " + se.getMessage ();
410             logger.debug (errorMsg + " -> BAILING OUT");
411             throw new JahiaException ("Cannot load page data from the database",
412                     errorMsg, JahiaException.DATABASE_ERROR,
413                     JahiaException.CRITICAL_SEVERITY, se);
414         } finally {
415
416             closeStatement (pStmt);
417         }
418         return result;
419     }
420
421     /**
422      * In case of a page move, a same page is pointed by both an active page field and a staged page field
423      * This method return a set of theses pages fields.
424      * The ids are sorted by workflows state ( most staged first )
425      *
426      * @param pageID int
427      * @return Vector
428      */

429     public Vector JavaDoc getStagingAndActivePageFieldIDs(int pageID) throws JahiaException {
430         Vector JavaDoc v = new Vector JavaDoc();
431         int id = getStagedPageFieldID(pageID);
432         if ( id != -1 ){
433             v.add(new Integer JavaDoc(id));
434         }
435
436         id = getActivePageFieldID(pageID);
437         if ( id != -1 && !v.contains(new Integer JavaDoc(id)) ){
438             v.add(new Integer JavaDoc(id));
439         }
440         return v;
441     }
442
443     private int getVersionedPageFieldID (int pageID, int parentPageID, int versionID,
444                                          boolean withActive)
445             throws JahiaException {
446
447         // get the DB connection
448
Connection dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
449         int result = -1;
450         if (dbConn == null) {
451             return result;
452         }
453
454         ContentPage contentPage = ContentPage.getPage (pageID, false);
455
456         PreparedStatement pStmt = null;
457         try {
458             // gets the pages ids
459
StringBuffer JavaDoc sqlQuery = new StringBuffer JavaDoc (
460                     "SELECT id_jahia_fields_data, version_id, workflow_state");
461             sqlQuery.append (" FROM jahia_fields_data");
462             sqlQuery.append (" WHERE value_jahia_fields_data = ?");
463             sqlQuery.append (" AND pageid_jahia_fields_data = ?");
464             sqlQuery.append (" AND type_jahia_fields_data = ?");
465             sqlQuery.append (" AND workflow_state <= ");
466             if (withActive) {
467                 sqlQuery.append (EntryLoadRequest.ACTIVE_WORKFLOW_STATE);
468             } else {
469                 sqlQuery.append (EntryLoadRequest.VERSIONED_WORKFLOW_STATE);
470             }
471             sqlQuery.append (" AND version_id<=?");
472             sqlQuery.append (" ORDER BY version_id DESC, workflow_state DESC");
473             pStmt = dbConn.prepareStatement (sqlQuery.toString ());
474             pStmt.setString (1, Integer.toString (pageID));
475             pStmt.setString (2, Integer.toString (parentPageID));
476             pStmt.setInt (3, FieldTypes.PAGE);
477             pStmt.setInt (4, versionID);
478             ResultSet rs = pStmt.executeQuery ();
479
480             int previousFieldID = 0;
481             int previousVersionID = 0;
482             int previousWS = 0;
483             if (rs != null) {
484                 while (rs.next ()) {
485                     int fieldID = rs.getInt ("id_jahia_fields_data");
486                     int v = rs.getInt ("version_id");
487                     int ws = rs.getInt ("workflow_state");
488                     if (result == -1) {
489                         result = fieldID;
490                     } else {
491                         if (v < previousVersionID) {
492                             break;
493                         }
494                     }
495                     previousFieldID = fieldID;
496                     previousVersionID = v;
497                     previousWS = ws;
498                 }
499             }
500         } catch (SQLException se) {
501             String JavaDoc errorMsg = "Error in getVersionedPageFieldID : " + se.getMessage ();
502             logger.debug (errorMsg + " -> BAILING OUT");
503             throw new JahiaException ("Cannot load page data from the database",
504                     errorMsg, JahiaException.DATABASE_ERROR,
505                     JahiaException.CRITICAL_SEVERITY, se);
506         } finally {
507
508             closeStatement (pStmt);
509         }
510         return result;
511     }
512
513     /**
514      * Returns all the different field of type page IDs in staging mode in a given page.
515      *
516      * @param pageID the page for which to retrieve the field of type page
517      * @return always returns a Set object, but it might be empty. If non
518      * empty it contains Integer objects that represent the page IDs.
519      * @throws JahiaException
520      */

521     public Set JavaDoc getStagingPageFieldIDsInPage (int pageID) throws JahiaException
522     {
523         // get the DB connection
524
Connection dbConn = org.jahia.services.database.ConnectionDispenser.getConnection();
525         Set JavaDoc result = new TreeSet JavaDoc();
526         if (dbConn == null) {
527             return result;
528         }
529         PreparedStatement pStmt = null;
530         try {
531             // gets the pages ids
532
StringBuffer JavaDoc sqlQuery = new StringBuffer JavaDoc("SELECT DISTINCT id_jahia_fields_data");
533             sqlQuery.append(" FROM jahia_fields_data");
534             sqlQuery.append(" WHERE (pageid_jahia_fields_data = ?");
535             sqlQuery.append(" AND type_jahia_fields_data=5 AND workflow_state >1)");
536             pStmt = dbConn.prepareStatement(sqlQuery.toString());
537             pStmt.setString(1, Integer.toString(pageID));
538             ResultSet rs = pStmt.executeQuery();
539             if (rs != null) {
540                 int fieldID = -1;
541                 while (rs.next()) {
542                     fieldID = rs.getInt ("id_jahia_fields_data");
543                     result.add(new Integer JavaDoc(fieldID));
544                 }
545             }
546         } catch (SQLException se) {
547             String JavaDoc errorMsg = "Error in getStagingPageFieldIDInsInPage : " + se.getMessage();
548             logger.debug(errorMsg + " -> BAILING OUT");
549             throw new JahiaException("Cannot load page data from the database",
550                                      errorMsg, JahiaException.DATABASE_ERROR,
551                                      JahiaException.CRITICAL_SEVERITY, se );
552         } finally {
553
554             closeStatement (pStmt);
555         }
556
557         return result;
558     }
559
560     /**
561      * Gets the page's field parent id in staging or active mode, uses the
562      * maximum staging state to retrieve the value.
563      *
564      * @param pageID The page id.
565      *
566      * @return A field id, or -1 if not found.
567      */

568     public int getPageFieldID (int pageID) throws JahiaException {
569         // get the DB connection
570
Connection dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
571         if (dbConn == null) {
572             return -1;
573         }
574         PreparedStatement pStmt = null;
575         int fieldID = -1;
576         try {
577             // gets the pages ids
578
StringBuffer JavaDoc sqlQuery = new StringBuffer JavaDoc (
579                     "SELECT id_jahia_fields_data, workflow_state");
580             sqlQuery.append (" FROM jahia_fields_data");
581             sqlQuery.append (" WHERE value_jahia_fields_data = ?");
582             sqlQuery.append (" AND type_jahia_fields_data = ?");
583             sqlQuery.append (" AND workflow_state >= ?");
584             sqlQuery.append (" AND version_id <> -1");
585             sqlQuery.append (" ORDER BY workflow_state DESC ");
586             pStmt = dbConn.prepareStatement (sqlQuery.toString ());
587             pStmt.setString (1, Integer.toString (pageID));
588             pStmt.setInt (2, FieldTypes.PAGE);
589             pStmt.setInt (3, org.jahia.services.version.EntryLoadRequest.ACTIVE_WORKFLOW_STATE);
590             ResultSet rs = pStmt.executeQuery ();
591             if (rs != null) {
592                 int maxWorkflowState = -1;
593                 while (rs.next ()) {
594                     int workflowState = rs.getInt ("workflow_state");
595                     if (workflowState > maxWorkflowState) {
596                         maxWorkflowState = workflowState;
597                         fieldID = rs.getInt ("id_jahia_fields_data");
598                     }
599                 }
600             }
601         } catch (SQLException se) {
602             String JavaDoc errorMsg = "Error in db_get_page_field : " + se.getMessage ();
603             logger.debug (errorMsg + " -> BAILING OUT");
604             throw new JahiaException ("Cannot load page data from the database",
605                     errorMsg, JahiaException.DATABASE_ERROR,
606                     JahiaException.CRITICAL_SEVERITY, se);
607         } finally {
608
609             closeStatement (pStmt);
610         }
611         return fieldID;
612     }
613
614
615     /**
616      *
617      */

618     public Vector JavaDoc getPageChildIDs (int pageID) throws JahiaException {
619         return getPageChildIDs (pageID, true);
620     }
621
622
623     /**
624      * @param pageID
625      * @param withDeleted
626      *
627      * @return
628      *
629      * @throws JahiaException
630      */

631     public Vector JavaDoc getPageChildIDs (int pageID, boolean withDeleted) throws JahiaException {
632         Vector JavaDoc result = new Vector JavaDoc ();
633
634         // get the DB connection
635
Connection dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
636         if (dbConn == null) {
637             return result;
638         }
639         PreparedStatement pStmt = null;
640         try {
641             StringBuffer JavaDoc sqlQuery =
642                     new StringBuffer JavaDoc (
643                             "SELECT DISTINCT id_jahia_pages_data, version_id, workflow_state, language_code");
644             sqlQuery.append (" FROM jahia_pages_data WHERE parentid_jahia_pages_data=? AND ");
645             if (!withDeleted) {
646                 sqlQuery.append ("workflow_state>0");
647             } else {
648                 sqlQuery.append ("workflow_state<>0");
649             }
650             sqlQuery.append (
651                     " ORDER BY id_jahia_pages_data, version_id DESC, workflow_state DESC ");
652             pStmt = dbConn.prepareStatement (sqlQuery.toString ());
653             pStmt.setInt (1, pageID);
654             ResultSet rs = pStmt.executeQuery ();
655             if (rs != null) {
656
657                 while (rs.next ()) {
658                     Integer JavaDoc I = new Integer JavaDoc (rs.getInt ("id_jahia_pages_data"));
659                     int versionID = rs.getInt ("version_id");
660                     int ws = rs.getInt ("workflow_state");
661                     String JavaDoc languageCode = rs.getString ("language_code");
662                     if (!result.contains (I)) {
663                         try {
664                             if (ws == EntryLoadRequest.DELETED_WORKFLOW_STATE) {
665                                 ContentPage contentPage = ContentPage.getPage (I.intValue (),
666                                         false);
667                                 if (!contentPage.isDeletedOrDoesNotExist (versionID) ||
668                                         pageID != contentPage.getParentID (
669                                                 EntryLoadRequest.STAGED)) {
670                                     continue;
671                                 }
672                             }
673                             result.add (I);
674                         } catch (Throwable JavaDoc t) {
675                             logger.debug (
676                                     "Exception retrieving page childs for pid[" + pageID + "]",
677                                     t);
678                         }
679                     }
680                 }
681             }
682         } catch (SQLException se) {
683             String JavaDoc errorMsg = "Error in getPageChildIDs : " + se.getMessage () + " -> BAILING OUT";
684             logger.debug (errorMsg);
685             throw new JahiaException ("Cannot load the page values from the database",
686                     errorMsg, JahiaException.DATABASE_ERROR,
687                     JahiaException.CRITICAL_SEVERITY);
688         } finally {
689
690             closeStatement (pStmt);
691         }
692
693         return result;
694     }
695
696     /**
697      * An implementation that return childs of the given page that are staging or active ( staging first )
698      * Childs can be marked for delete.
699      *
700      * @param pageID int
701      * @throws JahiaException
702      * @return Vector
703      */

704     public Vector JavaDoc getStagingPageChildIDs (int pageID) throws JahiaException {
705         Vector JavaDoc result = new Vector JavaDoc ();
706
707         // get the DB connection
708
Connection dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
709         if (dbConn == null) {
710             return result;
711         }
712         PreparedStatement pStmt = null;
713         try {
714             StringBuffer JavaDoc sqlQuery =
715                     new StringBuffer JavaDoc (
716                             "SELECT DISTINCT id_jahia_pages_data, version_id, workflow_state, language_code");
717             sqlQuery.append (" FROM jahia_pages_data WHERE parentid_jahia_pages_data=? AND workflow_state > 0");
718             sqlQuery.append (" ORDER BY id_jahia_pages_data, workflow_state DESC");
719             pStmt = dbConn.prepareStatement (sqlQuery.toString ());
720             pStmt.setInt (1, pageID);
721             ResultSet rs = pStmt.executeQuery ();
722             if (rs != null) {
723                 while (rs.next ()) {
724                     Integer JavaDoc I = new Integer JavaDoc (rs.getInt ("id_jahia_pages_data"));
725                     if (!result.contains (I)) {
726                         result.add (I);
727                     }
728                 }
729             }
730         } catch (SQLException se) {
731             String JavaDoc errorMsg = "Error : " + se.getMessage () + " -> BAILING OUT";
732             logger.debug (errorMsg,se);
733             throw new JahiaException ("Cannot load the page values from the database",
734                     errorMsg, JahiaException.DATABASE_ERROR,
735                     JahiaException.CRITICAL_SEVERITY);
736         } finally {
737
738             closeStatement (pStmt);
739         }
740         return result;
741     }
742
743     /**
744      * An implementation that return childs of the given page that are active
745      *
746      * @param pageID int
747      * @throws JahiaException
748      * @return Vector
749      */

750     public Vector JavaDoc getActivePageChildIDs (int pageID) throws JahiaException {
751         Vector JavaDoc result = new Vector JavaDoc ();
752
753         // get the DB connection
754
Connection dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
755         if (dbConn == null) {
756             return result;
757         }
758         PreparedStatement pStmt = null;
759         try {
760             StringBuffer JavaDoc sqlQuery =
761                     new StringBuffer JavaDoc (
762                             "SELECT DISTINCT id_jahia_pages_data, version_id, workflow_state, language_code");
763             sqlQuery.append (" FROM jahia_pages_data WHERE parentid_jahia_pages_data=? AND workflow_state = 1");
764             sqlQuery.append (" ORDER BY id_jahia_pages_data, version_id DESC ");
765             pStmt = dbConn.prepareStatement (sqlQuery.toString ());
766             pStmt.setInt (1, pageID);
767             ResultSet rs = pStmt.executeQuery ();
768             if (rs != null) {
769                 while (rs.next ()) {
770                     Integer JavaDoc I = new Integer JavaDoc (rs.getInt ("id_jahia_pages_data"));
771                     if (!result.contains (I)) {
772                         result.add (I);
773                     }
774                 }
775             }
776         } catch (SQLException se) {
777             String JavaDoc errorMsg = "Error : " + se.getMessage () + " -> BAILING OUT";
778             logger.debug (errorMsg,se);
779             throw new JahiaException ("Cannot load the page values from the database",
780                     errorMsg, JahiaException.DATABASE_ERROR,
781                     JahiaException.CRITICAL_SEVERITY);
782         } finally {
783
784             closeStatement (pStmt);
785         }
786         return result;
787     }
788
789     /**
790      * An implementation that return childs of the given page at a given version id
791      * Pages can be deleted
792      *
793      * @param pageID int
794      * @param versionID int
795      *
796      * @param pageID int
797      * @throws JahiaException
798      * @return Vector
799      */

800     public Vector JavaDoc getVersioningPageChildIDs (int pageID, int versionID) throws JahiaException {
801         Vector JavaDoc result = new Vector JavaDoc ();
802
803         // get the DB connection
804
Connection dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
805         if (dbConn == null) {
806             return result;
807         }
808         PreparedStatement pStmt = null;
809         try {
810             StringBuffer JavaDoc sqlQuery =
811                     new StringBuffer JavaDoc (
812                             "SELECT DISTINCT id_jahia_pages_data, version_id, workflow_state, language_code");
813             sqlQuery.append (" FROM jahia_pages_data WHERE parentid_jahia_pages_data=? AND workflow_state <= 1 AND version_id <= ?");
814             sqlQuery.append (" ORDER BY id_jahia_pages_data, version_id DESC ");
815             pStmt = dbConn.prepareStatement (sqlQuery.toString ());
816             pStmt.setInt (1, pageID);
817             pStmt.setInt (2, versionID);
818             ResultSet rs = pStmt.executeQuery ();
819             if (rs != null) {
820                 while (rs.next ()) {
821                     Integer JavaDoc I = new Integer JavaDoc (rs.getInt ("id_jahia_pages_data"));
822                     if (!result.contains (I)) {
823                         result.add (I);
824                     }
825                 }
826             }
827         } catch (SQLException se) {
828             String JavaDoc errorMsg = "Error : " + se.getMessage () + " -> BAILING OUT";
829             logger.debug (errorMsg,se);
830             throw new JahiaException ("Cannot load the page values from the database",
831                     errorMsg, JahiaException.DATABASE_ERROR,
832                     JahiaException.CRITICAL_SEVERITY);
833         } finally {
834
835             closeStatement (pStmt);
836         }
837         return result;
838     }
839
840     /**
841      * Return the amount of pages in the database.
842      *
843      * @return The amount of page.
844      */

845     public synchronized int getNbPages ()
846             throws JahiaDatabaseException {
847         return getNbPages (-1);
848     }
849
850
851     public synchronized int getRealActiveNbPages ()
852             throws JahiaDatabaseException {
853         return getRealActiveNbPages (-1);
854     }
855
856
857     /**
858      * Return the amount of pages in the database.
859      *
860      * @return The amount of page.
861      */

862     public synchronized int getNbPages (int siteID)
863             throws JahiaDatabaseException {
864         int counter = 0;
865
866         Connection dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
867         if (dbConn != null) {
868             String JavaDoc query = "";
869             Statement statement = null;
870
871             try {
872                 query = "SELECT COUNT(id_jahia_pages_data) as nbItems FROM jahia_pages_data";
873
874                 if (siteID != -1) {
875                     query += " where jahiaid_jahia_pages_data=" + siteID;
876                 }
877
878                 statement = dbConn.createStatement ();
879                 if (statement != null) {
880                     ResultSet rs = statement.executeQuery (query.toString ());
881
882                     if (rs != null) {
883                         if (rs.next ()) {
884                             counter = rs.getInt ("nbItems");
885                         }
886                     }
887
888                     // Result set not needed anymore.
889
rs = null;
890                 }
891             } catch (SQLException ex) {
892                 throw new JahiaDatabaseException ("Database error.", ex,
893                         JahiaDatabaseException.ERROR_SEVERITY);
894             } finally {
895                 query = null;
896
897                 closeStatement (statement);
898             }
899         }
900         return counter;
901     }
902
903
904     public synchronized int getRealActiveNbPages (int siteID)
905             throws JahiaDatabaseException {
906         int counter = 0;
907
908         Connection dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
909         if (dbConn != null) {
910             String JavaDoc query = "";
911             Statement statement = null;
912
913             try {
914                 query =
915                         "SELECT DISTINCT id_jahia_pages_data FROM jahia_pages_data where pagetype_jahia_pages_data=0 AND workflow_state=1";
916
917                 if (siteID != -1) {
918                     query += " and jahiaid_jahia_pages_data=" + siteID;
919                 }
920
921                 statement = dbConn.createStatement ();
922                 if (statement != null) {
923                     ResultSet rs = statement.executeQuery (query.toString ());
924
925                     if (rs != null) {
926                         while (rs.next ()) {
927                             counter++;
928                         }
929                     }
930                     rs.close ();
931
932                     // Result set not needed anymore.
933
rs = null;
934                 }
935             } catch (SQLException ex) {
936                 throw new JahiaDatabaseException ("Database error.", ex,
937                         JahiaDatabaseException.ERROR_SEVERITY);
938             } finally {
939                 query = null;
940
941                 closeStatement (statement);
942             }
943         }
944         return counter;
945     }
946
947
948     /**
949      * return a DOM document of all pages of a site
950      *
951      * @param siteID the site id
952      *
953      * @return JahiaDOMObject a DOM representation of this object
954      */

955     public JahiaDOMObject getPagesAsDOM (int siteID) throws JahiaException {
956
957         Connection dbConn = null;
958         Statement statement = null;
959         JahiaDBDOMObject dom = null;
960
961         try {
962             String JavaDoc sqlQuery = "SELECT * FROM jahia_pages_data where jahiaid_jahia_pages_data=" + siteID;
963
964             dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
965             statement = dbConn.createStatement ();
966             if (statement != null) {
967                 ResultSet rs = statement.executeQuery (sqlQuery);
968                 if (rs != null) {
969                     dom = new JahiaDBDOMObject ();
970                     dom.addTable ("jahia_pages_data", rs);
971                     return dom;
972                 }
973             }
974         } catch (SQLException se) {
975             String JavaDoc errorMsg = "Error in getPagesAsDOM(int siteID) : " + se.getMessage ();
976             logger.debug (errorMsg + " -> BAILING OUT");
977             throw new JahiaException ("Cannot load pages from the database",
978                     errorMsg, JahiaException.DATABASE_ERROR,
979                     JahiaException.CRITICAL_SEVERITY);
980         } finally {
981
982             closeStatement (statement);
983         }
984
985         return dom;
986     }
987
988
989     /**
990      * gets all acl ids used by pages of a site
991      *
992      * @return a Vector of all acl id of a site
993      *
994      * @throws JahiaException
995      * a warning JahiaException if cannot free resources
996      */

997     public Vector JavaDoc db_get_all_acl_id (int siteID)
998             throws JahiaException {
999         Connection dbConn = null;
1000        Statement stmt = null;
1001        ResultSet rs = null;
1002        Vector JavaDoc theIDs = new Vector JavaDoc ();
1003        try {
1004            String JavaDoc sqlQuery = "SELECT DISTINCT rights_jahia_pages_data FROM jahia_pages_data "
1005                    + "WHERE jahiaid_jahia_pages_data=" + siteID;
1006
1007            dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
1008            stmt = dbConn.createStatement ();
1009            rs = stmt.executeQuery (sqlQuery);
1010
1011            while (rs.next ()) {
1012                theIDs.add (new Integer JavaDoc (rs.getInt ("rights_jahia_pages_data")));
1013            }
1014        } catch (SQLException se) {
1015            String JavaDoc errorMsg = "Error in db_get_all_acl_id : " + se.getMessage ();
1016            logger.debug (errorMsg + " -> BAILING OUT");
1017            throw new JahiaException ("Cannot load acl id from the database",
1018                    errorMsg, JahiaException.DATABASE_ERROR,
1019                    JahiaException.CRITICAL_SEVERITY);
1020        } finally {
1021            closeStatement(stmt);
1022        }
1023        return theIDs;
1024
1025    }
1026
1027
1028    private void closeStatement (Statement statement) {
1029        // Close the opened statement
1030
try {
1031            if (statement != null) {
1032                statement.close ();
1033            }
1034        } catch (SQLException sqlEx) {
1035            logger.warn ("Cannot close a statement", sqlEx);
1036        }
1037    }
1038
1039
1040}
1041
Popular Tags