]> source.dussan.org Git - archiva.git/blob
747ec98028631badb89f88522d0a9a78f28dc10e
[archiva.git] /
1 package org.apache.maven.archiva.web.action.reports;
2
3 /*
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
11  *
12  *   http://www.apache.org/licenses/LICENSE-2.0
13  *
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
19  * under the License.
20  */
21
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;
39
40 import javax.servlet.http.HttpServletRequest;
41 import java.util.ArrayList;
42 import java.util.Collection;
43 import java.util.List;
44 import java.util.Map;
45 import java.util.TreeMap;
46
47 /**
48  * @plexus.component role="com.opensymphony.xwork.Action" role-hint="generateReport"
49  */
50 public class GenerateReportAction
51     extends PlexusActionSupport
52     implements SecureAction, ServletRequestAware, Preparable
53 {
54     /**
55      * @plexus.requirement role-hint="jdo"
56      */
57     protected ArchivaDAO dao;
58
59     protected Constraint constraint;
60
61     protected HttpServletRequest request;
62
63     protected List<RepositoryProblemReport> reports = new ArrayList<RepositoryProblemReport>();
64
65     protected String groupId;
66
67     protected String repositoryId;
68
69     protected String prev;
70
71     protected String next;
72
73     protected int[] range = new int[2];
74
75     protected int page = 1;
76
77     protected int rowCount = 100;
78
79     protected boolean isLastPage;
80
81     public static final String BLANK = "blank";
82
83     public static final String BASIC = "basic";
84
85     private static Boolean jasperPresent;
86
87     private Collection<String> repositoryIds;
88
89     public static final String ALL_REPOSITORIES = "All Repositories";
90     
91     protected Map<String, List<RepositoryProblemReport>> repositoriesMap = 
92                 new TreeMap<String, List<RepositoryProblemReport>>();
93
94     public void prepare()
95     {
96         repositoryIds = new ArrayList<String>();
97         repositoryIds.add( ALL_REPOSITORIES ); // comes first to be first in the list
98         repositoryIds.addAll(
99             dao.query( new UniqueFieldConstraint( RepositoryProblem.class.getName(), "repositoryId" ) ) );
100     }
101
102     public Collection<String> getRepositoryIds()
103     {
104         return repositoryIds;
105     }
106
107     public String execute()
108         throws Exception
109     {
110         List<RepositoryProblem> problemArtifacts =
111             dao.getRepositoryProblemDAO().queryRepositoryProblems( configureConstraint() );
112
113         String contextPath =
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++ )
118         {
119             problemArtifact = (RepositoryProblem) problemArtifacts.get( i );
120             problemArtifactReport = new RepositoryProblemReport( problemArtifact );
121
122             problemArtifactReport.setGroupURL( contextPath + "/browse/" + problemArtifact.getGroupId() );
123             problemArtifactReport.setArtifactURL(
124                 contextPath + "/browse/" + problemArtifact.getGroupId() + "/" + problemArtifact.getArtifactId() );
125
126             addToList( problemArtifactReport );
127             
128             // retained the reports list because this is the datasource for the jasper report            
129             reports.add( problemArtifactReport );
130         }
131         
132         if ( reports.size() <= rowCount )
133         {
134             isLastPage = true;
135         }
136         else
137         {
138             reports.remove( rowCount );
139         }
140
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;
145
146         if ( reports.size() == 0 && page == 1 )
147         {
148             return BLANK;
149         }
150         else if ( isJasperPresent() )
151         {
152             return "jasper";
153         }
154         else
155         {
156             return SUCCESS;
157         }
158     }
159
160     private static boolean isJasperPresent()
161     {
162         if ( jasperPresent == null )
163         {
164             try
165             {
166                 Class.forName( "net.sf.jasperreports.engine.JRExporterParameter" );
167                 jasperPresent = Boolean.TRUE;
168             }
169             catch ( NoClassDefFoundError e )
170             {
171                 jasperPresent = Boolean.FALSE;
172             }
173             catch ( ClassNotFoundException e )
174             {
175                 jasperPresent = Boolean.FALSE;
176             }
177         }
178         return jasperPresent.booleanValue();
179     }
180
181     private Constraint configureConstraint()
182     {
183         Constraint constraint;
184
185         range[0] = ( page - 1 ) * rowCount;
186         range[1] = ( page * rowCount ) + 1; // Add 1 to check if it's the last page or not.
187
188         if ( groupId != null && ( !groupId.equals( "" ) ) )
189         {
190             if ( repositoryId != null && ( !repositoryId.equals( "" ) && !repositoryId.equals( ALL_REPOSITORIES ) ) )
191             {
192                 constraint = new RepositoryProblemConstraint( range, groupId, repositoryId );
193             }
194             else
195             {
196                 constraint = new RepositoryProblemByGroupIdConstraint( range, groupId );
197             }
198         }
199         else if ( repositoryId != null && ( !repositoryId.equals( "" ) && !repositoryId.equals( ALL_REPOSITORIES ) ) )
200         {
201             constraint = new RepositoryProblemByRepositoryIdConstraint( range, repositoryId );
202         }
203         else
204         {
205             constraint = new RangeConstraint( range, "repositoryId" );
206         }
207
208         return constraint;
209     }
210
211     public void setServletRequest( HttpServletRequest request )
212     {
213         this.request = request;
214     }
215
216     public List<RepositoryProblemReport> getReports()
217     {
218         return reports;
219     }
220
221     public String getGroupId()
222     {
223         return groupId;
224     }
225
226     public void setGroupId( String groupId )
227     {
228         this.groupId = groupId;
229     }
230
231     public String getRepositoryId()
232     {
233         return repositoryId;
234     }
235
236     public void setRepositoryId( String repositoryId )
237     {
238         this.repositoryId = repositoryId;
239     }
240
241     public String getPrev()
242     {
243         return prev;
244     }
245
246     public String getNext()
247     {
248         return next;
249     }
250
251     public int getPage()
252     {
253         return page;
254     }
255
256     public void setPage( int page )
257     {
258         this.page = page;
259     }
260
261     public int getRowCount()
262     {
263         return rowCount;
264     }
265
266     public void setRowCount( int rowCount )
267     {
268         this.rowCount = rowCount;
269     }
270
271     public boolean getIsLastPage()
272     {
273         return isLastPage;
274     }
275
276     public void setRepositoriesMap( Map<String, List<RepositoryProblemReport>> repositoriesMap )
277     {
278         this.repositoriesMap = repositoriesMap;
279     }
280     
281     public Map<String, List<RepositoryProblemReport>> getRepositoriesMap()
282     {
283         return repositoriesMap;
284     }
285     
286     public SecureActionBundle getSecureActionBundle()
287         throws SecureActionException
288     {
289         SecureActionBundle bundle = new SecureActionBundle();
290
291         bundle.setRequiresAuthentication( true );
292         bundle.addRequiredAuthorization( ArchivaRoleConstants.OPERATION_ACCESS_REPORT, Resource.GLOBAL );
293
294         return bundle;
295     }
296     
297     private void addToList( RepositoryProblemReport repoProblemReport )
298     {
299         List<RepositoryProblemReport> problemsList = null;
300         
301         if ( repositoriesMap.containsKey( repoProblemReport.getRepositoryId() ) )
302         {
303                 problemsList = ( List<RepositoryProblemReport> ) repositoriesMap.get( repoProblemReport.getRepositoryId() );
304         }
305         else
306         {
307                 problemsList = new ArrayList<RepositoryProblemReport>();
308                 repositoriesMap.put( repoProblemReport.getRepositoryId(), problemsList );
309         }
310         
311         problemsList.add( repoProblemReport );
312     }
313 }