]> source.dussan.org Git - archiva.git/blob
04a55adbac270c522c59b83c945e784cfc60841b
[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 org.apache.maven.archiva.database.ArchivaDAO;
24 import org.apache.maven.archiva.database.Constraint;
25 import org.apache.maven.archiva.database.constraints.RangeConstraint;
26 import org.apache.maven.archiva.database.constraints.RepositoryProblemByGroupIdConstraint;
27 import org.apache.maven.archiva.database.constraints.RepositoryProblemByRepositoryIdConstraint;
28 import org.apache.maven.archiva.database.constraints.RepositoryProblemConstraint;
29 import org.apache.maven.archiva.model.RepositoryProblem;
30 import org.apache.maven.archiva.model.RepositoryProblemReport;
31 import org.apache.maven.archiva.security.ArchivaRoleConstants;
32 import org.codehaus.plexus.redback.rbac.Resource;
33 import org.codehaus.plexus.redback.xwork.interceptor.SecureAction;
34 import org.codehaus.plexus.redback.xwork.interceptor.SecureActionBundle;
35 import org.codehaus.plexus.redback.xwork.interceptor.SecureActionException;
36 import org.codehaus.plexus.xwork.action.PlexusActionSupport;
37
38 import javax.servlet.http.HttpServletRequest;
39 import java.util.ArrayList;
40 import java.util.List;
41
42 /**
43  * @plexus.component role="com.opensymphony.xwork.Action" role-hint="generateReport"
44  */
45 public class GenerateReportAction
46     extends PlexusActionSupport
47     implements SecureAction, ServletRequestAware
48 {
49     /**
50      * @plexus.requirement role-hint="jdo"
51      */
52     protected ArchivaDAO dao;
53
54     protected Constraint constraint;
55
56     protected HttpServletRequest request;
57
58     protected List<RepositoryProblemReport> reports = new ArrayList<RepositoryProblemReport>();
59
60     protected String groupId;
61
62     protected String repositoryId;
63
64     protected String prev;
65
66     protected String next;
67
68     protected int[] range = new int[2];
69
70     protected int page = 1;
71
72     protected int rowCount = 100;
73
74     protected boolean isLastPage = false;
75
76     public static final String BLANK = "blank";
77
78     public static final String BASIC = "basic";
79
80     private static Boolean jasperPresent;
81
82     public String input()
83         throws Exception
84     {
85         List<RepositoryProblem> problemArtifacts = dao.getRepositoryProblemDAO().queryRepositoryProblems( configureConstraint() );
86
87         String contextPath =
88             request.getRequestURL().substring( 0, request.getRequestURL().indexOf( request.getRequestURI() ) );
89         RepositoryProblem problemArtifact;
90         RepositoryProblemReport problemArtifactReport;
91         for ( int i = 0; i < problemArtifacts.size(); i++ )
92         {
93             problemArtifact = (RepositoryProblem) problemArtifacts.get( i );
94             problemArtifactReport = new RepositoryProblemReport( problemArtifact );
95
96             problemArtifactReport.setGroupURL( contextPath + "/browse/" + problemArtifact.getGroupId() );
97             problemArtifactReport.setArtifactURL(
98                 contextPath + "/browse/" + problemArtifact.getGroupId() + "/" + problemArtifact.getArtifactId() );
99
100             reports.add( problemArtifactReport );
101         }
102
103         if ( reports.size() <= rowCount )
104         {
105             isLastPage = true;
106         }
107         else
108         {
109             reports.remove( rowCount );
110         }
111
112         prev = request.getRequestURL() + "?page=" + ( page - 1 ) + "&rowCount=" + rowCount + "&groupId=" + groupId +
113             "&repositoryId=" + repositoryId;
114         next = request.getRequestURL() + "?page=" + ( page + 1 ) + "&rowCount=" + rowCount + "&groupId=" + groupId +
115             "&repositoryId=" + repositoryId;
116
117         if ( reports.size() == 0 && page == 1 )
118         {
119             return BLANK;
120         }
121         else if ( isJasperPresent() )
122         {
123             return "jasper";
124         }
125         else
126         {
127             return SUCCESS;
128         }
129     }
130
131     private static boolean isJasperPresent()
132     {
133         if ( jasperPresent == null )
134         {
135             try
136             {
137                 Class.forName( "net.sf.jasperreports.engine.JRExporterParameter" );
138                 jasperPresent = Boolean.TRUE;
139             }
140             catch ( NoClassDefFoundError e )
141             {
142                 jasperPresent = Boolean.FALSE;
143             }
144             catch ( ClassNotFoundException e )
145             {
146                 jasperPresent = Boolean.FALSE;
147             }
148         }
149         return jasperPresent.booleanValue();
150     }
151
152     private Constraint configureConstraint()
153     {
154         Constraint constraint;
155
156         range[0] = ( page - 1 ) * rowCount;
157         range[1] = ( page * rowCount ) + 1; // Add 1 to check if it's the last page or not.
158
159         if ( groupId != null && ( !groupId.equals( "" ) ) )
160         {
161             if ( repositoryId != null && ( !repositoryId.equals( "" ) && !repositoryId.equals( PickReportAction.ALL_REPOSITORIES ) ) )
162             {
163                 constraint = new RepositoryProblemConstraint( range, groupId, repositoryId );
164             }
165             else
166             {
167                 constraint = new RepositoryProblemByGroupIdConstraint( range, groupId );
168             }
169         }
170         else if ( repositoryId != null && ( !repositoryId.equals( "" ) && !repositoryId.equals( PickReportAction.ALL_REPOSITORIES ) ) )
171         {
172             constraint = new RepositoryProblemByRepositoryIdConstraint( range, repositoryId );
173         }
174         else
175         {
176             constraint = new RangeConstraint( range );
177         }
178
179         return constraint;
180     }
181
182     public void setServletRequest( HttpServletRequest request )
183     {
184         this.request = request;
185     }
186
187     public List<RepositoryProblemReport> getReports()
188     {
189         return reports;
190     }
191
192     public String getGroupId()
193     {
194         return groupId;
195     }
196
197     public void setGroupId( String groupId )
198     {
199         this.groupId = groupId;
200     }
201
202     public String getRepositoryId()
203     {
204         return repositoryId;
205     }
206
207     public void setRepositoryId( String repositoryId )
208     {
209         this.repositoryId = repositoryId;
210     }
211
212     public String getPrev()
213     {
214         return prev;
215     }
216
217     public String getNext()
218     {
219         return next;
220     }
221
222     public int getPage()
223     {
224         return page;
225     }
226
227     public void setPage( int page )
228     {
229         this.page = page;
230     }
231
232     public int getRowCount()
233     {
234         return rowCount;
235     }
236
237     public void setRowCount( int rowCount )
238     {
239         this.rowCount = rowCount;
240     }
241
242     public boolean getIsLastPage()
243     {
244         return isLastPage;
245     }
246
247     public SecureActionBundle getSecureActionBundle()
248         throws SecureActionException
249     {
250         SecureActionBundle bundle = new SecureActionBundle();
251
252         bundle.setRequiresAuthentication( true );
253         bundle.addRequiredAuthorization( ArchivaRoleConstants.OPERATION_ACCESS_REPORT, Resource.GLOBAL );
254
255         return bundle;
256     }
257 }