From 15768140ddc6fa717247ce115fccaf98d79816ee Mon Sep 17 00:00:00 2001 From: "Maria Odea B. Ching" Date: Mon, 14 Dec 2009 15:08:03 +0000 Subject: [PATCH] [MRM-1296] Audit Log Report o added constraint for custom query o support custom query in view audit log action git-svn-id: https://svn.apache.org/repos/asf/archiva/branches/MRM-1296@890361 13f79535-47bb-0310-9956-ffa450edef68 --- .../ArchivaAuditLogsConstraint.java | 116 ++++++++++++++++++ .../maven/archiva/database/jdo/JdoAccess.java | 2 +- .../database/jdo/JdoArchivaAuditLogsDao.java | 1 + .../reports/ViewAuditLogReportAction.java | 110 ++++++++++++++++- .../WEB-INF/jsp/reports/auditLogReport.jsp | 39 +++--- 5 files changed, 245 insertions(+), 23 deletions(-) create mode 100644 archiva-modules/archiva-database/src/main/java/org/apache/maven/archiva/database/constraints/ArchivaAuditLogsConstraint.java diff --git a/archiva-modules/archiva-database/src/main/java/org/apache/maven/archiva/database/constraints/ArchivaAuditLogsConstraint.java b/archiva-modules/archiva-database/src/main/java/org/apache/maven/archiva/database/constraints/ArchivaAuditLogsConstraint.java new file mode 100644 index 000000000..2cf8e3ac3 --- /dev/null +++ b/archiva-modules/archiva-database/src/main/java/org/apache/maven/archiva/database/constraints/ArchivaAuditLogsConstraint.java @@ -0,0 +1,116 @@ +package org.apache.maven.archiva.database.constraints; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +/** + * ArchivaAuditLogsConstraint + */ +public class ArchivaAuditLogsConstraint + extends RangeConstraint +{ + private String whereClause; + + /** + * Complete custom query! + * + * @param desiredArtifact + * @param desiredRepositoryId + * @param desiredEvent + * @param startDate + * @param endDate + */ + private void createWhereClause( String desiredArtifact, String desiredRepositoryId, String desiredEvent, + Date startDate, Date endDate ) + { + whereClause = "eventDate >= desiredStartDate && eventDate <= desiredEndDate"; + + declImports = new String[] { "import java.util.Date" }; + + List declParamsList = new ArrayList(); + declParamsList.add( "Date desiredStartDate" ); + declParamsList.add( "Date desiredEndDate" ); + + List paramsList = new ArrayList(); + paramsList.add( startDate ); + paramsList.add( endDate ); + + if ( desiredArtifact != null && !"".equals( desiredArtifact ) ) + { + whereClause = whereClause + " && artifact.like(desiredArtifact)"; + declParamsList.add( "String desiredArtifact" ); + paramsList.add( desiredArtifact + "%" ); + } + + if ( desiredRepositoryId != null && !"".equals( desiredRepositoryId ) ) + { + whereClause = whereClause + " && repositoryId == desiredRepositoryId"; + declParamsList.add( "String desiredRepositoryId" ); + paramsList.add( desiredRepositoryId ); + } + + if ( desiredEvent != null && !"".equals( desiredEvent ) ) + { + whereClause = whereClause + " && event == desiredEvent"; + declParamsList.add( "String desiredEvent" ); + paramsList.add( desiredEvent ); + } + + int size = declParamsList.size(); + int i = 0; + declParams = new String[size]; + + while( i < size ) + { + declParams[i] = declParamsList.get( i ); + i++; + } + + params = paramsList.toArray(); + } + + public ArchivaAuditLogsConstraint( int[] range, String desiredArtifact, String desiredRepositoryId, + String desiredEvent, Date startDate, Date endDate ) + { + super( range ); + createWhereClause( desiredArtifact, desiredRepositoryId, desiredEvent, startDate, endDate ); + } + + public ArchivaAuditLogsConstraint( String desiredArtifact, String desiredRepositoryId, + String desiredEvent, Date startDate, Date endDate ) + { + super(); + createWhereClause( desiredArtifact, desiredRepositoryId, desiredEvent, startDate, endDate ); + } + + + public String getSortColumn() + { + return "eventDate"; + } + + public String getWhereCondition() + { + return whereClause; + } +} diff --git a/archiva-modules/archiva-database/src/main/java/org/apache/maven/archiva/database/jdo/JdoAccess.java b/archiva-modules/archiva-database/src/main/java/org/apache/maven/archiva/database/jdo/JdoAccess.java index 27c39bf52..dd38dfe52 100644 --- a/archiva-modules/archiva-database/src/main/java/org/apache/maven/archiva/database/jdo/JdoAccess.java +++ b/archiva-modules/archiva-database/src/main/java/org/apache/maven/archiva/database/jdo/JdoAccess.java @@ -395,7 +395,7 @@ public class JdoAccess case 3: return (List) query.execute( parameters[0], parameters[1], parameters[2] ); default: - throw new JDOException( "Unable to use more than 3 parameters." ); + return (List) query.executeWithArray( parameters ); } } diff --git a/archiva-modules/archiva-database/src/main/java/org/apache/maven/archiva/database/jdo/JdoArchivaAuditLogsDao.java b/archiva-modules/archiva-database/src/main/java/org/apache/maven/archiva/database/jdo/JdoArchivaAuditLogsDao.java index 2e73fb1cc..64aa62012 100644 --- a/archiva-modules/archiva-database/src/main/java/org/apache/maven/archiva/database/jdo/JdoArchivaAuditLogsDao.java +++ b/archiva-modules/archiva-database/src/main/java/org/apache/maven/archiva/database/jdo/JdoArchivaAuditLogsDao.java @@ -48,6 +48,7 @@ public class JdoArchivaAuditLogsDao jdo.removeObject( logs ); } + @SuppressWarnings( "unchecked" ) public List queryAuditLogs( Constraint constraint ) throws ObjectNotFoundException, ArchivaDatabaseException { diff --git a/archiva-modules/archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/action/reports/ViewAuditLogReportAction.java b/archiva-modules/archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/action/reports/ViewAuditLogReportAction.java index f8b0bf929..392622302 100644 --- a/archiva-modules/archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/action/reports/ViewAuditLogReportAction.java +++ b/archiva-modules/archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/action/reports/ViewAuditLogReportAction.java @@ -20,7 +20,9 @@ package org.apache.maven.archiva.web.action.reports; */ import java.util.ArrayList; +import java.util.Calendar; import java.util.Collections; +import java.util.Date; import java.util.List; import javax.servlet.http.HttpServletRequest; @@ -29,8 +31,10 @@ import org.apache.maven.archiva.database.ArchivaAuditLogsDao; import org.apache.maven.archiva.database.ArchivaDatabaseException; import org.apache.maven.archiva.database.Constraint; import org.apache.maven.archiva.database.ObjectNotFoundException; +import org.apache.maven.archiva.database.constraints.ArchivaAuditLogsConstraint; import org.apache.maven.archiva.database.constraints.MostRecentArchivaAuditLogsConstraint; import org.apache.maven.archiva.model.ArchivaAuditLogs; +import org.apache.maven.archiva.repository.audit.AuditEvent; import org.apache.maven.archiva.security.AccessDeniedException; import org.apache.maven.archiva.security.ArchivaSecurityException; import org.apache.maven.archiva.security.PrincipalNotFoundException; @@ -69,11 +73,19 @@ public class ViewAuditLogReportAction private String groupId; private String artifactId; + + private Date startDate; + + private Date endDate; private int rowCount = 30; - private List auditLogs = new ArrayList(); + private int page; + + private List auditLogs; + private static final String ALL_REPOSITORIES = "all"; + public SecureActionBundle getSecureActionBundle() throws SecureActionException { @@ -88,7 +100,11 @@ public class ViewAuditLogReportAction public void prepare() throws Exception { - repositories = getObservableRepositories(); + repositories = new ArrayList(); + repositories.add( ALL_REPOSITORIES ); + repositories.addAll( getObservableRepositories() ); + + auditLogs = null; Constraint constraint = new MostRecentArchivaAuditLogsConstraint(); @@ -108,7 +124,65 @@ public class ViewAuditLogReportAction public String execute() throws Exception - { + { + auditLogs = null; + String artifact = null; + + if( groupId != null || !"".equals( groupId ) ) + { + artifact = groupId; + } + + if( artifactId != null || !"".equals( artifactId ) ) + { + artifact = artifact + ":" + artifactId; + } + + if( startDate == null ) + { + Calendar cal = Calendar.getInstance(); + cal.set( Calendar.HOUR, 0 ); + cal.set( Calendar.MINUTE, 0 ); + cal.set( Calendar.SECOND, 0 ); + + startDate = cal.getTime(); + } + + if( startDate.equals( endDate ) || endDate == null ) + { + endDate = Calendar.getInstance().getTime(); + } + + int[] range = {1, 30 }; + + ArchivaAuditLogsConstraint constraint = null; + if( !repository.equals( ALL_REPOSITORIES ) ) + { + //constraint = new ArchivaAuditLogsConstraint( range, artifact, repository, AuditEvent.UPLOAD_FILE, startDate, endDate ); + constraint = new ArchivaAuditLogsConstraint( artifact, repository, AuditEvent.UPLOAD_FILE, startDate, endDate ); + } + else + { + constraint = new ArchivaAuditLogsConstraint( range, artifact, null, AuditEvent.UPLOAD_FILE, startDate, endDate ); + } + + try + { + auditLogs = auditLogsDao.queryAuditLogs( constraint ); + startDate = null; + endDate = null; + } + catch ( ObjectNotFoundException e ) + { + addActionError( "No audit logs found." ); + return ERROR; + } + catch( ArchivaDatabaseException e ) + { + addActionError( "Error occurred while querying audit logs." ); + return ERROR; + } + return SUCCESS; } @@ -192,4 +266,34 @@ public class ViewAuditLogReportAction { this.rowCount = rowCount; } + + public Date getStartDate() + { + return startDate; + } + + public void setStartDate( Date startDate ) + { + this.startDate = startDate; + } + + public Date getEndDate() + { + return endDate; + } + + public void setEndDate( Date endDate ) + { + this.endDate = endDate; + } + + public int getPage() + { + return page; + } + + public void setPage( int page ) + { + this.page = page; + } } diff --git a/archiva-modules/archiva-web/archiva-webapp/src/main/webapp/WEB-INF/jsp/reports/auditLogReport.jsp b/archiva-modules/archiva-web/archiva-webapp/src/main/webapp/WEB-INF/jsp/reports/auditLogReport.jsp index d968a7ab6..6a84d1ebd 100644 --- a/archiva-modules/archiva-web/archiva-webapp/src/main/webapp/WEB-INF/jsp/reports/auditLogReport.jsp +++ b/archiva-modules/archiva-web/archiva-webapp/src/main/webapp/WEB-INF/jsp/reports/auditLogReport.jsp @@ -83,25 +83,26 @@ - - - - - - - - - - - - - - - - - - -
EventRepositoryArtifactEvent DateUsername
${auditLog.event}${auditLog.repositoryId}${auditLog.artifact}${auditLog.eventDate}${auditLog.username}
+ + + + + + + + + + + + + + + + + + +
EventRepositoryArtifactEvent DateUsername
${auditLog.event}${auditLog.repositoryId}${auditLog.artifact}${auditLog.eventDate}${auditLog.username}
+
-- 2.39.5