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-ffa450edef68MRM-1296
@@ -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<String> declParamsList = new ArrayList<String>(); | |||
declParamsList.add( "Date desiredStartDate" ); | |||
declParamsList.add( "Date desiredEndDate" ); | |||
List<Object> paramsList = new ArrayList<Object>(); | |||
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; | |||
} | |||
} |
@@ -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 ); | |||
} | |||
} | |||
@@ -48,6 +48,7 @@ public class JdoArchivaAuditLogsDao | |||
jdo.removeObject( logs ); | |||
} | |||
@SuppressWarnings( "unchecked" ) | |||
public List<ArchivaAuditLogs> queryAuditLogs( Constraint constraint ) | |||
throws ObjectNotFoundException, ArchivaDatabaseException | |||
{ |
@@ -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<ArchivaAuditLogs> auditLogs = new ArrayList<ArchivaAuditLogs>(); | |||
private int page; | |||
private List<ArchivaAuditLogs> 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<String>(); | |||
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; | |||
} | |||
} |
@@ -83,25 +83,26 @@ | |||
</div> | |||
</s:form> | |||
<table border="1"> | |||
<tr> | |||
<th align="center">Event</th> | |||
<th align="center">Repository</th> | |||
<th align="center">Artifact</th> | |||
<th align="center">Event Date</th> | |||
<th align="center">Username</th> | |||
</tr> | |||
<c:forEach items="${auditLogs}" var="auditLog" varStatus="i"> | |||
<tr> | |||
<td>${auditLog.event}</td> | |||
<td>${auditLog.repositoryId}</td> | |||
<td>${auditLog.artifact}</td> | |||
<td>${auditLog.eventDate}</td> | |||
<td>${auditLog.username}</td> | |||
</tr> | |||
</c:forEach> | |||
</table> | |||
<c:if test="${not empty (auditLogs)}"> | |||
<table border="1"> | |||
<tr> | |||
<th align="center">Event</th> | |||
<th align="center">Repository</th> | |||
<th align="center">Artifact</th> | |||
<th align="center">Event Date</th> | |||
<th align="center">Username</th> | |||
</tr> | |||
<c:forEach items="${auditLogs}" var="auditLog" varStatus="i"> | |||
<tr> | |||
<td>${auditLog.event}</td> | |||
<td>${auditLog.repositoryId}</td> | |||
<td>${auditLog.artifact}</td> | |||
<td>${auditLog.eventDate}</td> | |||
<td>${auditLog.username}</td> | |||
</tr> | |||
</c:forEach> | |||
</table> | |||
</c:if> | |||
</div> | |||