KickJava   Java API By Example, From Geeks To Geeks.

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


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 //
15
// db_load_container_structure( ctndefid )
16
// db_create_container_structure( theContainerDef )
17
// db_update_container_structure( theContainerDef )
18
// db_delete_container_structure( ctndefid )
19
//
20

21
22 package org.jahia.services.containers;
23
24 import org.jahia.data.JahiaDBDOMObject;
25 import org.jahia.data.JahiaDOMObject;
26 import org.jahia.data.containers.JahiaContainerStructure;
27 import org.jahia.data.containers.JahiaContainerSubDefinition;
28 import org.jahia.exceptions.JahiaException;
29 import org.jahia.services.database.ConnectionDispenser;
30
31 import java.sql.Connection JavaDoc;
32 import java.sql.PreparedStatement JavaDoc;
33 import java.sql.ResultSet JavaDoc;
34 import java.sql.SQLException JavaDoc;
35 import java.sql.Statement JavaDoc;
36 import java.util.Enumeration JavaDoc;
37 import java.util.Vector JavaDoc;
38
39
40 /**
41  * JahiaContainerStructuresDB
42  *
43  * @author Eric Vassalli
44  * Holds all the methods enabling container structures load
45  * @author update and delete.
46  */

47 public class JahiaContainerStructuresDB {
48
49     private static org.apache.log4j.Logger logger =
50             org.apache.log4j.Logger.getLogger (JahiaContainerStructuresDB.class);
51
52
53     /**
54      * constructor
55      */

56     public JahiaContainerStructuresDB () {
57     }
58
59
60     /**
61      * loads a container structure from its container definition id
62      *
63      * @param subDefID the sub definition ID
64      *
65      * @return returns a Vector of structures
66      *
67      * @throws throws a critical JahiaException if a data access occurs
68      * @throws throws a warning JahiaException if cannot free resources
69      */

70     public Vector JavaDoc db_load_container_structure (int subDefID)
71             throws JahiaException {
72         Connection JavaDoc dbConn = null;
73         PreparedStatement JavaDoc stmt = null;
74         ResultSet JavaDoc rs = null;
75         Vector JavaDoc theStructure = new Vector JavaDoc ();
76         try {
77             String JavaDoc sqlQuery = "SELECT * FROM jahia_ctn_struct WHERE ctnsubdefid_jahia_ctn_struct=? ORDER BY rank_jahia_ctn_struct ASC";
78
79             dbConn = ConnectionDispenser.getConnection ();
80             stmt = dbConn.prepareStatement (sqlQuery);
81             stmt.setInt(1, subDefID);
82             rs = stmt.executeQuery ();
83
84             while (rs.next ()) {
85                 int ctnsubdefid = rs.getInt ("ctnsubdefid_jahia_ctn_struct");
86                 int objTypeID = rs.getInt ("objtype_jahia_ctn_struct");
87                 int objDefID = rs.getInt ("objdefid_jahia_ctn_struct");
88                 int rank = rs.getInt ("rank_jahia_ctn_struct");
89                 theStructure.add (new JahiaContainerStructure (
90                         ctnsubdefid, objTypeID, objDefID, rank));
91             }
92
93         } catch (SQLException JavaDoc se) {
94             String JavaDoc errorMsg = "Error in db_load_container_structure : " + se.getMessage () + " -> BAILING OUT";
95             logger.error (errorMsg);
96             throw new JahiaException ("Cannot load structures from the database",
97                     errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL_SEVERITY,
98                     se);
99         } finally {
100             closeStatement(stmt);
101         }
102         if (theStructure.size () > 0) {
103             return theStructure;
104         } else {
105             return null;
106         }
107     } // end db_load_container_structure
108

109
110     /**
111      * creates a container structure from its container definition
112      *
113      * @param theDef the JahiaContainerDefinition object
114      *
115      * @see org.jahia.data.containers.JahiaContainerDefinition
116      */

117     public void db_create_container_structure (JahiaContainerSubDefinition theSubDef)
118             throws JahiaException {
119         Connection JavaDoc dbConn = null;
120         PreparedStatement JavaDoc stmt = null;
121         String JavaDoc sqlQuery =
122             "INSERT INTO jahia_ctn_struct (ctnsubdefid_jahia_ctn_struct, objtype_jahia_ctn_struct, objdefid_jahia_ctn_struct, rank_jahia_ctn_struct) VALUES(?,?,?,?)";
123
124         try {
125             // connects to the database
126
dbConn = ConnectionDispenser.getConnection ();
127
128             stmt = dbConn.prepareStatement (sqlQuery);
129
130             // inserts new structure
131
Vector JavaDoc structList = theSubDef.getStructure ();
132             if (structList != null) {
133                 Enumeration JavaDoc structure = structList.elements ();
134                 while (structure.hasMoreElements ()) {
135                     JahiaContainerStructure theStruct = (JahiaContainerStructure) structure.nextElement ();
136                     theStruct.setSubctndefid (theSubDef.getID ());
137                     stmt.setInt(1, theSubDef.getID ());
138                     stmt.setInt(2, theStruct.getObjectType ());
139                     stmt.setInt(3, theStruct.getObjectDefID ());
140                     stmt.setInt(4, theStruct.getRank ());
141                     stmt.executeUpdate ();
142                 }
143             }
144
145         } catch (SQLException JavaDoc se) {
146             String JavaDoc errorMsg = "Error in db_create_container_structure : " + se.getMessage () + " -> BAILING OUT";
147             logger.error (errorMsg);
148             throw new JahiaException ("Cannot create structures in the database",
149                     errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL_SEVERITY,
150                     se);
151         } finally {
152             closeStatement(stmt);
153         }
154
155     } // end db_create_container_structure
156

157
158     /**
159      * updates a container structure from its container definition
160      *
161      * @param theDef the JahiaContainerDefinition object
162      *
163      * @throws throws a critical JahiaException if a data access occurs
164      * @throws throws a warning JahiaException if cannot free resources
165      * @see org.jahia.data.containers.JahiaContainerDefinition
166      */

167     public void db_update_container_structure (JahiaContainerSubDefinition theSubDef)
168             throws JahiaException {
169         // clears old structure
170
db_delete_container_structure (theSubDef.getID ());
171         // inserts new structure
172
db_create_container_structure (theSubDef);
173     } // end db_update_container_structure
174

175
176     /**
177      * deletes a container structure from its container definition
178      *
179      * @param theDefID the JahiaContainerDefinition ID
180      *
181      * @throws throws a critical JahiaException if a data access occurs
182      * @throws throws a warning JahiaException if cannot free resources
183      * @see org.jahia.data.containers.JahiaContainerDefinition
184      */

185     public void db_delete_container_structure (int theSubDefID)
186             throws JahiaException {
187         Connection JavaDoc dbConn = null;
188         Statement JavaDoc stmt = null;
189         String JavaDoc sqlQuery = "";
190         try {
191             // connects to the database
192
dbConn = ConnectionDispenser.getConnection ();
193             stmt = dbConn.createStatement ();
194
195             sqlQuery = "DELETE FROM jahia_ctn_struct ";
196             sqlQuery += "WHERE ctnsubdefid_jahia_ctn_struct=" + theSubDefID;
197             stmt.executeUpdate (sqlQuery);
198
199         } catch (SQLException JavaDoc se) {
200             String JavaDoc errorMsg = "Error in db_delete_container_structure : " + se.getMessage () + " -> BAILING OUT";
201             logger.error (errorMsg);
202             throw new JahiaException ("Cannot delete structures in the database",
203                     errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL_SEVERITY,
204                     se);
205         } finally {
206             closeStatement(stmt);
207         }
208     }
209
210
211     /**
212      * return a DOM document of all container structures of a site
213      *
214      * @param int the site id
215      *
216      * @return JahiaDOMObject a DOM representation of this object
217      */

218     public JahiaDOMObject getContainerStructsAsDOM (int siteID)
219             throws JahiaException {
220
221         Connection JavaDoc dbConn = null;
222         Statement JavaDoc statement = null;
223         JahiaDBDOMObject dom = null;
224
225         try {
226
227             String JavaDoc sqlQuery = "SELECT DISTINCT jahia_ctn_struct.ctnsubdefid_jahia_ctn_struct,"
228                     + "jahia_ctn_struct.objtype_jahia_ctn_struct,jahia_ctn_struct.objdefid_jahia_ctn_struct,"
229                     + "jahia_ctn_struct.rank_jahia_ctn_struct FROM jahia_ctn_struct,jahia_fields_def,jahia_ctn_def WHERE "
230                     + "( jahia_ctn_struct.objtype_jahia_ctn_struct=1 AND jahia_ctn_struct.objdefid_jahia_ctn_struct="
231                     + "jahia_fields_def.id_jahia_fields_def AND jahia_fields_def.jahiaid_jahia_fields_def=" + siteID + " ) "
232                     + "OR ( jahia_ctn_struct.objtype_jahia_ctn_struct=2 AND jahia_ctn_struct.objdefid_jahia_ctn_struct="
233                     + "jahia_ctn_def.id_jahia_ctn_def AND jahia_ctn_def.jahiaid_jahia_ctn_def=" + siteID + " )";
234
235
236             dbConn = ConnectionDispenser.getConnection ();
237             statement = dbConn.createStatement ();
238             if (statement != null) {
239                 ResultSet JavaDoc rs = statement.executeQuery (sqlQuery);
240                 if (rs != null) {
241                     dom = new JahiaDBDOMObject ();
242                     dom.addTable ("jahia_ctn_struct", rs);
243                     return dom;
244                 }
245             }
246         } catch (SQLException JavaDoc se) {
247             String JavaDoc errorMsg = "Error in getContainerStructsAsDOM(int siteID) : " + se.getMessage ();
248             logger.error (errorMsg + " -> BAILING OUT");
249             throw new JahiaException ("Cannot load container structures from the database",
250                     errorMsg, JahiaException.DATABASE_ERROR,
251                     JahiaException.CRITICAL_SEVERITY);
252         } finally {
253             closeStatement (statement);
254         }
255
256         return dom;
257     }
258
259     private void closeStatement (Statement JavaDoc statement) {
260         // Close the opened statement
261
try {
262             if (statement != null) {
263                 statement.close ();
264             }
265
266         } catch (SQLException JavaDoc sqlEx) {
267             logger.error ("Cannot close a statement", sqlEx);
268         }
269     }
270
271
272 }
273
Popular Tags