1 package org.apache.maven.archiva.indexer.search;
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 org.apache.commons.lang.StringUtils;
23 import org.apache.maven.archiva.indexer.bytecode.BytecodeRecord;
24 import org.apache.maven.archiva.indexer.filecontent.FileContentRecord;
25 import org.apache.maven.archiva.indexer.hashcodes.HashcodesRecord;
26 import org.apache.maven.archiva.indexer.lucene.LuceneRepositoryContentRecord;
27 import org.apache.maven.archiva.model.ArchivaArtifact;
29 import java.util.ArrayList;
30 import java.util.HashMap;
31 import java.util.List;
39 public class SearchResults
41 private List repositories = new ArrayList();
43 private Map hits = new HashMap();
45 private int totalHits;
47 private SearchResultLimits limits;
49 public SearchResults()
54 public void addHit( LuceneRepositoryContentRecord record )
56 if ( record instanceof FileContentRecord )
58 FileContentRecord filecontent = (FileContentRecord) record;
59 addFileContentHit( filecontent );
61 else if ( record instanceof HashcodesRecord )
63 HashcodesRecord hashcodes = (HashcodesRecord) record;
64 addHashcodeHit( hashcodes );
66 else if ( record instanceof BytecodeRecord )
68 BytecodeRecord bytecode = (BytecodeRecord) record;
69 addBytecodeHit( bytecode );
73 private void addBytecodeHit( BytecodeRecord bytecode )
75 String key = toKey( bytecode.getArtifact() );
77 SearchResultHit hit = (SearchResultHit) this.hits.get( key );
81 hit = new SearchResultHit();
84 hit.setRepositoryId( bytecode.getRepositoryId() );
85 hit.addArtifact( bytecode.getArtifact() );
86 hit.setContext( null ); // TODO: provide context on why this is a valuable hit.
88 this.hits.put( key, hit );
91 private String toKey( ArchivaArtifact artifact )
93 StringBuffer key = new StringBuffer();
95 key.append( StringUtils.defaultString( artifact.getModel().getRepositoryId() ) ).append( ":" );
96 key.append( StringUtils.defaultString( artifact.getGroupId() ) ).append( ":" );
97 key.append( StringUtils.defaultString( artifact.getArtifactId() ) ).append( ":" );
98 key.append( StringUtils.defaultString( artifact.getVersion() ) );
100 return key.toString();
103 private void addHashcodeHit( HashcodesRecord hashcodes )
105 String key = toKey( hashcodes.getArtifact() );
107 SearchResultHit hit = (SearchResultHit) this.hits.get( key );
111 hit = new SearchResultHit();
114 hit.addArtifact( hashcodes.getArtifact() );
115 hit.setContext( null ); // TODO: provide context on why this is a valuable hit.
117 this.hits.put( key, hit );
120 public void addFileContentHit( FileContentRecord filecontent )
122 String key = filecontent.getPrimaryKey();
124 SearchResultHit hit = (SearchResultHit) this.hits.get( key );
128 // Only need to worry about this hit if it is truely new.
129 hit = new SearchResultHit();
131 hit.setRepositoryId( filecontent.getRepositoryId() );
132 hit.setUrl( filecontent.getRepositoryId() + "/" + filecontent.getFilename() );
133 hit.setContext( null ); // TODO: handle context + highlight later.
135 // Test for possible artifact reference ...
136 if( filecontent.getArtifact() != null )
138 hit.addArtifact( filecontent.getArtifact() );
141 this.hits.put( key, hit );
146 * Get the list of {@link SearchResultHit} objects.
148 * @return the list of {@link SearchResultHit} objects.
150 public List getHits()
152 return new ArrayList( hits.values() );
155 public List getRepositories()
160 public boolean isEmpty()
162 return hits.isEmpty();
165 public void setRepositories( List repositories )
167 this.repositories = repositories;
170 public SearchResultLimits getLimits()
175 public void setLimits( SearchResultLimits limits )
177 this.limits = limits;
180 public int getTotalHits()
185 public void setTotalHits( int totalHits )
187 this.totalHits = totalHits;