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.Enumeration ; 9 import java.util.Iterator ; 10 import java.util.Properties ; 11 import java.util.Hashtable ; 12 13 import org.jahia.exceptions.JahiaException; 14 import org.jahia.registries.ServicesRegistry; 15 16 17 26 class MarkupDefinitionPersistence { 27 28 static private final String LOAD_ALL_MARKUP_QUERY = "SELECT * FROM jahia_markupdef"; 29 static private final String GET_MARKUP_BYID_QUERY = "SELECT * FROM jahia_markupdef WHERE id_markup =?"; 30 static private final String UPDATE_MARKUP_BYID_QUERY = "UPDATE jahia_markupdef SET name_markup=?, case_sensitive=? WHERE id_markup =?"; 31 static private final String INSERT_MARKUP_QUERY = "INSERT INTO jahia_markupdef (id_markup, name_markup, case_sensitive) VALUES (?, ?, ?)"; 32 static private final String REMOVE_MARKUP_BYID_QUERY = "DELETE FROM jahia_markupdef WHERE id_markup=?"; 33 34 static private final String GET_ALL_MARKUP_PROP_BYID_QUERY = "SELECT * FROM jahia_markupdef_prop WHERE id_markup =?"; 35 static private final String INSERT_MARKUP_PROP_QUERY = "INSERT INTO jahia_markupdef_prop (id_markup, prop_name, prop_value) VALUES (?, ?, ?)"; 36 static private final String REMOVE_ALL_MARKUP_PROP_BYID_QUERY = "DELETE FROM jahia_markupdef_prop WHERE id_markup=?"; 37 38 private MarkupDefinitionPersistence() { 39 } 40 41 private static org.apache.log4j.Logger logger = 42 org.apache.log4j.Logger.getLogger(MarkupDefinitionPersistence.class); 43 44 private static MarkupDefinitionPersistence instance = null; 45 46 49 public static synchronized MarkupDefinitionPersistence getInstance () { 50 if (instance == null) { 51 instance = new MarkupDefinitionPersistence(); 52 } 53 return instance; 54 } 55 56 protected Hashtable loadAllMarkupDefinitions () 57 throws JahiaException { 58 59 Hashtable result = new Hashtable (); 60 61 MarkupDefinition def = null; 62 Connection dbConn = null; 63 PreparedStatement stmt = null; 64 ResultSet rs = null; 65 66 try { 67 68 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection(); 69 stmt = dbConn.prepareStatement(LOAD_ALL_MARKUP_QUERY); 70 rs = stmt.executeQuery(); 71 72 while (rs.next()) { 73 int id = rs.getInt(1); 74 String name = rs.getString(2); 75 int caseSensitive = rs.getInt(3); 76 77 def = new MarkupDefinition(name); 78 def.setId(id); 79 def.setIsCaseSensitive(caseSensitive==1); 80 result.put(new Integer (id),def); 81 } 82 83 } catch (SQLException se) { 84 throw new JahiaException("Error loading markup definitions from the database", 85 se.getMessage(), 86 JahiaException.DATABASE_ERROR, 87 JahiaException.ERROR_SEVERITY, se); 88 } finally { 89 try { 90 if (stmt != null) 91 stmt.close(); 92 } catch (SQLException ex) { 93 logger.debug("Cannot free resources",ex); 94 } 95 } 96 97 Iterator iterator = result.values().iterator(); 98 while ( iterator.hasNext() ){ 99 MarkupDefinition markupDef = (MarkupDefinition)iterator.next(); 100 markupDef.setProperties(loadMarkupDefinitionProperties(markupDef.getId())); 101 } 102 return result; 103 } 104 105 106 protected MarkupDefinition getDefinition (int id) 107 throws JahiaException { 108 109 MarkupDefinition def = null; 110 Connection dbConn = null; 111 PreparedStatement stmt = null; 112 ResultSet rs = null; 113 114 try { 115 116 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection(); 117 stmt = dbConn.prepareStatement(GET_MARKUP_BYID_QUERY); 118 stmt.setInt(1, id); 119 rs = stmt.executeQuery(); 120 121 if (rs.next()) { 123 String name = rs.getString(2); 125 int caseSensitive = rs.getInt(3); 126 127 def = new MarkupDefinition(name); 128 def.setId(id); 129 def.setIsCaseSensitive(caseSensitive==1); 130 } 131 132 } catch (SQLException se) { 133 throw new JahiaException("Cannot load markup definition " + id + 134 "from the database", 135 se.getMessage(), 136 JahiaException.DATABASE_ERROR, 137 JahiaException.ERROR_SEVERITY, se); 138 } finally { 139 try { 140 if (stmt != null) 141 stmt.close(); 142 } catch (SQLException ex) { 143 logger.debug("Cannot free resources",ex); 144 } 145 } 146 147 def.setProperties(loadMarkupDefinitionProperties(id)); 148 return def; 149 } 150 151 protected void createMarkupDefinition (MarkupDefinition markupDefinition) 152 throws JahiaException { 153 154 Connection dbConn = null; 155 PreparedStatement stmt = null; 156 ResultSet rs = null; 157 158 try { 159 int id = ServicesRegistry.getInstance().getJahiaIncrementorsDBService() 160 .autoIncrement("jahia_markupdef"); 161 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection(); 162 stmt = dbConn.prepareStatement(INSERT_MARKUP_QUERY); 163 stmt.setInt(1, id); 164 stmt.setString(2, markupDefinition.getName()); 165 int caseSensitive = markupDefinition.isCaseSensitive()?1:0; 166 stmt.setInt(3, caseSensitive); 167 stmt.executeUpdate(); 168 markupDefinition.setId(id); 169 } catch (SQLException se) { 170 throw new JahiaException("Cannot store markup definition in the database", 171 se.getMessage(), 172 JahiaException.DATABASE_ERROR, 173 JahiaException.ERROR_SEVERITY, se); 174 } finally { 175 try { 176 if (stmt != null) 177 stmt.close(); 178 } catch (SQLException ex) { 179 logger.debug("Cannot free resources",ex); 180 } 181 } 182 if ( markupDefinition.getId() != -1 ){ 183 saveMarkupDefinitionProperties(markupDefinition.getId(), 184 markupDefinition.getProperties()); 185 } 186 } 187 188 protected void updateMarkupDefinition (MarkupDefinition markupDefinition) 189 throws JahiaException { 190 Connection dbConn = null; 191 PreparedStatement stmt = null; 192 ResultSet rs = null; 193 194 try { 195 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection(); 196 stmt = dbConn.prepareStatement(UPDATE_MARKUP_BYID_QUERY); 197 stmt.setString(1, markupDefinition.getName()); 198 int caseSensitive = markupDefinition.isCaseSensitive()?1:0; 199 stmt.setInt(2, caseSensitive); 200 stmt.setInt(3, markupDefinition.getId()); 201 stmt.executeUpdate(); 202 } catch (SQLException se) { 203 throw new JahiaException("Cannot update markup definition " 204 +markupDefinition.getId()+" in the database", 205 se.getMessage(), 206 JahiaException.DATABASE_ERROR, 207 JahiaException.ERROR_SEVERITY, se); 208 } finally { 209 try { 210 if (stmt != null) 211 stmt.close(); 212 } catch (SQLException ex) { 213 logger.debug("Cannot free resources",ex); 214 } 215 } 216 217 saveMarkupDefinitionProperties(markupDefinition.getId(), 218 markupDefinition.getProperties()); 219 220 } 221 222 223 protected void removeMarkupDefinition (MarkupDefinition markupDefinition) 224 throws JahiaException { 225 226 Connection dbConn = null; 227 PreparedStatement stmt = null; 228 ResultSet rs = null; 229 230 try { 231 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection(); 232 stmt = dbConn.prepareStatement(REMOVE_MARKUP_BYID_QUERY); 233 stmt.setInt(1, markupDefinition.getId()); 234 stmt.executeUpdate(); 235 } catch (SQLException se) { 236 logger.debug("Cannot free resources",se); 237 } finally { 238 try { 239 if (stmt != null) 240 stmt.close(); 241 } catch (SQLException ex) { 242 logger.debug("Cannot free resources",ex); 243 } 244 } 245 246 deleteMarkupDefinitionProperties(markupDefinition.getId()); 247 } 248 249 protected Properties loadMarkupDefinitionProperties(int id) 250 throws JahiaException { 251 252 Properties properties = new Properties (); 253 Connection dbConn = null; 254 PreparedStatement stmt = null; 255 ResultSet rs = null; 256 257 try { 258 259 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection(); 260 stmt = dbConn.prepareStatement(GET_ALL_MARKUP_PROP_BYID_QUERY); 261 stmt.setInt(1, id); 262 rs = stmt.executeQuery(); 263 264 while (rs.next()) { 265 String propName = rs.getString(2); 266 String propValue = rs.getString(3); 267 properties.setProperty(propName,propValue); 268 } 269 270 } catch (SQLException se) { 271 throw new JahiaException("Cannot load markup definition prop for " + id + 272 "from the database", 273 se.getMessage(), 274 JahiaException.DATABASE_ERROR, 275 JahiaException.ERROR_SEVERITY, se); 276 } finally { 277 try { 278 if (stmt != null) 279 stmt.close(); 280 } catch (SQLException ex) { 281 logger.debug("Cannot free resources",ex); 282 } 283 } 284 return properties; 285 } 286 287 294 protected void deleteMarkupDefinitionProperties(int id) 295 throws JahiaException { 296 297 Connection dbConn = null; 298 PreparedStatement stmt = null; 299 300 try { 301 302 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection(); 303 stmt = dbConn.prepareStatement(REMOVE_ALL_MARKUP_PROP_BYID_QUERY); 304 stmt.setInt(1, id); 305 stmt.executeUpdate(); 306 307 } catch (SQLException se) { 308 throw new JahiaException("Cannot delete markup definition props for " + id + 309 "from the database", 310 se.getMessage(), 311 JahiaException.DATABASE_ERROR, 312 JahiaException.ERROR_SEVERITY, se); 313 } finally { 314 try { 315 if (stmt != null) 316 stmt.close(); 317 } catch (SQLException ex) { 318 throw new JahiaException("Cannot free resources", 319 "Cannot free resources", 320 JahiaException.DATABASE_ERROR, 321 JahiaException.WARNING_SEVERITY, ex); 322 } 323 } 324 } 325 326 protected void insertMarkupDefinitionProperty(int id, String propName, String propValue) 327 throws JahiaException { 328 329 Connection dbConn = null; 330 PreparedStatement stmt = null; 331 332 try { 333 334 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection(); 335 stmt = dbConn.prepareStatement(INSERT_MARKUP_PROP_QUERY); 336 stmt.setInt(1, id); 337 stmt.setString(2, propName); 338 stmt.setString(3, propValue); 339 stmt.executeUpdate(); 340 341 } catch (SQLException se) { 342 throw new JahiaException("Cannot insert markup definition prop for " + id + 343 "from the database", 344 se.getMessage(), 345 JahiaException.DATABASE_ERROR, 346 JahiaException.ERROR_SEVERITY, se); 347 } finally { 348 try { 349 if (stmt != null) 350 stmt.close(); 351 } catch (SQLException ex) { 352 logger.debug("Cannot free resources",ex); 353 } 354 } 355 } 356 357 365 protected void saveMarkupDefinitionProperties(int id, Properties properties) 366 throws JahiaException { 367 368 deleteMarkupDefinitionProperties(id); 370 371 Enumeration enumeration = properties.propertyNames(); 372 while ( enumeration.hasMoreElements() ){ 373 String name = (String )enumeration.nextElement(); 374 insertMarkupDefinitionProperty(id,name,properties.getProperty(name)); 375 } 376 } 377 378 } | Popular Tags |