1 package org.apache.maven.archiva.web.action;
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 java.net.MalformedURLException;
23 import java.util.Collections;
24 import java.util.List;
26 import com.opensymphony.xwork.ActionContext;
27 import org.apache.commons.collections.CollectionUtils;
28 import org.apache.commons.lang.StringUtils;
29 import org.apache.maven.archiva.database.ArchivaDAO;
30 import org.apache.maven.archiva.database.Constraint;
31 import org.apache.maven.archiva.database.constraints.ArtifactsByChecksumConstraint;
32 import org.apache.maven.archiva.indexer.RepositoryIndexException;
33 import org.apache.maven.archiva.indexer.RepositoryIndexSearchException;
34 import org.apache.maven.archiva.indexer.search.CrossRepositorySearch;
35 import org.apache.maven.archiva.indexer.search.SearchResultLimits;
36 import org.apache.maven.archiva.indexer.search.SearchResults;
37 import org.apache.maven.archiva.security.AccessDeniedException;
38 import org.apache.maven.archiva.security.ArchivaSecurityException;
39 import org.apache.maven.archiva.security.ArchivaXworkUser;
40 import org.apache.maven.archiva.security.PrincipalNotFoundException;
41 import org.apache.maven.archiva.security.UserRepositories;
42 import org.codehaus.plexus.xwork.action.PlexusActionSupport;
45 * Search all indexed fields by the given criteria.
47 * @plexus.component role="com.opensymphony.xwork.Action" role-hint="searchAction"
49 public class SearchAction
50 extends PlexusActionSupport
58 * @plexus.requirement role-hint="jdo"
60 private ArchivaDAO dao;
65 private SearchResults results;
68 * @plexus.requirement role-hint="default"
70 private CrossRepositorySearch crossRepoSearch;
75 private UserRepositories userRepositories;
77 private static final String RESULTS = "results";
79 private static final String ARTIFACT = "artifact";
81 private List databaseResults;
83 private int currentPage = 0;
85 private int totalPages;
87 public String quickSearch()
88 throws MalformedURLException, RepositoryIndexException, RepositoryIndexSearchException
90 /* TODO: give action message if indexing is in progress.
91 * This should be based off a count of 'unprocessed' artifacts.
92 * This (yet to be written) routine could tell the user that X (unprocessed) artifacts are not yet
93 * present in the full text search.
96 assert q != null && q.length() != 0;
98 SearchResultLimits limits = new SearchResultLimits( currentPage );
100 List<String> selectedRepos = getObservableRepos();
101 if ( CollectionUtils.isEmpty( selectedRepos ) )
103 return GlobalResults.ACCESS_TO_NO_REPOS;
106 results = crossRepoSearch.searchForTerm( getPrincipal(), selectedRepos, q, limits );
108 if ( results.isEmpty() )
110 addActionError( "No results found" );
114 totalPages = results.getTotalHits() / limits.getPageSize();
116 if( (results.getTotalHits() % limits.getPageSize()) != 0 )
118 totalPages = totalPages + 1;
120 // TODO: filter / combine the artifacts by version? (is that even possible with non-artifact hits?)
122 /* I don't think that we should, as I expect us to utilize the 'score' system in lucene in
123 * the future to return relevant links better.
124 * I expect the lucene scoring system to take multiple hits on different areas of a single document
125 * to result in a higher score.
132 public String findArtifact()
135 // TODO: give action message if indexing is in progress
137 if ( StringUtils.isBlank( q ) )
139 addActionError( "Unable to search for a blank checksum" );
143 Constraint constraint = new ArtifactsByChecksumConstraint( q );
144 databaseResults = dao.getArtifactDAO().queryArtifacts( constraint );
146 if ( databaseResults.isEmpty() )
148 addActionError( "No results found" );
152 if ( databaseResults.size() == 1 )
154 // 1 hit? return it's information directly!
162 public String doInput()
167 private String getPrincipal()
169 return ArchivaXworkUser.getActivePrincipal( ActionContext.getContext().getSession() );
172 private List<String> getObservableRepos()
176 return userRepositories.getObservableRepositoryIds( getPrincipal() );
178 catch ( PrincipalNotFoundException e )
180 getLogger().warn( e.getMessage(), e );
182 catch ( AccessDeniedException e )
184 getLogger().warn( e.getMessage(), e );
185 // TODO: pass this onto the screen.
187 catch ( ArchivaSecurityException e )
189 getLogger().warn( e.getMessage(), e );
191 return Collections.emptyList();
199 public void setQ( String q )
204 public SearchResults getResults()
209 public List getDatabaseResults()
211 return databaseResults;
214 public void setCurrentPage( int page )
216 this.currentPage = page;
219 public int getCurrentPage()
224 public int getTotalPages()
229 public void setTotalPages( int totalPages )
231 this.totalPages = totalPages;