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.indexer.search.SearchResultHit;
28 import org.apache.archiva.indexer.search.SearchResultLimits;
29 import org.apache.archiva.indexer.search.SearchResults;
30 import org.apache.archiva.web.xmlrpc.api.SearchService;
31 import org.apache.archiva.web.xmlrpc.api.beans.Artifact;
32 import org.apache.archiva.web.xmlrpc.api.beans.Dependency;
33 import org.apache.archiva.web.xmlrpc.security.XmlRpcUserRepositories;
34 import org.apache.maven.archiva.database.ArchivaDAO;
35 import org.apache.maven.archiva.database.ArchivaDatabaseException;
36 import org.apache.maven.archiva.database.ArtifactDAO;
37 import org.apache.maven.archiva.database.ObjectNotFoundException;
38 import org.apache.maven.archiva.database.browsing.BrowsingResults;
39 import org.apache.maven.archiva.database.browsing.RepositoryBrowsing;
40 import org.apache.maven.archiva.database.constraints.ArtifactsByChecksumConstraint;
41 import org.apache.maven.archiva.model.ArchivaArtifact;
42 import org.apache.maven.archiva.model.ArchivaProjectModel;
47 * @version $Id: SearchServiceImpl.java
49 public class SearchServiceImpl
50 implements SearchService
52 private RepositorySearch search;
54 private XmlRpcUserRepositories xmlRpcUserRepositories;
56 private ArchivaDAO archivaDAO;
58 private RepositoryBrowsing repoBrowsing;
60 public SearchServiceImpl( XmlRpcUserRepositories xmlRpcUserRepositories, ArchivaDAO archivaDAO,
61 RepositoryBrowsing repoBrowsing, RepositorySearch search )
63 this.xmlRpcUserRepositories = xmlRpcUserRepositories;
64 this.archivaDAO = archivaDAO;
65 this.repoBrowsing = repoBrowsing;
70 * quick/general text search which returns a list of artifacts
71 * query for an artifact based on a checksum
72 * query for all available versions of an artifact, sorted in version significance order
73 * query for all available versions of an artifact since a given date
74 * query for an artifact's direct dependencies
75 * query for an artifact's dependency tree (as with mvn dependency:tree - no duplicates should be included)
76 * query for all artifacts that depend on a given artifact
79 public List<Artifact> quickSearch( String queryString )
82 // 1. check whether bytecode search or ordinary search
83 // 2. get observable repos
84 // 3. convert results to a list of Artifact objects
86 List<Artifact> artifacts = new ArrayList<Artifact>();
87 List<String> observableRepos = xmlRpcUserRepositories.getObservableRepositories();
88 SearchResultLimits limits = new SearchResultLimits( SearchResultLimits.ALL_PAGES );
89 SearchResults results = null;
91 results = search.search( "", observableRepos, queryString, limits, null );
93 List<SearchResultHit> hits = results.getHits();
94 for( SearchResultHit hit : hits )
96 ArtifactDAO artifactDAO = archivaDAO.getArtifactDAO();
99 ArchivaArtifact pomArtifact = artifactDAO.getArtifact(
100 hit.getGroupId(), hit.getArtifactId(), hit.getVersion(), "", "pom", hit.getRepositoryId() );
102 if( pomArtifact != null )
104 Artifact artifact = new Artifact( pomArtifact.getModel().getRepositoryId(), pomArtifact.getGroupId(), pomArtifact.getArtifactId(), pomArtifact.getVersion(),
105 pomArtifact.getType() );
106 //pomArtifact.getType(), pomArtifact.getModel().getWhenGathered() );
107 artifacts.add( artifact );
114 catch ( ObjectNotFoundException e )
118 catch ( ArchivaDatabaseException e )
127 public List<Artifact> getArtifactByChecksum( String checksum )
130 // 1. get ArtifactDAO from ArchivaDAO
131 // 2. create ArtifactsByChecksumConstraint( "queryTerm" )
132 // 3. query artifacts using constraint
133 // 4. convert results to list of Artifact objects
135 List<Artifact> results = new ArrayList<Artifact>();
136 ArtifactDAO artifactDAO = archivaDAO.getArtifactDAO();
138 ArtifactsByChecksumConstraint constraint = new ArtifactsByChecksumConstraint( checksum );
139 List<ArchivaArtifact> artifacts = artifactDAO.queryArtifacts( constraint );
141 for( ArchivaArtifact archivaArtifact : artifacts )
143 Artifact artifact = new Artifact( archivaArtifact.getModel().getRepositoryId(), archivaArtifact.getModel().getGroupId(),
144 archivaArtifact.getModel().getArtifactId(), archivaArtifact.getModel().getVersion(), archivaArtifact.getType() );
145 //archivaArtifact.getModel().getWhenGathered() );
146 results.add( artifact );
152 public List<Artifact> getArtifactVersions( String groupId, String artifactId )
155 final List<Artifact> artifacts = new ArrayList<Artifact>();
156 final List<String> observableRepos = xmlRpcUserRepositories.getObservableRepositories();
158 final BrowsingResults results = repoBrowsing.selectArtifactId( "", observableRepos, groupId, artifactId );
159 final ArtifactDAO artifactDAO = archivaDAO.getArtifactDAO();
161 for( final String version : results.getVersions() )
163 final Artifact artifact = new Artifact( "", groupId, artifactId, version, "pom" );
164 //ArchivaArtifact pomArtifact = artifactDAO.getArtifact( groupId, artifactId, version, "", "pom", );
165 //Artifact artifact = new Artifact( "", groupId, artifactId, version, pomArtifact.getType() );
166 //pomArtifact.getModel().getWhenGathered() );
168 artifacts.add( artifact );
171 // 1. get observable repositories
172 // 2. use RepositoryBrowsing method to query uniqueVersions?
176 public List<Artifact> getArtifactVersionsByDate( String groupId, String artifactId, String version, Date since )
179 List<Artifact> artifacts = new ArrayList<Artifact>();
181 // 1. get observable repositories
182 // 2. use RepositoryBrowsing method to query uniqueVersions? (but with date)
187 public List<Dependency> getDependencies( String groupId, String artifactId, String version )
190 List<Dependency> dependencies = new ArrayList<Dependency>();
191 List<String> observableRepos = xmlRpcUserRepositories.getObservableRepositories();
195 ArchivaProjectModel model = repoBrowsing.selectVersion( "", observableRepos, groupId, artifactId, version );
196 List<org.apache.maven.archiva.model.Dependency> modelDeps = model.getDependencies();
197 for( org.apache.maven.archiva.model.Dependency dep : modelDeps )
199 Dependency dependency = new Dependency(
200 dep.getGroupId(), dep.getArtifactId(), dep.getVersion(), dep.getClassifier(), dep.getType(), dep.getScope() );
201 dependencies.add( dependency );
204 catch ( ObjectNotFoundException oe )
206 throw new Exception( "Artifact does not exist." );
212 public List<Artifact> getDependencyTree( String groupId, String artifactId, String version )
215 List<Artifact> a = new ArrayList<Artifact>();
220 //get artifacts that depend on a given artifact
221 public List<Artifact> getDependees( String groupId, String artifactId, String version )
224 List<Artifact> artifacts = new ArrayList<Artifact>();
225 List<String> observableRepos = xmlRpcUserRepositories.getObservableRepositories();
227 List<ArchivaProjectModel> dependees = repoBrowsing.getUsedBy( "", observableRepos, "org.apache.archiva", "archiva-test", "1.0" );
228 for( ArchivaProjectModel model : dependees )
231 new Artifact( "", model.getGroupId(), model.getArtifactId(), model.getVersion(), "" );
232 //model.getWhenIndexed() );
233 artifacts.add( artifact );