1 package org.jahia.services.htmlparser; 2 3 import java.sql.Connection ; 4 import java.sql.ResultSet ; 5 import java.sql.PreparedStatement ; 6 import java.sql.SQLException ; 7 8 import java.util.ArrayList ; 9 import java.util.Enumeration ; 10 import java.util.Iterator ; 11 import java.util.Properties ; 12 import java.util.Hashtable ; 13 14 import org.jahia.exceptions.JahiaException; 15 import org.jahia.registries.ServicesRegistry; 16 17 18 27 class MarkupSettingPersistence { 28 29 static private final String LOAD_ALL_MARKUP_SETTING_QUERY = "SELECT * FROM jahia_markup_set"; 30 static private final String GET_MARKUP_SETTING_BYID_QUERY = "SELECT * FROM jahia_markup_set WHERE setting_id =?"; 31 static private final String UPDATE_MARKUP_SETTING_BYID_QUERY = "UPDATE jahia_markup_set SET setting_type=?, markupdef_id=?, apply_to_all_site=? WHERE setting_id =?"; 32 static private final String INSERT_MARKUP_SETTING_QUERY = "INSERT INTO jahia_markup_set (setting_id, setting_type, markupdef_id, apply_to_all_site) VALUES (?, ?, ?, ?)"; 33 static private final String REMOVE_MARKUP_SETTING_BYID_QUERY = "DELETE FROM jahia_markup_set WHERE setting_id=?"; 34 35 static private final String GET_ALL_MARKUP_SETTING_PROP_BYID_QUERY = "SELECT * FROM jahia_markup_set_prop WHERE setting_id =?"; 36 static private final String INSERT_MARKUP_SETTING_PROP_QUERY = "INSERT INTO jahia_markup_set_prop (setting_id, prop_name, prop_value) VALUES (?, ?, ?)"; 37 static private final String REMOVE_ALL_MARKUP_SETTING_PROP_BYID_QUERY = "DELETE FROM jahia_markup_set_prop WHERE setting_id=?"; 38 39 static private final String GET_ALL_SITE_MARKUP_SETTING_QUERY = "SELECT * FROM jahia_markup_set_site"; 40 static private final String LOAD_ALL_SITE_MARKUP_SETTING_BY_SETTINGID_QUERY = "SELECT * FROM jahia_markup_set_site WHERE setting_id=?"; 41 static private final String REMOVE_SITE_MARKUP_SETTING_QUERY = "DELETE FROM jahia_markup_set_site WHERE site_id=? AND setting_id=?"; 42 static private final String INSERT_SITE_MARKUP_SETTING_QUERY = "INSERT INTO jahia_markup_set_site (setting_id, site_id) VALUES(?,?)";; 43 static private final String REMOVE_ALL_SITE_MARKUP_SETTING_BY_SETTINGID_QUERY = "DELETE FROM jahia_markup_set_site WHERE setting_id=?"; 44 45 private MarkupSettingPersistence() { 46 } 47 48 private static org.apache.log4j.Logger logger = 49 org.apache.log4j.Logger.getLogger(MarkupSettingPersistence.class); 50 51 private static MarkupSettingPersistence instance = null; 52 53 56 public static synchronized MarkupSettingPersistence getInstance () { 57 if (instance == null) { 58 instance = new MarkupSettingPersistence(); 59 } 60 return instance; 61 } 62 63 protected Hashtable loadAllMarkupSettings () 64 throws JahiaException { 65 66 Hashtable result = new Hashtable (); 67 68 MarkupSetting mSetting = null; 69 Connection dbConn = null; 70 PreparedStatement stmt = null; 71 ResultSet rs = null; 72 73 try { 74 75 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection(); 76 stmt = dbConn.prepareStatement(LOAD_ALL_MARKUP_SETTING_QUERY); 77 rs = stmt.executeQuery(); 78 79 while (rs.next()) { 80 int id = rs.getInt(1); 81 String settingType = rs.getString(2); 82 int markupDefId = rs.getInt(3); 83 int applyToAllSite = rs.getInt(4); 84 85 mSetting = new MarkupSetting(settingType, markupDefId); 86 mSetting.setSettingId(id); 87 mSetting.setApplyToAllSite(applyToAllSite==1); 88 result.put(new Integer (id),mSetting); 89 } 90 91 } catch (SQLException se) { 92 throw new JahiaException("Error loading markup settings from the database", 93 se.getMessage(), 94 JahiaException.DATABASE_ERROR, 95 JahiaException.ERROR_SEVERITY, se); 96 } finally { 97 try { 98 if (stmt != null) 99 stmt.close(); 100 } catch (SQLException ex) { 101 logger.debug("Cannot free resources",ex); 102 } 103 } 104 105 Iterator iterator = result.values().iterator(); 106 while ( iterator.hasNext() ){ 107 MarkupSetting setting = (MarkupSetting)iterator.next(); 108 setting.setProperties(loadMarkupSettingProperties(setting.getSettingId())); 109 setting.getSites().addAll(loadAllSiteMarkupSettingBySettingId(setting.getSettingId())); 110 } 111 return result; 112 } 113 114 115 protected MarkupSetting getMarkupSetting (int id) 116 throws JahiaException { 117 118 MarkupSetting mSetting = null; 119 Connection dbConn = null; 120 PreparedStatement stmt = null; 121 ResultSet rs = null; 122 123 try { 124 125 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection(); 126 stmt = dbConn.prepareStatement(GET_MARKUP_SETTING_BYID_QUERY); 127 stmt.setInt(1, id); 128 rs = stmt.executeQuery(); 129 130 if (rs.next()) { 132 String settingType = rs.getString(2); 134 int markupDefId = rs.getInt(3); 135 int applyToAllSite = rs.getInt(4); 136 137 mSetting = new MarkupSetting(settingType, markupDefId); 138 mSetting.setSettingId(id); 139 mSetting.setApplyToAllSite(applyToAllSite==1); 140 } 141 142 } catch (SQLException se) { 143 throw new JahiaException("Cannot load markup setting " + id + 144 "from the database", 145 se.getMessage(), 146 JahiaException.DATABASE_ERROR, 147 JahiaException.ERROR_SEVERITY, se); 148 } finally { 149 try { 150 if (stmt != null) 151 stmt.close(); 152 } catch (SQLException ex) { 153 logger.debug("Cannot free resources",ex); 154 } 155 } 156 157 mSetting.setProperties(loadMarkupSettingProperties(id)); 158 mSetting.getSites().addAll(loadAllSiteMarkupSettingBySettingId(id)); 159 return mSetting; 160 } 161 162 protected void createMarkupSetting (MarkupSetting mSetting) 163 throws JahiaException { 164 165 Connection dbConn = null; 166 PreparedStatement stmt = null; 167 ResultSet rs = null; 168 169 try { 170 int id = ServicesRegistry.getInstance().getJahiaIncrementorsDBService() 171 .autoIncrement("jahia_markup_set"); 172 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection(); 173 stmt = dbConn.prepareStatement(INSERT_MARKUP_SETTING_QUERY); 174 stmt.setInt(1, id); 175 stmt.setString(2, mSetting.getSettingType()); 176 stmt.setInt(3, mSetting.getMarkupDefId()); 177 int applyToAllSite = mSetting.applyToAllSite()?1:0; 178 stmt.setInt(4, applyToAllSite); 179 stmt.executeUpdate(); 180 mSetting.setSettingId(id); 181 } catch (SQLException se) { 182 throw new JahiaException("Cannot store markup setting in the database", 183 se.getMessage(), 184 JahiaException.DATABASE_ERROR, 185 JahiaException.ERROR_SEVERITY, se); 186 } finally { 187 try { 188 if (stmt != null) 189 stmt.close(); 190 } catch (SQLException ex) { 191 logger.debug("Cannot free resources",ex); 192 } 193 } 194 if ( mSetting.getSettingId() != -1 ){ 195 saveMarkupSettingProperties(mSetting.getSettingId(), 196 mSetting.getProperties()); 197 198 saveSiteMarkupSetting(mSetting); 199 } 200 } 201 202 protected void updateMarkupSetting (MarkupSetting mSetting) 203 throws JahiaException { 204 Connection dbConn = null; 205 PreparedStatement stmt = null; 206 ResultSet rs = null; 207 208 try { 209 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection(); 210 stmt = dbConn.prepareStatement(UPDATE_MARKUP_SETTING_BYID_QUERY); 211 stmt.setString(1, mSetting.getSettingType()); 212 stmt.setInt(2, mSetting.getMarkupDefId()); 213 int applyToAllSite = mSetting.applyToAllSite()?1:0; 214 stmt.setInt(3, applyToAllSite); 215 stmt.setInt(4, mSetting.getSettingId()); 216 stmt.executeUpdate(); 217 } catch (SQLException se) { 218 throw new JahiaException("Cannot update markup setting "+mSetting.getSettingId()+" in the database", 219 se.getMessage(), 220 JahiaException.DATABASE_ERROR, 221 JahiaException.ERROR_SEVERITY, se); 222 } finally { 223 try { 224 if (stmt != null) 225 stmt.close(); 226 } catch (SQLException ex) { 227 logger.debug("Cannot free resources",ex); 228 } 229 } 230 231 saveMarkupSettingProperties(mSetting.getSettingId(), 232 mSetting.getProperties()); 233 234 saveSiteMarkupSetting(mSetting); 235 236 } 237 238 239 protected void removeMarkupSetting (MarkupSetting mSetting) 240 throws JahiaException { 241 242 Connection dbConn = null; 243 PreparedStatement stmt = null; 244 ResultSet rs = null; 245 246 try { 247 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection(); 248 stmt = dbConn.prepareStatement(REMOVE_MARKUP_SETTING_BYID_QUERY); 249 stmt.setInt(1, mSetting.getSettingId()); 250 stmt.executeUpdate(); 251 } catch (SQLException se) { 252 logger.debug("Cannot free resources",se); 253 } finally { 254 try { 255 if (stmt != null) 256 stmt.close(); 257 } catch (SQLException ex) { 258 logger.debug("Cannot free resources",ex); 259 } 260 } 261 262 deleteMarkupSettingProperties(mSetting.getSettingId()); 263 removeAllSiteMarkupSettingBySettingId(mSetting.getSettingId()); 264 mSetting.getSites().clear(); 265 } 266 267 protected Properties loadMarkupSettingProperties(int id) 268 throws JahiaException { 269 270 Properties properties = new Properties (); 271 Connection dbConn = null; 272 PreparedStatement stmt = null; 273 ResultSet rs = null; 274 275 try { 276 277 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection(); 278 stmt = dbConn.prepareStatement(GET_ALL_MARKUP_SETTING_PROP_BYID_QUERY); 279 stmt.setInt(1, id); 280 rs = stmt.executeQuery(); 281 282 while (rs.next()) { 283 String propName = rs.getString(2); 284 String propValue = rs.getString(3); 285 properties.setProperty(propName,propValue); 286 } 287 288 } catch (SQLException se) { 289 throw new JahiaException("Cannot load markup setting prop for " + id + 290 "from the database", 291 se.getMessage(), 292 JahiaException.DATABASE_ERROR, 293 JahiaException.ERROR_SEVERITY, se); 294 } finally { 295 try { 296 if (stmt != null) 297 stmt.close(); 298 } catch (SQLException ex) { 299 logger.debug("Cannot free resources",ex); 300 } 301 } 302 return properties; 303 } 304 305 312 protected void deleteMarkupSettingProperties(int id) 313 throws JahiaException { 314 315 Connection dbConn = null; 316 PreparedStatement stmt = null; 317 318 try { 319 320 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection(); 321 stmt = dbConn.prepareStatement(REMOVE_ALL_MARKUP_SETTING_PROP_BYID_QUERY); 322 stmt.setInt(1, id); 323 stmt.executeUpdate(); 324 325 } catch (SQLException se) { 326 throw new JahiaException("Cannot delete markup setting props for " + id + 327 "from the database", 328 se.getMessage(), 329 JahiaException.DATABASE_ERROR, 330 JahiaException.ERROR_SEVERITY, se); 331 } finally { 332 try { 333 if (stmt != null) 334 stmt.close(); 335 } catch (SQLException ex) { 336 throw new JahiaException("Cannot free resources", 337 "Cannot free resources", 338 JahiaException.DATABASE_ERROR, 339 JahiaException.WARNING_SEVERITY, ex); 340 } 341 } 342 } 343 344 protected void insertMarkupSettingProperty(int id, String propName, String propValue) 345 throws JahiaException { 346 347 Connection dbConn = null; 348 PreparedStatement stmt = null; 349 350 try { 351 352 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection(); 353 stmt = dbConn.prepareStatement(INSERT_MARKUP_SETTING_PROP_QUERY); 354 stmt.setInt(1, id); 355 stmt.setString(2, propName); 356 stmt.setString(3, propValue); 357 stmt.executeUpdate(); 358 359 } catch (SQLException se) { 360 throw new JahiaException("Cannot insert markup setting prop for " + id + 361 "from the database", 362 se.getMessage(), 363 JahiaException.DATABASE_ERROR, 364 JahiaException.ERROR_SEVERITY, se); 365 } finally { 366 try { 367 if (stmt != null) 368 stmt.close(); 369 } catch (SQLException ex) { 370 logger.debug("Cannot free resources",ex); 371 } 372 } 373 } 374 375 383 protected void saveMarkupSettingProperties(int id, Properties properties) 384 throws JahiaException { 385 386 deleteMarkupSettingProperties(id); 388 389 Enumeration enumeration = properties.propertyNames(); 390 while ( enumeration.hasMoreElements() ){ 391 String name = (String )enumeration.nextElement(); 392 insertMarkupSettingProperty(id,name,properties.getProperty(name)); 393 } 394 } 395 396 protected void insertSiteMarkupSetting(int settingId, int siteId) 397 throws JahiaException { 398 399 Connection dbConn = null; 400 PreparedStatement stmt = null; 401 402 try { 403 404 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection(); 405 stmt = dbConn.prepareStatement(INSERT_SITE_MARKUP_SETTING_QUERY); 406 stmt.setInt(1, settingId); 407 stmt.setInt(2, siteId); 408 stmt.executeUpdate(); 409 410 } catch (SQLException se) { 411 throw new JahiaException("Cannot insert site markup setting" + 412 "from the database", 413 se.getMessage(), 414 JahiaException.DATABASE_ERROR, 415 JahiaException.ERROR_SEVERITY, se); 416 } finally { 417 try { 418 if (stmt != null) 419 stmt.close(); 420 } catch (SQLException ex) { 421 logger.debug("Cannot free resources",ex); 422 } 423 } 424 } 425 426 protected void removeSiteMarkupSetting(int settingId, int siteId) 427 throws JahiaException { 428 429 Connection dbConn = null; 430 PreparedStatement stmt = null; 431 432 try { 433 434 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection(); 435 stmt = dbConn.prepareStatement(REMOVE_SITE_MARKUP_SETTING_QUERY); 436 stmt.setInt(1, settingId); 437 stmt.setInt(2, siteId); 438 stmt.executeUpdate(); 439 440 } catch (SQLException se) { 441 throw new JahiaException("Cannot remove site markup setting" + 442 "from the database", 443 se.getMessage(), 444 JahiaException.DATABASE_ERROR, 445 JahiaException.ERROR_SEVERITY, se); 446 } finally { 447 try { 448 if (stmt != null) 449 stmt.close(); 450 } catch (SQLException ex) { 451 logger.debug("Cannot free resources",ex); 452 } 453 } 454 } 455 456 protected void removeAllSiteMarkupSettingBySettingId(int settingId) 457 throws JahiaException { 458 459 Connection dbConn = null; 460 PreparedStatement stmt = null; 461 462 try { 463 464 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection(); 465 stmt = dbConn.prepareStatement(REMOVE_ALL_SITE_MARKUP_SETTING_BY_SETTINGID_QUERY); 466 stmt.setInt(1, settingId); 467 stmt.executeUpdate(); 468 469 } catch (SQLException se) { 470 throw new JahiaException("Cannot remove all site markup setting for markup " 471 + settingId + "from the database", 472 se.getMessage(), 473 JahiaException.DATABASE_ERROR, 474 JahiaException.ERROR_SEVERITY, se); 475 } finally { 476 try { 477 if (stmt != null) 478 stmt.close(); 479 } catch (SQLException ex) { 480 logger.debug("Cannot free resources",ex); 481 } 482 } 483 } 484 485 protected ArrayList loadAllSiteMarkupSettingBySettingId(int settingId) 486 throws JahiaException { 487 488 ArrayList result = new ArrayList (); 489 Connection dbConn = null; 490 PreparedStatement stmt = null; 491 ResultSet rs = null; 492 try { 493 494 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection(); 495 stmt = dbConn.prepareStatement(LOAD_ALL_SITE_MARKUP_SETTING_BY_SETTINGID_QUERY); 496 stmt.setInt(1, settingId); 497 rs = stmt.executeQuery(); 498 499 while (rs.next()) { 500 int siteId = rs.getInt(2); 501 result.add(new Integer (siteId)); 502 } 503 504 } catch (SQLException se) { 505 throw new JahiaException("Cannot load site markup setting for markup " 506 + settingId + "from the database", 507 se.getMessage(), 508 JahiaException.DATABASE_ERROR, 509 JahiaException.ERROR_SEVERITY, se); 510 } finally { 511 try { 512 if (stmt != null) 513 stmt.close(); 514 } catch (SQLException ex) { 515 logger.debug("Cannot free resources",ex); 516 } 517 } 518 return result; 519 } 520 521 protected void saveSiteMarkupSetting(MarkupSetting mSetting) 522 throws JahiaException { 523 this.removeAllSiteMarkupSettingBySettingId(mSetting.getSettingId()); 524 Iterator iterator = mSetting.getSites().iterator(); 525 while ( iterator.hasNext() ){ 526 Integer I = (Integer )iterator.next(); 527 this.insertSiteMarkupSetting(I.intValue(),mSetting.getSettingId()); 528 } 529 } 530 531 } | Popular Tags |