KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > services > categories > CategoryDB


1 package org.jahia.services.categories;
2
3 import org.jahia.exceptions.JahiaException;
4 import org.jahia.registries.ServicesRegistry;
5 import org.jahia.services.acl.JahiaBaseACL;
6
7 import java.sql.Connection JavaDoc;
8 import java.sql.PreparedStatement JavaDoc;
9 import java.sql.ResultSet JavaDoc;
10 import java.sql.SQLException JavaDoc;
11
12 /**
13  * <p>Title: Category database persistence system.</p>
14  * <p>Description: This class handles the retrieval, updating, and removal
15  * of CategoryBean objects to/from the database</p>
16  * <p>Copyright: Copyright (c) 2002</p>
17  * <p>Company: Jahia Ltd</p>
18  *
19  * @author Serge Huber
20  * @version 1.0
21  */

22
23 class CategoryDB {
24     private CategoryDB () {
25     }
26
27     private static org.apache.log4j.Logger logger =
28             org.apache.log4j.Logger.getLogger (CategoryDB.class);
29
30     private static CategoryDB singletonInstance = null;
31
32     static private final String JavaDoc GET_CATEGORY_BYID_QUERY = "SELECT key_category, aclid_category FROM jahia_category WHERE id_category=?";
33     static private final String JavaDoc GET_CATEGORY_BYKEY_QUERY = "SELECT id_category, aclid_category FROM jahia_category WHERE key_category=?";
34     static private final String JavaDoc UPDATE_CATEGORY_BYID_QUERY = "UPDATE jahia_category SET key_category=?, aclid_category=? WHERE id_category=?";
35     static private final String JavaDoc INSERT_CATEGORY_QUERY = "INSERT INTO jahia_category (id_category, key_category, aclid_category) VALUES (?, ?, ?)";
36     static private final String JavaDoc REMOVE_CATEGORY_BYID_QUERY = "DELETE FROM jahia_category WHERE id_category=?";
37
38     /**
39      * @return an instance of this class
40      */

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

50     protected CategoryBean getCategory (int categoryID)
51             throws JahiaException {
52
53         CategoryBean category = null;
54         Connection JavaDoc dbConn = null;
55         PreparedStatement JavaDoc stmt = null;
56         ResultSet JavaDoc rs = null;
57
58         try {
59
60             dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
61             stmt = dbConn.prepareStatement (GET_CATEGORY_BYID_QUERY);
62             stmt.setInt (1, categoryID);
63             rs = stmt.executeQuery ();
64
65             // is there at least one element in the resultset ?
66
if (rs.next ()) {
67                 // yes, we get first value
68
String JavaDoc categoryKey = rs.getString ("key_category");
69                 int categoryAclID = rs.getInt ("aclid_category");
70
71                 category = new CategoryBean (categoryID, categoryKey, categoryAclID);
72             }
73
74         } catch (SQLException JavaDoc se) {
75             throw new JahiaException ("Cannot load category " + categoryID +
76                     "from the database",
77                     se.getMessage (),
78                     JahiaException.DATABASE_ERROR,
79                     JahiaException.ERROR_SEVERITY, se);
80         } finally {
81             try {
82                 if (stmt != null)
83                     stmt.close ();
84             } catch (SQLException JavaDoc ex) {
85                 throw new JahiaException ("Cannot free resources",
86                         "Cannot free resources",
87                         JahiaException.DATABASE_ERROR,
88                         JahiaException.WARNING_SEVERITY, ex);
89             }
90         }
91
92         return category;
93     }
94
95     protected CategoryBean findCategoryByKey (String JavaDoc categoryKey)
96             throws JahiaException {
97
98         CategoryBean category = null;
99         Connection JavaDoc dbConn = null;
100         PreparedStatement JavaDoc stmt = null;
101         ResultSet JavaDoc rs = null;
102
103         try {
104
105             dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
106             stmt = dbConn.prepareStatement (GET_CATEGORY_BYKEY_QUERY);
107             stmt.setString (1, categoryKey);
108             rs = stmt.executeQuery ();
109
110             // is there at least one element in the resultset ?
111
if (rs.next ()) {
112                 // yes, we get first value
113
int categoryID = rs.getInt ("id_category");
114                 int categoryAclID = rs.getInt ("aclid_category");
115
116                 category = new CategoryBean (categoryID, categoryKey, categoryAclID);
117             }
118
119         } catch (SQLException JavaDoc se) {
120             throw new JahiaException ("Cannot load category " + categoryKey +
121                     "from the database",
122                     se.getMessage (),
123                     JahiaException.DATABASE_ERROR,
124                     JahiaException.ERROR_SEVERITY, se);
125         } finally {
126             try {
127                 if (stmt != null)
128                     stmt.close ();
129             } catch (SQLException JavaDoc ex) {
130                 throw new JahiaException ("Cannot free resources",
131                         "Cannot free resources",
132                         JahiaException.DATABASE_ERROR,
133                         JahiaException.WARNING_SEVERITY, ex);
134             }
135         }
136
137         return category;
138     }
139
140     protected void createCategory (CategoryBean category)
141             throws JahiaException {
142         Connection JavaDoc dbConn = null;
143         PreparedStatement JavaDoc stmt = null;
144         ResultSet JavaDoc rs = null;
145
146         int categoryID = ServicesRegistry.getInstance ().getJahiaIncrementorsDBService ()
147                 .autoIncrement ("jahia_category");
148         category.setId (categoryID);
149
150         // now we must also create the ACL
151
JahiaBaseACL newAcl = new JahiaBaseACL ();
152         // the category ACLs are root ACLs.
153
newAcl.create (0);
154         category.setAclID (newAcl.getID ());
155
156         try {
157             dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
158             stmt = dbConn.prepareStatement (INSERT_CATEGORY_QUERY);
159             stmt.setInt (1, category.getId ());
160             stmt.setString (2, category.getKey ());
161             stmt.setInt (3, category.getAclID ());
162             stmt.executeUpdate ();
163         } catch (SQLException JavaDoc se) {
164             throw new JahiaException ("Cannot store category in the database",
165                     se.getMessage (),
166                     JahiaException.DATABASE_ERROR,
167                     JahiaException.ERROR_SEVERITY, se);
168         } finally {
169             try {
170                 if (stmt != null)
171                     stmt.close ();
172             } catch (SQLException JavaDoc ex) {
173                 throw new JahiaException ("Cannot free resources",
174                         "Cannot free resources",
175                         JahiaException.DATABASE_ERROR,
176                         JahiaException.WARNING_SEVERITY, ex);
177             }
178         }
179     }
180
181     protected void updateCategory (CategoryBean category)
182             throws JahiaException {
183         Connection JavaDoc dbConn = null;
184         PreparedStatement JavaDoc stmt = null;
185         ResultSet JavaDoc rs = null;
186
187         try {
188             dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
189             stmt = dbConn.prepareStatement (UPDATE_CATEGORY_BYID_QUERY);
190             stmt.setString (1, category.getKey ());
191             stmt.setInt (2, category.getAclID ());
192             stmt.setInt (3, category.getId ());
193             stmt.executeUpdate ();
194         } catch (SQLException JavaDoc se) {
195             throw new JahiaException (
196                     "Cannot update category " + category.getId () + " in the database",
197                     se.getMessage (),
198                     JahiaException.DATABASE_ERROR,
199                     JahiaException.ERROR_SEVERITY, se);
200         } finally {
201             try {
202                 if (stmt != null)
203                     stmt.close ();
204             } catch (SQLException JavaDoc ex) {
205                 throw new JahiaException ("Cannot free resources",
206                         "Cannot free resources",
207                         JahiaException.DATABASE_ERROR,
208                         JahiaException.WARNING_SEVERITY, ex);
209             }
210         }
211     }
212
213
214     protected void removeCategory (CategoryBean category)
215             throws JahiaException {
216
217         Connection JavaDoc dbConn = null;
218         PreparedStatement JavaDoc stmt = null;
219         ResultSet JavaDoc rs = null;
220
221         try {
222             dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
223             stmt = dbConn.prepareStatement (REMOVE_CATEGORY_BYID_QUERY);
224             stmt.setInt (1, category.getId ());
225             stmt.executeUpdate ();
226
227             JahiaBaseACL categoryACL = new JahiaBaseACL (category.getAclID ());
228             categoryACL.delete ();
229         } catch (SQLException JavaDoc se) {
230             throw new JahiaException (
231                     "Cannot remove category " + category.getId () + " from database",
232                     se.getMessage (),
233                     JahiaException.DATABASE_ERROR,
234                     JahiaException.ERROR_SEVERITY, se);
235         } finally {
236             try {
237                 if (stmt != null)
238                     stmt.close ();
239             } catch (SQLException JavaDoc ex) {
240                 throw new JahiaException ("Cannot free resources",
241                         "Cannot free resources",
242                         JahiaException.DATABASE_ERROR,
243                         JahiaException.WARNING_SEVERITY, ex);
244             }
245         }
246     }
247
248 }
Popular Tags