You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

GenerateReportAction.java 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. package org.apache.maven.archiva.web.action.reports;
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. import org.apache.struts2.interceptor.ServletRequestAware;
  21. import com.opensymphony.xwork2.Preparable;
  22. import org.apache.maven.archiva.database.ArchivaDAO;
  23. import org.apache.maven.archiva.database.Constraint;
  24. import org.apache.maven.archiva.database.constraints.RangeConstraint;
  25. import org.apache.maven.archiva.database.constraints.RepositoryProblemByGroupIdConstraint;
  26. import org.apache.maven.archiva.database.constraints.RepositoryProblemByRepositoryIdConstraint;
  27. import org.apache.maven.archiva.database.constraints.RepositoryProblemConstraint;
  28. import org.apache.maven.archiva.database.constraints.UniqueFieldConstraint;
  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.apache.maven.archiva.web.action.PlexusActionSupport;
  33. import org.codehaus.plexus.redback.rbac.Resource;
  34. import org.codehaus.plexus.redback.struts2.interceptor.SecureAction;
  35. import org.codehaus.plexus.redback.struts2.interceptor.SecureActionBundle;
  36. import org.codehaus.plexus.redback.struts2.interceptor.SecureActionException;
  37. import javax.servlet.http.HttpServletRequest;
  38. import java.util.ArrayList;
  39. import java.util.Collection;
  40. import java.util.List;
  41. import java.util.Map;
  42. import java.util.TreeMap;
  43. /**
  44. * @plexus.component role="com.opensymphony.xwork2.Action" role-hint="generateReport"
  45. */
  46. public class GenerateReportAction
  47. extends PlexusActionSupport
  48. implements SecureAction, ServletRequestAware, Preparable
  49. {
  50. /**
  51. * @plexus.requirement role-hint="jdo"
  52. */
  53. protected ArchivaDAO dao;
  54. protected Constraint constraint;
  55. protected HttpServletRequest request;
  56. protected List<RepositoryProblemReport> reports = new ArrayList<RepositoryProblemReport>();
  57. protected String groupId;
  58. protected String repositoryId;
  59. protected String prev;
  60. protected String next;
  61. protected int[] range = new int[2];
  62. protected int page = 1;
  63. protected int rowCount = 100;
  64. protected boolean isLastPage;
  65. public static final String BLANK = "blank";
  66. public static final String BASIC = "basic";
  67. private static Boolean jasperPresent;
  68. private Collection<String> repositoryIds;
  69. public static final String ALL_REPOSITORIES = "All Repositories";
  70. protected Map<String, List<RepositoryProblemReport>> repositoriesMap =
  71. new TreeMap<String, List<RepositoryProblemReport>>();
  72. public void prepare()
  73. {
  74. repositoryIds = new ArrayList<String>();
  75. repositoryIds.add( ALL_REPOSITORIES ); // comes first to be first in the list
  76. repositoryIds.addAll(
  77. dao.query( new UniqueFieldConstraint( RepositoryProblem.class.getName(), "repositoryId" ) ) );
  78. }
  79. public Collection<String> getRepositoryIds()
  80. {
  81. return repositoryIds;
  82. }
  83. public String execute()
  84. throws Exception
  85. {
  86. List<RepositoryProblem> problemArtifacts =
  87. dao.getRepositoryProblemDAO().queryRepositoryProblems( configureConstraint() );
  88. String contextPath =
  89. request.getRequestURL().substring( 0, request.getRequestURL().indexOf( request.getRequestURI() ) );
  90. RepositoryProblem problemArtifact;
  91. RepositoryProblemReport problemArtifactReport;
  92. for ( int i = 0; i < problemArtifacts.size(); i++ )
  93. {
  94. problemArtifact = (RepositoryProblem) problemArtifacts.get( i );
  95. problemArtifactReport = new RepositoryProblemReport( problemArtifact );
  96. problemArtifactReport.setGroupURL( contextPath + "/browse/" + problemArtifact.getGroupId() );
  97. problemArtifactReport.setArtifactURL(
  98. contextPath + "/browse/" + problemArtifact.getGroupId() + "/" + problemArtifact.getArtifactId() );
  99. addToList( problemArtifactReport );
  100. // retained the reports list because this is the datasource for the jasper report
  101. reports.add( problemArtifactReport );
  102. }
  103. if ( reports.size() <= rowCount )
  104. {
  105. isLastPage = true;
  106. }
  107. else
  108. {
  109. reports.remove( rowCount );
  110. }
  111. prev = request.getRequestURL() + "?page=" + ( page - 1 ) + "&rowCount=" + rowCount + "&groupId=" + groupId +
  112. "&repositoryId=" + repositoryId;
  113. next = request.getRequestURL() + "?page=" + ( page + 1 ) + "&rowCount=" + rowCount + "&groupId=" + groupId +
  114. "&repositoryId=" + repositoryId;
  115. if ( reports.size() == 0 && page == 1 )
  116. {
  117. return BLANK;
  118. }
  119. else if ( isJasperPresent() )
  120. {
  121. return "jasper";
  122. }
  123. else
  124. {
  125. return SUCCESS;
  126. }
  127. }
  128. private static boolean isJasperPresent()
  129. {
  130. if ( jasperPresent == null )
  131. {
  132. try
  133. {
  134. Class.forName( "net.sf.jasperreports.engine.JRExporterParameter" );
  135. jasperPresent = Boolean.TRUE;
  136. }
  137. catch ( NoClassDefFoundError e )
  138. {
  139. jasperPresent = Boolean.FALSE;
  140. }
  141. catch ( ClassNotFoundException e )
  142. {
  143. jasperPresent = Boolean.FALSE;
  144. }
  145. }
  146. return jasperPresent.booleanValue();
  147. }
  148. private Constraint configureConstraint()
  149. {
  150. Constraint constraint;
  151. range[0] = ( page - 1 ) * rowCount;
  152. range[1] = ( page * rowCount ) + 1; // Add 1 to check if it's the last page or not.
  153. if ( groupId != null && ( !groupId.equals( "" ) ) )
  154. {
  155. if ( repositoryId != null && ( !repositoryId.equals( "" ) && !repositoryId.equals( ALL_REPOSITORIES ) ) )
  156. {
  157. constraint = new RepositoryProblemConstraint( range, groupId, repositoryId );
  158. }
  159. else
  160. {
  161. constraint = new RepositoryProblemByGroupIdConstraint( range, groupId );
  162. }
  163. }
  164. else if ( repositoryId != null && ( !repositoryId.equals( "" ) && !repositoryId.equals( ALL_REPOSITORIES ) ) )
  165. {
  166. constraint = new RepositoryProblemByRepositoryIdConstraint( range, repositoryId );
  167. }
  168. else
  169. {
  170. constraint = new RangeConstraint( range, "repositoryId" );
  171. }
  172. return constraint;
  173. }
  174. public void setServletRequest( HttpServletRequest request )
  175. {
  176. this.request = request;
  177. }
  178. public List<RepositoryProblemReport> getReports()
  179. {
  180. return reports;
  181. }
  182. public String getGroupId()
  183. {
  184. return groupId;
  185. }
  186. public void setGroupId( String groupId )
  187. {
  188. this.groupId = groupId;
  189. }
  190. public String getRepositoryId()
  191. {
  192. return repositoryId;
  193. }
  194. public void setRepositoryId( String repositoryId )
  195. {
  196. this.repositoryId = repositoryId;
  197. }
  198. public String getPrev()
  199. {
  200. return prev;
  201. }
  202. public String getNext()
  203. {
  204. return next;
  205. }
  206. public int getPage()
  207. {
  208. return page;
  209. }
  210. public void setPage( int page )
  211. {
  212. this.page = page;
  213. }
  214. public int getRowCount()
  215. {
  216. return rowCount;
  217. }
  218. public void setRowCount( int rowCount )
  219. {
  220. this.rowCount = rowCount;
  221. }
  222. public boolean getIsLastPage()
  223. {
  224. return isLastPage;
  225. }
  226. public void setRepositoriesMap( Map<String, List<RepositoryProblemReport>> repositoriesMap )
  227. {
  228. this.repositoriesMap = repositoriesMap;
  229. }
  230. public Map<String, List<RepositoryProblemReport>> getRepositoriesMap()
  231. {
  232. return repositoriesMap;
  233. }
  234. public SecureActionBundle getSecureActionBundle()
  235. throws SecureActionException
  236. {
  237. SecureActionBundle bundle = new SecureActionBundle();
  238. bundle.setRequiresAuthentication( true );
  239. bundle.addRequiredAuthorization( ArchivaRoleConstants.OPERATION_ACCESS_REPORT, Resource.GLOBAL );
  240. return bundle;
  241. }
  242. private void addToList( RepositoryProblemReport repoProblemReport )
  243. {
  244. List<RepositoryProblemReport> problemsList = null;
  245. if ( repositoriesMap.containsKey( repoProblemReport.getRepositoryId() ) )
  246. {
  247. problemsList = ( List<RepositoryProblemReport> ) repositoriesMap.get( repoProblemReport.getRepositoryId() );
  248. }
  249. else
  250. {
  251. problemsList = new ArrayList<RepositoryProblemReport>();
  252. repositoriesMap.put( repoProblemReport.getRepositoryId(), problemsList );
  253. }
  254. problemsList.add( repoProblemReport );
  255. }
  256. }