1 package org.apache.maven.archiva.indexer.lucene;
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.lucene.document.DateTools;
24 import org.apache.lucene.document.Document;
25 import org.apache.lucene.document.Field;
26 import org.apache.lucene.document.NumberTools;
27 import org.apache.maven.archiva.indexer.record.RepositoryIndexRecord;
28 import org.apache.maven.archiva.indexer.record.StandardArtifactIndexRecord;
29 import org.apache.maven.archiva.indexer.record.StandardIndexRecordFields;
31 import java.text.ParseException;
32 import java.util.Arrays;
35 * Convert the standard index record to a Lucene document.
37 * @author <a href="mailto:brett@apache.org">Brett Porter</a>
39 public class LuceneStandardIndexRecordConverter
40 implements LuceneIndexRecordConverter
42 public Document convert( RepositoryIndexRecord record )
44 StandardArtifactIndexRecord rec = (StandardArtifactIndexRecord) record;
46 Document document = new Document();
47 addTokenizedField( document, StandardIndexRecordFields.FILENAME, rec.getFilename() );
48 addTokenizedField( document, StandardIndexRecordFields.GROUPID, rec.getGroupId() );
49 addExactField( document, StandardIndexRecordFields.GROUPID_EXACT, rec.getGroupId() );
50 addTokenizedField( document, StandardIndexRecordFields.ARTIFACTID, rec.getArtifactId() );
51 addExactField( document, StandardIndexRecordFields.ARTIFACTID_EXACT, rec.getArtifactId() );
52 addTokenizedField( document, StandardIndexRecordFields.VERSION, rec.getVersion() );
53 addExactField( document, StandardIndexRecordFields.VERSION_EXACT, rec.getVersion() );
54 addTokenizedField( document, StandardIndexRecordFields.BASE_VERSION, rec.getBaseVersion() );
55 addExactField( document, StandardIndexRecordFields.BASE_VERSION_EXACT, rec.getBaseVersion() );
56 addUntokenizedField( document, StandardIndexRecordFields.TYPE, rec.getType() );
57 addTokenizedField( document, StandardIndexRecordFields.CLASSIFIER, rec.getClassifier() );
58 addUntokenizedField( document, StandardIndexRecordFields.PACKAGING, rec.getPackaging() );
59 addUntokenizedField( document, StandardIndexRecordFields.REPOSITORY, rec.getRepository() );
60 addUntokenizedField( document, StandardIndexRecordFields.LAST_MODIFIED,
61 DateTools.timeToString( rec.getLastModified(), DateTools.Resolution.SECOND ) );
62 addUntokenizedField( document, StandardIndexRecordFields.FILE_SIZE, NumberTools.longToString( rec.getSize() ) );
63 addUntokenizedField( document, StandardIndexRecordFields.MD5, rec.getMd5Checksum() );
64 addUntokenizedField( document, StandardIndexRecordFields.SHA1, rec.getSha1Checksum() );
65 if ( rec.getClasses() != null )
67 addTokenizedField( document, StandardIndexRecordFields.CLASSES,
68 StringUtils.join( rec.getClasses().iterator(), "\n" ) );
70 if ( rec.getFiles() != null )
72 addTokenizedField( document, StandardIndexRecordFields.FILES,
73 StringUtils.join( rec.getFiles().iterator(), "\n" ) );
75 addUntokenizedField( document, StandardIndexRecordFields.PLUGIN_PREFIX, rec.getPluginPrefix() );
76 addUntokenizedField( document, StandardIndexRecordFields.INCEPTION_YEAR, rec.getInceptionYear() );
77 addTokenizedField( document, StandardIndexRecordFields.PROJECT_NAME, rec.getProjectName() );
78 addTokenizedField( document, StandardIndexRecordFields.PROJECT_DESCRIPTION, rec.getProjectDescription() );
79 if ( rec.getDependencies() != null )
81 addTokenizedField( document, StandardIndexRecordFields.DEPENDENCIES,
82 StringUtils.join( rec.getDependencies().iterator(), "\n" ) );
84 if ( rec.getDevelopers() != null )
86 addTokenizedField( document, StandardIndexRecordFields.DEVELOPERS,
87 StringUtils.join( rec.getDevelopers().iterator(), "\n" ) );
90 document.add( Field.Keyword( StandardIndexRecordFields.FLD_LICENSE_URLS, "" ) );
91 document.add( Field.Keyword( StandardIndexRecordFields.FLD_PLUGINS_REPORT, "" ) );
92 document.add( Field.Keyword( StandardIndexRecordFields.FLD_PLUGINS_BUILD, "" ) );
98 public RepositoryIndexRecord convert( Document document )
101 StandardArtifactIndexRecord record = new StandardArtifactIndexRecord();
103 record.setFilename( document.get( StandardIndexRecordFields.FILENAME ) );
104 record.setGroupId( document.get( StandardIndexRecordFields.GROUPID ) );
105 record.setArtifactId( document.get( StandardIndexRecordFields.ARTIFACTID ) );
106 record.setVersion( document.get( StandardIndexRecordFields.VERSION ) );
107 record.setBaseVersion( document.get( StandardIndexRecordFields.BASE_VERSION ) );
108 record.setType( document.get( StandardIndexRecordFields.TYPE ) );
109 record.setClassifier( document.get( StandardIndexRecordFields.CLASSIFIER ) );
110 record.setPackaging( document.get( StandardIndexRecordFields.PACKAGING ) );
111 record.setRepository( document.get( StandardIndexRecordFields.REPOSITORY ) );
112 record.setLastModified( DateTools.stringToTime( document.get( StandardIndexRecordFields.LAST_MODIFIED ) ) );
113 record.setSize( NumberTools.stringToLong( document.get( StandardIndexRecordFields.FILE_SIZE ) ) );
114 record.setMd5Checksum( document.get( StandardIndexRecordFields.MD5 ) );
115 record.setSha1Checksum( document.get( StandardIndexRecordFields.SHA1 ) );
116 String classes = document.get( StandardIndexRecordFields.CLASSES );
117 if ( classes != null )
119 record.setClasses( Arrays.asList( classes.split( "\n" ) ) );
121 String files = document.get( StandardIndexRecordFields.FILES );
124 record.setFiles( Arrays.asList( files.split( "\n" ) ) );
126 String dependencies = document.get( StandardIndexRecordFields.DEPENDENCIES );
127 if ( dependencies != null )
129 record.setDependencies( Arrays.asList( dependencies.split( "\n" ) ) );
131 String developers = document.get( StandardIndexRecordFields.DEVELOPERS );
132 if ( developers != null )
134 record.setDevelopers( Arrays.asList( developers.split( "\n" ) ) );
136 record.setPluginPrefix( document.get( StandardIndexRecordFields.PLUGIN_PREFIX ) );
137 record.setInceptionYear( document.get( StandardIndexRecordFields.INCEPTION_YEAR ) );
138 record.setProjectName( document.get( StandardIndexRecordFields.PROJECT_NAME ) );
139 record.setProjectDescription( document.get( StandardIndexRecordFields.PROJECT_DESCRIPTION ) );
144 private static void addUntokenizedField( Document document, String name, String value )
148 document.add( new Field( name, value, Field.Store.YES, Field.Index.UN_TOKENIZED ) );
152 private static void addExactField( Document document, String name, String value )
156 document.add( new Field( name, value, Field.Store.NO, Field.Index.UN_TOKENIZED ) );
160 private static void addTokenizedField( Document document, String name, String value )
164 document.add( new Field( name, value, Field.Store.YES, Field.Index.TOKENIZED ) );