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 ; 10 import java.sql.ResultSet ; 11 import java.sql.SQLException ; 12 import java.sql.Statement ; 13 import java.util.Vector ; 14 import org.jahia.services.cache.Cache; 15 import org.jahia.services.cache.CacheFactory; 16 17 28 29 public class SiteLanguageMappingPersistance { 30 31 private static org.apache.log4j.Logger logger = 32 org.apache.log4j.Logger.getLogger (SiteLanguageMappingPersistance.class); 33 34 35 private static SiteLanguageMappingPersistance m_Instance = null; 36 37 public static final String SITE_LANGUAGEMAPPINGS_CACHE = "siteLanguageMappingsCache"; 39 40 public static final String LANGUAGEMAPPINGS_CACHE = "languageMappingsCache"; 42 private Cache languageMappingCache = null; 43 private Cache siteLanguageMappingsCache = null; 44 45 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 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 84 public Vector getSiteLanguageMappings (int siteID) throws JahiaException { 85 86 Connection dbConn = null; 87 Statement statement = null; 88 89 Vector siteLanguageMappings = new Vector (); 90 SiteLanguageMapping siteLanguageMapping = null; 91 92 if (siteLanguageMappingsCache.containsKey(new Integer (siteID))) { 93 return (Vector ) siteLanguageMappingsCache.get(new Integer (siteID)); 94 } 95 96 try { 97 String 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 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 (siteID), siteLanguageMappings); 111 } 112 113 } catch (SQLException se) { 114 String 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 141 public SiteLanguageMapping getSiteLanguageMapping (int id) throws JahiaException { 142 143 Connection dbConn = null; 144 Statement statement = null; 145 146 SiteLanguageMapping siteLangMapping = null; 147 148 if (languageMappingCache.containsKey(new Integer (id))) { 149 return (SiteLanguageMapping) languageMappingCache.get(new Integer (id)); 150 } 151 152 try { 153 String 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 rs = statement.executeQuery (sqlQuery); 159 if (rs != null) { 160 if (rs.next ()) { 161 siteLangMapping = getSiteLanguageMappingFromResultSet (rs); 162 } 163 languageMappingCache.put(new Integer (id), siteLangMapping); 164 siteLanguageMappingsCache.flush(); 165 } 166 } 167 168 } catch (SQLException se) { 169 String 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 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 sqlQuery = new StringBuffer ( 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 (id), siteLangMapping); 223 siteLanguageMappingsCache.flush(); 224 225 } catch (JahiaException je) { 226 String 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 244 public void removeSiteLanguageMapping (int id) throws JahiaException { 245 246 try { 247 String sqlQuery = "DELETE FROM jahia_site_lang_maps WHERE id=" + id; 248 executeQueryNoResultSet (sqlQuery); 249 languageMappingCache.remove(new Integer (id)); 250 siteLanguageMappingsCache.flush(); 251 252 } catch (JahiaException je) { 253 String 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 271 public void updateSiteLanguageMapping (SiteLanguageMapping siteLangMapping) 272 throws JahiaException { 273 try { 274 StringBuffer sqlQuery = new StringBuffer ("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 (siteLangMapping.getId ())); 289 siteLanguageMappingsCache.flush(); 290 291 } catch (JahiaException je) { 292 String 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 312 protected SiteLanguageMapping getSiteLanguageMappingFromResultSet (ResultSet 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 fromLanguageCode = ""; 322 String 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 se) { 335 String 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 queryStr) throws JahiaException { 350 351 Connection dbConn = null; 352 Statement 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 se) { 361 String 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 statement) { 375 try { 377 if (statement != null) { 378 statement.close (); 379 statement = null; 380 } 381 } catch (SQLException sqlEx) { 382 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 403 public JahiaDOMObject getSiteLanguageMappingsAsDOM (int siteID) throws JahiaException { 404 405 Connection dbConn = null; 406 Statement statement = null; 407 408 String output = null; 409 JahiaDBDOMObject dom = null; 410 411 try { 412 String 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 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 se) { 425 String 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 |