KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > resourcebundle > DatabaseResourcesDB


1 package org.jahia.resourcebundle;
2
3 import org.jahia.exceptions.JahiaException;
4 import java.sql.Connection JavaDoc;
5 import java.sql.PreparedStatement JavaDoc;
6 import java.sql.ResultSet JavaDoc;
7 import java.sql.SQLException JavaDoc;
8 import java.util.ArrayList JavaDoc;
9
10 /**
11  * <p>Title: Database persistance management class for resources stored in
12  * the Jahia database </p>
13  * <p>Description: </p>
14  * <p>Copyright: Copyright (c) 2002</p>
15  * <p>Company: Jahia Ltd</p>
16  * @author Serge Huber
17  * @version 1.0
18  */

19
20 public class DatabaseResourcesDB {
21
22     private DatabaseResourcesDB () {
23     }
24
25     private static org.apache.log4j.Logger logger =
26         org.apache.log4j.Logger.getLogger(DatabaseResourcesDB.class);
27
28     private static DatabaseResourcesDB singletonInstance = null;
29
30     static private final String JavaDoc GET_RESOURCE_BYNAMEANDLANGUAGE_QUERY = "SELECT value_resource FROM jahia_resources WHERE name_resource=? AND languagecode_resource=?";
31     static private final String JavaDoc GET_RESOURCE_BYNAME_QUERY = "SELECT value_resource, languagecode_resource FROM jahia_resources WHERE name_resource=?";
32     static private final String JavaDoc UPDATE_RESOURCE_BYNAMEANDLANGUAGE_QUERY = "UPDATE jahia_resources SET value_resource=? WHERE name_resource=? AND languagecode_resource=?";
33     static private final String JavaDoc INSERT_RESOURCE_QUERY = "INSERT INTO jahia_resources (name_resource, value_resource, languagecode_resource) VALUES (?, ?, ?)";
34     static private final String JavaDoc REMOVE_RESOURCE_BYNAMEANDLANGUAGE_QUERY =
35         "DELETE FROM jahia_resources WHERE name_resource=? AND languagecode_resource=?";
36
37     /**
38      * @return an instance of this class
39      */

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

49     /**
50      * Retrieves a resource by it's name and it's language
51      * @param name the name of the resource
52      * @param languageCode the language for which to retrieve the resource
53      * @return a bean containing the full resource data
54      * @throws JahiaException if there was a problem communicating with the
55      * database
56      */

57     public DatabaseResourceBean getDatabaseResource (String JavaDoc name,
58         String JavaDoc languageCode)
59         throws JahiaException {
60
61         DatabaseResourceBean databaseResource = null;
62         Connection JavaDoc dbConn = null;
63         PreparedStatement JavaDoc stmt = null;
64         ResultSet JavaDoc rs = null;
65
66         try {
67
68             dbConn = org.jahia.services.database.ConnectionDispenser.
69                      getConnection();
70             stmt = dbConn.prepareStatement(GET_RESOURCE_BYNAMEANDLANGUAGE_QUERY);
71             stmt.setString(1, name);
72             stmt.setString(2, languageCode);
73             rs = stmt.executeQuery();
74
75             // is there at least one element in the resultset ?
76
if (rs.next()) {
77                 // yes, we get first value
78
String JavaDoc value = rs.getString("value_resource");
79
80                 databaseResource = new DatabaseResourceBean(name, value,
81                     languageCode);
82             }
83
84         } catch (SQLException JavaDoc se) {
85             throw new JahiaException("Cannot load resource " + name +
86                                      " in language " + languageCode +
87                                      " from the database",
88                                      se.getMessage(),
89                                      JahiaException.DATABASE_ERROR,
90                                      JahiaException.ERROR_SEVERITY, se);
91         } finally {
92             try {
93                 if (stmt != null)
94                     stmt.close();
95             } catch (SQLException JavaDoc ex) {
96                 throw new JahiaException("Cannot free resources",
97                                          "Cannot free resources",
98                                          JahiaException.DATABASE_ERROR,
99                                          JahiaException.WARNING_SEVERITY, ex);
100             }
101         }
102
103         return databaseResource;
104     }
105
106     /**
107      * Retrieves all languages for a given resource name.
108      * @param name the name for which to retrieve all the language beans
109      * @return an ArrayList of DatabaseResourceBean objects that contain all
110      * the different values for the given resource name
111      * @throws JahiaException if there was a problem communicating with the
112      * database
113      */

114     public ArrayList JavaDoc getAllDatabaseResource (String JavaDoc name)
115         throws JahiaException {
116
117         DatabaseResourceBean databaseResource = null;
118         Connection JavaDoc dbConn = null;
119         PreparedStatement JavaDoc stmt = null;
120         ResultSet JavaDoc rs = null;
121         ArrayList JavaDoc allResourceEntries = new ArrayList JavaDoc();
122
123         try {
124
125             dbConn = org.jahia.services.database.ConnectionDispenser.
126                      getConnection();
127             stmt = dbConn.prepareStatement(GET_RESOURCE_BYNAME_QUERY);
128             stmt.setString(1, name);
129             rs = stmt.executeQuery();
130
131             // is there at least one element in the resultset ?
132
while (rs.next()) {
133                 // yes, we get first value
134
String JavaDoc value = rs.getString("value_resource");
135                 String JavaDoc languageCode = rs.getString("languagecode_resource");
136
137                 databaseResource = new DatabaseResourceBean(name, value,
138                     languageCode);
139                 allResourceEntries.add(databaseResource);
140             }
141
142         } catch (SQLException JavaDoc se) {
143             throw new JahiaException("Cannot load all resources for key " +
144                                      name +
145                                      " from the database",
146                                      se.getMessage(),
147                                      JahiaException.DATABASE_ERROR,
148                                      JahiaException.ERROR_SEVERITY, se);
149         } finally {
150             try {
151                 if (stmt != null)
152                     stmt.close();
153             } catch (SQLException JavaDoc 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 allResourceEntries;
162     }
163
164     /**
165      * Creates a new resource in the database, for a given language code.
166      * @param databaseResource the database resource properly initialized to be
167      * stored in the database. It must contain a name, a value and a language
168      * code
169      * @throws JahiaException if there was a problem communicating with the
170      * database
171      */

172     public void createDatabaseResource (DatabaseResourceBean databaseResource)
173         throws JahiaException {
174         Connection JavaDoc dbConn = null;
175         PreparedStatement JavaDoc stmt = null;
176         ResultSet JavaDoc rs = null;
177
178         try {
179             dbConn = org.jahia.services.database.ConnectionDispenser.
180                      getConnection();
181             stmt = dbConn.prepareStatement(INSERT_RESOURCE_QUERY);
182             stmt.setString(1, databaseResource.getName());
183             stmt.setString(2, databaseResource.getValue());
184             stmt.setString(3, databaseResource.getLanguageCode());
185             stmt.executeUpdate();
186         } catch (SQLException JavaDoc se) {
187             throw new JahiaException("Cannot store resource " +
188                                      databaseResource.getName() +
189                                      " for language " +
190                                      databaseResource.getLanguageCode() +
191                                      " in the database",
192                                      se.getMessage(),
193                                      JahiaException.DATABASE_ERROR,
194                                      JahiaException.ERROR_SEVERITY, se);
195         } finally {
196             try {
197                 if (stmt != null)
198                     stmt.close();
199             } catch (SQLException JavaDoc ex) {
200                 throw new JahiaException("Cannot free resources",
201                                          "Cannot free resources",
202                                          JahiaException.DATABASE_ERROR,
203                                          JahiaException.WARNING_SEVERITY, ex);
204             }
205         }
206     }
207
208     /**
209      * Modifies the value of a resource specified by it's name and language
210      * code
211      * @param databaseResource the bean containing all the data for updating
212      * the value by it's name and language code.
213      * @throws JahiaException if there was a problem communicating with the
214      * database
215      */

216     public void updateDatabaseResource (DatabaseResourceBean databaseResource)
217         throws JahiaException {
218         Connection JavaDoc dbConn = null;
219         PreparedStatement JavaDoc stmt = null;
220         ResultSet JavaDoc rs = null;
221
222         try {
223             dbConn = org.jahia.services.database.ConnectionDispenser.
224                      getConnection();
225             stmt = dbConn.prepareStatement(
226                 UPDATE_RESOURCE_BYNAMEANDLANGUAGE_QUERY);
227             stmt.setString(1, databaseResource.getValue());
228             stmt.setString(2, databaseResource.getName());
229             stmt.setString(3, databaseResource.getLanguageCode());
230             stmt.executeUpdate();
231         } catch (SQLException JavaDoc se) {
232             throw new JahiaException("Cannot update resource " +
233                                      databaseResource.getName() +
234                                      " for language " +
235                                      databaseResource.getLanguageCode() +
236                                      " in the database",
237                                      se.getMessage(),
238                                      JahiaException.DATABASE_ERROR,
239                                      JahiaException.ERROR_SEVERITY, se);
240         } finally {
241             try {
242                 if (stmt != null)
243                     stmt.close();
244             } catch (SQLException JavaDoc ex) {
245                 throw new JahiaException("Cannot free resources",
246                                          "Cannot free resources",
247                                          JahiaException.DATABASE_ERROR,
248                                          JahiaException.WARNING_SEVERITY, ex);
249             }
250         }
251     }
252
253     /**
254      * Removes a specific resource specified by it's name and language code
255      * from the database. This in effect removes a "value" for a specific
256      * language code for a resource "name".
257      * @param databaseResource the bean containing the name and language code
258      * (the value is not used) to remove from the database
259      * @throws JahiaException if there was a problem communicating with the
260      * database
261      */

262     public void removeDatabaseResource (DatabaseResourceBean databaseResource)
263         throws JahiaException {
264
265         Connection JavaDoc dbConn = null;
266         PreparedStatement JavaDoc stmt = null;
267         ResultSet JavaDoc rs = null;
268
269         try {
270             dbConn = org.jahia.services.database.ConnectionDispenser.
271                      getConnection();
272             stmt = dbConn.prepareStatement(
273                 REMOVE_RESOURCE_BYNAMEANDLANGUAGE_QUERY);
274             stmt.setString(1, databaseResource.getName());
275             stmt.setString(2, databaseResource.getLanguageCode());
276             stmt.executeUpdate();
277
278         } catch (SQLException JavaDoc se) {
279             throw new JahiaException("Cannot remove resource " +
280                                      databaseResource.getName() +
281                                      " for language " +
282                                      databaseResource.getLanguageCode() +
283                                      " from database",
284                                      se.getMessage(),
285                                      JahiaException.DATABASE_ERROR,
286                                      JahiaException.ERROR_SEVERITY, se);
287         } finally {
288             try {
289                 if (stmt != null)
290                     stmt.close();
291             } catch (SQLException JavaDoc ex) {
292                 throw new JahiaException("Cannot free resources",
293                                          "Cannot free resources",
294                                          JahiaException.DATABASE_ERROR,
295                                          JahiaException.WARNING_SEVERITY, ex);
296             }
297         }
298     }
299
300 }
Popular Tags