⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jahiadbauditlogmanagerservice.java

📁 java 写的一个新闻发布系统
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
                int     jahiaID         = rs.getInt    ("id_jahia_audit_log");                String  time            = rs.getString ("time_jahia_audit_log");                String  username        = rs.getString ("username_jahia_audit_log");                String  operation       = rs.getString ("operation_jahia_audit_log");                int     objtype         = rs.getInt    ("objecttype_jahia_audit_log");                int     objid           = rs.getInt    ("objectid_jahia_audit_log");                String  sitekey         = rs.getString ("site_jahia_audit_log");                String  objname         = rs.getString ("content_jahia_audit_log");                // get a human-readable data format                long myTime           = Long.parseLong (time);                java.util.Date myDate = new java.util.Date (myTime);                time                  = DateFormat.getDateTimeInstance(3,3).format(myDate);                // get the object full name                try {                    JahiaObjectTool theInstance = JahiaObjectTool.getInstance();                    String mytest = theInstance.getObjectName (objtype, objid);                    objname = JahiaObjectTool.getInstance().getObjectName (objtype, objid);                } catch( Exception e ) {                    // do nothing... keep existing objname                }                HashMap record = new HashMap();                record.put ("timeStr", time);                record.put ("username", username);                record.put ("operation", operation);                record.put ("objecttype", Integer.toString(objtype));                record.put ("objectid", Integer.toString(objid));                record.put ("sitekey", sitekey);                record.put ("objectname", objname);                logData.add (i, record);                i++;            }        }   catch (SQLException sqlEx) {            // do nothing        }        return logData;    } // end buildLogEntriesList    /**     * utility method to flush log entries for a specific object type and  ID     *     * @author  Mikha雔 Janson     * @param   flushItems  an <code>ArrayList</code> of <code>Integer[2]</code> arrays     *                      with objectType in position 0 and objectID in position 1.     * @return              the number of rows deleted, as an <code>int</code>     */    public int flushLogs ( int objectType, int objectID, ParamBean jParams ) {        // Get a database connection        Connection  dbConn = getDBConnection (1002);        if (dbConn == null) {            return 0;        }        Vector    flushItems = getAllChildren( dbConn, objectType, objectID, null );        int       result     = 0;        Statement stmt       = null;        String    query      = "";        try {            stmt         = dbConn.createStatement();            // build the query            query      = "DELETE FROM " + TABLE_NAME + " WHERE ";            query += "(objecttype_jahia_audit_log=" + Integer.toString(objectType);         // put me in too !            query += " AND objectid_jahia_audit_log=" + Integer.toString(objectID) + ")";   // (along with my children)            for( int i = 0; i < flushItems.size(); i++) {                                   // loop on item list                Integer[] row = (Integer[]) flushItems.get(i);                query += " OR (objecttype_jahia_audit_log=" + row[0].toString();            // 1st ary elt is objectType                query += " AND objectid_jahia_audit_log=" + row[1].toString() + ")";        // 2nd ary elt is objectID            }            // delete the entries            result = ServicesRegistry.getInstance().getDBPoolService().executeUpdate(stmt,query);            if( result > 0 ) {                // if flush succeeded, log the flush itself ;o)                // Get the next available entry ID                int entryID;                try {                    entryID = mIncrementorService.autoIncrement ("jahia_audit_log");                    toConsole ("got new entry ID = ["+Integer.toString(entryID)+"]");                } catch (JahiaException ex) {                    JahiaConsole.println ("AuditLogManager", "Exception !!! Could not get a new entry ID from the incrementor DB");                    return result;                }                String timeStr       = Long.toString((new java.util.Date()).getTime());                String userNameStr   = (String) jParams.getUser().getUsername();                String objectTypeStr = Integer.toString(objectType);                String objectIDStr   = Integer.toString(objectID);                String siteKey       = "";                String operationStr  = "flushed logs ";                // looks nicer with the name of the objecttype the logs are flushed for                try {                    operationStr    += JahiaObjectTool.getInstance().getObjectTypeName(objectType);                    siteKey          = ((JahiaSite)jParams.getSession().getAttribute(ParamBean.SESSION_SITE)).getSiteKey();                } catch( JahiaException je ) {                    ;// do nothing... keep existing operationStr                }//                try {//                    siteKey       = ((JahiaSite)jParams.getSession().getAttribute(ParamBean.SESSION_SITE)).getSiteKey();//                } catch (JahiaSessionExpirationException jsee ) {//                    ;//                }                insertAuditLogEntry ( entryID, timeStr, userNameStr, objectTypeStr, objectIDStr, "0",  "0", siteKey, operationStr, "" );            }        }        catch (SQLException sqlEx) {            toConsole ("SQL Exception occured!" + sqlEx.getMessage());        }        finally {            CloseDBConnection (dbConn);            CloseStatement (stmt);        }        return result;    } // end flushLogs    /**     * overloaded method to flush all log entries older than a given number of days     *     * @author  Mikha雔 Janson     * @param   theUser     a reference to the JahiaUser object representing the user requesting the flush     * @param   maxlogsdays the number of days to keep existing log entries for     * @return              true on success, false otherwise     */    public boolean flushLogs( JahiaUser theUser, Integer maxlogsdays ) {        String oldestEntryTime = "999999999999999";        // Get a database connection        Connection  dbConn = getDBConnection (1002);        if (dbConn == null) {            return false;        }        Statement stmt   = null;        String    query  = "";        if( maxlogsdays != null ) {            oldestEntryTime = Long.toString( System.currentTimeMillis() - ( maxlogsdays.intValue() * 86400000 ) );            oldestEntryTime = padTimeString(oldestEntryTime);        }        try {            stmt   = dbConn.createStatement();            // build the query            query  = "DELETE FROM " + TABLE_NAME;            query += " WHERE time_jahia_audit_log<" + "'" + oldestEntryTime + "'";            // delete the entries            ServicesRegistry.getInstance().getDBPoolService().executeUpdate(stmt,query);            // check if there are any older records left in the table            query  = "SELECT id_jahia_audit_log FROM " + TABLE_NAME;            query += " WHERE time_jahia_audit_log<" + "'" + oldestEntryTime + "'";                    ResultSet rs = ServicesRegistry.getInstance().getDBPoolService().executeQuery(stmt,query);            // log the flush itself if it succeeded            if(!rs.next()) {                // Get the next available entry ID                int entryID;                try {                    entryID = mIncrementorService.autoIncrement ("jahia_audit_log");                    toConsole ("got new entry ID = ["+Integer.toString(entryID)+"]");                } catch (JahiaException ex) {                    JahiaConsole.println ("AuditLogManager", "Exception !!! Could not get a new entry ID from the incrementor DB");                    return false;                }                String timeStr       = Long.toString((new java.util.Date()).getTime());                String userNameStr   = theUser.getUsername();                String objectTypeStr = Integer.toString(JahiaObjectTool.SERVER_TYPE);                String objectIDStr   = "0";                String operationStr  = "";                if( maxlogsdays != null ) {                    operationStr  = "flushed logs > " + maxlogsdays + " days";                } else {                    operationStr  = "flushed all logs ";                }                insertAuditLogEntry ( entryID, timeStr, userNameStr, objectTypeStr, objectIDStr, "0",  "0", "", operationStr, "" );            }        }        catch (SQLException sqlEx) {            toConsole ("An SQL Exception occurred: " + sqlEx.getMessage());        }        finally {            CloseDBConnection (dbConn);            CloseStatement (stmt);        }        return true;    } // end flushLogs ( user, maxlogsdays )    /**     * utility method to flush all log entries of a site     *     * @author  Mikha雔 Janson     * @param   theUser     a reference to the JahiaUser object representing the administrator user requesting the flush     * @return  true on success, false on any error     */    public boolean flushSiteLogs ( JahiaUser theUser, String siteKey ){        // Get a database connection        Connection  dbConn = getDBConnection (1002);        if (dbConn == null) {            return false;        }        Statement stmt   = null;        String    query  = "";        try {            stmt   = dbConn.createStatement();            // build the query            query  = "DELETE FROM " + TABLE_NAME + " WHERE site_jahia_audit_log='" + siteKey + "'";            // delete the entries            ServicesRegistry.getInstance().getDBPoolService().executeUpdate(stmt,query);        }        catch (SQLException sqlEx) {            toConsole ("SQL Exception occured!" + sqlEx.getMessage());        }        finally {            CloseDBConnection (dbConn);            CloseStatement (stmt);        }        return true;    } // end flushSiteLogs ( user, siteID )    /**     * write a log entry to the database     *     * @author  Mikha雔 Janson     * @param   entryID     * @param   time     * @param   userNameStr     * @param   objectTypeStr     * @param   objectIDStr     * @param   operationStr     * @param   contentStr     */    private boolean insertAuditLogEntry ( int    entryID,                                          String timeStr,                                          String userNameStr,                                          String objectTypeStr,                                          String objectIDStr,                                          String parentObjIDStr,                                          String parentTypeStr,                                          String siteKey,                                          String operationStr,                                          String contentStr )    {        // Get a database connection        Connection  dbConn = getDBConnection (1002);        if (dbConn == null) {            return false;        }        // set default values        int         maxLogs   = 500;        int         numRows   = 0;        boolean     result    = true;        Statement   statement = null;        String      query     = "";                // Milliseconds time is stored as text in the database.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -