1 package org.apache.maven.repository.manager.web.action;
4 * Copyright 2005-2006 The Apache Software Foundation.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 import com.opensymphony.xwork.ActionSupport;
20 import org.apache.lucene.analysis.standard.StandardAnalyzer;
21 import org.apache.lucene.index.Term;
22 import org.apache.lucene.queryParser.MultiFieldQueryParser;
23 import org.apache.lucene.queryParser.ParseException;
24 import org.apache.lucene.search.TermQuery;
25 import org.apache.maven.repository.configuration.Configuration;
26 import org.apache.maven.repository.configuration.ConfigurationStore;
27 import org.apache.maven.repository.configuration.ConfigurationStoreException;
28 import org.apache.maven.repository.configuration.ConfiguredRepositoryFactory;
29 import org.apache.maven.repository.indexing.RepositoryArtifactIndex;
30 import org.apache.maven.repository.indexing.RepositoryArtifactIndexFactory;
31 import org.apache.maven.repository.indexing.RepositoryIndexException;
32 import org.apache.maven.repository.indexing.RepositoryIndexSearchException;
33 import org.apache.maven.repository.indexing.lucene.LuceneQuery;
34 import org.apache.maven.repository.indexing.record.StandardIndexRecordFields;
37 import java.net.MalformedURLException;
38 import java.util.List;
41 * Search all indexed fields by the given criteria.
43 * @plexus.component role="com.opensymphony.xwork.Action" role-hint="searchAction"
45 public class SearchAction
54 * The MD5 to search by.
61 private List searchResults;
66 private RepositoryArtifactIndexFactory factory;
71 private ConfiguredRepositoryFactory repositoryFactory;
76 private ConfigurationStore configurationStore;
78 private static final String NO_RESULTS = "noResults";
80 private static final String RESULTS = "results";
82 private static final String ARTIFACT = "artifact";
84 public String quickSearch()
85 throws MalformedURLException, RepositoryIndexException, RepositoryIndexSearchException,
86 ConfigurationStoreException, ParseException
88 // TODO: give action message if indexing is in progress
90 assert q != null && q.length() != 0;
92 RepositoryArtifactIndex index = getIndex();
94 if ( !index.exists() )
96 addActionError( "The repository is not yet indexed. Please wait, and then try again." );
100 // TODO! this is correct, but ugly
101 MultiFieldQueryParser parser = new MultiFieldQueryParser( new String[]{StandardIndexRecordFields.GROUPID,
102 StandardIndexRecordFields.ARTIFACTID, StandardIndexRecordFields.BASE_VERSION,
103 StandardIndexRecordFields.CLASSIFIER, StandardIndexRecordFields.CLASSES, StandardIndexRecordFields.FILES,
104 StandardIndexRecordFields.TYPE, StandardIndexRecordFields.PROJECT_NAME,
105 StandardIndexRecordFields.PROJECT_DESCRIPTION}, new StandardAnalyzer() );
106 searchResults = index.search( new LuceneQuery( parser.parse( q ) ) );
111 public String findArtifact()
114 // TODO: give action message if indexing is in progress
116 assert md5 != null && md5.length() != 0;
118 RepositoryArtifactIndex index = getIndex();
120 if ( !index.exists() )
122 addActionError( "The repository is not yet indexed. Please wait, and then try again." );
126 searchResults = index.search(
127 new LuceneQuery( new TermQuery( new Term( StandardIndexRecordFields.MD5, md5.toLowerCase() ) ) ) );
129 if ( searchResults.isEmpty() )
133 if ( searchResults.size() == 1 )
143 private RepositoryArtifactIndex getIndex()
144 throws ConfigurationStoreException, RepositoryIndexException
146 Configuration configuration = configurationStore.getConfigurationFromStore();
147 File indexPath = new File( configuration.getIndexPath() );
149 return factory.createStandardIndex( indexPath );
152 public String doInput()
162 public void setQ( String q )
167 public String getMd5()
172 public void setMd5( String md5 )
177 public List getSearchResults()
179 return searchResults;