1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.search.lucene;
16  
17  import com.liferay.portal.kernel.util.InstancePool;
18  import com.liferay.portal.util.PropsValues;
19  
20  import java.io.File;
21  import java.io.IOException;
22  import java.io.InputStream;
23  
24  import java.util.Date;
25  
26  import org.apache.lucene.document.DateTools;
27  import org.apache.lucene.document.Field;
28  
29  /**
30   * <a href="LuceneFields.java.html"><b><i>View Source</i></b></a>
31   *
32   * @author Brian Wing Shun Chan
33   */
34  public class LuceneFields {
35  
36      public static String getUID(String portletId, long field1) {
37          return getUID(portletId, String.valueOf(field1));
38      }
39  
40      public static String getUID(String portletId, Long field1) {
41          return getUID(portletId, field1.longValue());
42      }
43  
44      public static String getUID(String portletId, String field1) {
45          return getUID(portletId, field1, null);
46      }
47  
48      public static String getUID(
49          String portletId, long field1, String field2) {
50  
51          return getUID(portletId, String.valueOf(field1), field2);
52      }
53  
54      public static String getUID(
55          String portletId, Long field1, String field2) {
56  
57          return getUID(portletId, field1.longValue(), field2);
58      }
59  
60      public static String getUID(
61          String portletId, String field1, String field2) {
62  
63          return getUID(portletId, field1, field2, null);
64      }
65  
66      public static String getUID(
67          String portletId, String field1, String field2, String field3) {
68  
69          String uid = portletId + _UID_PORTLET + field1;
70  
71          if (field2 != null) {
72              uid += _UID_FIELD + field2;
73          }
74  
75          if (field3 != null) {
76              uid += _UID_FIELD + field3;
77          }
78  
79          return uid;
80      }
81  
82      public static Field getDate(String field) {
83          return getDate(field, new Date());
84      }
85  
86      public static Field getDate(String field, Date date) {
87          if (date == null) {
88              return getDate(field);
89          }
90          else {
91              return new Field(
92                  field,
93                  DateTools.dateToString(date, DateTools.Resolution.SECOND),
94                  Field.Store.YES, Field.Index.NOT_ANALYZED);
95          }
96      }
97  
98      public static Field getFile(String field, InputStream is, String fileExt) {
99          LuceneFileExtractor fileExtractor =
100             (LuceneFileExtractor)InstancePool.get(
101                 PropsValues.LUCENE_FILE_EXTRACTOR);
102 
103         return fileExtractor.getFile(field, is, fileExt);
104     }
105 
106     public static Field getFile(String field, byte[] bytes, String fileExt) {
107         LuceneFileExtractor fileExtractor =
108             (LuceneFileExtractor)InstancePool.get(
109                 PropsValues.LUCENE_FILE_EXTRACTOR);
110 
111         return fileExtractor.getFile(field, bytes, fileExt);
112     }
113 
114     public static Field getFile(String field, File file, String fileExt)
115         throws IOException {
116 
117         LuceneFileExtractor fileExtractor =
118             (LuceneFileExtractor)InstancePool.get(
119                 PropsValues.LUCENE_FILE_EXTRACTOR);
120 
121         return fileExtractor.getFile(field, file, fileExt);
122     }
123 
124     public static Field getKeyword(String field, double keyword) {
125         return getKeyword(field, String.valueOf(keyword));
126     }
127 
128     public static Field getKeyword(String field, long keyword) {
129         return getKeyword(field, String.valueOf(keyword));
130     }
131 
132     public static Field getKeyword(String field, Long keyword) {
133         return getKeyword(field, keyword.longValue());
134     }
135 
136     public static Field getKeyword(String field, String keyword) {
137         //keyword = KeywordsUtil.escape(keyword);
138 
139         Field fieldObj = new Field(
140             field, keyword, Field.Store.YES, Field.Index.NOT_ANALYZED);
141 
142         //fieldObj.setBoost(0);
143 
144         return fieldObj;
145     }
146 
147     public static Field getText(String field, String text) {
148         return new Field(field, text, Field.Store.YES, Field.Index.ANALYZED);
149     }
150 
151     public static Field getText(String field, StringBuilder sb) {
152         return getText(field, sb.toString());
153     }
154 
155     private static final String _UID_PORTLET = "_PORTLET_";
156 
157     private static final String _UID_FIELD = "_FIELD_";
158 
159 }