1 41 42 43 package org.jahia.services.database; 44 45 import org.jahia.data.JahiaDBDOMObject; 46 import org.jahia.data.JahiaDOMObject; 47 import org.jahia.exceptions.JahiaException; 48 49 import java.sql.Connection ; 50 import java.sql.ResultSet ; 51 import java.sql.SQLException ; 52 import java.sql.Statement ; 53 import java.util.HashMap ; 54 import java.util.Map ; 55 56 57 public class JahiaIncrementorsDBBaseService extends JahiaIncrementorsDBService { 58 59 private static org.apache.log4j.Logger logger = 60 org.apache.log4j.Logger.getLogger (JahiaIncrementorsDBBaseService.class); 61 62 63 private static JahiaIncrementorsDBBaseService instance = null; 64 65 private static Map tableNames = new HashMap (); 66 67 72 protected JahiaIncrementorsDBBaseService () { 73 logger.debug ("***** Starting the Jahia Incrementors DB Base Service *****"); 74 } 75 76 77 82 public static synchronized JahiaIncrementorsDBBaseService getInstance () { 83 if (instance == null) { 84 instance = new JahiaIncrementorsDBBaseService (); 85 } 86 return instance; 87 } 88 89 90 95 public int autoIncrement (String tableName) 96 throws JahiaException { 97 98 synchronized(tableNames) { 99 if (tableNames.containsKey(tableName)) { 101 tableName = (String ) tableNames.get(tableName); 102 } else { 103 tableNames.put(tableName, tableName); 104 } 105 } 106 107 try { 108 synchronized(tableName) { 109 int lastID = getLastID (tableName); 111 112 lastID++; 114 updateLastID (tableName, lastID); 115 return lastID; 116 } 117 } 118 catch (JahiaException je) { 120 logger.debug ("error in autoIncrement -> BAILING OUT", je); 121 throw je; 122 } 123 } 124 125 126 131 private int getLastID (String tableName) 132 throws JahiaException { 133 Connection dbConn = null; 134 Statement stmt = null; 135 ResultSet rs = null; 136 int lastID = 0; 137 int thereCanBeOnlyOne = 0; 138 boolean success = false; 139 140 try { 141 int current = 0; 142 String sqlQuery = ""; 144 sqlQuery += "SELECT jahia_autoids_currentindex FROM jahia_autoids "; 145 sqlQuery += "WHERE jahia_autoids_tablename='" + tableName + "'"; 146 147 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 148 149 stmt = dbConn.createStatement (); 151 rs = stmt.executeQuery (sqlQuery); 152 153 while (rs.next ()) { 155 current = rs.getInt ("jahia_autoids_currentindex"); 156 if (current > thereCanBeOnlyOne) { 157 thereCanBeOnlyOne = current; 158 } 159 } 160 161 success = true; 162 163 } catch (SQLException se) { 164 String errorMsg = "Error in finding last ID : " + se.getMessage (); 165 logger.debug (errorMsg, se); 166 throw new JahiaException ("Cannot access database incrementors", 167 errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL_SEVERITY, se); 168 } finally { 169 try { 170 171 if (stmt != null) stmt.close (); 172 } catch (SQLException ex) { 174 logger.debug ("Error while freeing connection or statement", ex); 175 throw new JahiaException ("Cannot free resources", 176 "getLastID : cannot free resources", 177 JahiaException.DATABASE_ERROR, JahiaException.WARNING_SEVERITY, ex); 178 } 179 } 180 if (success) { 181 if (thereCanBeOnlyOne == 0) { 183 createNewID (tableName); 184 } 185 186 lastID = thereCanBeOnlyOne; 187 } 188 return lastID; 189 } 190 191 192 198 private synchronized void createNewID (String tableName) 199 throws JahiaException { 200 Connection dbConn = null; 201 Statement stmt = null; 202 try { 203 String sqlQuery = ""; 205 sqlQuery += "INSERT INTO jahia_autoids (jahia_autoids_tablename, jahia_autoids_currentindex) "; 206 sqlQuery += "VALUES('" + tableName + "',1)"; 207 208 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 209 210 stmt = dbConn.createStatement (); 212 stmt.executeUpdate (sqlQuery); 213 214 logger.debug ("Creating new ID for " + tableName); 215 216 } catch (SQLException se) { 217 String errorMsg = "Error in createNewID : " + se.getMessage (); 218 logger.debug (errorMsg, se); 219 throw new JahiaException ("Cannot create new identifiers in the database", 220 errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL_SEVERITY, se); 221 } finally { 222 try { 223 224 if (stmt != null) stmt.close (); 225 } catch (SQLException ex) { 226 logger.debug ("Error while freeing connection or statement", ex); 227 throw new JahiaException ("Cannot free resources", 228 "createNewID : cannot free resources", 229 JahiaException.DATABASE_ERROR, JahiaException.WARNING_SEVERITY, ex); 230 } 231 } 232 } 233 234 235 241 private synchronized void updateLastID (String tableName, int lastID) 242 throws JahiaException { 243 Statement stmt = null; 244 Connection dbConn = null; 245 246 try { 247 StringBuffer sqlQuery = new StringBuffer ("UPDATE jahia_autoids SET "); 249 sqlQuery.append ("jahia_autoids_currentindex="); 250 sqlQuery.append (lastID); 251 sqlQuery.append (" WHERE jahia_autoids_tablename='" + tableName + "'"); 252 253 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 254 255 stmt = dbConn.createStatement (); 257 stmt.executeUpdate (sqlQuery.toString ()); 258 259 } catch (SQLException se) { 260 String errorMsg = "Error in updateLastID : " + se.getMessage (); 261 logger.debug ("Cannot update auto-ids for table " + tableName + " : " + errorMsg, se); 262 throw new JahiaException ("Cannot update identifiers in the database", 263 errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL_SEVERITY, se); 264 } finally { 265 try { 266 267 if (stmt != null) stmt.close (); 268 } catch (SQLException ex) { 269 logger.debug ("Error while freeing connection or statement", ex); 270 throw new JahiaException ("Cannot free resources", 271 "updateLastID : cannot free resources", 272 JahiaException.DATABASE_ERROR, JahiaException.WARNING_SEVERITY, ex); 273 } 274 } 275 } 276 277 278 283 public JahiaDOMObject getAutoIdsAsDOM () 284 throws JahiaException { 285 286 Connection dbConn = null; 287 Statement statement = null; 288 ResultSet rs = null; 289 290 JahiaDBDOMObject dom = null; 291 292 try { 293 String sqlQuery = "SELECT * FROM jahia_autoids"; 294 295 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 296 statement = dbConn.createStatement (); 297 if (statement != null) { 298 rs = statement.executeQuery (sqlQuery); 299 if (rs != null) { 300 dom = new JahiaDBDOMObject (); 301 dom.addTable ("jahia_autoids", rs); 302 return dom; 303 } 304 } 305 } catch (SQLException se) { 306 String errorMsg = "Error in getAutoIdsAsDOM() : " + se.getMessage (); 307 logger.debug ("Error while exporting auto-ids as DOM ", se); 308 throw new JahiaException ("Cannot load data from the database", 309 errorMsg, JahiaException.DATABASE_ERROR, 310 JahiaException.CRITICAL_SEVERITY, se); 311 } finally { 312 try { 313 rs.close (); 314 rs = null; 315 } catch (Throwable t) { 316 logger.debug ("Error while trying to free ResultSet object", t); 317 } 318 closeStatement (statement); 319 } 320 321 return dom; 322 } 323 324 325 326 private void closeStatement (Statement statement) { 327 try { 329 if (statement != null) { 330 statement.close (); 331 } 332 } catch (SQLException sqlEx) { 333 logger.debug ("Error while closing statement", sqlEx); 336 } 337 } 338 339 } 340 | Popular Tags |