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.Date;
24 import java.util.List;
26 import org.apache.archiva.indexer.search.RepositorySearch;
27 import org.apache.archiva.web.xmlrpc.api.SearchService;
28 import org.apache.archiva.web.xmlrpc.api.beans.Artifact;
29 import org.apache.archiva.web.xmlrpc.api.beans.Dependency;
30 import org.apache.archiva.web.xmlrpc.security.XmlRpcUserRepositories;
31 import org.apache.maven.archiva.database.ArchivaDAO;
32 import org.apache.maven.archiva.database.ArtifactDAO;
33 import org.apache.maven.archiva.database.ObjectNotFoundException;
34 import org.apache.maven.archiva.database.browsing.BrowsingResults;
35 import org.apache.maven.archiva.database.browsing.RepositoryBrowsing;
36 import org.apache.maven.archiva.database.constraints.ArtifactsByChecksumConstraint;
37 import org.apache.maven.archiva.indexer.search.SearchResultHit;
38 import org.apache.maven.archiva.indexer.search.SearchResultLimits;
39 import org.apache.maven.archiva.indexer.search.SearchResults;
40 import org.apache.maven.archiva.model.ArchivaArtifact;
41 import org.apache.maven.archiva.model.ArchivaProjectModel;
46 * @version $Id: SearchServiceImpl.java
48 public class SearchServiceImpl
49 implements SearchService
51 private RepositorySearch search;
53 private XmlRpcUserRepositories xmlRpcUserRepositories;
55 private ArchivaDAO archivaDAO;
57 private RepositoryBrowsing repoBrowsing;
59 public SearchServiceImpl( XmlRpcUserRepositories xmlRpcUserRepositories, ArchivaDAO archivaDAO,
60 RepositoryBrowsing repoBrowsing, RepositorySearch search )
62 this.xmlRpcUserRepositories = xmlRpcUserRepositories;
63 this.archivaDAO = archivaDAO;
64 this.repoBrowsing = repoBrowsing;
69 * quick/general text search which returns a list of artifacts
70 * query for an artifact based on a checksum
71 * query for all available versions of an artifact, sorted in version significance order
72 * query for all available versions of an artifact since a given date
73 * query for an artifact's direct dependencies
74 * query for an artifact's dependency tree (as with mvn dependency:tree - no duplicates should be included)
75 * query for all artifacts that depend on a given artifact
78 public List<Artifact> quickSearch( String queryString )
81 // 1. check whether bytecode search or ordinary search
82 // 2. get observable repos
83 // 3. convert results to a list of Artifact objects
85 List<Artifact> artifacts = new ArrayList<Artifact>();
86 List<String> observableRepos = xmlRpcUserRepositories.getObservableRepositories();
87 SearchResultLimits limits = new SearchResultLimits( SearchResultLimits.ALL_PAGES );
88 SearchResults results = null;
90 results = search.search( "", observableRepos, queryString, limits, null );
92 List<SearchResultHit> hits = results.getHits();
93 for( SearchResultHit hit : hits )
95 ArtifactDAO artifactDAO = archivaDAO.getArtifactDAO();
96 ArchivaArtifact pomArtifact = artifactDAO.getArtifact(
97 hit.getGroupId(), hit.getArtifactId(), hit.getVersion(), "", "pom" );
99 if( pomArtifact != null )
101 Artifact artifact = new Artifact( pomArtifact.getModel().getRepositoryId(), pomArtifact.getGroupId(), pomArtifact.getArtifactId(), pomArtifact.getVersion(),
102 pomArtifact.getType(), pomArtifact.getModel().getWhenGathered() );
103 artifacts.add( artifact );
114 public List<Artifact> getArtifactByChecksum( String checksum )
117 // 1. get ArtifactDAO from ArchivaDAO
118 // 2. create ArtifactsByChecksumConstraint( "queryTerm" )
119 // 3. query artifacts using constraint
120 // 4. convert results to list of Artifact objects
122 List<Artifact> results = new ArrayList<Artifact>();
123 ArtifactDAO artifactDAO = archivaDAO.getArtifactDAO();
125 ArtifactsByChecksumConstraint constraint = new ArtifactsByChecksumConstraint( checksum );
126 List<ArchivaArtifact> artifacts = artifactDAO.queryArtifacts( constraint );
128 for( ArchivaArtifact archivaArtifact : artifacts )
130 Artifact artifact = new Artifact( archivaArtifact.getModel().getRepositoryId(), archivaArtifact.getModel().getGroupId(),
131 archivaArtifact.getModel().getArtifactId(), archivaArtifact.getModel().getVersion(), archivaArtifact.getType(),
132 archivaArtifact.getModel().getWhenGathered() );
133 results.add( artifact );
139 public List<Artifact> getArtifactVersions( String groupId, String artifactId )
142 List<Artifact> artifacts = new ArrayList<Artifact>();
143 List<String> observableRepos = xmlRpcUserRepositories.getObservableRepositories();
145 BrowsingResults results = repoBrowsing.selectArtifactId( "", observableRepos, groupId, artifactId );
146 ArtifactDAO artifactDAO = archivaDAO.getArtifactDAO();
148 for( String version : results.getVersions() )
150 ArchivaArtifact pomArtifact = artifactDAO.getArtifact( groupId, artifactId, version, "", "pom" );
151 Artifact artifact = new Artifact( "", groupId, artifactId, version, pomArtifact.getType(),
152 pomArtifact.getModel().getWhenGathered() );
154 artifacts.add( artifact );
157 // 1. get observable repositories
158 // 2. use RepositoryBrowsing method to query uniqueVersions?
162 public List<Artifact> getArtifactVersionsByDate( String groupId, String artifactId, String version, Date since )
165 List<Artifact> artifacts = new ArrayList<Artifact>();
167 // 1. get observable repositories
168 // 2. use RepositoryBrowsing method to query uniqueVersions? (but with date)
173 public List<Dependency> getDependencies( String groupId, String artifactId, String version )
176 List<Dependency> dependencies = new ArrayList<Dependency>();
177 List<String> observableRepos = xmlRpcUserRepositories.getObservableRepositories();
181 ArchivaProjectModel model = repoBrowsing.selectVersion( "", observableRepos, groupId, artifactId, version );
182 List<org.apache.maven.archiva.model.Dependency> modelDeps = model.getDependencies();
183 for( org.apache.maven.archiva.model.Dependency dep : modelDeps )
185 Dependency dependency = new Dependency(
186 dep.getGroupId(), dep.getArtifactId(), dep.getVersion(), dep.getClassifier(), dep.getType(), dep.getScope() );
187 dependencies.add( dependency );
190 catch ( ObjectNotFoundException oe )
192 throw new Exception( "Artifact does not exist." );
198 public List<Artifact> getDependencyTree( String groupId, String artifactId, String version )
201 List<Artifact> a = new ArrayList<Artifact>();
206 //get artifacts that depend on a given artifact
207 public List<Artifact> getDependees( String groupId, String artifactId, String version )
210 List<Artifact> artifacts = new ArrayList<Artifact>();
211 List<String> observableRepos = xmlRpcUserRepositories.getObservableRepositories();
213 List<ArchivaProjectModel> dependees = repoBrowsing.getUsedBy( "", observableRepos, "org.apache.archiva", "archiva-test", "1.0" );
214 for( ArchivaProjectModel model : dependees )
216 Artifact artifact = new Artifact( "", model.getGroupId(), model.getArtifactId(), model.getVersion(), "", model.getWhenIndexed() );
217 artifacts.add( artifact );