1 package org.jahia.resourcebundle; 2 3 import org.jahia.exceptions.JahiaException; 4 import java.sql.Connection ; 5 import java.sql.PreparedStatement ; 6 import java.sql.ResultSet ; 7 import java.sql.SQLException ; 8 import java.util.ArrayList ; 9 10 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 GET_RESOURCE_BYNAMEANDLANGUAGE_QUERY = "SELECT value_resource FROM jahia_resources WHERE name_resource=? AND languagecode_resource=?"; 31 static private final String GET_RESOURCE_BYNAME_QUERY = "SELECT value_resource, languagecode_resource FROM jahia_resources WHERE name_resource=?"; 32 static private final String UPDATE_RESOURCE_BYNAMEANDLANGUAGE_QUERY = "UPDATE jahia_resources SET value_resource=? WHERE name_resource=? AND languagecode_resource=?"; 33 static private final String INSERT_RESOURCE_QUERY = "INSERT INTO jahia_resources (name_resource, value_resource, languagecode_resource) VALUES (?, ?, ?)"; 34 static private final String REMOVE_RESOURCE_BYNAMEANDLANGUAGE_QUERY = 35 "DELETE FROM jahia_resources WHERE name_resource=? AND languagecode_resource=?"; 36 37 40 public static synchronized DatabaseResourcesDB getInstance () { 41 if (singletonInstance == null) { 42 singletonInstance = new DatabaseResourcesDB(); 43 } 44 return singletonInstance; 45 } 46 47 49 57 public DatabaseResourceBean getDatabaseResource (String name, 58 String languageCode) 59 throws JahiaException { 60 61 DatabaseResourceBean databaseResource = null; 62 Connection dbConn = null; 63 PreparedStatement stmt = null; 64 ResultSet 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 if (rs.next()) { 77 String value = rs.getString("value_resource"); 79 80 databaseResource = new DatabaseResourceBean(name, value, 81 languageCode); 82 } 83 84 } catch (SQLException 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 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 114 public ArrayList getAllDatabaseResource (String name) 115 throws JahiaException { 116 117 DatabaseResourceBean databaseResource = null; 118 Connection dbConn = null; 119 PreparedStatement stmt = null; 120 ResultSet rs = null; 121 ArrayList allResourceEntries = new ArrayList (); 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 while (rs.next()) { 133 String value = rs.getString("value_resource"); 135 String languageCode = rs.getString("languagecode_resource"); 136 137 databaseResource = new DatabaseResourceBean(name, value, 138 languageCode); 139 allResourceEntries.add(databaseResource); 140 } 141 142 } catch (SQLException 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 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 172 public void createDatabaseResource (DatabaseResourceBean databaseResource) 173 throws JahiaException { 174 Connection dbConn = null; 175 PreparedStatement stmt = null; 176 ResultSet 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 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 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 216 public void updateDatabaseResource (DatabaseResourceBean databaseResource) 217 throws JahiaException { 218 Connection dbConn = null; 219 PreparedStatement stmt = null; 220 ResultSet 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 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 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 262 public void removeDatabaseResource (DatabaseResourceBean databaseResource) 263 throws JahiaException { 264 265 Connection dbConn = null; 266 PreparedStatement stmt = null; 267 ResultSet 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 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 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 |