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.MinimalArtifactIndexRecord;
28 import org.apache.maven.archiva.indexer.record.MinimalIndexRecordFields;
29 import org.apache.maven.archiva.indexer.record.RepositoryIndexRecord;
31 import java.text.ParseException;
32 import java.util.Arrays;
35 * Convert the minimal index record to a Lucene document.
37 * @author <a href="mailto:brett@apache.org">Brett Porter</a>
39 public class LuceneMinimalIndexRecordConverter
40 implements LuceneIndexRecordConverter
42 public Document convert( RepositoryIndexRecord record )
44 MinimalArtifactIndexRecord rec = (MinimalArtifactIndexRecord) record;
46 Document document = new Document();
47 addTokenizedField( document, MinimalIndexRecordFields.FILENAME, rec.getFilename() );
48 addUntokenizedField( document, MinimalIndexRecordFields.LAST_MODIFIED,
49 DateTools.timeToString( rec.getLastModified(), DateTools.Resolution.SECOND ) );
50 addUntokenizedField( document, MinimalIndexRecordFields.FILE_SIZE, NumberTools.longToString( rec.getSize() ) );
51 addUntokenizedField( document, MinimalIndexRecordFields.MD5, rec.getMd5Checksum() );
52 addTokenizedField( document, MinimalIndexRecordFields.CLASSES,
53 StringUtils.join( rec.getClasses().iterator(), "\n" ) );
58 public RepositoryIndexRecord convert( Document document )
61 MinimalArtifactIndexRecord record = new MinimalArtifactIndexRecord();
63 record.setFilename( document.get( MinimalIndexRecordFields.FILENAME ) );
64 record.setLastModified( DateTools.stringToTime( document.get( MinimalIndexRecordFields.LAST_MODIFIED ) ) );
65 record.setSize( NumberTools.stringToLong( document.get( MinimalIndexRecordFields.FILE_SIZE ) ) );
66 record.setMd5Checksum( document.get( MinimalIndexRecordFields.MD5 ) );
67 record.setClasses( Arrays.asList( document.get( MinimalIndexRecordFields.CLASSES ).split( "\n" ) ) );
72 private static void addUntokenizedField( Document document, String name, String value )
76 document.add( new Field( name, value, Field.Store.YES, Field.Index.UN_TOKENIZED ) );
80 private static void addTokenizedField( Document document, String name, String value )
84 document.add( new Field( name, value, Field.Store.YES, Field.Index.TOKENIZED ) );