1 40 41 42 package org.jahia.services.audit; 43 44 import java.sql.Connection ; 45 import java.sql.ResultSet ; 46 import java.sql.SQLException ; 47 import java.sql.Statement ; 48 import java.text.DateFormat ; 49 import java.util.ArrayList ; 50 import java.util.HashMap ; 51 import java.util.HashSet ; 52 import java.util.List ; 53 import java.util.Set ; 54 import java.util.Vector ; 55 56 import org.jahia.data.JahiaDBDOMObject; 57 import org.jahia.data.JahiaDOMObject; 58 import org.jahia.data.containers.JahiaContainer; 59 import org.jahia.data.containers.JahiaContainerList; 60 import org.jahia.data.events.JahiaEvent; 61 import org.jahia.data.fields.JahiaField; 62 import org.jahia.exceptions.JahiaException; 63 import org.jahia.params.ParamBean; 64 import org.jahia.registries.ServicesRegistry; 65 import org.jahia.services.database.JahiaIncrementorsDBService; 66 import org.jahia.services.pages.JahiaPage; 67 import org.jahia.services.pages.JahiaPageDefinition; 68 import org.jahia.services.sites.JahiaSite; 69 import org.jahia.services.usermanager.JahiaUser; 70 import org.jahia.settings.SettingsBean; 71 import org.jahia.utils.JahiaObjectTool; 72 73 74 public class JahiaDBAuditLogManagerService extends JahiaAuditLogManagerService { 75 76 private static org.apache.log4j.Logger logger = 77 org.apache.log4j.Logger.getLogger (JahiaDBAuditLogManagerService.class); 78 79 public static final String TABLE_NAME = "jahia_audit_log"; 80 public static final int FIELD_TYPE = JahiaObjectTool.FIELD_TYPE; 81 public static final int CONTAINER_TYPE = JahiaObjectTool.CONTAINER_TYPE; 82 public static final int CONTAINERLIST_TYPE = JahiaObjectTool.CONTAINERLIST_TYPE; 83 public static final int PAGE_TYPE = JahiaObjectTool.PAGE_TYPE; 84 public static final int TEMPLATE_TYPE = JahiaObjectTool.TEMPLATE_TYPE; 85 public static final int ACL_TYPE = JahiaObjectTool.ACL_TYPE; 86 87 public static final String ENTRY_ID = "ENTRY_ID"; 88 public static final String TIME = "TIME"; 89 public static final String USER_ID = "USER_ID"; 90 public static final String OBJECT_TYPE = "OBJECT_TYPE"; 91 public static final String OBJECT_ID = "OBJECT_ID"; 92 public static final String CONTENT = "CONTENT"; 93 94 private static final String MSG_INTERNAL_ERROR = new String ("Audit Log Manager internal error"); 95 private static final String JSP_FILE = "/jsp/jahia/engines/logs/viewlogs.jsp"; 96 97 98 private static JahiaDBAuditLogManagerService instance = null; 99 private JahiaIncrementorsDBService mIncrementorService = null; 100 101 private int maxLogs; 102 103 104 109 private JahiaDBAuditLogManagerService () 110 throws JahiaException { 111 logger.info ("***** Starting AuditLogManager *****"); 112 113 ServicesRegistry registry = ServicesRegistry.getInstance (); 115 if (registry != null) { 116 mIncrementorService = registry.getJahiaIncrementorsDBService (); 117 if (mIncrementorService == null) { 118 throw new JahiaException (MSG_INTERNAL_ERROR, "Audit Log manager could not get the Incrementors DB Service instance.", 119 JahiaException.SERVICE_ERROR, JahiaException.CRITICAL_SEVERITY); 120 } 121 122 } else { 123 throw new JahiaException (MSG_INTERNAL_ERROR, "Audit Log manager could not get the Service Registry instance.", 124 JahiaException.REGISTRY_ERROR, JahiaException.CRITICAL_SEVERITY); 125 } 126 } 128 129 132 public static synchronized JahiaDBAuditLogManagerService getInstance () 133 throws JahiaException { 134 if (instance == null) { 135 instance = new JahiaDBAuditLogManagerService (); 136 } 137 return instance; 138 } 139 140 141 149 public boolean logEvent (JahiaEvent je, int objectType, String operationStr) { 150 long time; 151 String timeStr = ""; 152 String userNameStr = "jahia"; 153 String objTypeStr = ""; 154 String objIDStr = "0"; 155 String parentObjIDStr = "0"; 156 String parentObjTypeStr = ""; 157 String siteKey = ""; 158 String contentStr = ""; 159 160 int entryID; 162 try { 163 entryID = mIncrementorService.autoIncrement ("jahia_audit_log"); 164 } catch (JahiaException ex) { 166 logger.debug ("Exception !!! Could not get a new entry ID from the incrementor DB"); 167 return false; 168 } 169 170 try { 171 time = je.getEventTime (); 172 timeStr = new Long (je.getEventTime ()).toString (); 173 ParamBean jParams = je.getParams (); 174 if (jParams != null) { userNameStr = (String )jParams.getUser ().getUsername (); 176 JahiaSite site = (JahiaSite)jParams.getSession ().getAttribute (ParamBean.SESSION_SITE); 177 if (site != null) { 178 siteKey = site.getSiteKey (); 179 } 180 objTypeStr = new Integer (objectType).toString (); 181 objIDStr = Integer.toString (getObjectID (je, objectType)); 182 String [] parent = getParent (je, objectType); 183 parentObjIDStr = parent[0]; 184 parentObjTypeStr = parent[1]; 185 contentStr = JahiaObjectTool.getInstance ().getObjectName (objectType, getObjectID (je, objectType), jParams); 186 187 return insertAuditLogEntry (entryID, timeStr, userNameStr, objTypeStr, objIDStr, parentObjIDStr, parentObjTypeStr, siteKey, operationStr, contentStr); 188 } 189 return false; 190 } catch (JahiaException jex) { 191 return false; 192 } 193 } 195 196 204 public ArrayList getLog (int objectType, int objectID, ParamBean jParams) { 205 ArrayList logData = new ArrayList (); 206 207 Connection dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 209 if (dbConn == null) { 210 return logData; 211 } 212 213 List childrenObjectList = getAllChildren (dbConn, objectType, objectID, null); 215 216 Statement stmt = null; 218 StringBuffer query = null; 219 220 try { 221 stmt = dbConn.createStatement (); 222 query = new StringBuffer ("SELECT * FROM " + TABLE_NAME); 223 query.append(" WHERE (objecttype_jahia_audit_log="); 224 query.append(Integer.toString (objectType)); 225 query.append(" AND objectid_jahia_audit_log="); 226 query.append(Integer.toString (objectID)); 227 query.append(")"); 228 if (!childrenObjectList.isEmpty ()) { 229 for (int i = 0; i < childrenObjectList.size (); i++) { 230 Integer [] thisChildObject = (Integer [])childrenObjectList.get (i); 231 query.append(" OR (objecttype_jahia_audit_log="); 232 query.append(thisChildObject[0].toString ()); query.append(" AND objectid_jahia_audit_log="); 234 query.append(thisChildObject[1].toString ()); query.append(")"); 236 } 237 } 238 query.append(" ORDER BY time_jahia_audit_log DESC"); 239 logger.debug("querySize=" + query.length() + " bytes"); 240 241 ResultSet rs = stmt.executeQuery (query.toString()); 242 logData = buildLogEntriesList (rs, logData, jParams); 243 } catch (SQLException sqlEx) { 244 logger.debug ("SQL Exception occurred!" + sqlEx.getMessage () + " for query " + query); 245 } finally { 246 247 CloseStatement (stmt); 248 } 249 return logData; 250 } 252 253 260 public ArrayList getLog (ParamBean jParams) { 261 ArrayList logData = new ArrayList (); 262 Connection dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 264 if (dbConn == null) { 265 return logData; 266 } 267 268 Statement stmt = null; 270 String query = ""; 271 String maxLogs = SettingsBean.readJahiaPropertiesFile ().getProperty ("jahiaMaxLogs"); 272 273 try { 274 stmt = dbConn.createStatement (); 275 query = "SELECT * FROM " + TABLE_NAME + " ORDER BY time_jahia_audit_log DESC"; 276 ResultSet rs = stmt.executeQuery (query); 277 logData = buildLogEntriesList (rs, logData, jParams); 278 } catch (SQLException sqlEx) { 279 logger.debug ("SQL Exception occurred!" + sqlEx.getMessage () + " for query " + query); 280 } finally { 281 282 CloseStatement (stmt); 283 } 284 return logData; 285 } 287 288 296 private ArrayList buildLogEntriesList (ResultSet rs, ArrayList logData, ParamBean jParams) { 297 int i = 0; 298 299 try { 300 while (rs.next ()) { 301 int jahiaID = rs.getInt ("id_jahia_audit_log"); 302 String time = rs.getString ("time_jahia_audit_log"); 303 String username = rs.getString ("username_jahia_audit_log"); 304 String operation = rs.getString ("operation_jahia_audit_log"); 305 int objtype = rs.getInt ("objecttype_jahia_audit_log"); 306 int objid = rs.getInt ("objectid_jahia_audit_log"); 307 String sitekey = rs.getString ("site_jahia_audit_log"); 308 String objname = rs.getString ("content_jahia_audit_log"); 309 310 long myTime = Long.parseLong (time); 312 java.util.Date myDate = new java.util.Date (myTime); 313 time = DateFormat.getDateTimeInstance (3, 3).format (myDate); 314 315 try { 317 JahiaObjectTool theInstance = JahiaObjectTool.getInstance (); 318 319 321 objname = JahiaObjectTool.getInstance ().getObjectName (objtype, objid, jParams); 322 } catch (Exception e) { 323 } 325 326 HashMap record = new HashMap (); 327 record.put ("timeStr", time); 328 record.put ("username", username); 329 record.put ("operation", operation); 330 record.put ("objecttype", Integer.toString (objtype)); 331 record.put ("objectid", Integer.toString (objid)); 332 record.put ("sitekey", sitekey); 333 record.put ("objectname", objname); 334 335 logData.add (i, record); 336 i++; 337 } 338 } catch (SQLException sqlEx) { 339 } 341 return logData; 342 } 344 345 353 public int flushLogs (int objectType, int objectID, ParamBean jParams) { 354 355 Connection dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 357 if (dbConn == null) { 358 return 0; 359 } 360 361 Vector flushItems = getAllChildren (dbConn, objectType, objectID, null); 362 int result = 0; 363 Statement stmt = null; 364 String query = ""; 365 366 try { 367 stmt = dbConn.createStatement (); 368 369 query = "DELETE FROM " + TABLE_NAME + " WHERE "; 371 query += "(objecttype_jahia_audit_log=" + Integer.toString (objectType); query += " AND objectid_jahia_audit_log=" + Integer.toString (objectID) + ")"; for (int i = 0; i < flushItems.size (); i++) { Integer [] row = (Integer [])flushItems.get (i); 375 query += " OR (objecttype_jahia_audit_log=" + row[0].toString (); query += " AND objectid_jahia_audit_log=" + row[1].toString () + ")"; } 378 379 result = stmt.executeUpdate (query); 381 382 if (result > 0) { 383 384 int entryID; 387 try { 388 entryID = mIncrementorService.autoIncrement ("jahia_audit_log"); 389 logger.debug ("got new entry ID = [" + Integer.toString (entryID) + "]"); 390 } catch (JahiaException ex) { 391 logger.debug ("Exception !!! Could not get a new entry ID from the incrementor DB"); 392 return result; 393 } 394 String timeStr = Long.toString ((new java.util.Date ()).getTime ()); 395 String userNameStr = (String )jParams.getUser ().getUsername (); 396 String objectTypeStr = Integer.toString (objectType); 397 String objectIDStr = Integer.toString (objectID); 398 String siteKey = ""; 399 String operationStr = "flushed logs "; 400 401 try { 403 operationStr += JahiaObjectTool.getInstance ().getObjectTypeName (objectType); 404 siteKey = ((JahiaSite)jParams.getSession ().getAttribute (ParamBean.SESSION_SITE)).getSiteKey (); 405 } catch (JahiaException je) { 406 ; } 408 insertAuditLogEntry (entryID, timeStr, userNameStr, objectTypeStr, objectIDStr, "0", "0", siteKey, operationStr, ""); 414 } 415 } catch (SQLException sqlEx) { 416 logger.debug ("SQL Exception occured!" + sqlEx.getMessage ()); 417 } finally { 418 419 CloseStatement (stmt); 420 } 421 return result; 422 } 424 425 433 public boolean flushLogs (JahiaUser theUser, Integer maxlogsdays) { 434 435 String oldestEntryTime = "999999999999999"; 436 437 Connection dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 439 if (dbConn == null) { 440 return false; 441 } 442 Statement stmt = null; 443 String query = ""; 444 445 446 if (maxlogsdays != null) { 447 oldestEntryTime = Long.toString (System.currentTimeMillis () - (maxlogsdays.intValue () * 86400000)); 448 oldestEntryTime = padTimeString (oldestEntryTime); 449 } 450 451 try { 452 453 stmt = dbConn.createStatement (); 454 455 query = "DELETE FROM " + TABLE_NAME; 457 query += " WHERE time_jahia_audit_log<" + "'" + oldestEntryTime + "'"; 458 459 stmt.executeUpdate (query); 461 462 query = "SELECT id_jahia_audit_log FROM " + TABLE_NAME; 464 query += " WHERE time_jahia_audit_log<" + "'" + oldestEntryTime + "'"; 465 466 ResultSet rs = stmt.executeQuery (query); 467 468 if (!rs.next ()) { 470 471 int entryID; 473 try { 474 entryID = mIncrementorService.autoIncrement ("jahia_audit_log"); 475 logger.debug ("got new entry ID = [" + Integer.toString (entryID) + "]"); 476 } catch (JahiaException ex) { 477 logger.debug ("Exception !!! Could not get a new entry ID from the incrementor DB"); 478 return false; 479 } 480 String timeStr = Long.toString ((new java.util.Date ()).getTime ()); 481 String userNameStr = theUser.getUsername (); 482 String objectTypeStr = Integer.toString (JahiaObjectTool.SERVER_TYPE); 483 String objectIDStr = "0"; 484 String operationStr = ""; 485 if (maxlogsdays != null) { 486 operationStr = "flushed logs > " + maxlogsdays + " days"; 487 } else { 488 operationStr = "flushed all logs "; 489 } 490 491 insertAuditLogEntry (entryID, timeStr, userNameStr, objectTypeStr, objectIDStr, "0", "0", "", operationStr, ""); 492 493 } 494 } catch (SQLException sqlEx) { 495 logger.debug ("An SQL Exception occurred: " + sqlEx.getMessage ()); 496 } finally { 497 498 CloseStatement (stmt); 499 } 500 return true; 501 } 503 504 511 public boolean flushSiteLogs (JahiaUser theUser, String siteKey) { 512 513 514 Connection dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 516 if (dbConn == null) { 517 return false; 518 } 519 Statement stmt = null; 520 String query = ""; 521 522 523 try { 524 stmt = dbConn.createStatement (); 525 query = "DELETE FROM " + TABLE_NAME + " WHERE site_jahia_audit_log='" + siteKey + "'"; 527 stmt.executeUpdate (query); 529 } catch (SQLException sqlEx) { 530 logger.debug ("SQL Exception occured!" + sqlEx.getMessage ()); 531 } finally { 532 533 CloseStatement (stmt); 534 } 535 return true; 536 } 538 539 551 private boolean insertAuditLogEntry (int entryID, 552 String timeStr, 553 String userNameStr, 554 String objectTypeStr, 555 String objectIDStr, 556 String parentObjIDStr, 557 String parentTypeStr, 558 String siteKey, 559 String operationStr, 560 String contentStr) { 561 Connection dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 563 if (dbConn == null) { 564 return false; 565 } 566 int maxLogs = 500; 568 int numRows = 0; 569 boolean result = true; 570 Statement statement = null; 571 String query = ""; 572 573 574 timeStr = padTimeString (timeStr); 578 579 try { 580 statement = dbConn.createStatement (); 581 582 maxLogs = Integer.parseInt (SettingsBean.readJahiaPropertiesFile ().getProperty ("jahiaMaxLogs")); 584 query = "SELECT COUNT(*) AS numRows FROM " + TABLE_NAME; 585 ResultSet rs = statement.executeQuery (query); 586 if (rs.next ()) { 587 numRows = rs.getInt ("numRows"); 588 } 589 590 if ((numRows + 1) > maxLogs) { 591 deleteOldestRow (); 592 } 593 594 query = "INSERT INTO " + TABLE_NAME + " (id_jahia_audit_log, time_jahia_audit_log, username_jahia_audit_log, objecttype_jahia_audit_log, operation_jahia_audit_log, objectid_jahia_audit_log, parentid_jahia_audit_log, parenttype_jahia_audit_log, site_jahia_audit_log, content_jahia_audit_log ) VALUES (" 596 + Integer.toString (entryID) 597 + ",'" + timeStr 598 + "','" + userNameStr 599 + "'," + objectTypeStr 600 + ",'" + operationStr 601 + "'," + objectIDStr 602 + "," + parentObjIDStr 603 + "," + parentTypeStr 604 + ",'" + siteKey 605 + "','" + contentStr + "')"; 606 607 statement.executeUpdate (query); 608 } catch (SQLException sqlEx) { 609 logger.debug ("SQL Exception occured!" + sqlEx.getMessage ()); 610 result = false; 611 } catch (NumberFormatException nfe) { 612 } finally { 614 615 CloseStatement (statement); 616 } 617 return result; 618 } 619 620 621 629 private int getObjectID (JahiaEvent je, int objectType) 630 throws JahiaException { 631 try { 632 switch (objectType) { 633 634 case FIELD_TYPE: 635 return ((JahiaField)je.getObject ()).getID (); 636 case CONTAINER_TYPE: 637 return ((JahiaContainer)je.getObject ()).getID (); 638 case CONTAINERLIST_TYPE: 639 return ((JahiaContainerList)je.getObject ()).getID (); 640 case PAGE_TYPE: 641 return ((JahiaPage)je.getObject ()).getID (); 642 case TEMPLATE_TYPE: 643 return ((JahiaPageDefinition)je.getObject ()).getID (); 644 default: 645 throw new JahiaException (MSG_INTERNAL_ERROR, 646 "Incompatible Object Type passed to JahiaAuditLogManager", 647 JahiaException.SERVICE_ERROR, 648 JahiaException.CRITICAL_SEVERITY); 649 } 650 } catch (Exception e) { 651 logger.debug ("EXCEPTION MESSAGE :" + e.getMessage ()); 652 throw new JahiaException (MSG_INTERNAL_ERROR, 653 "Exception occurred while retrieving Event Object ID", 654 JahiaException.SERVICE_ERROR, 655 JahiaException.CRITICAL_SEVERITY); 656 } 657 } 659 660 668 private String [] getParent (JahiaEvent je, int objectType) 669 throws JahiaException { 670 String [] parent = new String [2]; 671 try { 672 switch (objectType) { 673 674 case FIELD_TYPE: 675 if (((JahiaField)je.getObject ()).getctnid () == 0) { 676 parent[0] = Integer.toString (((JahiaField)je.getObject ()).getPageID ()); 677 parent[1] = Integer.toString (PAGE_TYPE); 678 return parent; 679 } else { 680 parent[0] = Integer.toString (((JahiaField)je.getObject ()).getctnid ()); 681 parent[1] = Integer.toString (CONTAINER_TYPE); 682 return parent; 683 } 684 685 case CONTAINER_TYPE: 686 parent[0] = Integer.toString (((JahiaContainer)je.getObject ()).getListID ()); 687 parent[1] = Integer.toString (CONTAINERLIST_TYPE); 688 return parent; 689 690 case CONTAINERLIST_TYPE: 691 if (((JahiaContainerList)je.getObject ()).getParentEntryID () == 0) { 692 parent[0] = Integer.toString (((JahiaContainerList)je.getObject ()).getPageID ()); 693 parent[1] = Integer.toString (PAGE_TYPE); 694 return parent; 695 } else { 696 parent[0] = Integer.toString (((JahiaContainerList)je.getObject ()).getParentEntryID ()); 697 parent[1] = Integer.toString (CONTAINER_TYPE); 698 return parent; 699 } 700 701 case PAGE_TYPE: 702 parent[0] = Integer.toString (((JahiaPage)je.getObject ()).getParentID ()); 703 parent[1] = Integer.toString (PAGE_TYPE); 704 return parent; 705 706 case TEMPLATE_TYPE: 707 parent[0] = "-1"; parent[1] = Integer.toString (TEMPLATE_TYPE); 709 return parent; 710 711 default: 712 throw new JahiaException (MSG_INTERNAL_ERROR, 713 "Incompatible Object Type passed to JahiaAuditLogManager", 714 JahiaException.SERVICE_ERROR, 715 JahiaException.CRITICAL_SEVERITY); 716 } 717 } catch (Exception e) { 718 logger.debug ("EXCEPTION MESSAGE :" + e.getMessage ()); 719 throw new JahiaException (MSG_INTERNAL_ERROR, 720 "Exception occurred while retrieving Event Object ID", 721 JahiaException.SERVICE_ERROR, 722 JahiaException.CRITICAL_SEVERITY); 723 } 724 } 725 726 727 736 private Vector getChildrenList (Connection dbConn, int objectType, int objectID) { 737 738 Statement stmt = null; 739 ResultSet rs = null; 740 Vector childrenVect = new Vector (); 741 String table = ""; 742 String objectWord = ""; 743 String query = ""; 744 745 try { 747 stmt = dbConn.createStatement (); 748 Set addedChilds = new HashSet (); 749 750 switch (objectType) { 751 752 case LoggingEventListener.FIELD_TYPE: 754 break; 755 756 case LoggingEventListener.CONTAINERLIST_TYPE: 758 objectWord = "list"; 759 table = "jahia_ctn_entries"; 760 query = makeQuery (table, objectWord, objectID); 761 rs = stmt.executeQuery (query); 762 while (rs.next ()) { 763 Integer [] child = new Integer [2]; 764 child[0] = new Integer (JahiaObjectTool.CONTAINER_TYPE); 765 child[1] = new Integer (rs.getString ("id_" + table)); 766 if (!addedChilds.contains ((Integer )child[1])) { 767 childrenVect.addElement (child); 768 } 769 } 770 break; 771 772 case LoggingEventListener.CONTAINER_TYPE: 774 objectWord = "ctn"; 775 table = "jahia_fields_data"; 776 query = makeQuery (table, objectWord, objectID); 777 rs = stmt.executeQuery (query); 778 779 if (rs.next ()) { 781 while (rs.next ()) { 782 Integer [] child = new Integer [2]; 783 child[0] = new Integer (JahiaObjectTool.FIELD_TYPE); 784 child[1] = new Integer (rs.getString ("id_" + table)); 785 if (!addedChilds.contains ((Integer )child[1])) { 786 childrenVect.addElement (child); 787 } 788 } 789 790 } else { 792 table = "jahia_ctn_lists"; 793 query = makeQuery (table, objectWord, objectID); 794 rs = stmt.executeQuery (query); 795 while (rs.next ()) { 796 Integer [] child = new Integer [2]; 797 child[0] = new Integer (JahiaObjectTool.CONTAINERLIST_TYPE); 798 child[1] = new Integer (rs.getString ("id_" + table)); 799 if (!addedChilds.contains ((Integer )child[1])) { 800 childrenVect.addElement (child); 801 } 802 } 803 } 804 break; 805 806 case LoggingEventListener.PAGE_TYPE: 808 objectWord = "page"; 809 table = "jahia_fields_data"; 810 query = makeQuery (table, objectWord, objectID); 811 rs = stmt.executeQuery (query); 812 813 while (rs.next ()) { 815 Integer [] child = new Integer [2]; 816 child[0] = new Integer (JahiaObjectTool.FIELD_TYPE); 817 child[1] = new Integer (rs.getString ("id_" + table)); 818 if (!addedChilds.contains ((Integer )child[1])) { 819 childrenVect.addElement (child); 820 } 821 } 822 addedChilds.clear (); 823 824 table = "jahia_ctn_lists"; 826 query = makeQuery (table, objectWord, objectID); 827 rs = stmt.executeQuery (query); 828 while (rs.next ()) { 829 Integer [] child = new Integer [2]; 830 child[0] = new Integer (LoggingEventListener.CONTAINERLIST_TYPE); 831 child[1] = new Integer (rs.getString ("id_" + table)); 832 if (!addedChilds.contains ((Integer )child[1])) { 833 childrenVect.addElement (child); 834 } 835 } 836 addedChilds.clear (); 837 838 853 break; 854 } 855 } catch (SQLException sqlEx) { 856 logger.debug ("SQL Exception occured!", sqlEx); 857 } finally { 858 CloseStatement (stmt); 859 } 860 861 return childrenVect; 862 } 864 865 875 private Vector getAllChildren (Connection dbConn, int objectType, int objectID, Vector prevChildrenList) { 876 Vector fullChildrenList = (prevChildrenList == null) ? new Vector () : prevChildrenList; Vector tempChildrenList = getChildrenList (dbConn, objectType, objectID); if (!tempChildrenList.isEmpty ()) { for (int i = 0; i < tempChildrenList.size (); i++) { fullChildrenList.add (tempChildrenList.get (i)); Integer [] newChild = (Integer [])tempChildrenList.get (i); 882 Integer newObjType = newChild[0]; 883 Integer newObjID = newChild[1]; 884 getAllChildren (dbConn, newObjType.intValue (), newObjID.intValue (), fullChildrenList); } 886 } 887 return fullChildrenList; 888 } 890 891 900 private String makeQuery (String tableName, String objectWord, int objectID) { 901 StringBuffer buf = new StringBuffer (); 902 903 buf.append ("SELECT DISTINCT ").append ("id_").append (tableName); 904 buf.append (", workflow_state FROM ").append (tableName); 905 buf.append (" WHERE ").append (objectWord).append ("id_").append (tableName).append ("=").append (Integer.toString (objectID)); 906 if ("parent".equals (objectWord)) { 907 buf.append (" AND workflow_state=1 "); } else { 909 buf.append (" AND ( workflow_state=1 OR"); buf.append (" (workflow_state>1 AND version_id <> -1) ) "); 911 } 912 buf.append (" ORDER BY workflow_state DESC "); 913 return buf.toString (); 914 } 915 916 917 924 public int enforceMaxLogs (int maxLogs) { 925 926 int result = 0; 927 928 if (maxLogs == 0) { 930 return 0; 931 } 932 933 Connection dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 935 if (dbConn == null) { 936 return result; 937 } 938 939 Statement stmt = null; 941 int numRows = 0; 942 String query = ""; 943 944 try { 946 stmt = dbConn.createStatement (); 947 query = "SELECT COUNT(*) AS numRows FROM " + TABLE_NAME; 948 ResultSet rs = stmt.executeQuery (query); 949 if (rs.next ()) { 950 numRows = rs.getInt ("numRows"); 951 } 952 953 int numDeletes = numRows - maxLogs; 954 955 if (numDeletes > 0) { 957 958 query = "SELECT id_jahia_audit_log FROM " + TABLE_NAME; 960 rs = stmt.executeQuery (query); 961 for (int i = 0; i < (numDeletes - 1); i++) { 962 rs.next (); 963 } 964 if (rs.next ()) { 965 String victimID = rs.getString ("id_jahia_audit_log"); 966 967 query = "DELETE FROM " + TABLE_NAME + " WHERE id_jahia_audit_log<" + victimID + " OR id_jahia_audit_log=" + victimID; 969 result = stmt.executeUpdate (query); 970 } 971 } 972 } catch (SQLException sqlEx) { 973 logger.debug ("SQL Exception occured! " + sqlEx.getMessage ()); 974 } finally { 975 976 CloseStatement (stmt); 977 } 978 return result; 979 } 981 982 987 public boolean deleteOldestRow () { 988 989 boolean result = false; 990 991 Connection dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 993 if (dbConn == null) { 994 return result; 995 } 996 Statement stmt = null; 997 String query = ""; 998 999 try { 1000 stmt = dbConn.createStatement (); 1001 query = "SELECT MIN(id_jahia_audit_log) AS minID FROM " + TABLE_NAME; 1003 ResultSet rs = stmt.executeQuery (query); 1004 if (rs.next ()) { 1005 String victimID = rs.getString ("minID"); 1006 1007 query = "DELETE FROM " + TABLE_NAME + " WHERE id_jahia_audit_log=" + victimID; 1009 if (stmt.executeUpdate (query) > 0) { 1010 result = true; 1011 } 1012 } 1013 } catch (SQLException sqlEx) { 1014 logger.debug ("SQL Exception occured! " + sqlEx.getMessage ()); 1015 result = false; 1016 } finally { 1017 1018 CloseStatement (stmt); 1019 } 1020 return result; 1021 } 1023 1024 1034 public JahiaDOMObject getLogsAsDOM (String siteKey) 1035 throws JahiaException { 1036 1037 if (siteKey == null) { 1038 return null; 1039 } 1040 1041 Connection dbConn = null; 1042 Statement statement = null; 1043 1044 String output = null; 1045 JahiaDBDOMObject dom = null; 1046 1047 try { 1048 String sqlQuery = "SELECT * FROM jahia_audit_log where site_jahia_audit_log='" + siteKey + "'"; 1049 1050 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 1051 statement = dbConn.createStatement (); 1052 if (statement != null) { 1053 ResultSet rs = statement.executeQuery (sqlQuery); 1054 if (rs != null) { 1055 dom = new JahiaDBDOMObject (); 1056 dom.addTable ("jahia_audit_log", rs); 1057 return dom; 1058 } 1059 } 1060 } catch (SQLException se) { 1061 String errorMsg = "Error in getLogsAsDOM(int siteID) : " + se.getMessage (); 1062 logger.debug (errorMsg); 1063 throw new JahiaException ("Cannot load logs from the database", 1064 errorMsg, JahiaException.DATABASE_ERROR, 1065 JahiaException.CRITICAL_SEVERITY); 1066 } finally { 1067 1068 CloseStatement (statement); 1069 } 1070 1071 return dom; 1072 } 1073 1074 1084 1085 private String padTimeString (String timeStr) { 1086 int timeStrLen = timeStr.length (); 1087 if (timeStrLen < 15) { 1088 StringBuffer buf = new StringBuffer (); 1089 for (int i = timeStrLen; i < 15; i++) { 1090 buf.append ("0"); 1091 } 1092 timeStr = buf.append (timeStr).toString (); 1093 } 1094 return timeStr; 1095 } 1096 1097 1098 private void CloseStatement (Statement statement) { 1100 try { 1102 if (statement != null) { 1103 statement.close (); 1104 } 1105 } catch (SQLException se) { 1106 } 1109 } 1110 1111} 1112 | Popular Tags |