]> source.dussan.org Git - archiva.git/blob
cb90bc185e97346e6460a68fbfaacde901e79c73
[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 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;
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.xwork2.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     @Override
108     public String execute()
109         throws Exception
110     {
111         List<RepositoryProblem> problemArtifacts =
112             dao.getRepositoryProblemDAO().queryRepositoryProblems( configureConstraint() );
113
114         String contextPath =
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++ )
119         {
120             problemArtifact = (RepositoryProblem) problemArtifacts.get( i );
121             problemArtifactReport = new RepositoryProblemReport( problemArtifact );
122
123             problemArtifactReport.setGroupURL( contextPath + "/browse/" + problemArtifact.getGroupId() );
124             problemArtifactReport.setArtifactURL(
125                 contextPath + "/browse/" + problemArtifact.getGroupId() + "/" + problemArtifact.getArtifactId() );
126
127             addToList( problemArtifactReport );
128             
129             // retained the reports list because this is the datasource for the jasper report            
130             reports.add( problemArtifactReport );
131         }
132         
133         if ( reports.size() <= rowCount )
134         {
135             isLastPage = true;
136         }
137         else
138         {
139             reports.remove( rowCount );
140         }
141
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;
146
147         if ( reports.size() == 0 && page == 1 )
148         {
149             return BLANK;
150         }
151         else if ( isJasperPresent() )
152         {
153             return "jasper";
154         }
155         else
156         {
157             return SUCCESS;
158         }
159     }
160
161     private static boolean isJasperPresent()
162     {
163         if ( jasperPresent == null )
164         {
165             try
166             {
167                 Class.forName( "net.sf.jasperreports.engine.JRExporterParameter" );
168                 jasperPresent = Boolean.TRUE;
169             }
170             catch ( NoClassDefFoundError e )
171             {
172                 jasperPresent = Boolean.FALSE;
173             }
174             catch ( ClassNotFoundException e )
175             {
176                 jasperPresent = Boolean.FALSE;
177             }
178         }
179         return jasperPresent.booleanValue();
180     }
181
182     private Constraint configureConstraint()
183     {
184         Constraint constraint;
185
186         range[0] = ( page - 1 ) * rowCount;
187         range[1] = ( page * rowCount ) + 1; // Add 1 to check if it's the last page or not.
188
189         if ( groupId != null && ( !groupId.equals( "" ) ) )
190         {
191             if ( repositoryId != null && ( !repositoryId.equals( "" ) && !repositoryId.equals( ALL_REPOSITORIES ) ) )
192             {
193                 constraint = new RepositoryProblemConstraint( range, groupId, repositoryId );
194             }
195             else
196             {
197                 constraint = new RepositoryProblemByGroupIdConstraint( range, groupId );
198             }
199         }
200         else if ( repositoryId != null && ( !repositoryId.equals( "" ) && !repositoryId.equals( ALL_REPOSITORIES ) ) )
201         {
202             constraint = new RepositoryProblemByRepositoryIdConstraint( range, repositoryId );
203         }
204         else
205         {
206             constraint = new RangeConstraint( range, "repositoryId" );
207         }
208
209         return constraint;
210     }
211
212     public void setServletRequest( HttpServletRequest request )
213     {
214         this.request = request;
215     }
216
217     public List<RepositoryProblemReport> getReports()
218     {
219         return reports;
220     }
221
222     public String getGroupId()
223     {
224         return groupId;
225     }
226
227     public void setGroupId( String groupId )
228     {
229         this.groupId = groupId;
230     }
231
232     public String getRepositoryId()
233     {
234         return repositoryId;
235     }
236
237     public void setRepositoryId( String repositoryId )
238     {
239         this.repositoryId = repositoryId;
240     }
241
242     public String getPrev()
243     {
244         return prev;
245     }
246
247     public String getNext()
248     {
249         return next;
250     }
251
252     public int getPage()
253     {
254         return page;
255     }
256
257     public void setPage( int page )
258     {
259         this.page = page;
260     }
261
262     public int getRowCount()
263     {
264         return rowCount;
265     }
266
267     public void setRowCount( int rowCount )
268     {
269         this.rowCount = rowCount;
270     }
271
272     public boolean getIsLastPage()
273     {
274         return isLastPage;
275     }
276
277     public void setRepositoriesMap( Map<String, List<RepositoryProblemReport>> repositoriesMap )
278     {
279         this.repositoriesMap = repositoriesMap;
280     }
281     
282     public Map<String, List<RepositoryProblemReport>> getRepositoriesMap()
283     {
284         return repositoriesMap;
285     }
286     
287     public SecureActionBundle getSecureActionBundle()
288         throws SecureActionException
289     {
290         SecureActionBundle bundle = new SecureActionBundle();
291
292         bundle.setRequiresAuthentication( true );
293         bundle.addRequiredAuthorization( ArchivaRoleConstants.OPERATION_ACCESS_REPORT, Resource.GLOBAL );
294
295         return bundle;
296     }
297     
298     private void addToList( RepositoryProblemReport repoProblemReport )
299     {
300         List<RepositoryProblemReport> problemsList = null;
301         
302         if ( repositoriesMap.containsKey( repoProblemReport.getRepositoryId() ) )
303         {
304                 problemsList = ( List<RepositoryProblemReport> ) repositoriesMap.get( repoProblemReport.getRepositoryId() );
305         }
306         else
307         {
308                 problemsList = new ArrayList<RepositoryProblemReport>();
309                 repositoriesMap.put( repoProblemReport.getRepositoryId(), problemsList );
310         }
311         
312         problemsList.add( repoProblemReport );
313     }
314 }