001    /**
002     * Copyright (c) 2000-2012 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.GetterUtil;
018    import com.liferay.portal.kernel.util.InstancePool;
019    import com.liferay.portal.util.PropsValues;
020    
021    import java.io.File;
022    import java.io.IOException;
023    import java.io.InputStream;
024    
025    import java.util.Date;
026    
027    import org.apache.lucene.document.DateTools;
028    import org.apache.lucene.document.Field;
029    import org.apache.lucene.document.NumericField;
030    
031    /**
032     * @author Brian Wing Shun Chan
033     */
034    public class LuceneFields {
035    
036            public static String getUID(String portletId, long field1) {
037                    return getUID(portletId, String.valueOf(field1));
038            }
039    
040            public static String getUID(String portletId, Long field1) {
041                    return getUID(portletId, field1.longValue());
042            }
043    
044            public static String getUID(String portletId, String field1) {
045                    return getUID(portletId, field1, null);
046            }
047    
048            public static String getUID(String portletId, long field1, String field2) {
049                    return getUID(portletId, String.valueOf(field1), field2);
050            }
051    
052            public static String getUID(String portletId, Long field1, String field2) {
053                    return getUID(portletId, field1.longValue(), field2);
054            }
055    
056            public static String getUID(
057                    String portletId, String field1, String field2) {
058    
059                    return getUID(portletId, field1, field2, null);
060            }
061    
062            public static String getUID(
063                    String portletId, String field1, String field2, String field3) {
064    
065                    String uid = portletId + _UID_PORTLET + field1;
066    
067                    if (field2 != null) {
068                            uid += _UID_FIELD + field2;
069                    }
070    
071                    if (field3 != null) {
072                            uid += _UID_FIELD + field3;
073                    }
074    
075                    return uid;
076            }
077    
078            public static Field getDate(String field) {
079                    return getDate(field, new Date());
080            }
081    
082            public static Field getDate(String field, Date date) {
083                    if (date == null) {
084                            return getDate(field);
085                    }
086                    else {
087                            return new Field(
088                                    field,
089                                    DateTools.dateToString(date, DateTools.Resolution.SECOND),
090                                    Field.Store.YES, Field.Index.NOT_ANALYZED);
091                    }
092            }
093    
094            public static Field getFile(String field, InputStream is, String fileExt) {
095                    LuceneFileExtractor fileExtractor =
096                            (LuceneFileExtractor)InstancePool.get(
097                                    PropsValues.LUCENE_FILE_EXTRACTOR);
098    
099                    return fileExtractor.getFile(field, is, fileExt);
100            }
101    
102            public static Field getFile(String field, byte[] bytes, String fileExt) {
103                    LuceneFileExtractor fileExtractor =
104                            (LuceneFileExtractor)InstancePool.get(
105                                    PropsValues.LUCENE_FILE_EXTRACTOR);
106    
107                    return fileExtractor.getFile(field, bytes, fileExt);
108            }
109    
110            public static Field getFile(String field, File file, String fileExt)
111                    throws IOException {
112    
113                    LuceneFileExtractor fileExtractor =
114                            (LuceneFileExtractor)InstancePool.get(
115                                    PropsValues.LUCENE_FILE_EXTRACTOR);
116    
117                    return fileExtractor.getFile(field, file, fileExt);
118            }
119    
120            public static Field getKeyword(String field, double keyword) {
121                    return getKeyword(field, String.valueOf(keyword));
122            }
123    
124            public static Field getKeyword(String field, long keyword) {
125                    return getKeyword(field, String.valueOf(keyword));
126            }
127    
128            public static Field getKeyword(String field, Long keyword) {
129                    return getKeyword(field, keyword.longValue());
130            }
131    
132            public static Field getKeyword(String field, String keyword) {
133                    //keyword = KeywordsUtil.escape(keyword);
134    
135                    Field fieldObj = new Field(
136                            field, keyword, Field.Store.YES, Field.Index.NOT_ANALYZED);
137    
138                    //fieldObj.setBoost(0);
139    
140                    return fieldObj;
141            }
142    
143            public static NumericField getNumber(String field, String number) {
144                    NumericField numericField = new NumericField(
145                            field, Field.Store.YES, true);
146    
147                    numericField.setLongValue(GetterUtil.getLong(number));
148    
149                    return numericField;
150            }
151    
152            public static Field getText(String field, String text) {
153                    return new Field(field, text, Field.Store.YES, Field.Index.ANALYZED);
154            }
155    
156            public static Field getText(String field, StringBuilder sb) {
157                    return getText(field, sb.toString());
158            }
159    
160            private static final String _UID_PORTLET = "_PORTLET_";
161    
162            private static final String _UID_FIELD = "_FIELD_";
163    
164    }