]> source.dussan.org Git - archiva.git/blob
2f998f406ab715da79233b7b69e49b77e2b83ca3
[archiva.git] /
1 package org.apache.archiva.web.xmlrpc.services;
2
3 /*
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
11  *
12  *  http://www.apache.org/licenses/LICENSE-2.0
13  *
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
19  * under the License.
20  */
21
22 import org.apache.archiva.indexer.search.RepositorySearch;
23 import org.apache.archiva.indexer.search.SearchResultHit;
24 import org.apache.archiva.indexer.search.SearchResultLimits;
25 import org.apache.archiva.indexer.search.SearchResults;
26 import org.apache.archiva.metadata.model.ArtifactMetadata;
27 import org.apache.archiva.metadata.model.FacetedMetadata;
28 import org.apache.archiva.metadata.model.ProjectVersionMetadata;
29 import org.apache.archiva.metadata.model.ProjectVersionReference;
30 import org.apache.archiva.metadata.repository.MetadataRepository;
31 import org.apache.archiva.metadata.repository.MetadataResolver;
32 import org.apache.archiva.metadata.repository.storage.maven2.MavenArtifactFacet;
33 import org.apache.archiva.metadata.repository.storage.maven2.MavenProjectFacet;
34 import org.apache.archiva.web.xmlrpc.api.SearchService;
35 import org.apache.archiva.web.xmlrpc.api.beans.Artifact;
36 import org.apache.archiva.web.xmlrpc.api.beans.Dependency;
37 import org.apache.archiva.web.xmlrpc.security.XmlRpcUserRepositories;
38
39 import java.util.ArrayList;
40 import java.util.Collection;
41 import java.util.Date;
42 import java.util.List;
43
44 public class SearchServiceImpl
45     implements SearchService
46 {
47     private RepositorySearch search;
48
49     private XmlRpcUserRepositories xmlRpcUserRepositories;
50
51     private MetadataResolver metadataResolver;
52
53     private MetadataRepository metadataRepository;
54
55     public SearchServiceImpl( XmlRpcUserRepositories xmlRpcUserRepositories, MetadataResolver metadataResolver,
56                               MetadataRepository metadataRepository, RepositorySearch search )
57     {
58         this.xmlRpcUserRepositories = xmlRpcUserRepositories;
59         this.search = search;
60         this.metadataResolver = metadataResolver;
61         this.metadataRepository = metadataRepository;
62     }
63
64     @SuppressWarnings( "unchecked" )
65     public List<Artifact> quickSearch( String queryString )
66         throws Exception
67     {
68         List<Artifact> artifacts = new ArrayList<Artifact>();
69         List<String> observableRepos = xmlRpcUserRepositories.getObservableRepositories();
70         SearchResultLimits limits = new SearchResultLimits( SearchResultLimits.ALL_PAGES );
71         SearchResults results;
72
73         results = search.search( "", observableRepos, queryString, limits, null );
74
75         for ( SearchResultHit resultHit : results.getHits() )
76         {
77             List<String> resultHitVersions = resultHit.getVersions();
78             if ( resultHitVersions != null )
79             {
80                 for ( String version : resultHitVersions )
81                 {
82                     Artifact artifact = null;
83                     for ( String repoId : observableRepos )
84                     {
85                         // slight behaviour change to previous implementation: instead of allocating "jar" when not
86                         // found in the database, we can rely on the metadata repository to create it on the fly. We
87                         // just allocate the default packaging if the Maven facet is not found.
88                         FacetedMetadata model = metadataResolver.resolveProjectVersion( repoId, resultHit.getGroupId(),
89                                                                                         resultHit.getArtifactId(),
90                                                                                         version );
91
92                         if ( model != null )
93                         {
94                             String packaging = "jar";
95
96                             MavenProjectFacet facet = (MavenProjectFacet) model.getFacet( MavenProjectFacet.FACET_ID );
97                             if ( facet != null && facet.getPackaging() != null )
98                             {
99                                 packaging = facet.getPackaging();
100                             }
101                             artifact = new Artifact( repoId, resultHit.getGroupId(), resultHit.getArtifactId(), version,
102                                                      packaging );
103                             break;
104                         }
105                     }
106
107                     if ( artifact != null )
108                     {
109                         artifacts.add( artifact );
110                     }
111                 }
112             }
113         }
114
115         return artifacts;
116     }
117
118     public List<Artifact> getArtifactByChecksum( String checksum )
119         throws Exception
120     {
121         List<String> observableRepos = xmlRpcUserRepositories.getObservableRepositories();
122
123         List<Artifact> results = new ArrayList<Artifact>();
124         for ( String repoId : observableRepos )
125         {
126             for ( ArtifactMetadata artifact : metadataRepository.getArtifactsByChecksum( repoId, checksum ) )
127             {
128                 // TODO: customise XMLRPC to handle non-Maven artifacts
129                 MavenArtifactFacet facet = (MavenArtifactFacet) artifact.getFacet( MavenArtifactFacet.FACET_ID );
130
131                 results.add( new Artifact( artifact.getRepositoryId(), artifact.getNamespace(), artifact.getProject(),
132                                            artifact.getVersion(), facet != null ? facet.getType() : null ) );
133             }
134         }
135         return results;
136     }
137
138     public List<Artifact> getArtifactVersions( String groupId, String artifactId )
139         throws Exception
140     {
141         List<Artifact> artifacts = new ArrayList<Artifact>();
142         List<String> observableRepos = xmlRpcUserRepositories.getObservableRepositories();
143
144         for ( String repoId : observableRepos )
145         {
146             Collection<String> results = metadataResolver.resolveProjectVersions( repoId, groupId, artifactId );
147
148             for ( final String version : results )
149             {
150                 final Artifact artifact = new Artifact( repoId, groupId, artifactId, version, "pom" );
151
152                 artifacts.add( artifact );
153             }
154         }
155
156         return artifacts;
157     }
158
159     public List<Artifact> getArtifactVersionsByDate( String groupId, String artifactId, String version, Date since )
160         throws Exception
161     {
162 //        List<Artifact> artifacts = new ArrayList<Artifact>();
163
164         // 1. get observable repositories
165         // 2. use RepositoryBrowsing method to query uniqueVersions? (but with date)
166
167         throw new UnsupportedOperationException( "getArtifactVersionsByDate not yet implemented" );
168
169 //        return artifacts;
170     }
171
172     public List<Dependency> getDependencies( String groupId, String artifactId, String version )
173         throws Exception
174     {
175         List<String> observableRepos = xmlRpcUserRepositories.getObservableRepositories();
176
177         for ( String repoId : observableRepos )
178         {
179             ProjectVersionMetadata model = metadataResolver.resolveProjectVersion( repoId, groupId, artifactId,
180                                                                                    version );
181             if ( model != null )
182             {
183                 List<Dependency> dependencies = new ArrayList<Dependency>();
184                 List<org.apache.archiva.metadata.model.Dependency> modelDeps = model.getDependencies();
185                 for ( org.apache.archiva.metadata.model.Dependency dep : modelDeps )
186                 {
187                     Dependency dependency = new Dependency( dep.getGroupId(), dep.getArtifactId(), dep.getVersion(),
188                                                             dep.getClassifier(), dep.getType(), dep.getScope() );
189                     dependencies.add( dependency );
190                 }
191                 return dependencies;
192             }
193         }
194         throw new Exception( "Artifact does not exist." );
195     }
196
197     public List<Artifact> getDependencyTree( String groupId, String artifactId, String version )
198         throws Exception
199     {
200 //        List<Artifact> a = new ArrayList<Artifact>();
201
202         throw new UnsupportedOperationException( "getDependencyTree not yet implemented" );
203 //        return a;
204     }
205
206     public List<Artifact> getDependees( String groupId, String artifactId, String version )
207         throws Exception
208     {
209         List<Artifact> artifacts = new ArrayList<Artifact>();
210         List<String> observableRepos = xmlRpcUserRepositories.getObservableRepositories();
211
212         for ( String repoId : observableRepos )
213         {
214             Collection<ProjectVersionReference> refs = metadataResolver.resolveProjectReferences( repoId, groupId,
215                                                                                                   artifactId, version );
216             for ( ProjectVersionReference ref : refs )
217             {
218                 artifacts.add( new Artifact( repoId, ref.getNamespace(), ref.getProjectId(), ref.getProjectVersion(),
219                                              "" ) );
220             }
221         }
222
223         return artifacts;
224     }
225 }