1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.search.lucene;
24  
25  import com.liferay.portal.kernel.util.InstancePool;
26  import com.liferay.portal.util.PropsValues;
27  
28  import java.io.File;
29  import java.io.IOException;
30  import java.io.InputStream;
31  
32  import java.util.Date;
33  
34  import org.apache.lucene.document.DateTools;
35  import org.apache.lucene.document.Field;
36  
37  /**
38   * <a href="LuceneFields.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Brian Wing Shun Chan
41   *
42   */
43  public class LuceneFields {
44  
45      public static String getUID(String portletId, long field1) {
46          return getUID(portletId, String.valueOf(field1));
47      }
48  
49      public static String getUID(String portletId, Long field1) {
50          return getUID(portletId, field1.longValue());
51      }
52  
53      public static String getUID(String portletId, String field1) {
54          return getUID(portletId, field1, null);
55      }
56  
57      public static String getUID(
58          String portletId, long field1, String field2) {
59  
60          return getUID(portletId, String.valueOf(field1), field2);
61      }
62  
63      public static String getUID(
64          String portletId, Long field1, String field2) {
65  
66          return getUID(portletId, field1.longValue(), field2);
67      }
68  
69      public static String getUID(
70          String portletId, String field1, String field2) {
71  
72          return getUID(portletId, field1, field2, null);
73      }
74  
75      public static String getUID(
76          String portletId, String field1, String field2, String field3) {
77  
78          String uid = portletId + _UID_PORTLET + field1;
79  
80          if (field2 != null) {
81              uid += _UID_FIELD + field2;
82          }
83  
84          if (field3 != null) {
85              uid += _UID_FIELD + field3;
86          }
87  
88          return uid;
89      }
90  
91      public static Field getDate(String field) {
92          return getDate(field, new Date());
93      }
94  
95      public static Field getDate(String field, Date date) {
96          if (date == null) {
97              return getDate(field);
98          }
99          else {
100             return new Field(
101                 field,
102                 DateTools.dateToString(date, DateTools.Resolution.SECOND),
103                 Field.Store.YES, Field.Index.UN_TOKENIZED);
104         }
105     }
106 
107     public static Field getFile(String field, InputStream is, String fileExt) {
108         LuceneFileExtractor fileExtractor =
109             (LuceneFileExtractor)InstancePool.get(
110                 PropsValues.LUCENE_FILE_EXTRACTOR);
111 
112         return fileExtractor.getFile(field, is, fileExt);
113     }
114 
115     public static Field getFile(String field, byte[] bytes, String fileExt) {
116         LuceneFileExtractor fileExtractor =
117             (LuceneFileExtractor)InstancePool.get(
118                 PropsValues.LUCENE_FILE_EXTRACTOR);
119 
120         return fileExtractor.getFile(field, bytes, fileExt);
121     }
122 
123     public static Field getFile(String field, File file, String fileExt)
124         throws IOException {
125 
126         LuceneFileExtractor fileExtractor =
127             (LuceneFileExtractor)InstancePool.get(
128                 PropsValues.LUCENE_FILE_EXTRACTOR);
129 
130         return fileExtractor.getFile(field, file, fileExt);
131     }
132 
133     public static Field getKeyword(String field, double keyword) {
134         return getKeyword(field, String.valueOf(keyword));
135     }
136 
137     public static Field getKeyword(String field, long keyword) {
138         return getKeyword(field, String.valueOf(keyword));
139     }
140 
141     public static Field getKeyword(String field, Long keyword) {
142         return getKeyword(field, keyword.longValue());
143     }
144 
145     public static Field getKeyword(String field, String keyword) {
146         //keyword = KeywordsUtil.escape(keyword);
147 
148         Field fieldObj = new Field(
149             field, keyword, Field.Store.YES, Field.Index.UN_TOKENIZED);
150 
151         //fieldObj.setBoost(0);
152 
153         return fieldObj;
154     }
155 
156     public static Field getText(String field, String text) {
157         return new Field(field, text, Field.Store.YES, Field.Index.TOKENIZED);
158     }
159 
160     public static Field getText(String field, StringBuilder sb) {
161         return getText(field, sb.toString());
162     }
163 
164     private static final String _UID_PORTLET = "_PORTLET_";
165 
166     private static final String _UID_FIELD = "_FIELD_";
167 
168 }