Author: brett
Date: Tue Jan 19 11:21:10 2010
New Revision: 900726

URL: http://svn.apache.org/viewvc?rev=900726&view=rev
Log:
[MRM-1269] restore pseudo-search by artifact in audit logs

Modified:
    
archiva/branches/MRM-1025/archiva-modules/archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/action/reports/ViewAuditLogReportAction.java
    
archiva/branches/MRM-1025/archiva-modules/plugins/audit/src/main/java/org/apache/archiva/audit/AuditManager.java
    
archiva/branches/MRM-1025/archiva-modules/plugins/audit/src/main/java/org/apache/archiva/audit/DefaultAuditManager.java
    
archiva/branches/MRM-1025/archiva-modules/plugins/audit/src/test/java/org/apache/archiva/audit/AuditManagerTest.java

Modified: 
archiva/branches/MRM-1025/archiva-modules/archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/action/reports/ViewAuditLogReportAction.java
URL: 
http://svn.apache.org/viewvc/archiva/branches/MRM-1025/archiva-modules/archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/action/reports/ViewAuditLogReportAction.java?rev=900726&r1=900725&r2=900726&view=diff
==============================================================================
--- 
archiva/branches/MRM-1025/archiva-modules/archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/action/reports/ViewAuditLogReportAction.java
 (original)
+++ 
archiva/branches/MRM-1025/archiva-modules/archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/action/reports/ViewAuditLogReportAction.java
 Tue Jan 19 11:21:10 2010
@@ -44,7 +44,7 @@
 
 /**
  * @plexus.component role="com.opensymphony.xwork2.Action" 
role-hint="viewAuditLogReport"
- *                   instantiation-strategy="per-lookup"
+ * instantiation-strategy="per-lookup"
  */
 public class ViewAuditLogReportAction
     extends PlexusActionSupport
@@ -68,7 +68,7 @@
     private String startDate;
 
     private String endDate;
-    
+
     private int rowCount = 30;
 
     private int page = 1;
@@ -84,18 +84,18 @@
     private static final String ALL_REPOSITORIES = "all";
 
     protected int[] range = new int[2];
-    
+
     private String initial = "true";
-    
+
     private String headerName;
-    
+
     private static final String HEADER_LATEST_EVENTS = "Latest Events";
-    
+
     private static final String HEADER_RESULTS = "Results";
-    
-    private String[] datePatterns = new String[] { "MM/dd/yy", "MM/dd/yyyy", 
"MMMMM/dd/yyyy", "MMMMM/dd/yy", 
-        "dd MMMMM yyyy", "dd/MM/yy", "dd/MM/yyyy", "yyyy/MM/dd", "yyyy-MM-dd", 
"yyyy-dd-MM", "MM-dd-yyyy",
-        "MM-dd-yy" };
+
+    private String[] datePatterns =
+        new String[]{"MM/dd/yy", "MM/dd/yyyy", "MMMMM/dd/yyyy", "MMMMM/dd/yy", 
"dd MMMMM yyyy", "dd/MM/yy",
+            "dd/MM/yyyy", "yyyy/MM/dd", "yyyy-MM-dd", "yyyy-dd-MM", 
"MM-dd-yyyy", "MM-dd-yy"};
 
     /**
      * @plexus.requirement
@@ -119,7 +119,7 @@
         this.request = request;
     }
 
-    @SuppressWarnings( "unchecked" )
+    @SuppressWarnings("unchecked")
     public void prepare()
         throws Exception
     {
@@ -132,8 +132,8 @@
         groupId = "";
         artifactId = "";
         repository = "";
-                
-        if( Boolean.parseBoolean( initial ) )
+
+        if ( Boolean.parseBoolean( initial ) )
         {
             headerName = HEADER_LATEST_EVENTS;
         }
@@ -148,27 +148,14 @@
     public String execute()
         throws Exception
     {
-        Date startDateInDF;
-        Date endDateInDF;
-        if ( startDate == null || "".equals( startDate ) )
-        {            
-            Calendar cal = Calendar.getInstance();
-            cal.set( Calendar.HOUR, 0 );
-            cal.set( Calendar.MINUTE, 0 );
-            cal.set( Calendar.SECOND, 0 );
-
-            startDateInDF = cal.getTime();
-        }
-        else
+        Date startDateInDF = null;
+        Date endDateInDF = null;
+        if ( !StringUtils.isEmpty( startDate ) )
         {
             startDateInDF = DateUtils.parseDate( startDate, datePatterns );
         }
 
-        if ( endDate == null || "".equals( endDate ) )
-        {
-            endDateInDF = Calendar.getInstance().getTime();
-        } 
-        else
+        if ( !StringUtils.isEmpty( endDate ) )
         {
             endDateInDF = DateUtils.parseDate( endDate, datePatterns );
             Calendar cal = Calendar.getInstance();
@@ -176,8 +163,8 @@
             cal.set( Calendar.HOUR, 23 );
             cal.set( Calendar.MINUTE, 59 );
             cal.set( Calendar.SECOND, 59 );
-            
-            endDateInDF = cal.getTime();            
+
+            endDateInDF = cal.getTime();
         }
 
         range[0] = ( page - 1 ) * rowCount;
@@ -195,10 +182,30 @@
                 repos = Collections.emptyList();
             }
         }
-        // TODO: query by artifact
-        auditLogs = auditManager.getAuditEventsInRange( repos, startDateInDF, 
endDateInDF );
 
-        if( auditLogs.isEmpty() )
+        if ( StringUtils.isEmpty( groupId ) && !StringUtils.isEmpty( 
artifactId ) )
+        {
+            // Until we store the full artifact metadata in the audit event, 
we can't query by these individually
+            addActionError( "If you specify an artifact ID, you must specify a 
group ID" );
+            return INPUT;
+        }
+
+        String resource = null;
+        if ( !StringUtils.isEmpty( groupId ) )
+        {
+            if ( StringUtils.isEmpty( artifactId ) )
+            {
+                resource = groupId;
+            }
+            else
+            {
+                resource = groupId + "/" + artifactId;
+            }
+        }
+
+        auditLogs = auditManager.getAuditEventsInRange( repos, resource, 
startDateInDF, endDateInDF );
+
+        if ( auditLogs.isEmpty() )
         {
             addActionError( "No audit logs found." );
             initial = "true";
@@ -213,7 +220,7 @@
 
         return SUCCESS;
     }
-    
+
     private void paginate()
     {
         if ( auditLogs.size() <= rowCount )
@@ -221,21 +228,19 @@
             isLastPage = true;
         }
         else
-        {   
+        {
             isLastPage = false;
             auditLogs.remove( rowCount );
         }
 
-        prev =
-            request.getRequestURL() + "?page=" + ( page - 1 ) + "&rowCount=" + 
rowCount + "&groupId=" + groupId +
-                "&artifactId=" + artifactId + "&repository=" + repository + 
"&startDate=" + startDate + "&endDate=" +
-                endDate;
-        
-        next =
-            request.getRequestURL() + "?page=" + ( page + 1 ) + "&rowCount=" + 
rowCount + "&groupId=" + groupId +
-                "&artifactId=" + artifactId + "&repository=" + repository + 
"&startDate=" + startDate + "&endDate=" +
-                endDate;
-        
+        prev = request.getRequestURL() + "?page=" + ( page - 1 ) + 
"&rowCount=" + rowCount + "&groupId=" + groupId +
+            "&artifactId=" + artifactId + "&repository=" + repository + 
"&startDate=" + startDate + "&endDate=" +
+            endDate;
+
+        next = request.getRequestURL() + "?page=" + ( page + 1 ) + 
"&rowCount=" + rowCount + "&groupId=" + groupId +
+            "&artifactId=" + artifactId + "&repository=" + repository + 
"&startDate=" + startDate + "&endDate=" +
+            endDate;
+
         prev = StringUtils.replace( prev, " ", "%20" );
         next = StringUtils.replace( next, " ", "%20" );
     }
@@ -355,7 +360,7 @@
     {
         this.isLastPage = isLastPage;
     }
-    
+
     public String getPrev()
     {
         return prev;
@@ -375,7 +380,7 @@
     {
         this.next = next;
     }
-    
+
     public String getInitial()
     {
         return initial;

Modified: 
archiva/branches/MRM-1025/archiva-modules/plugins/audit/src/main/java/org/apache/archiva/audit/AuditManager.java
URL: 
http://svn.apache.org/viewvc/archiva/branches/MRM-1025/archiva-modules/plugins/audit/src/main/java/org/apache/archiva/audit/AuditManager.java?rev=900726&r1=900725&r2=900726&view=diff
==============================================================================
--- 
archiva/branches/MRM-1025/archiva-modules/plugins/audit/src/main/java/org/apache/archiva/audit/AuditManager.java
 (original)
+++ 
archiva/branches/MRM-1025/archiva-modules/plugins/audit/src/main/java/org/apache/archiva/audit/AuditManager.java
 Tue Jan 19 11:21:10 2010
@@ -33,5 +33,25 @@
 
     void deleteAuditEvents( String repositoryId );
 
+    /**
+     * Get all audit events from the given repositories that match a certain 
range
+     *
+     * @param repositoryIds the repositories to retrieve events for
+     * @param startTime     find events only after this time
+     * @param endTime       find events only before this time
+     * @return the list of events found
+     */
     List<AuditEvent> getAuditEventsInRange( Collection<String> repositoryIds, 
Date startTime, Date endTime );
+
+    /**
+     * Get all audit events from the given repositories that match a certain 
range and resource pattern
+     *
+     * @param repositoryIds   the repositories to retrieve events for
+     * @param resourcePattern find all events whose resources start with this 
string
+     * @param startTime       find events only after this time
+     * @param endTime         find events only before this time
+     * @return the list of events found
+     */
+    List<AuditEvent> getAuditEventsInRange( Collection<String> repositoryIds, 
String resourcePattern, Date startTime,
+                                            Date endTime );
 }
\ No newline at end of file

Modified: 
archiva/branches/MRM-1025/archiva-modules/plugins/audit/src/main/java/org/apache/archiva/audit/DefaultAuditManager.java
URL: 
http://svn.apache.org/viewvc/archiva/branches/MRM-1025/archiva-modules/plugins/audit/src/main/java/org/apache/archiva/audit/DefaultAuditManager.java?rev=900726&r1=900725&r2=900726&view=diff
==============================================================================
--- 
archiva/branches/MRM-1025/archiva-modules/plugins/audit/src/main/java/org/apache/archiva/audit/DefaultAuditManager.java
 (original)
+++ 
archiva/branches/MRM-1025/archiva-modules/plugins/audit/src/main/java/org/apache/archiva/audit/DefaultAuditManager.java
 Tue Jan 19 11:21:10 2010
@@ -93,6 +93,12 @@
 
     public List<AuditEvent> getAuditEventsInRange( Collection<String> 
repositoryIds, Date startTime, Date endTime )
     {
+        return getAuditEventsInRange( repositoryIds, null, startTime, endTime 
);
+    }
+
+    public List<AuditEvent> getAuditEventsInRange( Collection<String> 
repositoryIds, String resource, Date startTime,
+                                                   Date endTime )
+    {
         List<AuditEvent> results = new ArrayList<AuditEvent>();
         for ( String repositoryId : repositoryIds )
         {
@@ -107,7 +113,11 @@
                     {
                         AuditEvent event =
                             (AuditEvent) metadataRepository.getMetadataFacet( 
repositoryId, AuditEvent.FACET_ID, name );
-                        results.add( event );
+
+                        if ( resource == null || 
event.getResource().startsWith( resource ) )
+                        {
+                            results.add( event );
+                        }
                     }
                 }
                 catch ( ParseException e )

Modified: 
archiva/branches/MRM-1025/archiva-modules/plugins/audit/src/test/java/org/apache/archiva/audit/AuditManagerTest.java
URL: 
http://svn.apache.org/viewvc/archiva/branches/MRM-1025/archiva-modules/plugins/audit/src/test/java/org/apache/archiva/audit/AuditManagerTest.java?rev=900726&r1=900725&r2=900726&view=diff
==============================================================================
--- 
archiva/branches/MRM-1025/archiva-modules/plugins/audit/src/test/java/org/apache/archiva/audit/AuditManagerTest.java
 (original)
+++ 
archiva/branches/MRM-1025/archiva-modules/plugins/audit/src/test/java/org/apache/archiva/audit/AuditManagerTest.java
 Tue Jan 19 11:21:10 2010
@@ -68,7 +68,7 @@
     private static SimpleDateFormat createTimestampFormat()
     {
         SimpleDateFormat fmt = new SimpleDateFormat( 
AuditEvent.TIMESTAMP_FORMAT );
-        fmt.setTimeZone( TimeZone.getTimeZone( "UTC" ));
+        fmt.setTimeZone( TimeZone.getTimeZone( "UTC" ) );
         return fmt;
     }
 
@@ -434,6 +434,79 @@
         metadataRepositoryControl.verify();
     }
 
+    public void testGetEventsWithResource()
+        throws ParseException
+    {
+        Date current = new Date();
+
+        String name1 = TIMESTAMP_FORMAT.format( new Date( current.getTime() - 
12345 ) );
+        AuditEvent expectedEvent1 = createTestEvent( name1 );
+        Date expectedTimestamp = new Date( current.getTime() - 3000 );
+        String name2 = TIMESTAMP_FORMAT.format( expectedTimestamp );
+        AuditEvent expectedEvent2 = createTestEvent( name2 );
+        expectedEvent2.setResource( "different-resource" );
+        String name3 = TIMESTAMP_FORMAT.format( new Date( current.getTime() - 
1000 ) );
+        AuditEvent expectedEvent3 = createTestEvent( name3 );
+
+        metadataRepositoryControl.expectAndReturn(
+            metadataRepository.getMetadataFacets( TEST_REPO_ID, 
AuditEvent.FACET_ID ),
+            Arrays.asList( name1, name2, name3 ) );
+
+        metadataRepositoryControl.expectAndReturn(
+            metadataRepository.getMetadataFacet( TEST_REPO_ID, 
AuditEvent.FACET_ID, name1 ), expectedEvent1 );
+        metadataRepositoryControl.expectAndReturn(
+            metadataRepository.getMetadataFacet( TEST_REPO_ID, 
AuditEvent.FACET_ID, name2 ), expectedEvent2 );
+        metadataRepositoryControl.expectAndReturn(
+            metadataRepository.getMetadataFacet( TEST_REPO_ID, 
AuditEvent.FACET_ID, name3 ), expectedEvent3 );
+
+        metadataRepositoryControl.replay();
+
+        List<AuditEvent> events =
+            auditManager.getAuditEventsInRange( Collections.singletonList( 
TEST_REPO_ID ), TEST_RESOURCE_BASE,
+                                                new Date( current.getTime() - 
20000 ), current );
+
+        assertEquals( 2, events.size() );
+        assertEvent( events.get( 0 ), name3, expectedEvent3.getResource() );
+        assertEvent( events.get( 1 ), name1, expectedEvent1.getResource() );
+
+        metadataRepositoryControl.verify();
+    }
+
+    public void testGetEventsWithNonExistantResource()
+        throws ParseException
+    {
+        Date current = new Date();
+
+        String name1 = TIMESTAMP_FORMAT.format( new Date( current.getTime() - 
12345 ) );
+        AuditEvent expectedEvent1 = createTestEvent( name1 );
+        Date expectedTimestamp = new Date( current.getTime() - 3000 );
+        String name2 = TIMESTAMP_FORMAT.format( expectedTimestamp );
+        AuditEvent expectedEvent2 = createTestEvent( name2 );
+        expectedEvent2.setResource( "different-resource" );
+        String name3 = TIMESTAMP_FORMAT.format( new Date( current.getTime() - 
1000 ) );
+        AuditEvent expectedEvent3 = createTestEvent( name3 );
+
+        metadataRepositoryControl.expectAndReturn(
+            metadataRepository.getMetadataFacets( TEST_REPO_ID, 
AuditEvent.FACET_ID ),
+            Arrays.asList( name1, name2, name3 ) );
+
+        metadataRepositoryControl.expectAndReturn(
+            metadataRepository.getMetadataFacet( TEST_REPO_ID, 
AuditEvent.FACET_ID, name1 ), expectedEvent1 );
+        metadataRepositoryControl.expectAndReturn(
+            metadataRepository.getMetadataFacet( TEST_REPO_ID, 
AuditEvent.FACET_ID, name2 ), expectedEvent2 );
+        metadataRepositoryControl.expectAndReturn(
+            metadataRepository.getMetadataFacet( TEST_REPO_ID, 
AuditEvent.FACET_ID, name3 ), expectedEvent3 );
+
+        metadataRepositoryControl.replay();
+
+        List<AuditEvent> events = auditManager.getAuditEventsInRange( 
Collections.singletonList( TEST_REPO_ID ), "foo",
+                                                                      new 
Date( current.getTime() - 20000 ), current );
+
+        assertEquals( 0, events.size() );
+
+        metadataRepositoryControl.verify();
+    }
+
     public void testGetEventsRangeMultipleRepositories()
         throws ParseException
     {
@@ -478,12 +551,9 @@
         Date current = new Date();
 
         String name1 = TIMESTAMP_FORMAT.format( new Date( current.getTime() - 
12345 ) );
-        AuditEvent expectedEvent1 = createTestEvent( name1 );
         Date expectedTimestamp = new Date( current.getTime() - 3000 );
         String name2 = TIMESTAMP_FORMAT.format( expectedTimestamp );
-        AuditEvent expectedEvent2 = createTestEvent( name2 );
         String name3 = TIMESTAMP_FORMAT.format( new Date( current.getTime() - 
1000 ) );
-        AuditEvent expectedEvent3 = createTestEvent( name3 );
 
         metadataRepositoryControl.expectAndReturn(
             metadataRepository.getMetadataFacets( TEST_REPO_ID, 
AuditEvent.FACET_ID ),


Reply via email to