001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.search.lucene;
016    
017    import com.liferay.portal.kernel.util.InstancePool;
018    import com.liferay.portal.util.PropsValues;
019    
020    import java.io.File;
021    import java.io.IOException;
022    import java.io.InputStream;
023    
024    import java.util.Date;
025    
026    import org.apache.lucene.document.DateTools;
027    import org.apache.lucene.document.Field;
028    
029    /**
030     * @author Brian Wing Shun Chan
031     */
032    public class LuceneFields {
033    
034            public static String getUID(String portletId, long field1) {
035                    return getUID(portletId, String.valueOf(field1));
036            }
037    
038            public static String getUID(String portletId, Long field1) {
039                    return getUID(portletId, field1.longValue());
040            }
041    
042            public static String getUID(String portletId, String field1) {
043                    return getUID(portletId, field1, null);
044            }
045    
046            public static String getUID(
047                    String portletId, long field1, String field2) {
048    
049                    return getUID(portletId, String.valueOf(field1), field2);
050            }
051    
052            public static String getUID(
053                    String portletId, Long field1, String field2) {
054    
055                    return getUID(portletId, field1.longValue(), field2);
056            }
057    
058            public static String getUID(
059                    String portletId, String field1, String field2) {
060    
061                    return getUID(portletId, field1, field2, null);
062            }
063    
064            public static String getUID(
065                    String portletId, String field1, String field2, String field3) {
066    
067                    String uid = portletId + _UID_PORTLET + field1;
068    
069                    if (field2 != null) {
070                            uid += _UID_FIELD + field2;
071                    }
072    
073                    if (field3 != null) {
074                            uid += _UID_FIELD + field3;
075                    }
076    
077                    return uid;
078            }
079    
080            public static Field getDate(String field) {
081                    return getDate(field, new Date());
082            }
083    
084            public static Field getDate(String field, Date date) {
085                    if (date == null) {
086                            return getDate(field);
087                    }
088                    else {
089                            return new Field(
090                                    field,
091                                    DateTools.dateToString(date, DateTools.Resolution.SECOND),
092                                    Field.Store.YES, Field.Index.NOT_ANALYZED);
093                    }
094            }
095    
096            public static Field getFile(String field, InputStream is, String fileExt) {
097                    LuceneFileExtractor fileExtractor =
098                            (LuceneFileExtractor)InstancePool.get(
099                                    PropsValues.LUCENE_FILE_EXTRACTOR);
100    
101                    return fileExtractor.getFile(field, is, fileExt);
102            }
103    
104            public static Field getFile(String field, byte[] bytes, String fileExt) {
105                    LuceneFileExtractor fileExtractor =
106                            (LuceneFileExtractor)InstancePool.get(
107                                    PropsValues.LUCENE_FILE_EXTRACTOR);
108    
109                    return fileExtractor.getFile(field, bytes, fileExt);
110            }
111    
112            public static Field getFile(String field, File file, String fileExt)
113                    throws IOException {
114    
115                    LuceneFileExtractor fileExtractor =
116                            (LuceneFileExtractor)InstancePool.get(
117                                    PropsValues.LUCENE_FILE_EXTRACTOR);
118    
119                    return fileExtractor.getFile(field, file, fileExt);
120            }
121    
122            public static Field getKeyword(String field, double keyword) {
123                    return getKeyword(field, String.valueOf(keyword));
124            }
125    
126            public static Field getKeyword(String field, long keyword) {
127                    return getKeyword(field, String.valueOf(keyword));
128            }
129    
130            public static Field getKeyword(String field, Long keyword) {
131                    return getKeyword(field, keyword.longValue());
132            }
133    
134            public static Field getKeyword(String field, String keyword) {
135                    //keyword = KeywordsUtil.escape(keyword);
136    
137                    Field fieldObj = new Field(
138                            field, keyword, Field.Store.YES, Field.Index.NOT_ANALYZED);
139    
140                    //fieldObj.setBoost(0);
141    
142                    return fieldObj;
143            }
144    
145            public static Field getText(String field, String text) {
146                    return new Field(field, text, Field.Store.YES, Field.Index.ANALYZED);
147            }
148    
149            public static Field getText(String field, StringBuilder sb) {
150                    return getText(field, sb.toString());
151            }
152    
153            private static final String _UID_PORTLET = "_PORTLET_";
154    
155            private static final String _UID_FIELD = "_FIELD_";
156    
157    }