KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > services > fields > JahiaFieldUtilsDB


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
// JahiaFieldUtilsDB
15
// EV 05.01.2001
16
//
17
//
18
// db_get_all_fields_id -> ids vector
19
// db_get_field_ids_in_page( pageID ) -> ids vector
20
// db_get_all_field_definition_ids() -> ids vector
21
// db_get_field_id( fieldName, pageID ) -> id
22
//
23

24
25 package org.jahia.services.fields;
26
27 import java.sql.Connection JavaDoc;
28 import java.sql.PreparedStatement JavaDoc;
29 import java.sql.ResultSet JavaDoc;
30 import java.sql.SQLException JavaDoc;
31 import java.util.HashMap JavaDoc;
32 import java.util.Map JavaDoc;
33 import java.util.Vector JavaDoc;
34
35 import org.apache.log4j.Logger;
36 import org.jahia.data.fields.FieldTypes;
37 import org.jahia.exceptions.JahiaException;
38 import org.jahia.registries.ServicesRegistry;
39 import org.jahia.services.database.ConnectionDispenser;
40 import org.jahia.services.pages.ContentPage;
41 import org.jahia.services.version.EntryLoadRequest;
42
43
44 public class JahiaFieldUtilsDB {
45
46     private static Logger logger = Logger.getLogger (JahiaFieldUtilsDB.class);
47
48     /**
49      * constructor
50      */

51     public JahiaFieldUtilsDB () {
52     } // end constructor
53

54
55     /**
56      * gets all the fields ids
57      *
58      * @return a Vector if ids
59      *
60      * @throws throws a critical JahiaException if SQL error
61      * @throws throws a warning JahiaException if cannot free resources
62      */

63     public Vector JavaDoc db_get_all_fields_id ()
64             throws JahiaException {
65         Vector JavaDoc result = new Vector JavaDoc ();
66         Connection JavaDoc dbConn = null;
67         PreparedStatement JavaDoc stmt = null;
68         ResultSet JavaDoc rs = null;
69         Integer JavaDoc nb_to_insert;
70         try {
71             String JavaDoc sqlQuery = "SELECT DISTINCT id_jahia_fields_data FROM jahia_fields_data";
72
73             dbConn = ConnectionDispenser.getConnection ();
74             stmt = dbConn.prepareStatement (sqlQuery);
75             rs = stmt.executeQuery ();
76
77             while (rs.next ()) {
78                 nb_to_insert = new Integer JavaDoc (rs.getInt ("id_jahia_fields_data"));
79                 result.addElement (nb_to_insert);
80             }
81
82         } catch (SQLException JavaDoc se) {
83             String JavaDoc errorMsg = "Error in db_get_all_fields_id : " + se.getMessage ();
84             logger.error (errorMsg + " -> BAILING OUT");
85             new JahiaException ("Cannot load fields from the database",
86                     errorMsg, JahiaException.DATABASE_ERROR, JahiaException.ERROR_SEVERITY);
87         } finally {
88             try {
89
90                 if (stmt != null) stmt.close ();
91             } catch (SQLException JavaDoc ex) {
92                 new JahiaException ("Cannot free resources",
93                         "db_get_all_fields_id : cannot free resources",
94                         JahiaException.DATABASE_ERROR, JahiaException.WARNING_SEVERITY);
95             }
96         }
97
98         return result;
99     } // end db_get_all_fields_id
100

101
102     /**
103      * gets all the fields ids for a gived site
104      *
105      * @return a Vector if ids
106      *
107      * @throws throws a critical JahiaException if SQL error
108      * @throws throws a warning JahiaException if cannot free resources
109      * @param siteID
110      */

111     public Vector JavaDoc db_get_all_fields_id (int siteID)
112             throws JahiaException {
113         Vector JavaDoc result = new Vector JavaDoc ();
114         Connection JavaDoc dbConn = null;
115         PreparedStatement JavaDoc stmt = null;
116         ResultSet JavaDoc rs = null;
117         Integer JavaDoc nb_to_insert;
118         try {
119             String JavaDoc sqlQuery = "SELECT DISTINCT id_jahia_fields_data FROM jahia_fields_data where jahiaid_jahia_fields_data=?";
120
121             dbConn = ConnectionDispenser.getConnection ();
122             stmt = dbConn.prepareStatement (sqlQuery);
123             stmt.setInt(1, siteID);
124             rs = stmt.executeQuery ();
125
126             while (rs.next ()) {
127                 nb_to_insert = new Integer JavaDoc (rs.getInt ("id_jahia_fields_data"));
128                 result.addElement (nb_to_insert);
129             }
130
131         } catch (SQLException JavaDoc se) {
132             String JavaDoc errorMsg = "Error in db_get_all_fields_id : " + se.getMessage ();
133             logger.error (errorMsg + " -> BAILING OUT");
134             new JahiaException ("Cannot load fields from the database",
135                     errorMsg, JahiaException.DATABASE_ERROR, JahiaException.ERROR_SEVERITY);
136         } finally {
137             try {
138
139                 if (stmt != null) stmt.close ();
140             } catch (SQLException JavaDoc ex) {
141                 new JahiaException ("Cannot free resources",
142                         "db_get_all_fields_id : cannot free resources",
143                         JahiaException.DATABASE_ERROR, JahiaException.WARNING_SEVERITY);
144             }
145         }
146
147         return result;
148     } // end db_get_all_fields_id
149

150
151     /**
152      * Returns ALL the field IDs in a page, including the field IDs of
153      * fields within containers.
154      *
155      * @param pageID the page for which to retrieve the field IDs
156      *
157      * @return a Vector of Integer objects containing the field IDs on the
158      * page, in all workflow states and languages.
159      *
160      * @throws JahiaException if there was an error communicating with the
161      * database
162      */

163     public Vector JavaDoc dbGetAllFieldIDsInPage (int pageID)
164             throws JahiaException {
165         return dbGetAllFieldIDsInPageByWorkflowState (pageID, null);
166     }
167
168     /**
169      * gets the field ids in the specified page
170      *
171      * @param pageID the page ID
172      *
173      * @return a Vector if field list ids
174      *
175      * @throws throws a critical JahiaException if SQL error
176      * @throws throws a warning JahiaException if cannot free resources
177      */

178     public Vector JavaDoc dbGetNonContainerFieldIDsInPage (int pageID)
179             throws JahiaException {
180
181         return dbGetNonContainerFieldIDsInPageByWorkflowState (pageID, null);
182     }
183
184     /**
185      * gets only the field IDs that are in staging (ACTIVE>1) for the current page
186      *
187      * @param pageID the page ID
188      *
189      * @return a Vector if field list ids
190      *
191      * @throws throws a critical JahiaException if SQL error
192      * @throws throws a warning JahiaException if cannot free resources
193      */

194     public Vector JavaDoc db_get_only_staged_field_ids_in_page (int pageID)
195             throws JahiaException {
196         Connection JavaDoc dbConn = null;
197         PreparedStatement JavaDoc stmt = null;
198         ResultSet JavaDoc rs = null;
199         Vector JavaDoc fieldIDs = new Vector JavaDoc ();
200         try {
201             dbConn = ConnectionDispenser.getConnection ();
202
203             String JavaDoc sqlQuery = "SELECT DISTINCT id_jahia_fields_data FROM jahia_fields_data " +
204                     "WHERE pageid_jahia_fields_data=? " +
205                     "AND (workflow_state>1)" +
206                     "ORDER BY id_jahia_fields_data ASC";
207             
208             stmt = dbConn.prepareStatement (sqlQuery);
209             stmt.setInt(1, pageID);
210             rs = stmt.executeQuery ();
211
212             while (rs.next ()) {
213                 fieldIDs.add (new Integer JavaDoc (rs.getInt ("id_jahia_fields_data")));
214             }
215         } catch (SQLException JavaDoc se) {
216             String JavaDoc errorMsg = "Error in db_get_only_staged_field_ids_in_page : " + se.getMessage ();
217             logger.error (errorMsg + " -> BAILING OUT");
218             throw new JahiaException ("Cannot load fields from the database",
219                     errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL_SEVERITY);
220         } finally {
221             try {
222
223                 if (stmt != null) stmt.close ();
224             } catch (SQLException JavaDoc ex) {
225                 new JahiaException ("Cannot free resources",
226                         "db_get_only_staged_field_ids_in_page : cannot free resources",
227                         JahiaException.DATABASE_ERROR, JahiaException.WARNING_SEVERITY);
228             }
229         }
230
231         return fieldIDs;
232     } // end db_get_field_ids_in_page
233

234     /**
235      * gets only the non container field IDs that are in active
236      * for the current page
237      *
238      * @param pageID the page ID
239      *
240      * @return a Vector of field list ids (Integer objects)
241      *
242      * @throws throws a critical JahiaException if SQL error
243      * @throws throws a warning JahiaException if cannot free resources
244      */

245     public Vector JavaDoc db_get_only_active_non_container_field_ids_in_page (int pageID)
246             throws JahiaException {
247         Connection JavaDoc dbConn = null;
248         PreparedStatement JavaDoc stmt = null;
249         ResultSet JavaDoc rs = null;
250         Vector JavaDoc fieldIDs = new Vector JavaDoc ();
251         try {
252             dbConn = ConnectionDispenser.getConnection ();
253
254             String JavaDoc sqlQuery = "SELECT DISTINCT id_jahia_fields_data FROM jahia_fields_data " +
255                     "WHERE pageid_jahia_fields_data=? " +
256                     "AND (workflow_state=1) AND (ctnid_jahia_fields_data=0) " +
257                     "ORDER BY id_jahia_fields_data ASC";
258             
259             stmt = dbConn.prepareStatement (sqlQuery);
260             stmt.setInt(1, pageID);
261             rs = stmt.executeQuery ();
262
263             while (rs.next ()) {
264                 fieldIDs.add (new Integer JavaDoc (rs.getInt ("id_jahia_fields_data")));
265             }
266         } catch (SQLException JavaDoc se) {
267             String JavaDoc errorMsg = "Error in db_get_only_active_non_container_field_ids_in_page : " + se.getMessage ();
268             logger.error (errorMsg + " -> BAILING OUT");
269             throw new JahiaException ("Cannot load fields from the database",
270                     errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL_SEVERITY);
271         } finally {
272             try {
273
274                 if (stmt != null) stmt.close ();
275             } catch (SQLException JavaDoc ex) {
276                 new JahiaException ("Cannot free resources",
277                         "db_get_only_active_field_ids_in_page : cannot free resources",
278                         JahiaException.DATABASE_ERROR, JahiaException.WARNING_SEVERITY);
279             }
280         }
281
282         return fieldIDs;
283     } // end db_get_field_ids_in_page
284

285
286     /**
287      * gets only the non container field IDs that are in staging (ACTIVE>1)
288      * for the current page
289      *
290      * @param pageID the page ID
291      *
292      * @return a Vector of field list ids (Integer objects)
293      *
294      * @throws throws a critical JahiaException if SQL error
295      * @throws throws a warning JahiaException if cannot free resources
296      */

297     public Vector JavaDoc db_get_only_staged_non_container_field_ids_in_page (int
298             pageID)
299             throws JahiaException {
300
301         Connection JavaDoc dbConn = null;
302         PreparedStatement JavaDoc stmt = null;
303         ResultSet JavaDoc rs = null;
304         Vector JavaDoc fieldIDs = new Vector JavaDoc ();
305         try {
306             dbConn = ConnectionDispenser.getConnection ();
307
308             final String JavaDoc sqlQuery =
309                     "SELECT DISTINCT id_jahia_fields_data FROM jahia_fields_data " +
310                     "WHERE pageid_jahia_fields_data=? " +
311                     "AND (workflow_state>1) AND (ctnid_jahia_fields_data=0) " +
312                     "ORDER BY id_jahia_fields_data ASC";
313
314             stmt = dbConn.prepareStatement (sqlQuery);
315             stmt.setInt (1, pageID);
316
317             rs = stmt.executeQuery ();
318
319             while (rs.next ()) {
320                 fieldIDs.add (new Integer JavaDoc (rs.getInt ("id_jahia_fields_data")));
321             }
322         } catch (SQLException JavaDoc se) {
323             String JavaDoc errorMsg =
324                     "Error in db_get_only_staged_non_container_field_ids_in_page(" +
325                     pageID + ") : " + se.getMessage ();
326             logger.error (errorMsg + " -> BAILING OUT");
327             throw new JahiaException ("Cannot load fields from the database",
328                     errorMsg, JahiaException.DATABASE_ERROR,
329                     JahiaException.CRITICAL_SEVERITY, se);
330         } finally {
331             try {
332                 if (stmt != null)
333                     stmt.close ();
334             } catch (SQLException JavaDoc ex) {
335                 new JahiaException ("Cannot free resources",
336                         "db_get_only_staged_field_ids_in_page : cannot free resources",
337                         JahiaException.DATABASE_ERROR,
338                         JahiaException.WARNING_SEVERITY, ex);
339             }
340         }
341
342         return fieldIDs;
343     } // end db_get_field_ids_in_page
344

345
346     /**
347      * Returns the field IDs that are in staged OR active state.
348      *
349      * @param pageID the page ID
350      *
351      * @return a Vector if field list ids
352      *
353      * @throws throws a critical JahiaException if SQL error
354      * @throws throws a warning JahiaException if cannot free resources
355      */

356     public Vector JavaDoc getActiveOrStagedFieldIDsInPage (int pageID)
357             throws JahiaException {
358         Connection JavaDoc dbConn = null;
359         PreparedStatement JavaDoc stmt = null;
360         ResultSet JavaDoc rs = null;
361         Vector JavaDoc fieldIDs = new Vector JavaDoc ();
362         try {
363             dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
364
365             final String JavaDoc sqlQuery = "SELECT DISTINCT id_jahia_fields_data FROM jahia_fields_data " +
366                     "WHERE pageid_jahia_fields_data=? AND (workflow_state>=1) " +
367                     "ORDER BY id_jahia_fields_data ASC";
368
369             stmt = dbConn.prepareStatement (sqlQuery);
370             stmt.setInt (1, pageID);
371
372             rs = stmt.executeQuery ();
373
374             while (rs.next ()) {
375                 Integer JavaDoc curFieldID = new Integer JavaDoc (rs.getInt (1));
376                 fieldIDs.add (curFieldID);
377             }
378         } catch (SQLException JavaDoc se) {
379             String JavaDoc errorMsg = "Error in getActiveOrStagedFieldIDsInPage : " +
380                     se.getMessage ();
381             logger.error (errorMsg + " -> BAILING OUT");
382             throw new JahiaException ("Cannot load fields from the database",
383                     errorMsg, JahiaException.DATABASE_ERROR,
384                     JahiaException.CRITICAL_SEVERITY, se);
385         } finally {
386             try {
387                 if (stmt != null)
388                     stmt.close ();
389             } catch (SQLException JavaDoc ex) {
390                 new JahiaException ("Cannot free resources",
391                         "db_get_only_staged_field_ids_in_page : cannot free resources",
392                         JahiaException.DATABASE_ERROR,
393                         JahiaException.WARNING_SEVERITY, ex);
394             }
395         }
396
397         return fieldIDs;
398     } // end db_get_field_ids_in_page
399

400     /**
401      * gets the fields ids of type PAGE in the specified page
402      *
403      * @param pageID the page ID
404      *
405      * @return a Vector if field list ids
406      *
407      * @throws throws a critical JahiaException if SQL error
408      * @throws throws a warning JahiaException if cannot free resources
409      */

410     public Vector JavaDoc db_get_pagefield_ids_in_page (int pageID)
411             throws JahiaException {
412         Connection JavaDoc dbConn = null;
413         PreparedStatement JavaDoc stmt = null;
414         ResultSet JavaDoc rs = null;
415         Vector JavaDoc fieldIDs = new Vector JavaDoc ();
416         try {
417             StringBuffer JavaDoc buf = new StringBuffer JavaDoc ();
418             buf.append ("SELECT DISTINCT id_jahia_fields_data FROM jahia_fields_data ");
419             buf.append ("WHERE ((pageid_jahia_fields_data=?");
420             buf.append (") AND (type_jahia_fields_data=");
421             buf.append (FieldTypes.PAGE);
422             buf.append ("))");
423             buf.append ("ORDER BY id_jahia_fields_data ASC");
424
425             dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
426             stmt = dbConn.prepareStatement (buf.toString ());
427             stmt.setInt(1, pageID);
428             rs = stmt.executeQuery ();
429
430             while (rs.next ()) {
431                 fieldIDs.add (new Integer JavaDoc (rs.getInt ("id_jahia_fields_data")));
432             }
433         } catch (SQLException JavaDoc se) {
434             String JavaDoc errorMsg = "Error in db_get_pagefield_ids_in_page : " + se.getMessage ();
435             logger.error (errorMsg + " -> BAILING OUT");
436             throw new JahiaException ("Cannot load fields from the database",
437                     errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL_SEVERITY);
438         } finally {
439             try {
440
441                 if (stmt != null) stmt.close ();
442             } catch (SQLException JavaDoc ex) {
443                 new JahiaException ("Cannot free resources",
444                         "db_get_pagefield_ids_in_page : cannot free resources",
445                         JahiaException.DATABASE_ERROR, JahiaException.WARNING_SEVERITY);
446             }
447         }
448
449         return fieldIDs;
450     } // end db_get_pagefield_ids_in_page
451

452
453     /**
454      * gets all field definition ids
455      *
456      * @return a Vector if field definition ids
457      *
458      * @throws throws a critical JahiaException if SQL error
459      * @throws throws a warning JahiaException if cannot free resources
460      */

461     public Vector JavaDoc db_get_all_field_definition_ids ()
462             throws JahiaException {
463         Connection JavaDoc dbConn = null;
464         PreparedStatement JavaDoc stmt = null;
465         ResultSet JavaDoc rs = null;
466         Vector JavaDoc theIDs = new Vector JavaDoc ();
467         try {
468             String JavaDoc sqlQuery = "SELECT id_jahia_fields_def FROM jahia_fields_def";
469
470             dbConn = ConnectionDispenser.getConnection ();
471             stmt = dbConn.prepareStatement (sqlQuery);
472             rs = stmt.executeQuery ();
473
474             while (rs.next ()) {
475                 theIDs.add (new Integer JavaDoc (rs.getInt ("id_jahia_fields_def")));
476             }
477         } catch (SQLException JavaDoc se) {
478             String JavaDoc errorMsg = "Error in db_get_all_field_definition_ids : " + se.getMessage ();
479             logger.error (errorMsg + " -> BAILING OUT");
480             throw new JahiaException ("Cannot load fields from the database",
481                     errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL_SEVERITY);
482         } finally {
483             try {
484                 if (stmt != null) stmt.close ();
485             } catch (SQLException JavaDoc ex) {
486                 new JahiaException ("Cannot free resources",
487                         "db_get_all_field_definition_ids : cannot free resources",
488                         JahiaException.DATABASE_ERROR, JahiaException.WARNING_SEVERITY);
489             }
490         }
491         return theIDs;
492
493     } // end db_get_all_field_definition_ids
494

495
496     /**
497      * gets a field id from its field name and page id
498      *
499      * @return a field id
500      *
501      * @throws throws a critical JahiaException if SQL error
502      * @throws throws a warning JahiaException if cannot free resources
503      */

504     public int db_get_field_id (String JavaDoc fieldName, int pageID)
505             throws JahiaException {
506         Connection JavaDoc dbConn = null;
507         PreparedStatement JavaDoc stmt = null;
508         ResultSet JavaDoc rs = null;
509         int fieldDefID = 0;
510         int fieldID = -1;
511         try {
512             // connects to database
513
dbConn = ConnectionDispenser.getConnection ();
514
515             // gets the page
516
ContentPage page = ServicesRegistry.getInstance ().
517                     getJahiaPageService ().lookupContentPage (pageID, true);
518
519             if (page == null) {
520                 String JavaDoc errorMsg = "Field " + fieldName +
521                         " is being loaded from an unexisting page (" +
522                         pageID + ")";
523                 new JahiaException (errorMsg,
524                         errorMsg, JahiaException.DATABASE_ERROR,
525                         JahiaException.WARNING_SEVERITY);
526                 return -1;
527             }
528
529             // gets field definition id
530
String JavaDoc sqlQuery =
531                     "SELECT id_jahia_fields_def FROM jahia_fields_def ";
532             sqlQuery += "WHERE (name_jahia_fields_def=? and jahiaid_jahia_fields_def=?)";
533             
534             stmt = dbConn.prepareStatement (sqlQuery);
535             stmt.setString(1, fieldName);
536             stmt.setInt(2, page.getJahiaID ());
537             rs = stmt.executeQuery ();
538             
539             if (rs.next ()) {
540                 fieldDefID = rs.getInt ("id_jahia_fields_def");
541             } else {
542                 String JavaDoc errorMsg = "Field " + fieldName + " is not defined";
543                 new JahiaException (errorMsg,
544                         errorMsg, JahiaException.DATABASE_ERROR,
545                         JahiaException.WARNING_SEVERITY);
546                 return -1;
547             }
548             
549             stmt.close();
550
551             // gets field id
552
sqlQuery = "SELECT id_jahia_fields_data FROM jahia_fields_data ";
553             sqlQuery += "WHERE ((pageid_jahia_fields_data=?) AND ";
554             sqlQuery += "(fielddefid_jahia_fields_data=?))";
555             
556             stmt = dbConn.prepareStatement (sqlQuery);
557             stmt.setInt(1, pageID);
558             stmt.setInt(2, fieldDefID);
559             rs = stmt.executeQuery ();
560             
561             if (rs.next ()) {
562                 fieldID = rs.getInt ("id_jahia_fields_data");
563             } else {
564                 return -1;
565             }
566
567         } catch (SQLException JavaDoc se) {
568             String JavaDoc errorMsg = "Error in db_get_field_id : " + se.getMessage ();
569             logger.error (errorMsg + " -> BAILING OUT");
570             throw new JahiaException ("Cannot load fields from the database",
571                     errorMsg, JahiaException.DATABASE_ERROR,
572                     JahiaException.CRITICAL_SEVERITY);
573         } finally {
574             try {
575                 if (stmt != null)
576                     stmt.close ();
577             } catch (SQLException JavaDoc ex) {
578                 new JahiaException ("Cannot free resources",
579                         "db_get_fieldid : cannot free resources",
580                         JahiaException.DATABASE_ERROR,
581                         JahiaException.WARNING_SEVERITY);
582             }
583         }
584         return fieldID;
585
586     } // end db_get_fieldid
587

588
589     /**
590      * gets all acl ids used by fields of a site
591      *
592      * @return a Vector of all acl id of a site
593      *
594      * @throws throws a warning JahiaException if cannot free resources
595      */

596     public Vector JavaDoc db_get_all_acl_id (int siteID)
597             throws JahiaException {
598         Connection JavaDoc dbConn = null;
599         PreparedStatement JavaDoc stmt = null;
600         ResultSet JavaDoc rs = null;
601         Vector JavaDoc theIDs = new Vector JavaDoc ();
602         try {
603             String JavaDoc sqlQuery = "SELECT DISTINCT rights_jahia_fields_data FROM jahia_fields_data "
604                     + "WHERE jahiaid_jahia_fields_data=?";
605
606             dbConn = ConnectionDispenser.getConnection ();
607             stmt = dbConn.prepareStatement(sqlQuery);
608             stmt.setInt(1, siteID);
609             rs = stmt.executeQuery ();
610
611             while (rs.next ()) {
612                 theIDs.add (new Integer JavaDoc (rs.getInt ("rights_jahia_fields_data")));
613             }
614         } catch (SQLException JavaDoc se) {
615             String JavaDoc errorMsg = "Error in db_get_all_acl_id : " + se.getMessage ();
616             logger.error (errorMsg + " -> BAILING OUT");
617             throw new JahiaException ("Cannot load acl id from the database",
618                     errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL_SEVERITY);
619         } finally {
620             try {
621                 if (stmt != null) stmt.close ();
622             } catch (SQLException JavaDoc ex) {
623                 new JahiaException ("Cannot free resources",
624                         "JahiaFieldUtilsDBdb_get_all_acl_id: cannot free resources",
625                         JahiaException.DATABASE_ERROR, JahiaException.WARNING_SEVERITY);
626             }
627         }
628         return theIDs;
629
630     }
631
632     /**
633      * Retrieves the field IDs of field in a page that are directly connected
634      * to the page, with a given workflow state and not included in any
635      * containers.
636      *
637      * @param pageID the page identifier for which to retrieve the field IDs
638      * @param loadVersion an EntryLoadRequest object that contains the
639      * workflowState information for which to load the field IDs. Currently only
640      * the workflowState information of this object is used.
641      *
642      * @return a Vector of Integer objects that contain the field identifiers
643      *
644      * @throws JahiaException thrown if there was an error communicating with
645      * the database.
646      */

647     public Vector JavaDoc dbGetNonContainerFieldIDsInPageByWorkflowState (
648             int pageID, EntryLoadRequest loadVersion)
649             throws JahiaException {
650
651         Connection JavaDoc dbConn = null;
652         PreparedStatement JavaDoc stmt = null;
653         ResultSet JavaDoc rs = null;
654         Vector JavaDoc fieldIDs = new Vector JavaDoc ();
655         try {
656             dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
657             
658             // active query
659
StringBuffer JavaDoc sqlQuery = new StringBuffer JavaDoc (
660                     "SELECT id_jahia_fields_data FROM jahia_fields_data ");
661             sqlQuery.append ("WHERE pageid_jahia_fields_data=?");
662             sqlQuery.append (" AND (ctnid_jahia_fields_data=0) ");
663             if (loadVersion != null) {
664                 if (loadVersion.isVersioned ()) {
665                     sqlQuery.append ("AND workflow_state<=1 ");
666                     sqlQuery.append ("AND version_id<=?");
667                 } else if (loadVersion.isCurrent ()) {
668                     sqlQuery.append ("AND workflow_state=?");
669                 } else if (loadVersion.isStaging ()) {
670                     sqlQuery.append ("AND workflow_state>=1");
671                 }
672             }
673             sqlQuery.append (" ORDER BY id_jahia_fields_data ASC");
674
675             stmt = dbConn.prepareStatement (sqlQuery.toString ());
676             stmt.setInt(1, pageID);
677             if (loadVersion != null) {
678                 if (loadVersion.isVersioned ()) {
679                     stmt.setInt(2, loadVersion.getVersionID ());
680                 } else if (loadVersion.isCurrent ()) {
681                   stmt.setInt(2, loadVersion.getWorkflowState ());
682                 }
683             }
684             rs = stmt.executeQuery ();
685
686             while (rs.next ()) {
687                 Integer JavaDoc id = new Integer JavaDoc (rs.getInt ("id_jahia_fields_data"));
688                 if (!fieldIDs.contains (id)) {
689                     fieldIDs.add (id);
690                 }
691             }
692         } catch (SQLException JavaDoc se) {
693             String JavaDoc errorMsg = "Error in dbGetNonContainerFieldIDsInPageByWorkflowState : " + se.getMessage ();
694             logger.error (errorMsg + " -> BAILING OUT");
695             throw new JahiaException ("Cannot load fields from the database",
696                     errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL_SEVERITY,
697                     se);
698         } finally {
699             try {
700
701                 if (stmt != null) stmt.close ();
702             } catch (SQLException JavaDoc ex) {
703                 new JahiaException ("Cannot free resources",
704                         "dbGetNonContainerFieldIDsInPageByWorkflowState : cannot free resources",
705                         JahiaException.DATABASE_ERROR, JahiaException.WARNING_SEVERITY, ex);
706             }
707         }
708         return fieldIDs;
709     }
710
711     /**
712      * Returns ALL the field IDs of the fields present in a page, whether they
713      * are directly attached to the page or within containers, for a given
714      * workflow state
715      *
716      * @param pageID the page identifier for which to retrieve the field IDs
717      * @param loadVersion an EntryLoadRequest object that specifies which
718      * workflow state to load. Only the workflowState of this object is used
719      * currently.
720      *
721      * @return a Vector of Integer object containing field identifier
722      * corresponding to the specified page as well as workflow state.
723      *
724      * @throws JahiaException thrown if there was an error communicating with
725      * the database.
726      */

727     public Vector JavaDoc dbGetAllFieldIDsInPageByWorkflowState (
728             int pageID, EntryLoadRequest loadVersion)
729             throws JahiaException {
730
731         Connection JavaDoc dbConn = null;
732         PreparedStatement JavaDoc stmt = null;
733         ResultSet JavaDoc rs = null;
734         Vector JavaDoc fieldIDs = new Vector JavaDoc ();
735         try {
736             dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
737             if (loadVersion != null) {
738                 stmt = dbConn.prepareStatement ("SELECT id_jahia_fields_data FROM jahia_fields_data " +
739                         "WHERE (pageid_jahia_fields_data=?) AND (workflow_state=?) " +
740                         "ORDER BY id_jahia_fields_data ASC");
741                 stmt.setInt (1, pageID);
742                 stmt.setInt (2, loadVersion.getWorkflowState ());
743             } else {
744                 stmt = dbConn.prepareStatement ("SELECT id_jahia_fields_data FROM jahia_fields_data " +
745                         "WHERE (pageid_jahia_fields_data=?) " +
746                         "ORDER BY id_jahia_fields_data ASC");
747                 stmt.setInt (1, pageID);
748             }
749
750             rs = stmt.executeQuery ();
751
752             while (rs.next ()) {
753                 Integer JavaDoc id = new Integer JavaDoc (rs.getInt ("id_jahia_fields_data"));
754                 if (!fieldIDs.contains (id)) {
755                     fieldIDs.add (id);
756                 }
757             }
758         } catch (SQLException JavaDoc se) {
759             String JavaDoc errorMsg = "Error in dbGetAllFieldIDsInPageByWorkflowState : " + se.getMessage ();
760             logger.error (errorMsg + " -> BAILING OUT");
761             throw new JahiaException ("Cannot load fields from the database",
762                     errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL_SEVERITY,
763                     se);
764         } finally {
765             try {
766
767                 if (stmt != null) stmt.close ();
768             } catch (SQLException JavaDoc ex) {
769                 logger.error ("Cannot free resources", ex);
770             }
771         }
772         return fieldIDs;
773     }
774
775     /**
776      * Returns the staging workflow state of the fields for all the languages
777      * on the specified page.
778      * Normally as currently we only do state changes on a page basis these
779      * should all be equal. This does not retrieve the state of absolute fields.
780      *
781      * @param pageID the page id on which to retrieve the fields workflow state
782      *
783      * @return a Map that contains all the states of the languages for the
784      * fields on the page (for the moment we assume all the objects are in the
785      * same state)
786      *
787      * @throws JahiaException in the case we couldn't load field language states
788      */

789     public Map JavaDoc getFieldsLanguagesState(int pageID) throws JahiaException
790     {
791       Connection JavaDoc dbConn = null;
792       PreparedStatement JavaDoc stmt = null;
793       ResultSet JavaDoc rs = null;
794       Map JavaDoc langStates = new HashMap JavaDoc();
795       try
796       {
797         dbConn = ConnectionDispenser.getConnection();
798         stmt = dbConn
799           .prepareStatement("SELECT LANGUAGE_CODE, MAX(WORKFLOW_STATE)"
800             + " FROM JAHIA_FIELDS_DATA"
801             + " WHERE PAGEID_JAHIA_FIELDS_DATA=? AND WORKFLOW_STATE >= 1"
802             + " GROUP BY LANGUAGE_CODE");
803         stmt.setInt(1, pageID);
804   
805         rs = stmt.executeQuery();
806   
807         while (rs.next())
808         {
809           langStates.put(rs.getString(1), new Integer JavaDoc(rs.getInt(2)));
810         }
811       }
812       catch (SQLException JavaDoc se)
813       {
814         String JavaDoc errorMsg = "Error in getFieldsLanguagesState : " + se.getMessage();
815         logger.error(errorMsg + " -> BAILING OUT");
816         throw new JahiaException(
817           "Cannot load fields language state from the database", errorMsg,
818           JahiaException.DATABASE_ERROR, JahiaException.ERROR_SEVERITY);
819       }
820       finally
821       {
822         try
823         {
824           if (stmt != null)
825             stmt.close();
826         }
827         catch (SQLException JavaDoc ex)
828         {
829           logger.warn("getFieldsLanguagesState : cannot free resources", ex);
830         }
831       }
832   
833       return langStates;
834     }
835
836 }
837
838
Popular Tags