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;
37 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
40 public class SearchResults
42 private List repositories = new ArrayList();
44 private Map hits = new HashMap();
46 private int totalHits;
48 private SearchResultLimits limits;
50 public SearchResults()
55 public void addHit( LuceneRepositoryContentRecord record )
57 if ( record instanceof FileContentRecord )
59 FileContentRecord filecontent = (FileContentRecord) record;
60 addFileContentHit( filecontent );
62 else if ( record instanceof HashcodesRecord )
64 HashcodesRecord hashcodes = (HashcodesRecord) record;
65 addHashcodeHit( hashcodes );
67 else if ( record instanceof BytecodeRecord )
69 BytecodeRecord bytecode = (BytecodeRecord) record;
70 addBytecodeHit( bytecode );
74 private void addBytecodeHit( BytecodeRecord bytecode )
76 String key = toKey( bytecode.getArtifact() );
78 SearchResultHit hit = (SearchResultHit) this.hits.get( key );
82 hit = new SearchResultHit();
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.getGroupId() ) ).append( ":" );
96 key.append( StringUtils.defaultString( artifact.getArtifactId() ) );
98 return key.toString();
101 private void addHashcodeHit( HashcodesRecord hashcodes )
103 String key = toKey( hashcodes.getArtifact() );
105 SearchResultHit hit = (SearchResultHit) this.hits.get( key );
109 hit = new SearchResultHit();
112 hit.addArtifact( hashcodes.getArtifact() );
113 hit.setContext( null ); // TODO: provide context on why this is a valuable hit.
115 this.hits.put( key, hit );
118 public void addFileContentHit( FileContentRecord filecontent )
120 String key = filecontent.getPrimaryKey();
122 SearchResultHit hit = (SearchResultHit) this.hits.get( key );
126 // Only need to worry about this hit if it is truely new.
127 hit = new SearchResultHit();
129 hit.setRepositoryId( filecontent.getRepositoryId() );
130 hit.setUrl( filecontent.getRepositoryId() + "/" + filecontent.getFilename() );
131 hit.setContext( null ); // TODO: handle context + highlight later.
133 // Test for possible artifact reference ...
134 if( filecontent.getArtifact() != null )
136 hit.addArtifact( filecontent.getArtifact() );
139 this.hits.put( key, hit );
144 * Get the list of {@link SearchResultHit} objects.
146 * @return the list of {@link SearchResultHit} objects.
148 public List getHits()
150 return new ArrayList( hits.values() );
153 public List getRepositories()
158 public boolean isEmpty()
160 return hits.isEmpty();
163 public void setRepositories( List repositories )
165 this.repositories = repositories;
168 public SearchResultLimits getLimits()
173 public void setLimits( SearchResultLimits limits )
175 this.limits = limits;
178 public int getTotalHits()
183 public void setTotalHits( int totalHits )
185 this.totalHits = totalHits;