KickJava   Java API By Example, From Geeks To Geeks.

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


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.containers;
14
15 import org.jahia.content.ContentObject;
16 import org.jahia.exceptions.JahiaException;
17 import org.jahia.services.version.ContentObjectEntryState;
18 import org.jahia.services.version.EntryStateable;
19
20 import java.sql.*;
21 import java.util.ArrayList JavaDoc;
22
23 /**
24  * <p>Title: </p>
25  * <p>Description: </p>
26  * <p>Copyright: Copyright (c) 2002</p>
27  * <p>Company: </p>
28  *
29  * @author Serge Huber
30  * @version 1.0
31  */

32
33 class ContentContainerDB {
34
35     private static org.apache.log4j.Logger logger =
36             org.apache.log4j.Logger.getLogger (ContentContainerDB.class);
37
38     private static ContentContainerDB singletonInstance = null;
39
40     /**
41      * Return an instance of this class
42      */

43     public static synchronized ContentContainerDB getInstance () {
44         if (singletonInstance == null) {
45             singletonInstance = new ContentContainerDB ();
46         }
47         return singletonInstance;
48     }
49
50 /////////////// BELOW ARE DATABASE ACCESS RELATED METHODS /////////////////
51

52     protected ContentContainer getContainer (int containerID) throws JahiaException {
53         ContentContainer resultContainer = null;
54         Connection dbConn = null;
55         PreparedStatement stmt = null;
56         ResultSet rs = null;
57
58         try {
59             // We first try to get all ACTIVE & STAGED fields, but this query
60
// won't give anything if the Field doesn't exist anymore (versioning)
61
final String JavaDoc sqlQuery = "SELECT DISTINCT jahiaid_jahia_ctn_entries,pageid_jahia_ctn_entries,listid_jahia_ctn_entries,ctndefid_jahia_ctn_entries,rank_jahia_ctn_entries,rights_jahia_ctn_entries,version_id,workflow_state FROM jahia_ctn_entries WHERE id_jahia_ctn_entries=? AND workflow_state>=1";
62
63             dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
64             stmt = dbConn.prepareStatement(sqlQuery);
65             stmt.setInt(1, containerID);
66             // rs can't be null (cf. api)
67
rs = stmt.executeQuery ();
68
69             // is there at least one element in the resultset ?
70
if (rs.next ()) {
71                 // yes, we get first value
72
int jahiaID = rs.getInt ("jahiaid_jahia_ctn_entries");
73                 int pageID = rs.getInt ("pageid_jahia_ctn_entries");
74                 int listID = rs.getInt ("listid_jahia_ctn_entries");
75                 int ctnDefID = rs.getInt ("ctndefid_jahia_ctn_entries");
76                 int rank = rs.getInt ("rank_jahia_ctn_entries");
77                 int rights = rs.getInt ("rights_jahia_ctn_entries");
78                 int versionID = rs.getInt ("version_id");
79                 int workflowState = rs.getInt ("workflow_state");
80
81                 ArrayList JavaDoc activeAndStagedVersions = new ArrayList JavaDoc ();
82                 activeAndStagedVersions.add (
83                         new ContentObjectEntryState (workflowState, versionID,
84                                 ContentObject.SHARED_LANGUAGE));
85
86                 // then we're only interested by the different version, active and lang values
87
// (the rest remains the same)
88
while (rs.next ()) {
89                     versionID = rs.getInt ("version_id");
90                     workflowState = rs.getInt ("workflow_state");
91                     activeAndStagedVersions.add (
92                             new ContentObjectEntryState (workflowState, versionID,
93                                     ContentObject.SHARED_LANGUAGE));
94                 }
95
96                 rs.close();
97
98                 resultContainer =
99                         new ContentContainer (
100                                 containerID, jahiaID, pageID, ctnDefID, listID, rights,
101                                 activeAndStagedVersions);
102             } else
103             // no element found in resultset, it means that the field is deleted (but maybe exist in
104
// an inactive version!) or it doesn't exist at all (trigers an exception then)
105
{
106                 // we try loading an old version
107
final String JavaDoc sqlQuery2 = "SELECT DISTINCT jahiaid_jahia_ctn_entries,pageid_jahia_ctn_entries,listid_jahia_ctn_entries,ctndefid_jahia_ctn_entries,rank_jahia_ctn_entries,rights_jahia_ctn_entries,version_id,workflow_state FROM jahia_ctn_entries WHERE id_jahia_ctn_entries=?";
108                 // we must close previous statement and result set otherwise
109
// we leak cursors on Oracle.
110
rs.close();
111                 stmt.close();
112                 stmt = dbConn.prepareStatement(sqlQuery2);
113                 stmt.setInt(1, containerID);
114                 rs = stmt.executeQuery ();
115
116                 // yes an inactive field does exist, we create the ContentField
117
if (rs.next ()) {
118                     int jahiaID = rs.getInt ("jahiaid_jahia_ctn_entries");
119                     int pageID = rs.getInt ("pageid_jahia_ctn_entries");
120                     int listID = rs.getInt ("listid_jahia_ctn_entries");
121                     int ctnDefID = rs.getInt ("ctndefid_jahia_ctn_entries");
122                     int rank = rs.getInt ("rank_jahia_ctn_entries");
123                     int rights = rs.getInt ("rights_jahia_ctn_entries");
124                     int versionID = rs.getInt ("version_id");
125                     int workflowState = rs.getInt ("workflow_state");
126                     resultContainer =
127                             new ContentContainer (
128                                     containerID, jahiaID, pageID, ctnDefID, listID, rights,
129                                     new ArrayList JavaDoc ());
130                 } else {
131                     /*
132                     // oops the field doesn't exist at all, let's trigger an exception!
133                     throw new JahiaException
134                             ("Container " + containerID + " not found in database!",
135                                     "Container " + containerID + " not found in database!",
136                                     JahiaException.DATABASE_ERROR,
137                                     JahiaException.ERROR_SEVERITY);
138                      }
139                     */

140                }
141             }
142
143         } catch (SQLException se) {
144             throw new JahiaException (
145                     "Cannot load container " + containerID + "from the database",
146                     se.getMessage (),
147                     JahiaException.DATABASE_ERROR,
148                     JahiaException.ERROR_SEVERITY, se);
149         } finally {
150             try {
151
152                 if (stmt != null) stmt.close ();
153             } catch (SQLException ex) {
154                 throw new JahiaException ("Cannot free resources",
155                         "Cannot free resources",
156                         JahiaException.DATABASE_ERROR,
157                         JahiaException.WARNING_SEVERITY, ex);
158             }
159         }
160
161         return resultContainer;
162     }
163
164     protected ArrayList JavaDoc getVersionedEntryStates (int containerID, boolean withActive)
165             throws JahiaException {
166         ArrayList JavaDoc resultEntryStates = new ArrayList JavaDoc ();
167         Connection dbConn = null;
168         Statement stmt = null;
169         ResultSet rs = null;
170
171         final String JavaDoc selectFields = "jahiaid_jahia_ctn_entries," +
172                 "pageid_jahia_ctn_entries," +
173                 "listid_jahia_ctn_entries," +
174                 "ctndefid_jahia_ctn_entries," +
175                 "rank_jahia_ctn_entries," +
176                 "rights_jahia_ctn_entries," +
177                 "version_id," +
178                 "workflow_state";
179
180         try {
181             // We first try to get all ACTIVE & STAGED fields, but this query
182
// won't give anything if the Field doesn't exist anymore (versioning)
183
String JavaDoc sqlQuery = "SELECT " +
184                     selectFields +
185                     " FROM jahia_ctn_entries WHERE id_jahia_ctn_entries=" + containerID;
186             if (withActive) {
187                 sqlQuery += " AND workflow_state<=1";
188             } else {
189                 sqlQuery += " AND workflow_state<=0";
190             }
191
192             dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
193             stmt = dbConn.createStatement ();
194             // rs can't be null (cf. api)
195
rs = stmt.executeQuery (sqlQuery);
196
197             // is there at least one element in the resultset ?
198
if (rs.next ()) {
199                 // yes, we get first value
200
int jahiaID = rs.getInt ("jahiaid_jahia_ctn_entries");
201                 int pageID = rs.getInt ("pageid_jahia_ctn_entries");
202                 int listID = rs.getInt ("listid_jahia_ctn_entries");
203                 int ctnDefID = rs.getInt ("ctndefid_jahia_ctn_entries");
204                 int rank = rs.getInt ("rank_jahia_ctn_entries");
205                 int rights = rs.getInt ("rights_jahia_ctn_entries");
206                 int versionID = rs.getInt ("version_id");
207                 int workflowState = rs.getInt ("workflow_state");
208
209                 resultEntryStates.add (new ContentObjectEntryState (
210                         workflowState, versionID, ContentObject.SHARED_LANGUAGE));
211
212                 // then we're only interested by the different version, active and lang values
213
// (the rest remains the same)
214
while (rs.next ()) {
215                     versionID = rs.getInt ("version_id");
216                     workflowState = rs.getInt ("workflow_state");
217                     resultEntryStates.add (new ContentObjectEntryState (
218                             workflowState, versionID, ContentObject.SHARED_LANGUAGE));
219                 }
220
221             }
222
223         } catch (SQLException se) {
224             throw new JahiaException (
225                     "Cannot load container " + containerID + "from the database",
226                     se.getMessage (),
227                     JahiaException.DATABASE_ERROR,
228                     JahiaException.ERROR_SEVERITY, se);
229         } finally {
230             try {
231
232                 if (stmt != null) stmt.close ();
233             } catch (SQLException ex) {
234                 throw new JahiaException ("Cannot free resources",
235                         "Cannot free resources",
236                         JahiaException.DATABASE_ERROR,
237                         JahiaException.WARNING_SEVERITY, ex);
238             }
239         }
240
241         return resultEntryStates;
242     }
243
244     protected void deleteEntry (int containerID, EntryStateable entryState)
245             throws JahiaException {
246
247         Connection dbConn = null;
248         PreparedStatement stmt = null;
249         ResultSet rs = null;
250
251         try {
252             String JavaDoc sqlQuery =
253                     "DELETE FROM jahia_ctn_entries WHERE id_jahia_ctn_entries=?" +
254                     " AND workflow_state=? AND version_id=?";
255             dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
256             stmt = dbConn.prepareStatement (sqlQuery);
257             stmt.setInt (1, containerID);
258             stmt.setInt (2, entryState.getWorkflowState ());
259             stmt.setInt (3, entryState.getVersionID ());
260             int resultCount = stmt.executeUpdate ();
261             logger.debug (resultCount + " rows affected by delete of entry");
262         } catch (SQLException se) {
263             throw new JahiaException ("Error while deleting entry",
264                     se.getMessage (),
265                     JahiaException.DATABASE_ERROR,
266                     JahiaException.ERROR_SEVERITY, se);
267         } finally {
268             try {
269                 if (stmt != null)
270                     stmt.close ();
271             } catch (SQLException ex) {
272                 throw new JahiaException ("Cannot free resources",
273                         "Cannot free resources",
274                         JahiaException.DATABASE_ERROR,
275                         JahiaException.WARNING_SEVERITY, ex);
276             }
277         }
278     }
279
280     /**
281      * Warning : this method assumes that the destination entry state DOES NOT
282      * exist in the database. Make sure you do this check before calling this
283      * method !
284      *
285      * @param containerID the container for which to perform the copy of
286      * entries
287      * @param fromEntryState the source entry state
288      * @param toEntryState the destination entry state (MUST NOT ALREADY EXIST
289      * IN DATABASE ! an Insert operation is performed here !)
290      *
291      * @throws JahiaException raised if there was an error copying the entry
292      * states, notably if the destination entry state already existed in the
293      * database and primary keys were in the database.
294      */

295     protected void copyEntry (int containerID,
296                               EntryStateable fromEntryState,
297                               EntryStateable toEntryState)
298             throws JahiaException {
299
300         Connection dbConn = null;
301         PreparedStatement stmt = null;
302
303         try {
304             String JavaDoc sqlQuery =
305                     "SELECT jahiaid_jahia_ctn_entries, " +
306                     "pageid_jahia_ctn_entries, " +
307                     "listid_jahia_ctn_entries, " +
308                     "ctndefid_jahia_ctn_entries, " +
309                     "rank_jahia_ctn_entries," +
310                     "rights_jahia_ctn_entries " +
311                     "FROM jahia_ctn_entries WHERE id_jahia_ctn_entries=?" +
312                     " AND workflow_state=? AND version_id=?";
313             dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
314             stmt = dbConn.prepareStatement (sqlQuery);
315             stmt.setInt (1, containerID);
316             stmt.setInt (2, fromEntryState.getWorkflowState ());
317             stmt.setInt (3, fromEntryState.getVersionID ());
318             ResultSet rs = stmt.executeQuery ();
319             if (rs.next ()) {
320                 int jahiaID = rs.getInt ("jahiaid_jahia_ctn_entries");
321                 int pageID = rs.getInt ("pageid_jahia_ctn_entries");
322                 int listID = rs.getInt ("listid_jahia_ctn_entries");
323                 int ctnDefID = rs.getInt ("ctndefid_jahia_ctn_entries");
324                 int rank = rs.getInt ("rank_jahia_ctn_entries");
325                 int rights = rs.getInt ("rights_jahia_ctn_entries");
326                 stmt.close ();
327                 String JavaDoc insertQuery = "INSERT INTO jahia_ctn_entries (" +
328                         "id_jahia_ctn_entries, " +
329                         "jahiaid_jahia_ctn_entries, " +
330                         "pageid_jahia_ctn_entries, " +
331                         "listid_jahia_ctn_entries, " +
332                         "ctndefid_jahia_ctn_entries, " +
333                         "rank_jahia_ctn_entries, " +
334                         "rights_jahia_ctn_entries, " +
335                         "version_id, " +
336                         "workflow_state" +
337                         ") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
338                 stmt = dbConn.prepareStatement (insertQuery);
339                 stmt.setInt (1, containerID);
340                 stmt.setInt (2, jahiaID);
341                 stmt.setInt (3, pageID);
342                 stmt.setInt (4, listID);
343                 stmt.setInt (5, ctnDefID);
344                 stmt.setInt (6, rank);
345                 stmt.setInt (7, rights);
346                 stmt.setInt (8, toEntryState.getVersionID ());
347                 stmt.setInt (9, toEntryState.getWorkflowState ());
348                 stmt.executeUpdate ();
349             }
350         } catch (SQLException se) {
351             throw new JahiaException ("Error while copying entry",
352                     se.getMessage (),
353                     JahiaException.DATABASE_ERROR,
354                     JahiaException.ERROR_SEVERITY, se);
355         } finally {
356             try {
357                 if (stmt != null)
358                     stmt.close ();
359             } catch (SQLException ex) {
360                 throw new JahiaException ("Cannot free resources",
361                         "Cannot free resources",
362                         JahiaException.DATABASE_ERROR,
363                         JahiaException.WARNING_SEVERITY, ex);
364             }
365         }
366     }
367
368 }
369
Popular Tags