1 package org.apache.maven.archiva.web.action.reports;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import com.opensymphony.webwork.interceptor.ServletRequestAware;
23 import com.opensymphony.xwork.Preparable;
24 import org.apache.maven.archiva.database.ArchivaDAO;
25 import org.apache.maven.archiva.database.Constraint;
26 import org.apache.maven.archiva.database.constraints.RangeConstraint;
27 import org.apache.maven.archiva.database.constraints.RepositoryProblemByGroupIdConstraint;
28 import org.apache.maven.archiva.database.constraints.RepositoryProblemByRepositoryIdConstraint;
29 import org.apache.maven.archiva.database.constraints.RepositoryProblemConstraint;
30 import org.apache.maven.archiva.database.constraints.UniqueFieldConstraint;
31 import org.apache.maven.archiva.model.RepositoryProblem;
32 import org.apache.maven.archiva.model.RepositoryProblemReport;
33 import org.apache.maven.archiva.security.ArchivaRoleConstants;
34 import org.codehaus.plexus.redback.rbac.Resource;
35 import org.codehaus.plexus.redback.xwork.interceptor.SecureAction;
36 import org.codehaus.plexus.redback.xwork.interceptor.SecureActionBundle;
37 import org.codehaus.plexus.redback.xwork.interceptor.SecureActionException;
38 import org.codehaus.plexus.xwork.action.PlexusActionSupport;
40 import javax.servlet.http.HttpServletRequest;
41 import java.util.ArrayList;
42 import java.util.Collection;
43 import java.util.List;
45 import java.util.TreeMap;
48 * @plexus.component role="com.opensymphony.xwork.Action" role-hint="generateReport"
50 public class GenerateReportAction
51 extends PlexusActionSupport
52 implements SecureAction, ServletRequestAware, Preparable
55 * @plexus.requirement role-hint="jdo"
57 protected ArchivaDAO dao;
59 protected Constraint constraint;
61 protected HttpServletRequest request;
63 protected List<RepositoryProblemReport> reports = new ArrayList<RepositoryProblemReport>();
65 protected String groupId;
67 protected String repositoryId;
69 protected String prev;
71 protected String next;
73 protected int[] range = new int[2];
75 protected int page = 1;
77 protected int rowCount = 100;
79 protected boolean isLastPage;
81 public static final String BLANK = "blank";
83 public static final String BASIC = "basic";
85 private static Boolean jasperPresent;
87 private Collection<String> repositoryIds;
89 public static final String ALL_REPOSITORIES = "All Repositories";
91 protected Map<String, List<RepositoryProblemReport>> repositoriesMap =
92 new TreeMap<String, List<RepositoryProblemReport>>();
96 repositoryIds = new ArrayList<String>();
97 repositoryIds.add( ALL_REPOSITORIES ); // comes first to be first in the list
99 dao.query( new UniqueFieldConstraint( RepositoryProblem.class.getName(), "repositoryId" ) ) );
102 public Collection<String> getRepositoryIds()
104 return repositoryIds;
107 public String execute()
110 List<RepositoryProblem> problemArtifacts =
111 dao.getRepositoryProblemDAO().queryRepositoryProblems( configureConstraint() );
114 request.getRequestURL().substring( 0, request.getRequestURL().indexOf( request.getRequestURI() ) );
115 RepositoryProblem problemArtifact;
116 RepositoryProblemReport problemArtifactReport;
117 for ( int i = 0; i < problemArtifacts.size(); i++ )
119 problemArtifact = (RepositoryProblem) problemArtifacts.get( i );
120 problemArtifactReport = new RepositoryProblemReport( problemArtifact );
122 problemArtifactReport.setGroupURL( contextPath + "/browse/" + problemArtifact.getGroupId() );
123 problemArtifactReport.setArtifactURL(
124 contextPath + "/browse/" + problemArtifact.getGroupId() + "/" + problemArtifact.getArtifactId() );
126 addToList( problemArtifactReport );
128 // retained the reports list because this is the datasource for the jasper report
129 reports.add( problemArtifactReport );
132 if ( reports.size() <= rowCount )
138 reports.remove( rowCount );
141 prev = request.getRequestURL() + "?page=" + ( page - 1 ) + "&rowCount=" + rowCount + "&groupId=" + groupId +
142 "&repositoryId=" + repositoryId;
143 next = request.getRequestURL() + "?page=" + ( page + 1 ) + "&rowCount=" + rowCount + "&groupId=" + groupId +
144 "&repositoryId=" + repositoryId;
146 if ( reports.size() == 0 && page == 1 )
150 else if ( isJasperPresent() )
160 private static boolean isJasperPresent()
162 if ( jasperPresent == null )
166 Class.forName( "net.sf.jasperreports.engine.JRExporterParameter" );
167 jasperPresent = Boolean.TRUE;
169 catch ( NoClassDefFoundError e )
171 jasperPresent = Boolean.FALSE;
173 catch ( ClassNotFoundException e )
175 jasperPresent = Boolean.FALSE;
178 return jasperPresent.booleanValue();
181 private Constraint configureConstraint()
183 Constraint constraint;
185 range[0] = ( page - 1 ) * rowCount;
186 range[1] = ( page * rowCount ) + 1; // Add 1 to check if it's the last page or not.
188 if ( groupId != null && ( !groupId.equals( "" ) ) )
190 if ( repositoryId != null && ( !repositoryId.equals( "" ) && !repositoryId.equals( ALL_REPOSITORIES ) ) )
192 constraint = new RepositoryProblemConstraint( range, groupId, repositoryId );
196 constraint = new RepositoryProblemByGroupIdConstraint( range, groupId );
199 else if ( repositoryId != null && ( !repositoryId.equals( "" ) && !repositoryId.equals( ALL_REPOSITORIES ) ) )
201 constraint = new RepositoryProblemByRepositoryIdConstraint( range, repositoryId );
205 constraint = new RangeConstraint( range, "repositoryId" );
211 public void setServletRequest( HttpServletRequest request )
213 this.request = request;
216 public List<RepositoryProblemReport> getReports()
221 public String getGroupId()
226 public void setGroupId( String groupId )
228 this.groupId = groupId;
231 public String getRepositoryId()
236 public void setRepositoryId( String repositoryId )
238 this.repositoryId = repositoryId;
241 public String getPrev()
246 public String getNext()
256 public void setPage( int page )
261 public int getRowCount()
266 public void setRowCount( int rowCount )
268 this.rowCount = rowCount;
271 public boolean getIsLastPage()
276 public void setRepositoriesMap( Map<String, List<RepositoryProblemReport>> repositoriesMap )
278 this.repositoriesMap = repositoriesMap;
281 public Map<String, List<RepositoryProblemReport>> getRepositoriesMap()
283 return repositoriesMap;
286 public SecureActionBundle getSecureActionBundle()
287 throws SecureActionException
289 SecureActionBundle bundle = new SecureActionBundle();
291 bundle.setRequiresAuthentication( true );
292 bundle.addRequiredAuthorization( ArchivaRoleConstants.OPERATION_ACCESS_REPORT, Resource.GLOBAL );
297 private void addToList( RepositoryProblemReport repoProblemReport )
299 List<RepositoryProblemReport> problemsList = null;
301 if ( repositoriesMap.containsKey( repoProblemReport.getRepositoryId() ) )
303 problemsList = ( List<RepositoryProblemReport> ) repositoriesMap.get( repoProblemReport.getRepositoryId() );
307 problemsList = new ArrayList<RepositoryProblemReport>();
308 repositoriesMap.put( repoProblemReport.getRepositoryId(), problemsList );
311 problemsList.add( repoProblemReport );