]> source.dussan.org Git - archiva.git/blob
d17b19349b4aefe441a5abb07b6e83244a01d4e7
[archiva.git] /
1 package org.apache.maven.repository.manager.web.action;
2
3 /*
4  * Copyright 2005-2006 The Apache Software Foundation.
5  *
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  */
18
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;
35
36 import java.io.File;
37 import java.net.MalformedURLException;
38 import java.util.List;
39
40 /**
41  * Search all indexed fields by the given criteria.
42  *
43  * @plexus.component role="com.opensymphony.xwork.Action" role-hint="searchAction"
44  */
45 public class SearchAction
46     extends ActionSupport
47 {
48     /**
49      * Query string.
50      */
51     private String q;
52
53     /**
54      * The MD5 to search by.
55      */
56     private String md5;
57
58     /**
59      * Search results.
60      */
61     private List searchResults;
62
63     /**
64      * @plexus.requirement
65      */
66     private RepositoryArtifactIndexFactory factory;
67
68     /**
69      * @plexus.requirement
70      */
71     private ConfiguredRepositoryFactory repositoryFactory;
72
73     /**
74      * @plexus.requirement
75      */
76     private ConfigurationStore configurationStore;
77
78     private static final String NO_RESULTS = "noResults";
79
80     private static final String RESULTS = "results";
81
82     private static final String ARTIFACT = "artifact";
83
84     public String quickSearch()
85         throws MalformedURLException, RepositoryIndexException, RepositoryIndexSearchException,
86         ConfigurationStoreException, ParseException
87     {
88         // TODO: give action message if indexing is in progress
89
90         assert q != null && q.length() != 0;
91
92         RepositoryArtifactIndex index = getIndex();
93
94         if ( !index.exists() )
95         {
96             addActionError( "The repository is not yet indexed. Please wait, and then try again." );
97             return ERROR;
98         }
99
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 ) ) );
107
108         return SUCCESS;
109     }
110
111     public String findArtifact()
112         throws Exception
113     {
114         // TODO: give action message if indexing is in progress
115
116         assert md5 != null && md5.length() != 0;
117
118         RepositoryArtifactIndex index = getIndex();
119
120         if ( !index.exists() )
121         {
122             addActionError( "The repository is not yet indexed. Please wait, and then try again." );
123             return ERROR;
124         }
125
126         searchResults = index.search(
127             new LuceneQuery( new TermQuery( new Term( StandardIndexRecordFields.MD5, md5.toLowerCase() ) ) ) );
128
129         if ( searchResults.isEmpty() )
130         {
131             return NO_RESULTS;
132         }
133         if ( searchResults.size() == 1 )
134         {
135             return ARTIFACT;
136         }
137         else
138         {
139             return RESULTS;
140         }
141     }
142
143     private RepositoryArtifactIndex getIndex()
144         throws ConfigurationStoreException, RepositoryIndexException
145     {
146         Configuration configuration = configurationStore.getConfigurationFromStore();
147         File indexPath = new File( configuration.getIndexPath() );
148
149         return factory.createStandardIndex( indexPath );
150     }
151
152     public String doInput()
153     {
154         return INPUT;
155     }
156
157     public String getQ()
158     {
159         return q;
160     }
161
162     public void setQ( String q )
163     {
164         this.q = q;
165     }
166
167     public String getMd5()
168     {
169         return md5;
170     }
171
172     public void setMd5( String md5 )
173     {
174         this.md5 = md5;
175     }
176
177     public List getSearchResults()
178     {
179         return searchResults;
180     }
181 }