1 | |
package org.apache.archiva.web.xmlrpc.services; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import java.util.ArrayList; |
23 | |
import java.util.Date; |
24 | |
import java.util.List; |
25 | |
|
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.common.utils.VersionUtil; |
35 | |
import org.apache.maven.archiva.database.ArchivaDAO; |
36 | |
import org.apache.maven.archiva.database.ArchivaDatabaseException; |
37 | |
import org.apache.maven.archiva.database.ArtifactDAO; |
38 | |
import org.apache.maven.archiva.database.ObjectNotFoundException; |
39 | |
import org.apache.maven.archiva.database.browsing.BrowsingResults; |
40 | |
import org.apache.maven.archiva.database.browsing.RepositoryBrowsing; |
41 | |
import org.apache.maven.archiva.database.constraints.ArtifactsByChecksumConstraint; |
42 | |
import org.apache.maven.archiva.database.constraints.UniqueVersionConstraint; |
43 | |
import org.apache.maven.archiva.model.ArchivaArtifact; |
44 | |
import org.apache.maven.archiva.model.ArchivaProjectModel; |
45 | |
import org.slf4j.Logger; |
46 | |
import org.slf4j.LoggerFactory; |
47 | |
|
48 | |
public class SearchServiceImpl |
49 | |
implements SearchService |
50 | |
{ |
51 | 0 | private Logger log = LoggerFactory.getLogger( SearchServiceImpl.class ); |
52 | |
|
53 | |
private RepositorySearch search; |
54 | |
|
55 | |
private XmlRpcUserRepositories xmlRpcUserRepositories; |
56 | |
|
57 | |
private ArchivaDAO archivaDAO; |
58 | |
|
59 | |
private RepositoryBrowsing repoBrowsing; |
60 | |
|
61 | |
public SearchServiceImpl( XmlRpcUserRepositories xmlRpcUserRepositories, ArchivaDAO archivaDAO, |
62 | |
RepositoryBrowsing repoBrowsing, RepositorySearch search ) |
63 | 0 | { |
64 | 0 | this.xmlRpcUserRepositories = xmlRpcUserRepositories; |
65 | 0 | this.archivaDAO = archivaDAO; |
66 | 0 | this.repoBrowsing = repoBrowsing; |
67 | 0 | this.search = search; |
68 | 0 | } |
69 | |
|
70 | |
@SuppressWarnings( "unchecked" ) |
71 | |
public List<Artifact> quickSearch( String queryString ) |
72 | |
throws Exception |
73 | |
{ |
74 | 0 | List<Artifact> artifacts = new ArrayList<Artifact>(); |
75 | 0 | List<String> observableRepos = xmlRpcUserRepositories.getObservableRepositories(); |
76 | 0 | SearchResultLimits limits = new SearchResultLimits( SearchResultLimits.ALL_PAGES ); |
77 | 0 | SearchResults results = null; |
78 | |
|
79 | 0 | results = search.search( "", observableRepos, queryString, limits, null ); |
80 | |
|
81 | 0 | for ( SearchResultHit resultHit : results.getHits() ) |
82 | |
{ |
83 | |
|
84 | 0 | final List<String> versions = |
85 | |
(List<String>) archivaDAO.query( new UniqueVersionConstraint( observableRepos, resultHit.getGroupId(), |
86 | |
resultHit.getArtifactId() ) ); |
87 | 0 | if ( versions != null && !versions.isEmpty() ) |
88 | |
{ |
89 | 0 | resultHit.setVersion( null ); |
90 | 0 | resultHit.setVersions( filterTimestampedSnapshots( versions ) ); |
91 | |
} |
92 | |
|
93 | 0 | List<String> resultHitVersions = resultHit.getVersions(); |
94 | 0 | if( resultHitVersions != null ) |
95 | |
{ |
96 | 0 | for( String version : resultHitVersions ) |
97 | |
{ |
98 | |
try |
99 | |
{ |
100 | 0 | ArchivaProjectModel model = repoBrowsing.selectVersion( "", observableRepos, resultHit.getGroupId(), resultHit.getArtifactId(), version ); |
101 | |
|
102 | 0 | String repoId = repoBrowsing.getRepositoryId( "", observableRepos, resultHit.getGroupId(), resultHit.getArtifactId(), version ); |
103 | |
|
104 | 0 | Artifact artifact = null; |
105 | 0 | if( model == null ) |
106 | |
{ |
107 | 0 | artifact = new Artifact( repoId, resultHit.getGroupId(), resultHit.getArtifactId(), version, "jar" ); |
108 | |
} |
109 | |
else |
110 | |
{ |
111 | 0 | artifact = new Artifact( repoId, model.getGroupId(), model.getArtifactId(), version, model.getPackaging() ); |
112 | |
} |
113 | 0 | artifacts.add( artifact ); |
114 | |
} |
115 | 0 | catch( ObjectNotFoundException e ) |
116 | |
{ |
117 | 0 | log.debug( "Unable to find pom artifact : " + e.getMessage() ); |
118 | |
} |
119 | 0 | catch( ArchivaDatabaseException e ) |
120 | |
{ |
121 | 0 | log.debug( "Error occurred while getting pom artifact from database : " + e.getMessage() ); |
122 | 0 | } |
123 | |
} |
124 | |
} |
125 | 0 | } |
126 | |
|
127 | 0 | return artifacts; |
128 | |
} |
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
private static List<String> filterTimestampedSnapshots(List<String> versions) |
134 | |
{ |
135 | 0 | final List<String> filtered = new ArrayList<String>(); |
136 | 0 | for (final String version : versions) |
137 | |
{ |
138 | 0 | final String baseVersion = VersionUtil.getBaseVersion(version); |
139 | 0 | if (!filtered.contains(baseVersion)) |
140 | |
{ |
141 | 0 | filtered.add(baseVersion); |
142 | |
} |
143 | 0 | } |
144 | 0 | return filtered; |
145 | |
} |
146 | |
|
147 | |
public List<Artifact> getArtifactByChecksum( String checksum ) |
148 | |
throws Exception |
149 | |
{ |
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | 0 | List<Artifact> results = new ArrayList<Artifact>(); |
156 | 0 | ArtifactDAO artifactDAO = archivaDAO.getArtifactDAO(); |
157 | |
|
158 | 0 | ArtifactsByChecksumConstraint constraint = new ArtifactsByChecksumConstraint( checksum ); |
159 | 0 | List<ArchivaArtifact> artifacts = artifactDAO.queryArtifacts( constraint ); |
160 | |
|
161 | 0 | for( ArchivaArtifact archivaArtifact : artifacts ) |
162 | |
{ |
163 | 0 | Artifact artifact = new Artifact( archivaArtifact.getModel().getRepositoryId(), archivaArtifact.getModel().getGroupId(), |
164 | |
archivaArtifact.getModel().getArtifactId(), archivaArtifact.getModel().getVersion(), archivaArtifact.getType() ); |
165 | |
|
166 | 0 | results.add( artifact ); |
167 | 0 | } |
168 | |
|
169 | 0 | return results; |
170 | |
} |
171 | |
|
172 | |
public List<Artifact> getArtifactVersions( String groupId, String artifactId ) |
173 | |
throws Exception |
174 | |
{ |
175 | 0 | final List<Artifact> artifacts = new ArrayList<Artifact>(); |
176 | 0 | final List<String> observableRepos = xmlRpcUserRepositories.getObservableRepositories(); |
177 | |
|
178 | 0 | final BrowsingResults results = repoBrowsing.selectArtifactId( "", observableRepos, groupId, artifactId ); |
179 | |
|
180 | 0 | for( final String version : results.getVersions() ) |
181 | |
{ |
182 | 0 | final Artifact artifact = new Artifact( "", groupId, artifactId, version, "pom" ); |
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | 0 | artifacts.add( artifact ); |
188 | 0 | } |
189 | |
|
190 | |
|
191 | |
|
192 | 0 | return artifacts; |
193 | |
} |
194 | |
|
195 | |
public List<Artifact> getArtifactVersionsByDate( String groupId, String artifactId, String version, Date since ) |
196 | |
throws Exception |
197 | |
{ |
198 | 0 | List<Artifact> artifacts = new ArrayList<Artifact>(); |
199 | |
|
200 | |
|
201 | |
|
202 | |
|
203 | 0 | throw new UnsupportedOperationException( "getArtifactVersionsByDate not yet implemented" ); |
204 | |
|
205 | |
|
206 | |
} |
207 | |
|
208 | |
public List<Dependency> getDependencies( String groupId, String artifactId, String version ) |
209 | |
throws Exception |
210 | |
{ |
211 | 0 | List<Dependency> dependencies = new ArrayList<Dependency>(); |
212 | 0 | List<String> observableRepos = xmlRpcUserRepositories.getObservableRepositories(); |
213 | |
|
214 | |
try |
215 | |
{ |
216 | 0 | ArchivaProjectModel model = repoBrowsing.selectVersion( "", observableRepos, groupId, artifactId, version ); |
217 | 0 | List<org.apache.maven.archiva.model.Dependency> modelDeps = model.getDependencies(); |
218 | 0 | for( org.apache.maven.archiva.model.Dependency dep : modelDeps ) |
219 | |
{ |
220 | 0 | Dependency dependency = new Dependency( |
221 | |
dep.getGroupId(), dep.getArtifactId(), dep.getVersion(), dep.getClassifier(), dep.getType(), dep.getScope() ); |
222 | 0 | dependencies.add( dependency ); |
223 | 0 | } |
224 | |
} |
225 | 0 | catch ( ObjectNotFoundException oe ) |
226 | |
{ |
227 | 0 | throw new Exception( "Artifact does not exist." ); |
228 | 0 | } |
229 | |
|
230 | 0 | return dependencies; |
231 | |
} |
232 | |
|
233 | |
public List<Artifact> getDependencyTree( String groupId, String artifactId, String version ) |
234 | |
throws Exception |
235 | |
{ |
236 | 0 | List<Artifact> a = new ArrayList<Artifact>(); |
237 | |
|
238 | 0 | throw new UnsupportedOperationException( "getDependencyTree not yet implemented" ); |
239 | |
|
240 | |
} |
241 | |
|
242 | |
|
243 | |
public List<Artifact> getDependees( String groupId, String artifactId, String version ) |
244 | |
throws Exception |
245 | |
{ |
246 | 0 | List<Artifact> artifacts = new ArrayList<Artifact>(); |
247 | 0 | List<String> observableRepos = xmlRpcUserRepositories.getObservableRepositories(); |
248 | |
|
249 | 0 | List<ArchivaProjectModel> dependees = repoBrowsing.getUsedBy( "", observableRepos, groupId, artifactId, version ); |
250 | 0 | for( ArchivaProjectModel model : dependees ) |
251 | |
{ |
252 | 0 | Artifact artifact = |
253 | |
new Artifact( "", model.getGroupId(), model.getArtifactId(), model.getVersion(), "" ); |
254 | |
|
255 | 0 | artifacts.add( artifact ); |
256 | 0 | } |
257 | |
|
258 | 0 | return artifacts; |
259 | |
} |
260 | |
} |