1 13 package org.jahia.services.filemanager; 14 15 16 import org.jahia.data.JahiaDBDOMObject; 17 import org.jahia.data.JahiaDOMObject; 18 import org.jahia.exceptions.JahiaException; 19 import org.jahia.registries.ServicesRegistry; 20 21 import java.sql.Connection ; 22 import java.sql.ResultSet ; 23 import java.sql.SQLException ; 24 import java.sql.Statement ; 25 26 27 33 public class FilemanagerDB { 34 private static org.apache.log4j.Logger logger = 35 org.apache.log4j.Logger.getLogger (FilemanagerDB.class); 36 37 private static boolean m_debugQuery = false; 38 private static FilemanagerDB m_instance = null; 39 40 43 protected FilemanagerDB () { 44 } 45 46 52 public static FilemanagerDB getInstance () { 53 if (m_instance == null) { 54 m_instance = new FilemanagerDB (); 55 } 56 return m_instance; 57 } 58 59 60 68 public boolean insert (Filemanager fmng) { 69 boolean success = false; 70 Connection dbConn = null; 71 Statement stmt = null; 72 ResultSet rs = null; 73 74 try { 75 int id = ServicesRegistry.getInstance ().getJahiaIncrementorsDBService () 76 .autoIncrement ("jahia_filemgr"); 77 78 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 80 81 82 String query = " insert into jahia_filemgr (id_jahia_filemgr,storagepath_jahia_filemgr,ownerid_jahia_filemgr)" + 84 " values(" 85 + id + ",'" 86 + fmng.getStoragePath () + "'," 87 + fmng.getOwnerID () + ")"; 88 89 stmt = dbConn.createStatement (); 90 logger.debug (query); 91 stmt.execute (query); 92 93 query = 95 "select * from jahia_filemgr where ownerid_jahia_filemgr=" + fmng.getOwnerID (); 96 97 logger.debug (query); 98 rs = stmt.executeQuery (query); 99 while (rs.next ()) { 100 int filemanagerID = rs.getInt ("id_jahia_filemgr"); 101 fmng.setFilemanagerID (filemanagerID); 102 } 103 success = true; 104 } catch (SQLException ex) { 105 processSQLException ("FilemanagerDB::insert()", ex); 106 } catch (JahiaException je) { 107 logger.error (je.getMessage ()); 108 } finally { 109 try { 110 111 if (stmt != null) stmt.close (); 112 } catch (SQLException ex) { 113 processSQLException ("FilemanagerDB:: insert() freeing connection fails", ex); 114 } 115 } 116 117 return success; 118 119 } 120 121 129 public boolean update (Filemanager fmng) { 130 boolean success = false; 131 Connection dbConn = null; 132 Statement stmt = null; 133 134 try { 135 136 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 138 139 String query = " update jahia_filemgr set " + 141 "storagepath_jahia_filemgr='" + fmng.getStoragePath () + "'," + 142 "ownerid_jahia_filemgr=" + fmng.getOwnerID () + 143 " where id_jahia_filemgr=" + fmng.getFilemanagerID (); 144 145 stmt = dbConn.createStatement (); 146 logger.debug (query); 147 stmt.executeUpdate (query); 148 success = true; 149 } catch (SQLException ex) { 150 processSQLException ("FilemanagerDB::update()", ex); 151 } finally { 152 try { 153 154 if (stmt != null) stmt.close (); 155 } catch (SQLException ex) { 156 processSQLException ("FilemanagerDB:: update() freeing connection fails", ex); 157 } 158 return success; 159 } 160 } 161 162 163 171 public boolean delete (int filemanagerID) { 172 boolean success = false; 173 Connection dbConn = null; 174 Statement stmt = null; 175 176 try { 177 178 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 180 181 String query = " delete from jahia_filemgr where id_jahia_filemgr=" + filemanagerID; 183 184 stmt = dbConn.createStatement (); 185 logger.debug (query); 186 stmt.execute (query); 187 success = true; 188 } catch (SQLException ex) { 189 processSQLException ("FilemanagerDB::delete()", ex); 190 } finally { 191 try { 192 193 if (stmt != null) stmt.close (); 194 } catch (SQLException ex) { 195 processSQLException ("FilemanagerDB:: update() freeing connection fails", ex); 196 } 197 return success; 198 } 199 200 } 201 202 203 211 public Filemanager getFilemanager (int filemanagerID) { 212 213 Filemanager fmng = null; 214 Connection dbConn = null; 215 Statement stmt = null; 216 ResultSet rs = null; 217 218 try { 219 220 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 222 223 String query = " select * from jahia_filemgr where id_jahia_filemgr=" + filemanagerID; 225 226 stmt = dbConn.createStatement (); 227 logger.debug (query); 228 rs = stmt.executeQuery (query); 229 while (rs.next ()) { 230 String storagePath = rs.getString ("storagepath_jahia_filemgr"); 231 int ownerID = rs.getInt ("ownerid_jahia_filemgr"); 232 fmng = new Filemanager (); 233 fmng.setStoragePath (storagePath); 234 fmng.setFilemanagerID (filemanagerID); 235 fmng.setOwnerID (ownerID); 236 237 } 238 } catch (SQLException ex) { 239 processSQLException ("FilemanagerDB::getFilemanager(id)", ex); 240 } finally { 241 try { 242 243 if (stmt != null) stmt.close (); 244 } catch (SQLException ex) { 245 processSQLException ( 246 "FilemanagerDB:: getFilemanager(filemanagerID) freeing connection fails", 247 ex); 248 } 249 } 250 251 return fmng; 252 253 } 254 255 256 264 public Filemanager getFilemanagerByOwner (int ownerID) { 265 266 Filemanager fmng = null; 267 Connection dbConn = null; 268 Statement stmt = null; 269 ResultSet rs = null; 270 271 try { 272 273 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 275 276 String query = "select * from jahia_filemgr where ownerid_jahia_filemgr=" + ownerID; 278 279 stmt = dbConn.createStatement (); 280 logger.debug (query); 281 rs = stmt.executeQuery (query); 282 while (rs.next ()) { 283 int filemanagerID = rs.getInt ("id_jahia_filemgr"); 284 String storagePath = rs.getString ("storagepath_jahia_filemgr"); 285 fmng = new Filemanager (); 286 fmng.setFilemanagerID (filemanagerID); 287 fmng.setStoragePath (storagePath); 288 fmng.setOwnerID (ownerID); 289 } 290 } catch (SQLException ex) { 291 processSQLException ("FilemanagerDB::getFilemanagerByOwner(id)", ex); 292 } finally { 293 try { 294 295 if (stmt != null) stmt.close (); 296 } catch (SQLException ex) { 297 processSQLException ( 298 "FilemanagerDB:: getFilemanager(ownerID) freeing connection fails", 299 ex); 300 } 301 302 } 303 304 return fmng; 305 306 } 307 308 309 319 public JahiaDOMObject getFileMgrAsDOM (int siteID) 320 throws JahiaException { 321 322 Connection dbConn = null; 323 Statement statement = null; 324 325 String output = null; 326 JahiaDBDOMObject dom = null; 327 328 try { 329 String sqlQuery = "SELECT * FROM jahia_filemgr where ownerid_jahia_filemgr=" + siteID; 330 331 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 332 statement = dbConn.createStatement (); 333 if (statement != null) { 334 ResultSet rs = statement.executeQuery (sqlQuery); 335 if (rs != null) { 336 dom = new JahiaDBDOMObject (); 337 dom.addTable ("jahia_filemgr", rs); 338 return dom; 339 } 340 } 341 } catch (SQLException se) { 342 String errorMsg = "Error in getFileMgrAsDOM(int siteID) : " + se.getMessage (); 343 logger.error (errorMsg); 344 throw new JahiaException ("Cannot load filemanager from the database", 345 errorMsg, JahiaException.DATABASE_ERROR, 346 JahiaException.CRITICAL_SEVERITY); 347 } finally { 348 349 closeStatement (statement); 350 } 351 352 return dom; 353 } 354 355 356 366 public JahiaDOMObject getFileMgrFilesAsDOM (int siteID) 367 throws JahiaException { 368 369 Connection dbConn = null; 370 Statement statement = null; 371 372 String output = null; 373 JahiaDBDOMObject dom = null; 374 375 376 try { 377 378 Filemanager fmng = FilemanagerDB.getInstance ().getFilemanagerByOwner (siteID); 379 if (fmng == null) { 380 dom = new JahiaDBDOMObject (); 381 dom.addTable ("jahia_filemgr_files", null); 382 return dom; 383 } 384 385 String sqlQuery = "SELECT * FROM jahia_filemgr_files where filemgrid_jahia_file=" + fmng.getFilemanagerID (); 386 387 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 388 statement = dbConn.createStatement (); 389 if (statement != null) { 390 ResultSet rs = statement.executeQuery (sqlQuery); 391 if (rs != null) { 392 dom = new JahiaDBDOMObject (); 393 dom.addTable ("jahia_filemgr_files", rs); 394 return dom; 395 } 396 } 397 } catch (SQLException se) { 398 String errorMsg = "Error in getFileMgrFilesAsDOM(int siteID) : " + se.getMessage (); 399 logger.error (errorMsg); 400 throw new JahiaException ("Cannot load filemanager files from the database", 401 errorMsg, JahiaException.DATABASE_ERROR, 402 JahiaException.CRITICAL_SEVERITY); 403 } finally { 404 405 closeStatement (statement); 406 } 407 408 return dom; 409 } 410 411 412 private void closeStatement (Statement statement) { 413 try { 415 if (statement != null) { 416 statement.close (); 417 } 418 } catch (SQLException sqlEx) { 419 JahiaException je = new JahiaException ("Cannot close a statement", 422 "Cannot close a statement", JahiaException.DATABASE_ERROR, 423 JahiaException.WARNING_SEVERITY, sqlEx); 424 logger.error ("Error:", je); 425 } 426 } 427 428 429 437 public static String filter (String input) { 438 StringBuffer filtered = new StringBuffer (input.length ()); 439 char c; 440 for (int i = 0; i < input.length (); i++) { 441 c = input.charAt (i); 442 if (c == '\'') { 443 filtered.append ("'"); 444 } else if (c == '"') { 445 filtered.append ("""); 446 } else { 447 filtered.append (c); 448 } 449 } 450 return (filtered.toString ()); 451 } 452 453 454 461 public void toConsole (String msg) { 462 if (false) { 463 } 465 } 466 467 468 475 public void processSQLException (String func, SQLException ex) { 476 if (m_debugQuery) { 477 478 while (ex != null) { 479 logger.debug ( 480 "SQL EXCEPTION in function [" + func + "]: SqlState = " + ex.getSQLState () + " Error code = " + ex.getErrorCode () + " Error msg = " + ex.toString ()); 481 ex = ex.getNextException (); 482 } 483 } 484 } 485 486 } 487 488 | Popular Tags |