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 org.apache.struts2.interceptor.ServletRequestAware;
23 import com.opensymphony.xwork2.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.apache.maven.archiva.web.action.PlexusActionSupport;
35 import org.codehaus.plexus.redback.rbac.Resource;
36 import org.codehaus.plexus.redback.struts2.interceptor.SecureAction;
37 import org.codehaus.plexus.redback.struts2.interceptor.SecureActionBundle;
38 import org.codehaus.plexus.redback.struts2.interceptor.SecureActionException;
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.xwork2.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;
108 public String execute()
111 List<RepositoryProblem> problemArtifacts =
112 dao.getRepositoryProblemDAO().queryRepositoryProblems( configureConstraint() );
115 request.getRequestURL().substring( 0, request.getRequestURL().indexOf( request.getRequestURI() ) );
116 RepositoryProblem problemArtifact;
117 RepositoryProblemReport problemArtifactReport;
118 for ( int i = 0; i < problemArtifacts.size(); i++ )
120 problemArtifact = (RepositoryProblem) problemArtifacts.get( i );
121 problemArtifactReport = new RepositoryProblemReport( problemArtifact );
123 problemArtifactReport.setGroupURL( contextPath + "/browse/" + problemArtifact.getGroupId() );
124 problemArtifactReport.setArtifactURL(
125 contextPath + "/browse/" + problemArtifact.getGroupId() + "/" + problemArtifact.getArtifactId() );
127 addToList( problemArtifactReport );
129 // retained the reports list because this is the datasource for the jasper report
130 reports.add( problemArtifactReport );
133 if ( reports.size() <= rowCount )
139 reports.remove( rowCount );
142 prev = request.getRequestURL() + "?page=" + ( page - 1 ) + "&rowCount=" + rowCount + "&groupId=" + groupId +
143 "&repositoryId=" + repositoryId;
144 next = request.getRequestURL() + "?page=" + ( page + 1 ) + "&rowCount=" + rowCount + "&groupId=" + groupId +
145 "&repositoryId=" + repositoryId;
147 if ( reports.size() == 0 && page == 1 )
151 else if ( isJasperPresent() )
161 private static boolean isJasperPresent()
163 if ( jasperPresent == null )
167 Class.forName( "net.sf.jasperreports.engine.JRExporterParameter" );
168 jasperPresent = Boolean.TRUE;
170 catch ( NoClassDefFoundError e )
172 jasperPresent = Boolean.FALSE;
174 catch ( ClassNotFoundException e )
176 jasperPresent = Boolean.FALSE;
179 return jasperPresent.booleanValue();
182 private Constraint configureConstraint()
184 Constraint constraint;
186 range[0] = ( page - 1 ) * rowCount;
187 range[1] = ( page * rowCount ) + 1; // Add 1 to check if it's the last page or not.
189 if ( groupId != null && ( !groupId.equals( "" ) ) )
191 if ( repositoryId != null && ( !repositoryId.equals( "" ) && !repositoryId.equals( ALL_REPOSITORIES ) ) )
193 constraint = new RepositoryProblemConstraint( range, groupId, repositoryId );
197 constraint = new RepositoryProblemByGroupIdConstraint( range, groupId );
200 else if ( repositoryId != null && ( !repositoryId.equals( "" ) && !repositoryId.equals( ALL_REPOSITORIES ) ) )
202 constraint = new RepositoryProblemByRepositoryIdConstraint( range, repositoryId );
206 constraint = new RangeConstraint( range, "repositoryId" );
212 public void setServletRequest( HttpServletRequest request )
214 this.request = request;
217 public List<RepositoryProblemReport> getReports()
222 public String getGroupId()
227 public void setGroupId( String groupId )
229 this.groupId = groupId;
232 public String getRepositoryId()
237 public void setRepositoryId( String repositoryId )
239 this.repositoryId = repositoryId;
242 public String getPrev()
247 public String getNext()
257 public void setPage( int page )
262 public int getRowCount()
267 public void setRowCount( int rowCount )
269 this.rowCount = rowCount;
272 public boolean getIsLastPage()
277 public void setRepositoriesMap( Map<String, List<RepositoryProblemReport>> repositoriesMap )
279 this.repositoriesMap = repositoriesMap;
282 public Map<String, List<RepositoryProblemReport>> getRepositoriesMap()
284 return repositoriesMap;
287 public SecureActionBundle getSecureActionBundle()
288 throws SecureActionException
290 SecureActionBundle bundle = new SecureActionBundle();
292 bundle.setRequiresAuthentication( true );
293 bundle.addRequiredAuthorization( ArchivaRoleConstants.OPERATION_ACCESS_REPORT, Resource.GLOBAL );
298 private void addToList( RepositoryProblemReport repoProblemReport )
300 List<RepositoryProblemReport> problemsList = null;
302 if ( repositoriesMap.containsKey( repoProblemReport.getRepositoryId() ) )
304 problemsList = ( List<RepositoryProblemReport> ) repositoriesMap.get( repoProblemReport.getRepositoryId() );
308 problemsList = new ArrayList<RepositoryProblemReport>();
309 repositoriesMap.put( repoProblemReport.getRepositoryId(), problemsList );
312 problemsList.add( repoProblemReport );