KickJava   Java API By Example, From Geeks To Geeks.

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


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

36 class ContentContainerListDB {
37
38     private static org.apache.log4j.Logger logger =
39             org.apache.log4j.Logger.getLogger (ContentContainerListDB.class);
40
41     private static ContentContainerListDB singletonInstance = null;
42
43     /**
44      * Return an instance of this class
45      */

46     public static synchronized ContentContainerListDB getInstance () {
47         if (singletonInstance == null) {
48             singletonInstance = new ContentContainerListDB ();
49         }
50         return singletonInstance;
51     }
52
53 /////////////// BELOW ARE DATABASE ACCESS RELATED METHODS /////////////////
54

55     protected ContentContainerList getContainerList (int containerListID)
56             throws JahiaException {
57         ContentContainerList resultContainerList = null;
58         Connection JavaDoc dbConn = null;
59         PreparedStatement JavaDoc stmt = null;
60         ResultSet JavaDoc rs = null;
61
62         final String JavaDoc selectFields = "parententryid_jahia_ctn_lists,pageid_jahia_ctn_lists,ctndefid_jahia_ctn_lists,rights_jahia_ctn_lists,version_id,workflow_state";
63
64         try {
65             // We first try to get all ACTIVE & STAGED fields, but this query
66
// won't give anything if the Field doesn't exist anymore (versioning)
67
String JavaDoc sqlQuery = "SELECT " +
68                     selectFields +
69                     " FROM jahia_ctn_lists WHERE id_jahia_ctn_lists=? AND workflow_state>=1";
70
71             dbConn = ConnectionDispenser.getConnection ();
72             stmt = dbConn.prepareStatement(sqlQuery);
73             stmt.setInt(1, containerListID);
74             // rs can't be null (cf. api)
75
rs = stmt.executeQuery ();
76
77             // is there at least one element in the resultset ?
78
if (rs.next ()) {
79                 // yes, we get first value
80
int parentContainerID = rs.getInt ("parententryid_jahia_ctn_lists");
81                 int pageID = rs.getInt ("pageid_jahia_ctn_lists");
82                 int ctnDefID = rs.getInt ("ctndefid_jahia_ctn_lists");
83                 int rights = rs.getInt ("rights_jahia_ctn_lists");
84                 int versionID = rs.getInt ("version_id");
85                 int workflowState = rs.getInt ("workflow_state");
86
87                 ArrayList JavaDoc activeAndStagedVersions = new ArrayList JavaDoc ();
88                 activeAndStagedVersions.add (
89                         new ContentObjectEntryState (workflowState, versionID,
90                                 ContentObject.SHARED_LANGUAGE));
91
92                 // then we're only interested by the different version, active and lang values
93
// (the rest remains the same)
94
while (rs.next ()) {
95                     versionID = rs.getInt ("version_id");
96                     workflowState = rs.getInt ("workflow_state");
97                     activeAndStagedVersions.add (
98                             new ContentObjectEntryState (workflowState, versionID,
99                                     ContentObject.SHARED_LANGUAGE));
100                 }
101
102                 resultContainerList =
103                         new ContentContainerList (
104                                 containerListID, parentContainerID, pageID, ctnDefID, rights,
105                                 activeAndStagedVersions);
106             } else
107             // no element found in resultset, it means that the field is deleted (but maybe exist in
108
// an inactive version!) or it doesn't exist at all (trigers an exception then)
109
{
110                 stmt.close ();
111                 // we try loading an old version
112
sqlQuery = "SELECT " +
113                         selectFields +
114                         " FROM jahia_ctn_lists WHERE id_jahia_ctn_lists=?";
115                 stmt = dbConn.prepareStatement(sqlQuery);
116                 stmt.setInt(1, containerListID);
117                 rs = stmt.executeQuery ();
118
119                 // yes an inactive field does exist, we create the ContentField
120
if (rs.next ()) {
121                     int parentContainerID = rs.getInt ("parententryid_jahia_ctn_lists");
122                     int pageID = rs.getInt ("pageid_jahia_ctn_lists");
123                     int ctnDefID = rs.getInt ("ctndefid_jahia_ctn_lists");
124                     int rights = rs.getInt ("rights_jahia_ctn_lists");
125                     resultContainerList =
126                             new ContentContainerList (
127                                     containerListID, parentContainerID, pageID, ctnDefID,
128                                     rights, new ArrayList JavaDoc ());
129
130                 } else {
131                     // oops the field doesn't exist at all, let's trigger an exception!
132
throw new JahiaException
133                             ("Container list " + containerListID + " not found in database!",
134                                     "Container list " + containerListID + " not found in database!",
135                                     JahiaException.DATABASE_ERROR,
136                                     JahiaException.ERROR_SEVERITY);
137                 }
138             }
139
140         } catch (SQLException JavaDoc se) {
141             throw new JahiaException (
142                     "Cannot load container list " + containerListID + "from the database",
143                     se.getMessage (),
144                     JahiaException.DATABASE_ERROR,
145                     JahiaException.ERROR_SEVERITY, se);
146         } finally {
147             try {
148                 if (stmt != null)
149                     stmt.close ();
150
151             } catch (SQLException JavaDoc ex) {
152                 throw new JahiaException ("Cannot free resources",
153                         "Cannot free resources",
154                         JahiaException.DATABASE_ERROR,
155                         JahiaException.WARNING_SEVERITY, ex);
156             }
157         }
158
159         return resultContainerList;
160     }
161
162     protected ArrayList JavaDoc getVersionedEntryStates (int containerListID, boolean withActive)
163             throws JahiaException {
164         ArrayList JavaDoc resultEntryStates = new ArrayList JavaDoc ();
165         Connection JavaDoc dbConn = null;
166         PreparedStatement JavaDoc stmt = null;
167         ResultSet JavaDoc rs = null;
168
169         final String JavaDoc selectFields = "version_id,workflow_state";
170
171         try {
172             // We first try to get all ACTIVE & STAGED fields, but this query
173
// won't give anything if the Field doesn't exist anymore (versioning)
174
String JavaDoc sqlQuery = "SELECT " + selectFields +
175                     " FROM jahia_ctn_lists WHERE id_jahia_ctn_lists=? AND workflow_state<=?";
176             
177             dbConn = ConnectionDispenser.getConnection ();
178             stmt = dbConn.prepareStatement (sqlQuery);
179             stmt.setInt(1, containerListID);
180             stmt.setInt(2, withActive ? 1 : 0);
181             // rs can't be null (cf. api)
182
rs = stmt.executeQuery ();
183
184             // is there at least one element in the resultset ?
185
if (rs.next ()) {
186                 // yes, we get first value
187
int versionID = rs.getInt ("version_id");
188                 int workflowState = rs.getInt ("workflow_state");
189
190                 resultEntryStates.add (
191                         new ContentObjectEntryState (workflowState, versionID,
192                                 ContentObject.SHARED_LANGUAGE));
193
194                 // then we're only interested by the different version, active and lang values
195
// (the rest remains the same)
196
while (rs.next ()) {
197                     versionID = rs.getInt ("version_id");
198                     workflowState = rs.getInt ("workflow_state");
199                     resultEntryStates.add (
200                             new ContentObjectEntryState (workflowState, versionID,
201                                     ContentObject.SHARED_LANGUAGE));
202                 }
203
204             }
205         } catch (SQLException JavaDoc se) {
206             throw new JahiaException (
207                     "Cannot load container list " + containerListID + "from the database",
208                     se.getMessage (),
209                     JahiaException.DATABASE_ERROR,
210                     JahiaException.ERROR_SEVERITY, se);
211         } finally {
212             try {
213
214                 if (stmt != null) stmt.close ();
215             } catch (SQLException JavaDoc ex) {
216                 throw new JahiaException ("Cannot free resources",
217                         "Cannot free resources",
218                         JahiaException.DATABASE_ERROR,
219                         JahiaException.WARNING_SEVERITY, ex);
220             }
221         }
222
223         return resultEntryStates;
224     }
225
226     protected void deleteEntry (int containerListID, EntryStateable entryState)
227             throws JahiaException {
228
229         Connection JavaDoc dbConn = null;
230         PreparedStatement JavaDoc stmt = null;
231
232         try {
233             String JavaDoc sqlQuery =
234                     "DELETE FROM jahia_ctn_lists WHERE id_jahia_ctn_lists=? AND workflow_state=? AND version_id=?";
235             dbConn = ConnectionDispenser.getConnection ();
236             stmt = dbConn.prepareStatement (sqlQuery);
237             stmt.setInt (1, containerListID);
238             stmt.setInt (2, entryState.getWorkflowState ());
239             stmt.setInt (3, entryState.getVersionID ());
240             int resultCount = stmt.executeUpdate ();
241             logger.debug (resultCount + " rows affected by delete of entry");
242
243         } catch (SQLException JavaDoc se) {
244             throw new JahiaException ("Error while deleting entry",
245                     se.getMessage (),
246                     JahiaException.DATABASE_ERROR,
247                     JahiaException.ERROR_SEVERITY, se);
248
249         } finally {
250             try {
251                 if (stmt != null)
252                     stmt.close ();
253
254             } catch (SQLException JavaDoc ex) {
255                 throw new JahiaException ("Cannot free resources",
256                         "Cannot free resources",
257                         JahiaException.DATABASE_ERROR,
258                         JahiaException.WARNING_SEVERITY, ex);
259             }
260         }
261     }
262
263     /**
264      * Warning : this method assumes that the destination entry state DOES NOT
265      * exist in the database. Make sure you do this check before calling this
266      * method !
267      *
268      * @param containerListID the container list for which to perform the copy
269      * of entries
270      * @param fromEntryState the source entry state
271      * @param toEntryState the destination entry state (MUST NOT ALREADY EXIST
272      * IN DATABASE ! an Insert operation is performed here !)
273      *
274      * @throws JahiaException raised if there was an error copying the entry
275      * states, notably if the destination entry state already existed in the
276      * database and primary keys were in the database.
277      */

278     protected void copyEntry (int containerListID,
279                               EntryStateable fromEntryState,
280                               EntryStateable toEntryState)
281             throws JahiaException {
282
283         Connection JavaDoc dbConn = null;
284         PreparedStatement JavaDoc stmt = null;
285
286         try {
287             String JavaDoc sqlQuery = "SELECT parententryid_jahia_ctn_lists, pageid_jahia_ctn_lists, ctndefid_jahia_ctn_lists, rights_jahia_ctn_lists FROM jahia_ctn_lists WHERE id_jahia_ctn_lists=? AND workflow_state=? AND version_id=?";
288             dbConn = ConnectionDispenser.getConnection ();
289             stmt = dbConn.prepareStatement (sqlQuery);
290             stmt.setInt (1, containerListID);
291             stmt.setInt (2, fromEntryState.getWorkflowState ());
292             stmt.setInt (3, fromEntryState.getVersionID ());
293             ResultSet JavaDoc rs = stmt.executeQuery ();
294             if (rs.next ()) {
295                 int parentEntryID = rs.getInt ("parententryid_jahia_ctn_lists");
296                 int pageID = rs.getInt ("pageid_jahia_ctn_lists");
297                 int ctnDefID = rs.getInt ("ctndefid_jahia_ctn_lists");
298                 int rights = rs.getInt ("rights_jahia_ctn_lists");
299                 stmt.close ();
300
301                 String JavaDoc insertQuery = "INSERT INTO jahia_ctn_lists (id_jahia_ctn_lists, parententryid_jahia_ctn_lists, pageid_jahia_ctn_lists, ctndefid_jahia_ctn_lists, rights_jahia_ctn_lists, version_id, workflow_state) VALUES (?, ?, ?, ?, ?, ?, ?)";
302                 stmt = dbConn.prepareStatement (insertQuery);
303                 stmt.setInt (1, containerListID);
304                 stmt.setInt (2, parentEntryID);
305                 stmt.setInt (3, pageID);
306                 stmt.setInt (4, ctnDefID);
307                 stmt.setInt (5, rights);
308                 stmt.setInt (6, toEntryState.getVersionID ());
309                 stmt.setInt (7, toEntryState.getWorkflowState ());
310                 stmt.executeUpdate ();
311             }
312         } catch (SQLException JavaDoc se) {
313             logger.debug ("Error copying entry for ctnlist["
314                     + containerListID + "]\n From entry :"
315                     + fromEntryState.toString ()
316                     + "\n To entry : " + toEntryState.toString ());
317             throw new JahiaException ("Error while copying entry",
318                     se.getMessage (),
319                     JahiaException.DATABASE_ERROR,
320                     JahiaException.ERROR_SEVERITY, se);
321         } finally {
322             try {
323                 if (stmt != null)
324                     stmt.close ();
325             } catch (SQLException JavaDoc ex) {
326                 throw new JahiaException ("Cannot free resources",
327                         "Cannot free resources",
328                         JahiaException.DATABASE_ERROR,
329                         JahiaException.WARNING_SEVERITY, ex);
330             }
331         }
332     }
333
334 }
Popular Tags