1 package org.apache.maven.indexer.examples.services.impl;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.lucene.queryparser.classic.ParseException;
23 import org.apache.maven.index.ArtifactInfo;
24 import org.apache.maven.indexer.examples.indexing.RepositoryIndexManager;
25 import org.apache.maven.indexer.examples.indexing.RepositoryIndexer;
26 import org.apache.maven.indexer.examples.indexing.SearchRequest;
27 import org.apache.maven.indexer.examples.indexing.SearchResults;
28 import org.apache.maven.indexer.examples.services.ArtifactIndexingService;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import org.springframework.beans.factory.annotation.Autowired;
32 import org.springframework.stereotype.Component;
33
34 import java.io.File;
35 import java.io.IOException;
36 import java.util.Arrays;
37 import java.util.Collection;
38 import java.util.LinkedHashMap;
39 import java.util.Map;
40 import java.util.Set;
41
42
43
44
45 @Component
46 public class ArtifactIndexingServiceImpl
47 implements ArtifactIndexingService
48 {
49
50 private static final Logger LOGGER = LoggerFactory.getLogger( ArtifactIndexingServiceImpl.class );
51
52 @Autowired
53 private RepositoryIndexManager repositoryIndexManager;
54
55
56 @Override
57 public void addToIndex( String repositoryId, File artifactFile, String groupId, String artifactId, String version,
58 String extension, String classifier )
59 throws IOException
60 {
61 final RepositoryIndexer indexer = repositoryIndexManager.getRepositoryIndex( repositoryId );
62
63 indexer.addArtifactToIndex( repositoryId, artifactFile, groupId, artifactId, version, extension, classifier );
64 }
65
66 @Override
67 public void deleteFromIndex( String repositoryId, String groupId, String artifactId, String version,
68 String extension, String classifier )
69 throws IOException
70 {
71 final RepositoryIndexer indexer = repositoryIndexManager.getRepositoryIndex( repositoryId );
72 if ( indexer != null )
73 {
74 indexer.delete( Arrays.asList(
75 new ArtifactInfo( repositoryId, groupId, artifactId, version, classifier, extension ) ) );
76 }
77 }
78
79 @Override
80 public SearchResults search( SearchRequest searchRequest )
81 throws IOException, ParseException
82 {
83 SearchResults searchResults = new SearchResults();
84
85 final String repositoryId = searchRequest.getRepository();
86
87 if ( repositoryId != null && !repositoryId.isEmpty() )
88 {
89 LOGGER.debug( "Repository: {}", repositoryId );
90
91 final Map<String, Collection<ArtifactInfo>> resultsMap =
92 getResultsMap( repositoryId, searchRequest.getQuery() );
93
94 if ( !resultsMap.isEmpty() )
95 {
96 searchResults.setResults( resultsMap );
97 }
98
99 if ( LOGGER.isDebugEnabled() )
100 {
101 int results = resultsMap.entrySet().iterator().next().getValue().size();
102
103 LOGGER.debug( "Results: {}", results );
104 }
105 }
106 else
107 {
108 Map<String, Collection<ArtifactInfo>> resultsMap = new LinkedHashMap<>();
109 for ( String repoId : repositoryIndexManager.getIndexes().keySet() )
110 {
111 LOGGER.debug( "Repository: {}", repoId );
112
113 final RepositoryIndexer repositoryIndex = repositoryIndexManager.getRepositoryIndex( repoId );
114 if ( repositoryIndex != null )
115 {
116 final Set<ArtifactInfo> artifactInfoResults =
117 repositoryIndexManager.getRepositoryIndex( repoId ).search( searchRequest.getQuery() );
118
119 if ( !artifactInfoResults.isEmpty() )
120 {
121 resultsMap.put( repoId, artifactInfoResults );
122 }
123
124 LOGGER.debug( "Results: {}", artifactInfoResults.size() );
125 }
126 }
127
128 searchResults.setResults( resultsMap );
129 }
130
131 return searchResults;
132 }
133
134 @Override
135 public boolean contains( SearchRequest searchRequest )
136 throws IOException, ParseException
137 {
138 return !getResultsMap( searchRequest.getRepository(), searchRequest.getQuery() ).isEmpty();
139 }
140
141 public Map<String, Collection<ArtifactInfo>> getResultsMap( String repositoryId, String query )
142 throws IOException, ParseException
143 {
144 Map<String, Collection<ArtifactInfo>> resultsMap = new LinkedHashMap<>();
145 final Set<ArtifactInfo> artifactInfoResults =
146 repositoryIndexManager.getRepositoryIndex( repositoryId ).search( query );
147
148 if ( !artifactInfoResults.isEmpty() )
149 {
150 resultsMap.put( repositoryId, artifactInfoResults );
151 }
152
153 return resultsMap;
154 }
155
156 public RepositoryIndexManager getRepositoryIndexManager()
157 {
158 return repositoryIndexManager;
159 }
160
161 public void setRepositoryIndexManager( RepositoryIndexManager repositoryIndexManager )
162 {
163 this.repositoryIndexManager = repositoryIndexManager;
164 }
165
166 }