📄 jahiadbauditlogmanagerservice.java
字号:
private Vector getAllChildren (Connection dbConn, int objectType, int objectID, Vector prevChildrenList ) { Vector fullChildrenList = (prevChildrenList == null) ? new Vector() : prevChildrenList ; // initialize from previous if not null Vector tempChildrenList = getChildrenList( dbConn, objectType, objectID ); // create sublist on current level if( !tempChildrenList.isEmpty() ) { // CHILDREN FOUND -> for( int i = 0; i < tempChildrenList.size(); i++ ) { // loop on children list fullChildrenList.add( tempChildrenList.get(i) ); // add child to Full List Integer[] newChild = (Integer[]) tempChildrenList.get(i); Integer newObjType = newChild[0]; Integer newObjID = newChild[1]; getAllChildren( dbConn, newObjType.intValue(), newObjID.intValue(), fullChildrenList ); // call myself ! } } return fullChildrenList; }// end getAllChildren /** * compose a JDBC query to look for an objectID in a table field, given a wordmark to be followed by "id_" and the table name. * * @author Mikha雔 Janson * @param tableName name of the SQL table to perform the query on * @param objectWord the wordmark matching a <code>String</code> followed by "id_" and the table name * @param objectID * @return the SQL query string */ private String makeQuery ( String tableName, String objectWord, int objectID ) { StringBuffer buf = new StringBuffer(); buf.append("SELECT ").append("id_").append(tableName); buf.append(" FROM ").append(tableName); buf.append(" WHERE ").append(objectWord).append("id_").append(tableName).append("=").append(Integer.toString(objectID)); return buf.toString(); } /** * delete the n oldest rows in the db * * @author Mikha雔 Janson * @param maxLogs the number of rows to delete * @return the number of rows deleted */ public int enforceMaxLogs(int maxLogs) { int result = 0; // no limit is maxLogs is set to 0 if(maxLogs == 0) { return 0; } // Get a database connection Connection dbConn = getDBConnection (1002); if (dbConn == null) { return result; } // initialize default values Statement stmt = null; int numRows = 0; String query = ""; // compare number of rows currently in table with the maxLogs property value try { stmt = dbConn.createStatement(); query = "SELECT COUNT(*) AS numRows FROM " + TABLE_NAME; ResultSet rs = ServicesRegistry.getInstance().getDBPoolService().executeQuery(stmt,query); if(rs.next()) { numRows = rs.getInt("numRows"); } int numDeletes = numRows - maxLogs; // if rows need to be deleted, get the highest ID to be deleted if(numDeletes > 0) { // build the query query = "SELECT id_jahia_audit_log FROM " + TABLE_NAME; rs = ServicesRegistry.getInstance().getDBPoolService().executeQuery(stmt,query); for(int i=0; i<(numDeletes - 1); i++) { rs.next(); } if(rs.next()) { String victimID = rs.getString("id_jahia_audit_log"); // build and execute new query to delete everything up to and including the victim row query = "DELETE FROM " + TABLE_NAME + " WHERE id_jahia_audit_log<" + victimID + " OR id_jahia_audit_log=" + victimID; result = ServicesRegistry.getInstance().getDBPoolService().executeUpdate(stmt,query); } } } catch (SQLException sqlEx) { toConsole ("SQL Exception occured! " + sqlEx.getMessage()); } finally { CloseDBConnection (dbConn); CloseStatement (stmt); } return result; } // end deleteOldestRows /** * delete the oldest row in the table * * @author Mikha雔 Janson */ public boolean deleteOldestRow() { boolean result = false; // Get a database connection Connection dbConn = getDBConnection (1002); if (dbConn == null) { return result; } Statement stmt = null; String query = ""; try { stmt = dbConn.createStatement(); // build the query query = "SELECT MIN(id_jahia_audit_log) AS minID FROM " + TABLE_NAME; ResultSet rs = ServicesRegistry.getInstance().getDBPoolService().executeQuery(stmt,query); if(rs.next()) { String victimID = rs.getString("minID"); // build and execute new query to delete the victim row query = "DELETE FROM " + TABLE_NAME + " WHERE id_jahia_audit_log=" + victimID; if (ServicesRegistry.getInstance().getDBPoolService().executeUpdate(stmt,query) > 0) { result = true; } } } catch (SQLException sqlEx) { toConsole ("SQL Exception occured! " + sqlEx.getMessage()); result = false; } finally { CloseDBConnection (dbConn); CloseStatement (stmt); } return result; } // end deleteOldestRow //-------------------------------------------------------------------------- /** * return a DOM document of all logs of a site * * @param String the site key * * @return JahiaDOMObject a DOM representation of this object * * @author NK */ public JahiaDOMObject getLogsAsDOM( String siteKey ) throws JahiaException{ if ( siteKey == null ){ return null; } Connection dbConn = null; Statement statement = null; String output = null; JahiaDBDOMObject dom = null; try { String sqlQuery = "SELECT * FROM jahia_audit_log where site_jahia_audit_log='"+siteKey+"'"; dbConn = getDBConnection(0); statement = dbConn.createStatement(); if (statement != null) { ResultSet rs = ServicesRegistry.getInstance().getDBPoolService().executeQuery( statement, sqlQuery ); if (rs != null) { dom = new JahiaDBDOMObject(); dom.addTable("jahia_audit_log",rs); return dom; } } } catch (SQLException se) { String errorMsg = "Error in getLogsAsDOM(int siteID) : " + se.getMessage(); JahiaConsole.println( "JahiaDBAuditLogManagerService", errorMsg ); throw new JahiaException( "Cannot load logs from the database", errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL ); } finally { CloseDBConnection (dbConn); CloseStatement (statement); } return dom; } //-------------------------------------------------------------------------- /** * Pad a string with initial "0"s to obtain a length of 15 positions * * @param String the String to pad * * @return String the padded String * * @author NK */ private String padTimeString(String timeStr) { int timeStrLen = timeStr.length(); if (timeStrLen < 15) { StringBuffer buf = new StringBuffer(); for (int i = timeStrLen; i < 15; i++) { buf.append("0"); } timeStr = buf.append(timeStr).toString(); } return timeStr; } //------------------------------------------------------------------------- private Connection getDBConnection (int debugInfo) { Connection dbConn = null; try { dbConn = mDBPoolService.getConnection (debugInfo); } catch (NullPointerException npe) { toConsole ("Null Pointer Exception, DB Pool Service instance might be null!"); } catch (SQLException se) { toConsole ("SQL Exception: cannot get a connection."); } return dbConn; } //------------------------------------------------------------------------- private void CloseDBConnection (Connection dbConn) { if ((mDBPoolService != null) && (dbConn != null)) { try { mDBPoolService.freeConnection (dbConn); } catch (SQLException se) { // FIXME -MJ- : Don't know yet what to do with this exception. // It should be logged somewhere ! } } } //------------------------------------------------------------------------- private void CloseStatement (Statement statement) { // Close the opened statement try { if (statement!=null) { statement.close(); } } catch (SQLException se) { // FIXME -MJ- : Don't know yet what to do with this exception. // It should be logged somewhere ! } } //-------------------------------------------------------------------------- private void toConsole (String message) { JahiaConsole.println ("AuditLogManager", message); }} // end JahiaDBAuditLogManager
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -