KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > services > sites > SiteLanguageMappingPersistance


1 package org.jahia.services.sites;
2
3 import org.jahia.data.JahiaDBDOMObject;
4 import org.jahia.data.JahiaDOMObject;
5 import org.jahia.exceptions.JahiaException;
6 import org.jahia.registries.ServicesRegistry;
7 import org.jahia.utils.JahiaTools;
8
9 import java.sql.Connection JavaDoc;
10 import java.sql.ResultSet JavaDoc;
11 import java.sql.SQLException JavaDoc;
12 import java.sql.Statement JavaDoc;
13 import java.util.Vector JavaDoc;
14 import org.jahia.services.cache.Cache;
15 import org.jahia.services.cache.CacheFactory;
16
17 /**
18  * <p>Title: Site language mappings persistance service</p>
19  * <p>Description: This class offers a persistance service for site language
20  * mapping beans to and from a database. In effect it stores the mapping for
21  * language codes to other languages code defined in a per-site manner. </p>
22  * <p>Copyright: Copyright (c) 2002</p>
23  * <p>Company: </p>
24  *
25  * @author Serge Huber
26  * @version 1.0
27  */

28
29 public class SiteLanguageMappingPersistance {
30
31     private static org.apache.log4j.Logger logger =
32             org.apache.log4j.Logger.getLogger (SiteLanguageMappingPersistance.class);
33
34     /** unique instance */
35     private static SiteLanguageMappingPersistance m_Instance = null;
36
37     // the Site Language Mappings cache name.
38
public static final String JavaDoc SITE_LANGUAGEMAPPINGS_CACHE = "siteLanguageMappingsCache";
39
40     // the Language Mapping cache name.
41
public static final String JavaDoc LANGUAGEMAPPINGS_CACHE = "languageMappingsCache";
42     private Cache languageMappingCache = null;
43     private Cache siteLanguageMappingsCache = null;
44
45     /**
46      * Private constructor since we are using a singleton pattern.
47      */

48     private SiteLanguageMappingPersistance () {
49         try {
50             languageMappingCache = CacheFactory.createCache(LANGUAGEMAPPINGS_CACHE);
51             siteLanguageMappingsCache = CacheFactory.createCache(SITE_LANGUAGEMAPPINGS_CACHE);
52         } catch (JahiaException je) {
53             logger.error("Error while trying to create site language mappings cache", je);
54         }
55     }
56
57     /**
58      * Retrieve the singleton object for this persistance manager
59      *
60      * @return a SiteLanguagePersistance object that is a singleton.
61      */

62     public static synchronized SiteLanguageMappingPersistance getInstance () {
63
64         if (m_Instance == null) {
65             m_Instance = new SiteLanguageMappingPersistance ();
66         }
67         return m_Instance;
68     }
69
70     /**
71      * Retrieve from the database a list of language mappings for a given
72      * site.
73      *
74      * @param siteID the identifier of the site for which to retrieve the
75      * language mappings
76      *
77      * @return a Vector containing SiteLanguageMapping objects that are the
78      * list of language mappings for the specified site. The Vector might be
79      * empty if there are no settings for a site but it should never be null.
80      *
81      * @throws JahiaException if an error occured while retrieving the language
82      * settings for the site.
83      */

84     public Vector JavaDoc getSiteLanguageMappings (int siteID) throws JahiaException {
85
86         Connection JavaDoc dbConn = null;
87         Statement JavaDoc statement = null;
88
89         Vector JavaDoc siteLanguageMappings = new Vector JavaDoc ();
90         SiteLanguageMapping siteLanguageMapping = null;
91
92         if (siteLanguageMappingsCache.containsKey(new Integer JavaDoc(siteID))) {
93             return (Vector JavaDoc) siteLanguageMappingsCache.get(new Integer JavaDoc(siteID));
94         }
95
96         try {
97             String JavaDoc sqlQuery = "SELECT * FROM jahia_site_lang_maps WHERE site_id=" +
98                     siteID;
99
100             dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
101             statement = dbConn.createStatement ();
102             if (statement != null) {
103                 ResultSet JavaDoc rs = statement.executeQuery (sqlQuery);
104                 if (rs != null) {
105                     while (rs.next ()) {
106                         siteLanguageMapping = getSiteLanguageMappingFromResultSet (rs);
107                         siteLanguageMappings.add (siteLanguageMapping);
108                     }
109                 }
110                 siteLanguageMappingsCache.put(new Integer JavaDoc(siteID), siteLanguageMappings);
111             }
112
113         } catch (SQLException JavaDoc se) {
114             String JavaDoc errorMsg = "Error while retrieving site language mappings";
115             logger.debug ("Error while retrieving site language mappings", se);
116             throw new JahiaException ("Cannot load site language mappings from the database",
117                     errorMsg,
118                     JahiaException.DATABASE_ERROR,
119                     JahiaException.CRITICAL_SEVERITY,
120                     se);
121         } finally {
122
123             closeStatement (statement);
124         }
125
126         return siteLanguageMappings;
127     }
128
129
130     /**
131      * Retrieve a language mapping entry specified by it's identifier
132      *
133      * @param id identifier of the site language mapping entry in the database
134      *
135      * @return a SiteLanguageMapping object corresponding to the identifier
136      * specified or null if not found
137      *
138      * @throws JahiaException thrown if a database error occured while retrieving
139      * the language settings entry
140      */

141     public SiteLanguageMapping getSiteLanguageMapping (int id) throws JahiaException {
142
143         Connection JavaDoc dbConn = null;
144         Statement JavaDoc statement = null;
145
146         SiteLanguageMapping siteLangMapping = null;
147
148         if (languageMappingCache.containsKey(new Integer JavaDoc(id))) {
149             return (SiteLanguageMapping) languageMappingCache.get(new Integer JavaDoc(id));
150         }
151
152         try {
153             String JavaDoc sqlQuery = "SELECT * FROM jahia_site_lang_maps where id=" + id;
154
155             dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
156             statement = dbConn.createStatement ();
157             if (statement != null) {
158                 ResultSet JavaDoc rs = statement.executeQuery (sqlQuery);
159                 if (rs != null) {
160                     if (rs.next ()) {
161                         siteLangMapping = getSiteLanguageMappingFromResultSet (rs);
162                     }
163                     languageMappingCache.put(new Integer JavaDoc(id), siteLangMapping);
164                     siteLanguageMappingsCache.flush();
165                 }
166             }
167
168         } catch (SQLException JavaDoc se) {
169             String JavaDoc errorMsg = "Error while retrieving site language mapping " + id + " : " + se.getMessage ();
170             logger.debug (errorMsg, se);
171             throw new JahiaException ("Cannot load site language mapping from the database",
172                     errorMsg, JahiaException.DATABASE_ERROR,
173                     JahiaException.CRITICAL_SEVERITY,
174                     se);
175         } finally {
176
177             closeStatement (statement);
178         }
179
180         return siteLangMapping;
181     }
182
183
184     /**
185      * Insert a new language mapping entry in the database. This new entry has
186      * all it's values already defined except it's identifier, which is
187      * determined in this call. The passed object is modified (!) by this method
188      * to include the new ID.
189      *
190      * @param siteLangMapping the object to insert into the database, and
191      * whose ID to set.
192      *
193      * @throws JahiaException is thrown if an error occured while communicating
194      * with the database.
195      */

196     public void addSiteLanguageMapping (SiteLanguageMapping siteLangMapping)
197             throws JahiaException {
198
199         try {
200
201             int id = ServicesRegistry.getInstance ().getJahiaIncrementorsDBService ()
202                     .autoIncrement ("jahia_site_lang_maps");
203
204             StringBuffer JavaDoc sqlQuery = new StringBuffer JavaDoc (
205                     "INSERT INTO jahia_site_lang_maps VALUES(");
206             sqlQuery.append (id);
207             sqlQuery.append (", ");
208             sqlQuery.append (siteLangMapping.getSiteID ());
209             sqlQuery.append (", '");
210             sqlQuery.append (JahiaTools.quote (siteLangMapping.getFromLanguageCode ()));
211             sqlQuery.append ("', '");
212             sqlQuery.append (JahiaTools.quote (siteLangMapping.getToLanguageCode ()));
213
214             sqlQuery.append ("')");
215
216             executeQueryNoResultSet (sqlQuery.toString ());
217
218             logger.debug (sqlQuery.toString ());
219
220             siteLangMapping.setId (id);
221
222             languageMappingCache.put(new Integer JavaDoc(id), siteLangMapping);
223             siteLanguageMappingsCache.flush();
224
225         } catch (JahiaException je) {
226             String JavaDoc errorMsg = "Error while adding new site language mapping in database : " + je.getMessage ();
227             logger.debug (errorMsg, je);
228             throw new JahiaException ("Cannot add site language mapping in the database",
229                     errorMsg,
230                     JahiaException.DATABASE_ERROR,
231                     JahiaException.CRITICAL_SEVERITY,
232                     je);
233         }
234     }
235
236     /**
237      * Removes a site language mapping entry from the database
238      *
239      * @param id the identifier of the entry to remove from the database
240      *
241      * @throws JahiaException is thrown if an error occured while executing
242      * the database request.
243      */

244     public void removeSiteLanguageMapping (int id) throws JahiaException {
245
246         try {
247             String JavaDoc sqlQuery = "DELETE FROM jahia_site_lang_maps WHERE id=" + id;
248             executeQueryNoResultSet (sqlQuery);
249             languageMappingCache.remove(new Integer JavaDoc(id));
250             siteLanguageMappingsCache.flush();
251
252         } catch (JahiaException je) {
253             String JavaDoc errorMsg = "Error while removing site language mapping from the database : " + je.getMessage ();
254             logger.debug (errorMsg, je);
255             throw new JahiaException ("Cannot remove site language mapping in the database",
256                     errorMsg, JahiaException.DATABASE_ERROR,
257                     JahiaException.CRITICAL_SEVERITY, je);
258         }
259     }
260
261
262     /**
263      * Updates the settings for a site mapping
264      *
265      * @param siteLangMapping the object containing the updated values to
266      * store in the database
267      *
268      * @throws JahiaException is thrown if an error occured while executing
269      * the database request.
270      */

271     public void updateSiteLanguageMapping (SiteLanguageMapping siteLangMapping)
272             throws JahiaException {
273         try {
274             StringBuffer JavaDoc sqlQuery = new StringBuffer JavaDoc ("UPDATE jahia_site_lang_maps SET ");
275             sqlQuery.append ("site_id=");
276             sqlQuery.append (siteLangMapping.getSiteID ());
277             sqlQuery.append (", from_lang_code='");
278             sqlQuery.append (JahiaTools.quote (siteLangMapping.getFromLanguageCode ()));
279             sqlQuery.append ("', to_lang_code='");
280             sqlQuery.append (JahiaTools.quote (siteLangMapping.getToLanguageCode ()));
281
282             sqlQuery.append ("'");
283             sqlQuery.append (" WHERE id=");
284             sqlQuery.append (siteLangMapping.getId ());
285
286             executeQueryNoResultSet (sqlQuery.toString ());
287
288             languageMappingCache.remove(new Integer JavaDoc(siteLangMapping.getId ()));
289             siteLanguageMappingsCache.flush();
290
291         } catch (JahiaException je) {
292             String JavaDoc errorMsg = "Error while updating a site language mapping " + siteLangMapping.getId () + " : " + je.getMessage ();
293             logger.debug (errorMsg, je);
294             throw new JahiaException ("Cannot update site in the database",
295                     errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL_SEVERITY,
296                     je);
297         }
298     }
299
300
301     /**
302      * Build a SiteLanguageMapping object from a database result set
303      *
304      * @param rs the result set from which to retrieve one line of data
305      *
306      * @return a SiteLanguageMapping object containing the data from the
307      * result set, or null if an error occured
308      *
309      * @throws JahiaException is thrown if an error occured while retrieving
310      * the object data from the result set.
311      */

312     protected SiteLanguageMapping getSiteLanguageMappingFromResultSet (ResultSet JavaDoc rs)
313             throws JahiaException {
314
315         SiteLanguageMapping siteLangMapping = null;
316
317         if (rs != null) {
318
319             int id = 0;
320             int site_id = 0;
321             String JavaDoc fromLanguageCode = "";
322             String JavaDoc toLanguageCode = "";
323
324             try {
325
326                 id = rs.getInt ("id");
327                 site_id = rs.getInt ("site_id");
328                 fromLanguageCode = rs.getString ("from_lang_code");
329                 toLanguageCode = rs.getString ("to_lang_code");
330
331                 siteLangMapping = new SiteLanguageMapping (id, site_id,
332                         fromLanguageCode, toLanguageCode);
333
334             } catch (SQLException JavaDoc se) {
335                 String JavaDoc errorMsg = "Error while retrieving a row of a site language mapping in the database : " + se.getMessage ();
336                 logger.debug (errorMsg, se);
337                 throw new JahiaException ("Cannot read data from resultset",
338                         errorMsg, JahiaException.DATABASE_ERROR,
339                         JahiaException.CRITICAL_SEVERITY,
340                         se);
341             }
342
343         }
344
345         return siteLangMapping;
346     }
347
348
349     private void executeQueryNoResultSet (String JavaDoc queryStr) throws JahiaException {
350
351         Connection JavaDoc dbConn = null;
352         Statement JavaDoc statement = null;
353
354         try {
355             dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
356             statement = dbConn.createStatement ();
357             if (statement != null) {
358                 statement.executeUpdate (queryStr);
359             }
360         } catch (SQLException JavaDoc se) {
361             String JavaDoc errorMsg = "Error while executing query with no result set on database (" + queryStr + ") : " + se.getMessage ();
362             logger.debug (errorMsg, se);
363             throw new JahiaException ("Cannot execute query" + queryStr,
364                     errorMsg, JahiaException.DATABASE_ERROR,
365                     JahiaException.CRITICAL_SEVERITY, se);
366         } finally {
367
368             closeStatement (statement);
369         }
370
371     }
372
373
374     private void closeStatement (Statement JavaDoc statement) {
375         // Close the opened statement
376
try {
377             if (statement != null) {
378                 statement.close ();
379                 statement = null;
380             }
381         } catch (SQLException JavaDoc sqlEx) {
382             // just create an exception without raising it, just to notify it
383
// in the logs.
384
JahiaException je = new JahiaException ("Cannot close a statement",
385                     "Cannot close a statement", JahiaException.DATABASE_ERROR,
386                     JahiaException.WARNING_SEVERITY, sqlEx);
387         }
388     }
389
390     /**
391      * Retrieve a DOM representing of the site language mappings table for a
392      * given site
393      *
394      * @param siteID the identifier of the site for which to retrieve the
395      * DOM tree
396      *
397      * @return a DOM object that contains the database table entries for the
398      * given site
399      *
400      * @throws JahiaException is thrown if an occured if while communicating
401      * with the database.
402      */

403     public JahiaDOMObject getSiteLanguageMappingsAsDOM (int siteID) throws JahiaException {
404
405         Connection JavaDoc dbConn = null;
406         Statement JavaDoc statement = null;
407
408         String JavaDoc output = null;
409         JahiaDBDOMObject dom = null;
410
411         try {
412             String JavaDoc sqlQuery = "SELECT * FROM jahia_site_lang_maps where site_id=" + siteID;
413
414             dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
415             statement = dbConn.createStatement ();
416             if (statement != null) {
417                 ResultSet JavaDoc rs = statement.executeQuery (sqlQuery);
418                 if (rs != null) {
419                     dom = new JahiaDBDOMObject ();
420                     dom.addTable ("jahia_site_lang_maps", rs);
421                     return dom;
422                 }
423             }
424         } catch (SQLException JavaDoc se) {
425             String JavaDoc errorMsg = "Error while retrieving site (" + siteID + ") language setting to export as a DOM tree : " + se.getMessage ();
426             logger.debug (errorMsg, se);
427             throw new JahiaException ("Cannot load site from the database",
428                     errorMsg, JahiaException.DATABASE_ERROR,
429                     JahiaException.CRITICAL_SEVERITY, se);
430         } finally {
431
432             closeStatement (statement);
433         }
434
435         return dom;
436     }
437 }
438
Popular Tags