1 package org.apache.archiva.web.xmlrpc.services;
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.Date;
25 import java.util.List;
27 import org.apache.archiva.indexer.search.RepositorySearch;
28 import org.apache.archiva.indexer.search.SearchResultHit;
29 import org.apache.archiva.indexer.search.SearchResultLimits;
30 import org.apache.archiva.indexer.search.SearchResults;
31 import org.apache.archiva.metadata.model.ProjectVersionMetadata;
32 import org.apache.archiva.metadata.model.ProjectVersionReference;
33 import org.apache.archiva.metadata.repository.MetadataResolver;
34 import org.apache.archiva.metadata.repository.storage.maven2.MavenProjectFacet;
35 import org.apache.archiva.web.xmlrpc.api.SearchService;
36 import org.apache.archiva.web.xmlrpc.api.beans.Artifact;
37 import org.apache.archiva.web.xmlrpc.api.beans.Dependency;
38 import org.apache.archiva.web.xmlrpc.security.XmlRpcUserRepositories;
39 import org.apache.maven.archiva.common.utils.VersionUtil;
40 import org.apache.maven.archiva.database.ArchivaDAO;
41 import org.apache.maven.archiva.database.ArtifactDAO;
42 import org.apache.maven.archiva.database.constraints.ArtifactsByChecksumConstraint;
43 import org.apache.maven.archiva.database.constraints.UniqueVersionConstraint;
44 import org.apache.maven.archiva.model.ArchivaArtifact;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
48 public class SearchServiceImpl
49 implements SearchService
51 private Logger log = LoggerFactory.getLogger( SearchServiceImpl.class );
53 private RepositorySearch search;
55 private XmlRpcUserRepositories xmlRpcUserRepositories;
57 private ArchivaDAO archivaDAO;
59 private MetadataResolver metadataResolver;
61 public SearchServiceImpl( XmlRpcUserRepositories xmlRpcUserRepositories, ArchivaDAO archivaDAO,
62 MetadataResolver metadataResolver, RepositorySearch search )
64 this.xmlRpcUserRepositories = xmlRpcUserRepositories;
65 this.archivaDAO = archivaDAO;
67 this.metadataResolver = metadataResolver;
70 @SuppressWarnings("unchecked")
71 public List<Artifact> quickSearch( String queryString )
74 List<Artifact> artifacts = new ArrayList<Artifact>();
75 List<String> observableRepos = xmlRpcUserRepositories.getObservableRepositories();
76 SearchResultLimits limits = new SearchResultLimits( SearchResultLimits.ALL_PAGES );
77 SearchResults results = null;
79 results = search.search( "", observableRepos, queryString, limits, null );
81 for ( SearchResultHit resultHit : results.getHits() )
83 // double-check all versions as done in SearchAction
84 final List<String> versions = (List<String>) archivaDAO.query(
85 new UniqueVersionConstraint( observableRepos, resultHit.getGroupId(), resultHit.getArtifactId() ) );
86 if ( versions != null && !versions.isEmpty() )
88 resultHit.setVersion( null );
89 resultHit.setVersions( filterTimestampedSnapshots( versions ) );
92 List<String> resultHitVersions = resultHit.getVersions();
93 if ( resultHitVersions != null )
95 for ( String version : resultHitVersions )
97 Artifact artifact = null;
98 for ( String repoId : observableRepos )
100 // slight behaviour change to previous implementation: instead of allocating "jar" when not
101 // found in the database, we can rely on the metadata repository to create it on the fly. We
102 // just allocate the default packaging if the Maven facet is not found.
103 ProjectVersionMetadata model =
104 metadataResolver.getProjectVersion( repoId, resultHit.getGroupId(),
105 resultHit.getArtifactId(), version );
109 String packaging = "jar";
111 MavenProjectFacet facet = (MavenProjectFacet) model.getFacet( MavenProjectFacet.FACET_ID );
112 if ( facet != null && facet.getPackaging() != null )
114 packaging = facet.getPackaging();
116 artifact = new Artifact( repoId, resultHit.getGroupId(), resultHit.getArtifactId(), version,
122 if ( artifact != null )
124 artifacts.add( artifact );
134 * Remove timestamped snapshots from versions
136 private static List<String> filterTimestampedSnapshots( List<String> versions )
138 final List<String> filtered = new ArrayList<String>();
139 for ( final String version : versions )
141 final String baseVersion = VersionUtil.getBaseVersion( version );
142 if ( !filtered.contains( baseVersion ) )
144 filtered.add( baseVersion );
150 public List<Artifact> getArtifactByChecksum( String checksum )
153 // 1. get ArtifactDAO from ArchivaDAO
154 // 2. create ArtifactsByChecksumConstraint( "queryTerm" )
155 // 3. query artifacts using constraint
156 // 4. convert results to list of Artifact objects
158 List<Artifact> results = new ArrayList<Artifact>();
159 ArtifactDAO artifactDAO = archivaDAO.getArtifactDAO();
161 ArtifactsByChecksumConstraint constraint = new ArtifactsByChecksumConstraint( checksum );
162 List<ArchivaArtifact> artifacts = artifactDAO.queryArtifacts( constraint );
164 for ( ArchivaArtifact archivaArtifact : artifacts )
167 new Artifact( archivaArtifact.getModel().getRepositoryId(), archivaArtifact.getModel().getGroupId(),
168 archivaArtifact.getModel().getArtifactId(), archivaArtifact.getModel().getVersion(),
169 archivaArtifact.getType() );
170 //archivaArtifact.getModel().getWhenGathered() );
171 results.add( artifact );
177 public List<Artifact> getArtifactVersions( String groupId, String artifactId )
180 List<Artifact> artifacts = new ArrayList<Artifact>();
181 List<String> observableRepos = xmlRpcUserRepositories.getObservableRepositories();
183 for ( String repoId : observableRepos )
185 Collection<String> results = metadataResolver.getProjectVersions( repoId, groupId, artifactId );
187 for ( final String version : results )
189 final Artifact artifact = new Artifact( repoId, groupId, artifactId, version, "pom" );
191 artifacts.add( artifact );
198 public List<Artifact> getArtifactVersionsByDate( String groupId, String artifactId, String version, Date since )
201 List<Artifact> artifacts = new ArrayList<Artifact>();
203 // 1. get observable repositories
204 // 2. use RepositoryBrowsing method to query uniqueVersions? (but with date)
206 throw new UnsupportedOperationException( "getArtifactVersionsByDate not yet implemented" );
211 public List<Dependency> getDependencies( String groupId, String artifactId, String version )
214 List<String> observableRepos = xmlRpcUserRepositories.getObservableRepositories();
216 for ( String repoId : observableRepos )
218 ProjectVersionMetadata model = metadataResolver.getProjectVersion( repoId, groupId, artifactId, version );
221 List<Dependency> dependencies = new ArrayList<Dependency>();
222 List<org.apache.archiva.metadata.model.Dependency> modelDeps = model.getDependencies();
223 for ( org.apache.archiva.metadata.model.Dependency dep : modelDeps )
225 Dependency dependency =
226 new Dependency( dep.getGroupId(), dep.getArtifactId(), dep.getVersion(), dep.getClassifier(),
227 dep.getType(), dep.getScope() );
228 dependencies.add( dependency );
233 throw new Exception( "Artifact does not exist." );
236 public List<Artifact> getDependencyTree( String groupId, String artifactId, String version )
239 List<Artifact> a = new ArrayList<Artifact>();
241 throw new UnsupportedOperationException( "getDependencyTree not yet implemented" );
245 public List<Artifact> getDependees( String groupId, String artifactId, String version )
248 List<Artifact> artifacts = new ArrayList<Artifact>();
249 List<String> observableRepos = xmlRpcUserRepositories.getObservableRepositories();
251 for ( String repoId : observableRepos )
253 Collection<ProjectVersionReference> refs =
254 metadataResolver.getProjectReferences( repoId, groupId, artifactId, version );
255 for ( ProjectVersionReference ref : refs )
258 new Artifact( repoId, ref.getNamespace(), ref.getProjectId(), ref.getProjectVersion(), "" ) );