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.ArrayList;
24 import java.util.LinkedHashMap;
25 import java.util.List;
28 import com.opensymphony.xwork2.Preparable;
29 import org.apache.archiva.indexer.search.RepositorySearch;
30 import org.apache.archiva.indexer.search.RepositorySearchException;
31 import org.apache.archiva.indexer.search.SearchFields;
32 import org.apache.archiva.indexer.search.SearchResultHit;
33 import org.apache.archiva.indexer.search.SearchResultLimits;
34 import org.apache.archiva.indexer.search.SearchResults;
35 import org.apache.archiva.metadata.model.ArtifactMetadata;
36 import org.apache.archiva.metadata.repository.MetadataRepository;
37 import org.apache.commons.collections.CollectionUtils;
38 import org.apache.commons.lang.StringUtils;
39 import org.apache.maven.archiva.common.utils.VersionUtil;
40 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
41 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
42 import org.apache.struts2.ServletActionContext;
43 import org.springframework.web.context.WebApplicationContext;
44 import org.springframework.web.context.support.WebApplicationContextUtils;
47 * Search all indexed fields by the given criteria.
49 * @plexus.component role="com.opensymphony.xwork2.Action" role-hint="searchAction" instantiation-strategy="per-lookup"
51 public class SearchAction
52 extends AbstractRepositoryBasedAction
59 private ArchivaConfiguration archivaConfiguration;
66 private SearchResults results;
68 private static final String RESULTS = "results";
70 private static final String ARTIFACT = "artifact";
72 private List<ArtifactMetadata> databaseResults;
74 private int currentPage = 0;
76 private int totalPages;
78 private boolean searchResultsOnly;
80 private String completeQueryString;
82 private static final String COMPLETE_QUERY_STRING_SEPARATOR = ";";
84 private List<String> managedRepositoryList;
86 private String groupId;
88 private String artifactId;
90 private String version;
92 private String className;
94 private int rowCount = 30;
96 private String repositoryId;
98 private boolean fromFilterSearch;
100 private boolean filterSearch = false;
102 private boolean fromResultsPage;
104 private RepositorySearch nexusSearch;
106 private Map<String, String> searchFields;
108 private String infoMessage;
111 * @plexus.requirement
113 private MetadataRepository metadataRepository;
115 public boolean isFromResultsPage()
117 return fromResultsPage;
120 public void setFromResultsPage( boolean fromResultsPage )
122 this.fromResultsPage = fromResultsPage;
125 public boolean isFromFilterSearch()
127 return fromFilterSearch;
130 public void setFromFilterSearch( boolean fromFilterSearch )
132 this.fromFilterSearch = fromFilterSearch;
135 public void prepare()
137 managedRepositoryList = new ArrayList<String>();
138 managedRepositoryList = getObservableRepos();
140 if ( managedRepositoryList.size() > 0 )
142 managedRepositoryList.add( "all" );
145 searchFields = new LinkedHashMap<String, String>();
146 searchFields.put( "groupId", "Group ID" );
147 searchFields.put( "artifactId", "Artifact ID" );
148 searchFields.put( "version", "Version" );
149 searchFields.put( "className", "Class/Package Name" );
150 searchFields.put( "rowCount", "Row Count" );
152 super.clearErrorsAndMessages();
156 private void clearSearchFields()
167 // advanced search MRM-90 -- filtered search
168 public String filteredSearch()
169 throws MalformedURLException
171 if ( ( groupId == null || "".equals( groupId ) ) &&
172 ( artifactId == null || "".equals( artifactId ) ) && ( className == null || "".equals( className ) ) &&
173 ( version == null || "".equals( version ) ) )
175 addActionError( "Advanced Search - At least one search criteria must be provided." );
179 fromFilterSearch = true;
181 if ( CollectionUtils.isEmpty( managedRepositoryList ) )
183 return GlobalResults.ACCESS_TO_NO_REPOS;
186 SearchResultLimits limits = new SearchResultLimits( currentPage );
187 limits.setPageSize( rowCount );
188 List<String> selectedRepos = new ArrayList<String>();
190 if ( repositoryId == null || StringUtils.isBlank( repositoryId ) ||
191 "all".equals( StringUtils.stripToEmpty( repositoryId ) ) )
193 selectedRepos = getObservableRepos();
197 selectedRepos.add( repositoryId );
200 if ( CollectionUtils.isEmpty( selectedRepos ) )
202 return GlobalResults.ACCESS_TO_NO_REPOS;
205 SearchFields searchFields =
206 new SearchFields( groupId, artifactId, version, null, className, selectedRepos );
208 // TODO: add packaging in the list of fields for advanced search (UI)?
211 results = getNexusSearch().search( getPrincipal(), searchFields, limits );
213 catch ( RepositorySearchException e )
215 addActionError( e.getMessage() );
219 if ( results.isEmpty() )
221 addActionError( "No results found" );
225 totalPages = results.getTotalHits() / limits.getPageSize();
227 if ( ( results.getTotalHits() % limits.getPageSize() ) != 0 )
229 totalPages = totalPages + 1;
232 for (SearchResultHit hit : results.getHits())
234 final String version = hit.getVersion();
237 hit.setVersion(VersionUtil.getBaseVersion(version));
244 @SuppressWarnings("unchecked")
245 public String quickSearch()
246 throws MalformedURLException
248 /* TODO: give action message if indexing is in progress.
249 * This should be based off a count of 'unprocessed' artifacts.
250 * This (yet to be written) routine could tell the user that X (unprocessed) artifacts are not yet
251 * present in the full text search.
254 assert q != null && q.length() != 0;
256 fromFilterSearch = false;
258 SearchResultLimits limits = new SearchResultLimits( currentPage );
260 List<String> selectedRepos = getObservableRepos();
261 if ( CollectionUtils.isEmpty( selectedRepos ) )
263 return GlobalResults.ACCESS_TO_NO_REPOS;
268 if( searchResultsOnly && !completeQueryString.equals( "" ) )
270 results = getNexusSearch().search( getPrincipal(), selectedRepos, q, limits, parseCompleteQueryString() );
274 completeQueryString = "";
275 results = getNexusSearch().search( getPrincipal(), selectedRepos, q, limits, null );
278 catch ( RepositorySearchException e )
280 addActionError( e.getMessage() );
284 if ( results.isEmpty() )
286 addActionError( "No results found" );
290 totalPages = results.getTotalHits() / limits.getPageSize();
292 if( (results.getTotalHits() % limits.getPageSize()) != 0 )
294 totalPages = totalPages + 1;
297 if( !isEqualToPreviousSearchTerm( q ) )
299 buildCompleteQueryString( q );
305 public String findArtifact()
308 // TODO: give action message if indexing is in progress
310 if ( StringUtils.isBlank( q ) )
312 addActionError( "Unable to search for a blank checksum" );
316 databaseResults = new ArrayList<ArtifactMetadata>();
317 for ( String repoId : getObservableRepos() )
319 databaseResults.addAll( metadataRepository.getArtifactsByChecksum( repoId, q ) );
322 if ( databaseResults.isEmpty() )
324 addActionError( "No results found" );
328 if ( databaseResults.size() == 1 )
330 // 1 hit? return it's information directly!
337 public String doInput()
342 private void buildCompleteQueryString( String searchTerm )
344 if ( searchTerm.indexOf( COMPLETE_QUERY_STRING_SEPARATOR ) != -1 )
346 searchTerm = StringUtils.remove( searchTerm, COMPLETE_QUERY_STRING_SEPARATOR );
349 if ( completeQueryString == null || "".equals( completeQueryString ) )
351 completeQueryString = searchTerm;
355 completeQueryString = completeQueryString + COMPLETE_QUERY_STRING_SEPARATOR + searchTerm;
359 private List<String> parseCompleteQueryString()
361 List<String> parsedCompleteQueryString = new ArrayList<String>();
362 String[] parsed = StringUtils.split( completeQueryString, COMPLETE_QUERY_STRING_SEPARATOR );
363 CollectionUtils.addAll( parsedCompleteQueryString, parsed );
365 return parsedCompleteQueryString;
368 private boolean isEqualToPreviousSearchTerm( String searchTerm )
370 if ( !"".equals( completeQueryString ) )
372 String[] parsed = StringUtils.split( completeQueryString, COMPLETE_QUERY_STRING_SEPARATOR );
373 if ( StringUtils.equalsIgnoreCase( searchTerm, parsed[parsed.length - 1] ) )
387 public void setQ( String q )
392 public SearchResults getResults()
397 public List<ArtifactMetadata> getDatabaseResults()
399 return databaseResults;
402 public void setCurrentPage( int page )
404 this.currentPage = page;
407 public int getCurrentPage()
412 public int getTotalPages()
417 public void setTotalPages( int totalPages )
419 this.totalPages = totalPages;
422 public boolean isSearchResultsOnly()
424 return searchResultsOnly;
427 public void setSearchResultsOnly( boolean searchResultsOnly )
429 this.searchResultsOnly = searchResultsOnly;
432 public String getCompleteQueryString()
434 return completeQueryString;
437 public void setCompleteQueryString( String completeQueryString )
439 this.completeQueryString = completeQueryString;
442 public ArchivaConfiguration getArchivaConfiguration()
444 return archivaConfiguration;
447 public void setArchivaConfiguration( ArchivaConfiguration archivaConfiguration )
449 this.archivaConfiguration = archivaConfiguration;
452 public Map<String, ManagedRepositoryConfiguration> getManagedRepositories()
454 return getArchivaConfiguration().getConfiguration().getManagedRepositoriesAsMap();
457 public void setManagedRepositories( Map<String, ManagedRepositoryConfiguration> managedRepositories )
461 public String getGroupId()
466 public void setGroupId( String groupId )
468 this.groupId = groupId;
471 public String getArtifactId()
476 public void setArtifactId( String artifactId )
478 this.artifactId = artifactId;
481 public String getVersion()
486 public void setVersion( String version )
488 this.version = version;
491 public int getRowCount()
496 public void setRowCount( int rowCount )
498 this.rowCount = rowCount;
501 public boolean isFilterSearch()
506 public void setFilterSearch( boolean filterSearch )
508 this.filterSearch = filterSearch;
511 public String getRepositoryId()
516 public void setRepositoryId( String repositoryId )
518 this.repositoryId = repositoryId;
521 public List<String> getManagedRepositoryList()
523 return managedRepositoryList;
526 public void setManagedRepositoryList( List<String> managedRepositoryList )
528 this.managedRepositoryList = managedRepositoryList;
531 public String getClassName()
536 public void setClassName( String className )
538 this.className = className;
541 public RepositorySearch getNexusSearch()
543 // no need to do this when wiring is already in spring
544 if( nexusSearch == null )
546 WebApplicationContext wac =
547 WebApplicationContextUtils.getRequiredWebApplicationContext( ServletActionContext.getServletContext() );
548 nexusSearch = ( RepositorySearch ) wac.getBean( "nexusSearch" );
553 public void setNexusSearch( RepositorySearch nexusSearch )
555 this.nexusSearch = nexusSearch;
558 public Map<String, String> getSearchFields()
563 public void setSearchFields( Map<String, String> searchFields )
565 this.searchFields = searchFields;
568 public String getInfoMessage()
573 public void setInfoMessage( String infoMessage )
575 this.infoMessage = infoMessage;
578 public void setMetadataRepository( MetadataRepository metadataRepository )
580 this.metadataRepository = metadataRepository;