1 package org.apache.maven.archiva.web.action;
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 java.util.ArrayList;
23 import java.util.Collection;
24 import java.util.Collections;
25 import java.util.LinkedHashSet;
26 import java.util.List;
29 import org.apache.archiva.metadata.model.ProjectVersionMetadata;
30 import org.apache.archiva.metadata.repository.MetadataResolver;
31 import org.apache.archiva.metadata.repository.MetadataResolverException;
32 import org.apache.archiva.metadata.repository.storage.maven2.MavenProjectFacet;
33 import org.apache.commons.collections.CollectionUtils;
34 import org.apache.commons.lang.StringUtils;
37 * Browse the repository.
39 * @todo implement repository selectors (all or specific repository)
40 * @plexus.component role="com.opensymphony.xwork2.Action" role-hint="browseAction" instantiation-strategy="per-lookup"
42 public class BrowseAction
43 extends AbstractRepositoryBasedAction
48 private MetadataResolver metadataResolver;
50 private String groupId;
52 private String artifactId;
54 private String repositoryId;
56 private ProjectVersionMetadata sharedModel;
58 private Collection<String> namespaces;
60 private Collection<String> projectIds;
62 private Collection<String> projectVersions;
64 public String browse()
66 List<String> selectedRepos = getObservableRepos();
67 if ( CollectionUtils.isEmpty( selectedRepos ) )
69 return GlobalResults.ACCESS_TO_NO_REPOS;
72 Set<String> namespaces = new LinkedHashSet<String>();
73 for ( String repoId : selectedRepos )
75 Collection<String> rootNamespaces = metadataResolver.getRootNamespaces( repoId );
76 // TODO: this logic should be optional, particularly remembering we want to keep this code simple
77 // it is located here to avoid the content repository implementation needing to do too much for what
78 // is essentially presentation code
79 for ( String n : rootNamespaces )
81 // TODO: check performance of this
82 namespaces.add( collapseNamespaces( repoId, n ) );
86 this.namespaces = getSortedList( namespaces );
90 private String collapseNamespaces( String repoId, String n )
92 Collection<String> subNamespaces = metadataResolver.getNamespaces( repoId, n );
93 if ( subNamespaces.size() != 1 )
99 Collection<String> projects = metadataResolver.getProjects( repoId, n );
100 if ( projects != null && !projects.isEmpty() )
106 return collapseNamespaces( repoId, n + "." + subNamespaces.iterator().next() );
111 public String browseGroup()
113 if ( StringUtils.isEmpty( groupId ) )
116 addActionError( "You must specify a group ID to browse" );
120 List<String> selectedRepos = getObservableRepos();
121 if ( CollectionUtils.isEmpty( selectedRepos ) )
123 return GlobalResults.ACCESS_TO_NO_REPOS;
126 Set<String> namespaces = new LinkedHashSet<String>();
127 Set<String> projects = new LinkedHashSet<String>();
128 for ( String repoId : selectedRepos )
130 Collection<String> childNamespaces = metadataResolver.getNamespaces( repoId, groupId );
131 // TODO: this logic should be optional, particularly remembering we want to keep this code simple
132 // it is located here to avoid the content repository implementation needing to do too much for what
133 // is essentially presentation code
134 for ( String n : childNamespaces )
136 // TODO: check performance of this
137 namespaces.add( collapseNamespaces( repoId, groupId + "." + n ) );
140 projects.addAll( metadataResolver.getProjects( repoId, groupId ) );
143 this.namespaces = getSortedList( namespaces );
144 this.projectIds = getSortedList( projects );
148 private ArrayList<String> getSortedList( Set<String> set )
150 ArrayList<String> list = new ArrayList<String>( set );
151 Collections.sort( list );
155 public String browseArtifact()
156 throws MetadataResolverException
158 if ( StringUtils.isEmpty( groupId ) )
161 addActionError( "You must specify a group ID to browse" );
165 if ( StringUtils.isEmpty( artifactId ) )
168 addActionError( "You must specify a artifact ID to browse" );
172 List<String> selectedRepos = getObservableRepos();
173 if ( CollectionUtils.isEmpty( selectedRepos ) )
175 return GlobalResults.ACCESS_TO_NO_REPOS;
178 Set<String> versions = new LinkedHashSet<String>();
179 for ( String repoId : selectedRepos )
181 versions.addAll( metadataResolver.getProjectVersions( repoId, groupId, artifactId ) );
184 // TODO: sort by known version ordering method
185 this.projectVersions = new ArrayList<String>( versions );
187 populateSharedModel( selectedRepos, versions );
192 private void populateSharedModel( Collection<String> selectedRepos, Collection<String> projectVersions )
193 throws MetadataResolverException
195 sharedModel = new ProjectVersionMetadata();
197 MavenProjectFacet mavenFacet = new MavenProjectFacet();
198 mavenFacet.setGroupId( groupId );
199 mavenFacet.setArtifactId( artifactId );
200 sharedModel.addFacet( mavenFacet );
202 boolean isFirstVersion = true;
204 for ( String version : projectVersions )
206 ProjectVersionMetadata versionMetadata = null;
207 for ( String repoId : selectedRepos )
209 if ( versionMetadata == null )
211 versionMetadata = metadataResolver.getProjectVersion( repoId, groupId, artifactId, version );
215 if ( versionMetadata == null )
220 if ( isFirstVersion )
222 sharedModel = versionMetadata;
223 sharedModel.setId( null );
227 MavenProjectFacet versionMetadataMavenFacet =
228 (MavenProjectFacet) versionMetadata.getFacet( MavenProjectFacet.FACET_ID );
229 if ( versionMetadataMavenFacet != null )
231 if ( mavenFacet.getPackaging() != null && !StringUtils.equalsIgnoreCase( mavenFacet.getPackaging(),
232 versionMetadataMavenFacet.getPackaging() ) )
234 mavenFacet.setPackaging( null );
238 if ( sharedModel.getName() != null &&
239 !StringUtils.equalsIgnoreCase( sharedModel.getName(), versionMetadata.getName() ) )
241 sharedModel.setName( "" );
244 if ( sharedModel.getDescription() != null &&
245 !StringUtils.equalsIgnoreCase( sharedModel.getDescription(), versionMetadata.getDescription() ) )
247 sharedModel.setDescription( null );
250 if ( sharedModel.getIssueManagement() != null && versionMetadata.getIssueManagement() != null &&
251 !StringUtils.equalsIgnoreCase( sharedModel.getIssueManagement().getUrl(),
252 versionMetadata.getIssueManagement().getUrl() ) )
254 sharedModel.setIssueManagement( null );
257 if ( sharedModel.getCiManagement() != null && versionMetadata.getCiManagement() != null &&
258 !StringUtils.equalsIgnoreCase( sharedModel.getCiManagement().getUrl(),
259 versionMetadata.getCiManagement().getUrl() ) )
261 sharedModel.setCiManagement( null );
264 if ( sharedModel.getOrganization() != null && versionMetadata.getOrganization() != null &&
265 !StringUtils.equalsIgnoreCase( sharedModel.getOrganization().getName(),
266 versionMetadata.getOrganization().getName() ) )
268 sharedModel.setOrganization( null );
271 if ( sharedModel.getUrl() != null &&
272 !StringUtils.equalsIgnoreCase( sharedModel.getUrl(), versionMetadata.getUrl() ) )
274 sharedModel.setUrl( null );
278 isFirstVersion = false;
282 public String getGroupId()
287 public void setGroupId( String groupId )
289 this.groupId = groupId;
292 public String getArtifactId()
297 public void setArtifactId( String artifactId )
299 this.artifactId = artifactId;
302 public Collection<String> getNamespaces()
307 public String getRepositoryId()
313 public void setRepositoryId( String repositoryId )
316 this.repositoryId = repositoryId;
319 public ProjectVersionMetadata getSharedModel()
324 public MetadataResolver getMetadataResolver()
326 return metadataResolver;
329 public Collection<String> getProjectIds()
334 public Collection<String> getProjectVersions()
336 return projectVersions;