KickJava   Java API By Example, From Geeks To Geeks.

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


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
// JahiaPagesDB
15
// EV 09.01.2001
16
//
17
//
18

19 package org.jahia.services.pages;
20
21 import org.jahia.exceptions.JahiaException;
22 import org.jahia.registries.ServicesRegistry;
23 import org.jahia.services.cache.Cache;
24 import org.jahia.services.version.EntryLoadRequest;
25 import org.jahia.services.version.EntryStateable;
26 import org.jahia.utils.JahiaTools;
27
28 import java.sql.*;
29 import java.util.Vector JavaDoc;
30
31
32 /**
33  * This class contains static methods accessing the database for page
34  * operations. This class is mainly used by the Jahia Page Service.
35  *
36  * @author Eric Vassalli
37  * @author Fulco Houkes
38  * @version 1.1
39  */

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

51     private JahiaPagesDB () {
52     }
53
54
55     //-------------------------------------------------------------------------
56
public static JahiaPagesDB getInstance () {
57         if (mObject == null) {
58             mObject = new JahiaPagesDB ();
59         }
60         return mObject;
61
62     }
63
64     //-------------------------------------------------------------------------
65
/**
66      * Loads the page info from the database for the requested page ID.
67      * A JahiaPage object is created
68      *
69      * @param pageID The page unique identification number.
70      * @param loadEntry a EntryLoadRequest object that specifies which
71      * entry we want to retrieve. If the WorkflowStatus is bigger than 0 then
72      * we return BOTH the active and staging entries. If it's equal to zero
73      * or lower we return all the versioned entries.
74      *
75      * @return a Vector containing JahiaPageInfo object inside. Might be
76      * empty if we couldn't find any page data. The vector contains all the
77      * info corresponding to the requested entry
78      *
79      * @throws JahiaException Throws a {@link org.jahia.exceptions.JahiaException JahiaException}
80      * on any database error.
81      */

82     public Vector JavaDoc loadPageInfos (int pageID, EntryLoadRequest loadEntry)
83             throws JahiaException {
84         // get the DB connection
85
Connection dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
86         if (dbConn == null) {
87             return null;
88         }
89
90         Vector JavaDoc result = new Vector JavaDoc ();
91         PreparedStatement stmt = null;
92
93         try {
94
95             String JavaDoc sqlQuery;
96             if (loadEntry.getWorkflowState () <= 0) {
97                 stmt = dbConn.prepareStatement("SELECT * FROM jahia_pages_data WHERE id_jahia_pages_data=? AND workflow_state<=0 ORDER BY version_id DESC");
98                 stmt.setInt(1, pageID);
99             } else {
100                 stmt = dbConn.prepareStatement("SELECT * FROM jahia_pages_data WHERE id_jahia_pages_data=? AND workflow_state>0");
101                 stmt.setInt(1, pageID);
102             }
103
104             if (stmt != null) {
105                 ResultSet rs = stmt.executeQuery ();
106                 if (rs != null) {
107                     while (rs.next ()) {
108                         JahiaPageInfo pageInfo = readPageInfoFromResultSet (pageID, rs);
109                         result.add (pageInfo);
110                     }
111                     rs.close();
112                 }
113             }
114         } catch (SQLException se) {
115             String JavaDoc errorMsg = "Error in JahiaPagesDB.loadPageInfos : " +
116                     se.getMessage () + " -> BAILING OUT";
117             logger.debug (errorMsg, se);
118             throw new JahiaException ("Cannot load the page values from the database",
119                     errorMsg,
120                     JahiaException.DATABASE_ERROR,
121                     JahiaException.CRITICAL_SEVERITY,
122                     se);
123         } finally {
124
125             closeStatement (stmt);
126         }
127
128         return result;
129     }
130
131     /**
132      * Loads a specific JahiaPageInfo by specifying the entry state to retrieve
133      *
134      * @param pageID the page for which to retrieve the page info (or row)
135      * object
136      * @param entryState the entry for which we want to find the jahia page
137      * info row
138      *
139      * @return a JahiaPageInfo corresponding to the given parameters, or null
140      * if not found
141      *
142      * @throws JahiaException thrown if there was an error communicating with
143      * the database.
144      */

145     public JahiaPageInfo loadPageInfo (int pageID, EntryStateable entryState)
146             throws JahiaException {
147         // get the DB connection
148
Connection dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
149         if (dbConn == null) {
150             return null;
151         }
152
153         JahiaPageInfo pageInfo = null;
154         PreparedStatement statement = null;
155
156         try {
157
158             String JavaDoc sqlQuery;
159             sqlQuery =
160                     "SELECT * FROM jahia_pages_data WHERE id_jahia_pages_data=?" +
161                     " AND workflow_state=? AND version_id=? AND language_code=?";
162
163             statement = dbConn.prepareStatement (sqlQuery);
164             statement.setInt (1, pageID);
165             statement.setInt (2, entryState.getWorkflowState ());
166             statement.setInt (3, entryState.getVersionID ());
167             statement.setString (4, entryState.getLanguageCode ());
168             if (statement != null) {
169                 logger.debug (sqlQuery);
170                 ResultSet rs = statement.executeQuery ();
171                 if (rs != null && rs.next ()) {
172                     pageInfo = readPageInfoFromResultSet (pageID, rs);
173                 } else {
174                     logger.debug ("Page not found for entrySate :" + entryState.toString ());
175                     logger.debug ("Page not found for with query :" + sqlQuery);
176                 }
177             }
178         } catch (SQLException se) {
179             String JavaDoc errorMsg = "Error in JahiaPagesDB.loadPageInfo : " +
180                     se.getMessage () + " -> BAILING OUT";
181             logger.debug (errorMsg, se);
182             throw new JahiaException ("Cannot load the page row from the database",
183                     errorMsg,
184                     JahiaException.DATABASE_ERROR,
185                     JahiaException.CRITICAL_SEVERITY,
186                     se);
187         } finally {
188
189             closeStatement (statement);
190         }
191
192         return pageInfo;
193     }
194
195     //-------------------------------------------------------------------------
196
/**
197      * Preloading the page info from the database in cache.
198      *
199      * @param pageInfosCache
200      * @param versioningPageInfosCache
201      * @throws JahiaException Throws a {@link org.jahia.exceptions.JahiaException JahiaException}
202      * on any database error.
203      */

204     public void preloadingPageInfos (Cache pageInfosCache, Cache versioningPageInfosCache) throws JahiaException {
205         // get the DB connection
206
Connection dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
207
208         if (pageInfosCache == null || versioningPageInfosCache == null) {
209             logger.warn(" Cache is null, skip preloading page infos");
210             return;
211         }
212
213         if (dbConn == null) {
214             logger.warn(" DB Connection is null, skip preloading page infos");
215             return;
216         }
217
218         Vector JavaDoc result = new Vector JavaDoc ();
219         Statement statement = null;
220
221         try {
222
223             String JavaDoc sqlQuery = "SELECT * FROM jahia_pages_data ORDER BY id_jahia_pages_data";
224
225             statement = dbConn.createStatement ();
226             if (statement != null) {
227                 logger.debug (sqlQuery);
228                 ResultSet rs = statement.executeQuery (sqlQuery);
229                 if (rs != null) {
230                     while (rs.next ()) {
231                         int pageId = rs.getInt("id_jahia_pages_data");
232                         JahiaPageInfo pageInfo = readPageInfoFromResultSet(pageId,rs);
233                         Vector JavaDoc v = null;
234                         if ( pageInfo.getWorkflowState()<EntryLoadRequest.ACTIVE_WORKFLOW_STATE ){
235                             v = (Vector JavaDoc) versioningPageInfosCache.get(new
236                                 Integer JavaDoc(pageId));
237                         } else {
238                             v = (Vector JavaDoc) pageInfosCache.get(new
239                                 Integer JavaDoc(pageId));
240                         }
241                         if ( v == null ){
242                             pageInfosCache.put(new Integer JavaDoc(pageId), new Vector JavaDoc());
243                             versioningPageInfosCache.put(new Integer JavaDoc(pageId), new Vector JavaDoc());
244                             v = new Vector JavaDoc();
245                             if ( pageInfo.getWorkflowState()<EntryLoadRequest.ACTIVE_WORKFLOW_STATE ){
246                                 versioningPageInfosCache.put(new Integer JavaDoc(pageId), v);
247                             } else {
248                                 pageInfosCache.put(new Integer JavaDoc(pageId), v);
249                             }
250                         }
251                         v.add(pageInfo);
252                     }
253                 }
254             }
255         } catch (SQLException se) {
256             String JavaDoc errorMsg = "Error in JahiaPagesDB.preloadingPageInfos : " +
257                     se.getMessage () + " -> BAILING OUT";
258             logger.debug (errorMsg, se);
259         } finally {
260             closeStatement (statement);
261         }
262     }
263
264     public int getPageEntriesNb (int pageID)
265             throws JahiaException {
266         // get the DB connection
267
Connection dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
268         if (dbConn == null) {
269             return -1;
270         }
271
272         int entriesCount = -1;
273         JahiaPageInfo pageInfo = null;
274         Statement statement = null;
275
276         try {
277
278             String JavaDoc sqlQuery;
279             sqlQuery =
280                     "SELECT COUNT(*) AS nbentries FROM jahia_pages_data WHERE id_jahia_pages_data=" + pageID;
281
282             statement = dbConn.createStatement ();
283             if (statement != null) {
284                 logger.debug (sqlQuery);
285                 ResultSet rs = statement.executeQuery (sqlQuery);
286                 if (rs != null) {
287                     while (rs.next ()) {
288                         entriesCount = rs.getInt ("nbentries");
289                     }
290                 }
291             }
292         } catch (SQLException se) {
293             String JavaDoc errorMsg = "Error in JahiaPagesDB.getPageEntriesNb : " +
294                     se.getMessage () + " -> BAILING OUT";
295             logger.debug (errorMsg, se);
296             throw new JahiaException ("Cannot load the page values from the database",
297                     errorMsg,
298                     JahiaException.DATABASE_ERROR,
299                     JahiaException.CRITICAL_SEVERITY,
300                     se);
301         } finally {
302
303             closeStatement (statement);
304         }
305
306         return entriesCount;
307
308     }
309
310
311
312     //-------------------------------------------------------------------------
313
/**
314      * Create a new page entry in the database.
315      *
316      * @param thePage The JahiaPage object reference to save. Must be a
317      * non-null object reference.
318      *
319      * @return Return true if the page info could be inserted into the
320      * database or false on any error.
321      *
322      * @throws JahiaException Throws a {@link org.jahia.exceptions.JahiaException JahiaException}
323      * on any database error.
324      */

325     public synchronized boolean createPageInfo (JahiaPageInfo pageInfo)
326             throws JahiaException {
327         // Get the database connection.
328
Connection dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
329         if (dbConn == null) {
330             throw new JahiaException (
331                     "Page creation error on database access",
332                     "Cannot create a new page in the database, could not obtain a DB connection.",
333                     JahiaException.DATABASE_ERROR, JahiaException.CRITICAL_SEVERITY);
334         }
335
336         boolean result = false;
337         Statement statement = null;
338         try {
339
340             // opens connection
341
statement = dbConn.createStatement ();
342             if (statement != null) {
343
344                 // create the SQL query
345
String JavaDoc sqlQuery2 = "INSERT INTO jahia_pages_data (" +
346                         "id_jahia_pages_data," +
347                         "jahiaid_jahia_pages_data," +
348                         "parentid_jahia_pages_data," +
349                         "pagetype_jahia_pages_data," +
350                         "title_jahia_pages_data," +
351                         "pagedefid_jahia_pages_data," +
352                         "remoteurl_jahia_pages_data," +
353                         "pagelinkid_jahia_pages_data," +
354                         "creator_jahia_pages_data," +
355                         "doc_jahia_pages_data," +
356                         "counter_jahia_pages_data," +
357                         "rights_jahia_pages_data," +
358                         "version_id," +
359                         "workflow_state," +
360                         "language_code) VALUES (" +
361                         pageInfo.getID () + "," +
362                         pageInfo.getJahiaID () + "," +
363                         pageInfo.getParentID () + "," +
364                         pageInfo.getPageType () + "," +
365                         "'" + JahiaTools.quote (pageInfo.getTitle ()) + "'," +
366                         pageInfo.getPageTemplateID () + "," +
367                         "'" + JahiaTools.quote (pageInfo.getRemoteURL ()) + "'," +
368                         pageInfo.getPageLinkID () + "," +
369                         "'" + JahiaTools.quote (pageInfo.getCreator ()) + "'," +
370                         "'" + JahiaTools.quote (pageInfo.getDoc ()) + "'," +
371                         pageInfo.getCounter () + "," +
372                         pageInfo.getAclID () + "," +
373                         pageInfo.getVersionID () + "," +
374                         pageInfo.getWorkflowState () + ",'" +
375                         JahiaTools.quote (pageInfo.getLanguageCode ()) +
376                         "')";
377                 // executes the query
378
logger.debug (sqlQuery2);
379                 statement.execute (sqlQuery2);
380
381                 result = true;
382
383             }
384
385         } // catches an exception in the query
386
catch (SQLException se) {
387             String JavaDoc errorMsg = "Error in createPageInfo : " + se.getMessage () + " -> BAILING OUT";
388             logger.debug (errorMsg, se);
389             throw new JahiaException ("Cannot create a new page in the database",
390                     errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL_SEVERITY);
391         } finally {
392
393             closeStatement (statement);
394         }
395
396         if (result) {
397             // now let's invalidate the page service's cache
398
ServicesRegistry.getInstance ().getJahiaPageService ().invalidatePageCache (
399                     pageInfo.getID ());
400
401         }
402         return result;
403     }
404
405
406     //-------------------------------------------------------------------------
407
/**
408      * Update the page info in the database.
409      *
410      * @param pageInfo The Jahiapage object reference to update.
411      *
412      * @return Return true if the page info could be updated in the database,
413      * return false on any error.
414      *
415      * @throws JahiaException Throws a {@link org.jahia.exceptions.JahiaException JahiaException}
416      * on any database error.
417      */

418     public boolean updatePageInfo (JahiaPageInfo pageInfo,
419                                    int newVersionID,
420                                    int newVersionStatus)
421             throws JahiaException {
422
423         // Get the database connection.
424
Connection dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
425         if (dbConn == null) {
426             throw new JahiaException (
427                     "Page update error on database access",
428                     "Cannot update the requested page in the database, could not obtain a DB connection.",
429                     JahiaException.DATABASE_ERROR, JahiaException.CRITICAL_SEVERITY);
430         }
431         boolean result = false;
432         Statement statement = null;
433         try {
434
435             // composes the query
436
StringBuffer JavaDoc sqlQuery = new StringBuffer JavaDoc ("UPDATE jahia_pages_data SET ");
437             sqlQuery.append ("jahiaid_jahia_pages_data = " + pageInfo.getJahiaID () + ",");
438             sqlQuery.append ("parentid_jahia_pages_data = " + pageInfo.getParentID () + ",");
439             sqlQuery.append ("pagetype_jahia_pages_data = " + pageInfo.getPageType () + ",");
440             sqlQuery.append (
441                     "title_jahia_pages_data = '" + JahiaTools.quote (pageInfo.getTitle ()) + "',");
442             sqlQuery.append (
443                     "pagedefid_jahia_pages_data = " + pageInfo.getPageTemplateID () + ",");
444             sqlQuery.append (
445                     "remoteurl_jahia_pages_data = '" + JahiaTools.quote (
446                             pageInfo.getRemoteURL ()) + "',");
447             sqlQuery.append ("pagelinkid_jahia_pages_data = " + pageInfo.getPageLinkID () + ",");
448             sqlQuery.append (
449                     "creator_jahia_pages_data = '" + JahiaTools.quote (pageInfo.getCreator ()) + "',");
450             sqlQuery.append (
451                     "doc_jahia_pages_data = '" + JahiaTools.quote (pageInfo.getDoc ()) + "',");
452             sqlQuery.append ("counter_jahia_pages_data = " + pageInfo.getCounter () + ",");
453             sqlQuery.append ("rights_jahia_pages_data = " + pageInfo.getAclID () + ",");
454             //sqlQuery.append ("version_id = " + pageInfo.getVersionID() + ",");
455
sqlQuery.append ("version_id = " + newVersionID + ",");
456             sqlQuery.append ("workflow_state = " + newVersionStatus);
457             sqlQuery.append (" WHERE id_jahia_pages_data = " + pageInfo.getID ());
458             sqlQuery.append (
459                     " AND language_code = '" + JahiaTools.quote (pageInfo.getLanguageCode ()) + "'");
460             sqlQuery.append (" AND workflow_state = " + pageInfo.getWorkflowState ());
461
462             // executes the query
463
statement = dbConn.createStatement ();
464             if (statement != null) {
465                 logger.debug (sqlQuery.toString ());
466                 statement.execute (sqlQuery.toString ());
467                 result = true;
468                 // now let's invalidate the page service's cache
469
ServicesRegistry.getInstance ().getJahiaPageService ().invalidatePageCache (
470                         pageInfo.getID ());
471             }
472         } // catches an exception in the query
473
catch (SQLException se) {
474             String JavaDoc errorMsg = "Error in JahiaPagesDB.updatePageInfo : " + se.getMessage () + " -> BAILING OUT";
475             logger.debug (errorMsg, se);
476             throw new JahiaException ("Cannot create a new page in the database",
477                     errorMsg,
478                     JahiaException.DATABASE_ERROR,
479                     JahiaException.CRITICAL_SEVERITY,
480                     se);
481         } finally {
482
483             closeStatement (statement);
484         }
485         return result;
486     }
487
488
489     //-------------------------------------------------------------------------
490
/**
491      * Remove only the page info from the database.
492      *
493      * @param pageID The page id to delete.
494      *
495      * @return Return true if the page info could be removed from the database,
496      * or false on any error.
497      *
498      * @throws JahiaException Throws a {@link org.jahia.exceptions.JahiaException JahiaException}
499      * on any database error.
500      */

501     public boolean deletePageInfo (JahiaPageInfo pageInfo)
502             throws JahiaException {
503         if (pageInfo == null) {
504             return false;
505         }
506
507         // Get the database connection.
508
Connection dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
509         if (dbConn == null) {
510             throw new JahiaException (
511                     "Page deletion error on database access",
512                     "Cannot delete the requested page in the database, could not obtain a DB connection.",
513                     JahiaException.DATABASE_ERROR, JahiaException.CRITICAL_SEVERITY);
514         }
515
516         boolean result = false;
517         Statement statement = null;
518         try {
519             // composes the query
520
String JavaDoc sqlQuery = "DELETE FROM jahia_pages_data WHERE id_jahia_pages_data = " +
521                     pageInfo.getID () +
522                     " AND workflow_state=" +
523                     pageInfo.getWorkflowState () +
524                     " AND language_code='" +
525                     JahiaTools.quote (pageInfo.getLanguageCode ()) + "'";
526             /*
527             if ( pageInfo.getWorkflowState()<EntryLoadRequest.ACTIVE_WORKFLOW_STATE ){
528                 sqlQuery += " AND version_id=" + pageInfo.getVersionID();
529             }*/

530
531             // executes the query
532
statement = dbConn.createStatement ();
533             if (statement != null) {
534                 logger.debug (sqlQuery);
535                 statement.execute (sqlQuery);
536                 result = true;
537                 // now let's invalidate the page service's cache
538
ServicesRegistry.getInstance ().getJahiaPageService ().invalidatePageCache (
539                         pageInfo.getID ());
540             }
541         } // catches an exception in the query
542
catch (SQLException se) {
543             String JavaDoc errorMsg = "Error in JahiaPagesDB.deletePageInfo : " +
544                     se.getMessage () + " -> BAILING OUT";
545             logger.debug (errorMsg, se);
546             throw new JahiaException ("Cannot delete a page in the database",
547                     errorMsg,
548                     JahiaException.DATABASE_ERROR,
549                     JahiaException.CRITICAL_SEVERITY,
550                     se);
551         } finally {
552
553             closeStatement (statement);
554         }
555
556         return result;
557     }
558
559     public void backupPageInfo (JahiaPageInfo pageInfo,
560                                 int newVersionID,
561                                 int newVersionStatus)
562             throws JahiaException {
563         Connection dbConn = null;
564         Statement stmt = null;
565         ResultSet rs = null;
566
567         int pageInfoID = pageInfo.getID ();
568
569         try {
570             String JavaDoc sqlQuery = "SELECT * FROM jahia_pages_data WHERE " +
571                     "id_jahia_pages_data = " + pageInfo.getID () +
572                     " AND workflow_state=" + pageInfo.getWorkflowState () +
573                     " AND version_id=" + pageInfo.getVersionID () +
574                     " AND language_code='" + pageInfo.getLanguageCode () + "'";
575
576             dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
577             stmt = dbConn.createStatement ();
578             logger.debug (sqlQuery);
579             rs = stmt.executeQuery (sqlQuery);
580             if (rs != null)
581                 if (rs.next ()) {
582                     // add the version to the list of versioning info in the field
583
int versionID = rs.getInt ("version_id");
584                     String JavaDoc languageCode = rs.getString ("language_code");
585                     sqlQuery = "INSERT INTO jahia_pages_data (" +
586                             "id_jahia_pages_data," +
587                             "jahiaid_jahia_pages_data," +
588                             "parentid_jahia_pages_data," +
589                             "pagetype_jahia_pages_data," +
590                             "title_jahia_pages_data," +
591                             "pagedefid_jahia_pages_data," +
592                             "remoteurl_jahia_pages_data," +
593                             "pagelinkid_jahia_pages_data," +
594                             "creator_jahia_pages_data," +
595                             "doc_jahia_pages_data," +
596                             "counter_jahia_pages_data," +
597                             "rights_jahia_pages_data," +
598                             "version_id," +
599                             "workflow_state," +
600                             "language_code) VALUES (" +
601                             pageInfoID + "," +
602                             rs.getInt ("jahiaid_jahia_pages_data") + "," +
603                             rs.getInt ("parentid_jahia_pages_data") + "," +
604                             rs.getInt ("pagetype_jahia_pages_data") + "," +
605                             "'" + JahiaTools.quote (rs.getString ("title_jahia_pages_data")) + "'," +
606                             rs.getInt ("pagedefid_jahia_pages_data") + "," +
607                             "'" + JahiaTools.quote (
608                                     rs.getString ("remoteurl_jahia_pages_data")) + "'," +
609                             rs.getInt ("pagelinkid_jahia_pages_data") + "," +
610                             "'" + JahiaTools.quote (rs.getString ("creator_jahia_pages_data")) + "'," +
611                             "'" + JahiaTools.quote (rs.getString ("doc_jahia_pages_data")) + "'," +
612                             rs.getInt ("counter_jahia_pages_data") + "," +
613                             rs.getInt ("rights_jahia_pages_data") + "," +
614                             newVersionID + "," +
615                             newVersionStatus + "," +
616                             "'" + JahiaTools.quote (languageCode) + "'" +
617                             ")";
618                     logger.debug (sqlQuery);
619                     stmt.execute (sqlQuery);
620                     // now let's invalidate the page service's cache
621
ServicesRegistry.getInstance ().getJahiaPageService ().invalidatePageCache (
622                             pageInfo.getID ());
623                 }
624         } catch (SQLException se) {
625             throw new JahiaException ("Cannot backup field in the database",
626                     se.getMessage (), JahiaException.DATABASE_ERROR,
627                     JahiaException.ERROR_SEVERITY, se);
628         } finally {
629             try {
630
631                 if (stmt != null) stmt.close ();
632             } catch (SQLException ex) {
633                 throw new JahiaException ("Cannot free resources", "Cannot free resources",
634                         JahiaException.DATABASE_ERROR, JahiaException.WARNING_SEVERITY, ex);
635             }
636         }
637     }
638
639
640     //-------------------------------------------------------------------------
641
/**
642      * Return the next available page info ID.
643      *
644      * @return Return the next available page info ID.
645      */

646     public synchronized int getNextID () {
647         int counter = -1;
648
649         Connection dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
650         if (dbConn != null) {
651             Statement statement = null;
652
653             try {
654                 String JavaDoc query = "SELECT MAX(id_jahia_pages_data) as MaxID FROM jahia_pages_data";
655                 statement = dbConn.createStatement ();
656                 if (statement != null) {
657                     logger.debug (query);
658                     ResultSet rs = statement.executeQuery (query);
659                     if (rs != null) {
660                         if (rs.next ()) {
661                             // get the highest used page ID
662
counter = rs.getInt ("MaxID");
663
664                             // increment by one to get the next counter
665
counter++;
666                         }
667                     }
668                 }
669             } catch (SQLException ex) {
670                 logger.debug ("Error", ex);
671             } finally {
672
673                 closeStatement (statement);
674             }
675         }
676         return counter;
677     }
678
679
680     //-------------------------------------------------------------------------
681
// Close the specified SQL statement.
682
private void closeStatement (Statement statement) {
683         // Close the opened statement
684
try {
685             if (statement != null) {
686                 statement.close ();
687             }
688         } catch (SQLException sqlEx) {
689             // just create an exception without raising it, just to notify it
690
// in the logs.
691
JahiaException je = new JahiaException ("Cannot close a statement",
692                     "Cannot close a statement", JahiaException.DATABASE_ERROR,
693                     JahiaException.WARNING_SEVERITY);
694         }
695     }
696
697     //-------------------------------------------------------------------------
698
private JahiaPageInfo readPageInfoFromResultSet (int pageID, ResultSet rs)
699             throws SQLException {
700         // Extract all the page info from the ResultSet
701
int jahiaID = rs.getInt ("jahiaid_jahia_pages_data");
702         int parentID = rs.getInt ("parentid_jahia_pages_data");
703         int pageType = rs.getInt ("pagetype_jahia_pages_data");
704         String JavaDoc pageTitle = rs.getString ("title_jahia_pages_data");
705         if (pageTitle == null) {
706             // this ugly hack is necessary because Oracle is incapable of
707
// distinguishing between NULL and empty string values.
708
pageTitle = "";
709         }
710
711         int pageTemplateID = rs.getInt ("pagedefid_jahia_pages_data");
712         String JavaDoc remoteUrl = rs.getString ("remoteurl_jahia_pages_data");
713         int pageLinkID = rs.getInt ("pagelinkid_jahia_pages_data") + 0;
714         String JavaDoc creator = rs.getString ("creator_jahia_pages_data") + "";
715         String JavaDoc doc = rs.getString ("doc_jahia_pages_data") + "";
716         int counter = rs.getInt ("counter_jahia_pages_data") + 0;
717         int rights = rs.getInt ("rights_jahia_pages_data") + 0;
718         int versionID = rs.getInt ("version_id") + 0;
719         int versionStatus = rs.getInt ("workflow_state") + 0;
720         String JavaDoc languageCode = rs.getString ("language_code");
721
722         if (remoteUrl == null) {
723             // this ugly hack is necessary because Oracle is incapable of
724
// distinguishing between NULL and empty string values.
725
remoteUrl = "<no url>";
726         }
727         if (remoteUrl.equals ("<no url>")) {
728             remoteUrl = null;
729         }
730
731         // create the page
732
return new JahiaPageInfo (pageID, jahiaID, parentID, pageType,
733                 pageTitle, pageTemplateID, remoteUrl, pageLinkID, creator,
734                 doc, counter, rights, versionID, versionStatus, languageCode);
735     }
736
737     protected void deleteEntry (int pageID, EntryStateable entryState)
738             throws JahiaException {
739
740         Connection dbConn = null;
741         PreparedStatement stmt = null;
742         ResultSet rs = null;
743
744         try {
745             String JavaDoc sqlQuery =
746                     "DELETE FROM jahia_pages_data WHERE id_jahia_pages_data=?" +
747                     " AND workflow_state=? AND version_id=? AND language_code=?";
748             dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
749             stmt = dbConn.prepareStatement (sqlQuery);
750             stmt.setInt (1, pageID);
751             stmt.setInt (2, entryState.getWorkflowState ());
752             stmt.setInt (3, entryState.getVersionID ());
753             stmt.setString (4, entryState.getLanguageCode ());
754             int resultCount = stmt.executeUpdate ();
755             logger.debug (resultCount + " rows affected by delete of entry");
756         } catch (SQLException se) {
757             throw new JahiaException ("Error while deleting entry",
758                     se.getMessage (),
759                     JahiaException.DATABASE_ERROR,
760                     JahiaException.ERROR_SEVERITY, se);
761         } finally {
762             try {
763                 if (stmt != null)
764                     stmt.close ();
765             } catch (SQLException ex) {
766                 throw new JahiaException ("Cannot free resources",
767                         "Cannot free resources",
768                         JahiaException.DATABASE_ERROR,
769                         JahiaException.WARNING_SEVERITY, ex);
770             }
771         }
772     }
773
774     /**
775      * Warning : this method assumes that the destination entry state DOES NOT
776      * exist in the database. Make sure you do this check before calling this
777      * method !
778      *
779      * @param pageID the page for which to perform the copy of
780      * entries
781      * @param fromEntryState the source entry state
782      * @param toEntryState the destination entry state (MUST NOT ALREADY EXIST
783      * IN DATABASE ! an Insert operation is performed here !)
784      *
785      * @throws JahiaException raised if there was an error copying the entry
786      * states, notably if the destination entry state already existed in the
787      * database and primary keys were in the database.
788      */

789     protected void copyEntry (int pageID,
790                               EntryStateable fromEntryState,
791                               EntryStateable toEntryState)
792             throws JahiaException {
793
794         Connection dbConn = null;
795         PreparedStatement stmt = null;
796
797         try {
798             String JavaDoc sqlQuery =
799                     "SELECT " +
800                     "id_jahia_pages_data," +
801                     "jahiaid_jahia_pages_data," +
802                     "parentid_jahia_pages_data," +
803                     "pagetype_jahia_pages_data," +
804                     "title_jahia_pages_data," +
805                     "pagedefid_jahia_pages_data," +
806                     "remoteurl_jahia_pages_data," +
807                     "pagelinkid_jahia_pages_data," +
808                     "creator_jahia_pages_data," +
809                     "doc_jahia_pages_data," +
810                     "counter_jahia_pages_data," +
811                     "rights_jahia_pages_data," +
812                     "version_id," +
813                     "workflow_state," +
814                     "language_code " +
815                     "FROM jahia_pages_data WHERE id_jahia_pages_data=?" +
816                     " AND workflow_state=? AND version_id=? AND language_code=?";
817             dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
818             stmt = dbConn.prepareStatement (sqlQuery);
819             stmt.setInt (1, pageID);
820             stmt.setInt (2, fromEntryState.getWorkflowState ());
821             stmt.setInt (3, fromEntryState.getVersionID ());
822             stmt.setString (4, fromEntryState.getLanguageCode ());
823             ResultSet rs = stmt.executeQuery ();
824             if (rs.next ()) {
825                 int jahiaID = rs.getInt ("jahiaid_jahia_pages_data");
826                 int parentID = rs.getInt ("parentid_jahia_pages_data");
827                 int pageType = rs.getInt ("pagetype_jahia_pages_data");
828                 String JavaDoc pageTitle = rs.getString ("title_jahia_pages_data");
829                 int pageTemplateID = rs.getInt ("pagedefid_jahia_pages_data");
830                 String JavaDoc remoteURL = rs.getString ("remoteurl_jahia_pages_data");
831                 int pageLinkID = rs.getInt ("pagelinkid_jahia_pages_data");
832                 String JavaDoc creator = rs.getString ("creator_jahia_pages_data");
833                 String JavaDoc doc = rs.getString ("doc_jahia_pages_data");
834                 int counter = rs.getInt ("counter_jahia_pages_data");
835                 int rights = rs.getInt ("rights_jahia_pages_data");
836                 int versionID = rs.getInt ("version_id");
837                 int versionStatus = rs.getInt ("workflow_state");
838                 String JavaDoc languageCode = rs.getString ("language_code");
839                 stmt.close ();
840                 String JavaDoc insertQuery = "INSERT INTO jahia_pages_data (" +
841                         "id_jahia_pages_data," +
842                         "jahiaid_jahia_pages_data," +
843                         "parentid_jahia_pages_data," +
844                         "pagetype_jahia_pages_data," +
845                         "title_jahia_pages_data," +
846                         "pagedefid_jahia_pages_data," +
847                         "remoteurl_jahia_pages_data," +
848                         "pagelinkid_jahia_pages_data," +
849                         "creator_jahia_pages_data," +
850                         "doc_jahia_pages_data," +
851                         "counter_jahia_pages_data," +
852                         "rights_jahia_pages_data," +
853                         "version_id," +
854                         "workflow_state," +
855                         "language_code" +
856                         ") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
857                 stmt = dbConn.prepareStatement (insertQuery);
858                 stmt.setInt (1, pageID);
859                 stmt.setInt (2, jahiaID);
860                 stmt.setInt (3, parentID);
861                 stmt.setInt (4, pageType);
862                 stmt.setString (5, pageTitle);
863                 stmt.setInt (6, pageTemplateID);
864                 stmt.setString (7, remoteURL);
865                 stmt.setInt (8, pageLinkID);
866                 stmt.setString (9, creator);
867                 stmt.setString (10, doc);
868                 stmt.setInt (11, counter);
869                 stmt.setInt (12, rights);
870                 stmt.setInt (13, toEntryState.getVersionID ());
871                 stmt.setInt (14, toEntryState.getWorkflowState ());
872                 stmt.setString (15, toEntryState.getLanguageCode ());
873                 stmt.executeUpdate ();
874             }
875         } catch (SQLException se) {
876             logger.debug (
877                     "Exception copying entry state from : " + fromEntryState.toString () + "\n to : " + toEntryState.toString () + ", for page : " + pageID);
878             throw new JahiaException ("Error while copying entry",
879                     se.getMessage (),
880                     JahiaException.DATABASE_ERROR,
881                     JahiaException.ERROR_SEVERITY, se);
882         } finally {
883             try {
884                 if (stmt != null)
885                     stmt.close ();
886             } catch (SQLException ex) {
887                 throw new JahiaException ("Cannot free resources",
888                         "Cannot free resources",
889                         JahiaException.DATABASE_ERROR,
890                         JahiaException.WARNING_SEVERITY, ex);
891             }
892         }
893     }
894
895 }
896
Popular Tags