1   /**
2    * Copyright (c) 2000-2007 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.lucene;
24  
25  import com.liferay.portal.kernel.util.InstancePool;
26  import com.liferay.portal.kernel.util.StringMaker;
27  import com.liferay.portal.util.PropsUtil;
28  
29  import java.io.File;
30  import java.io.IOException;
31  import java.io.InputStream;
32  
33  import java.util.Date;
34  
35  import org.apache.lucene.document.DateTools;
36  import org.apache.lucene.document.Field;
37  
38  /**
39   * <a href="LuceneFields.java.html"><b><i>View Source</i></b></a>
40   *
41   * @author Brian Wing Shun Chan
42   *
43   */
44  public class LuceneFields {
45  
46      public static final String UID = "uid";
47  
48      public static final String COMPANY_ID = "companyId";
49  
50      public static final String PORTLET_ID = "portletId";
51  
52      public static final String GROUP_ID = "groupId";
53  
54      public static final String USER_ID = "userId";
55  
56      public static final String USER_NAME = "userName";
57  
58      public static final String TYPE = "type";
59  
60      public static final String TITLE = "title";
61  
62      public static final String DESCRIPTION = "description";
63  
64      public static final String CONTENT = "content";
65  
66      public static final String PROPERTIES = "properties";
67  
68      public static final String MODIFIED = "modified";
69  
70      public static final String VERSION_LABEL = "versionLabel";
71  
72      public static String getUID(String portletId, long field1) {
73          return getUID(portletId, String.valueOf(field1));
74      }
75  
76      public static String getUID(String portletId, Long field1) {
77          return getUID(portletId, field1.longValue());
78      }
79  
80      public static String getUID(String portletId, String field1) {
81          return getUID(portletId, field1, null);
82      }
83  
84      public static String getUID(
85          String portletId, long field1, String field2) {
86  
87          return getUID(portletId, String.valueOf(field1), field2);
88      }
89  
90      public static String getUID(
91          String portletId, Long field1, String field2) {
92  
93          return getUID(portletId, field1.longValue(), field2);
94      }
95  
96      public static String getUID(
97          String portletId, String field1, String field2) {
98  
99          return getUID(portletId, field1, field2, null);
100     }
101 
102     public static String getUID(
103         String portletId, String field1, String field2, String field3) {
104 
105         String uid = portletId + _UID_PORTLET + field1;
106 
107         if (field2 != null) {
108             uid += _UID_FIELD + field2;
109         }
110 
111         if (field3 != null) {
112             uid += _UID_FIELD + field3;
113         }
114 
115         return uid;
116     }
117 
118     public static Field getDate(String field) {
119         return getDate(field, new Date());
120     }
121 
122     public static Field getDate(String field, Date date) {
123         if (date == null) {
124             return getDate(field);
125         }
126         else {
127             return new Field(
128                 field,
129                 DateTools.dateToString(date, DateTools.Resolution.SECOND),
130                 Field.Store.YES, Field.Index.UN_TOKENIZED);
131         }
132     }
133 
134     public static Field getFile(String field, InputStream is, String fileExt)
135         throws IOException {
136 
137         LuceneFileExtractor fileExtractor =
138             (LuceneFileExtractor)InstancePool.get(
139                 PropsUtil.get(PropsUtil.LUCENE_FILE_EXTRACTOR));
140 
141         return fileExtractor.getFile(field, is, fileExt);
142     }
143 
144     public static Field getFile(String field, byte[] byteArray, String fileExt)
145         throws IOException {
146 
147         LuceneFileExtractor fileExtractor =
148             (LuceneFileExtractor)InstancePool.get(
149                 PropsUtil.get(PropsUtil.LUCENE_FILE_EXTRACTOR));
150 
151         return fileExtractor.getFile(field, byteArray, fileExt);
152     }
153 
154     public static Field getFile(String field, File file, String fileExt)
155         throws IOException {
156 
157         LuceneFileExtractor fileExtractor =
158             (LuceneFileExtractor)InstancePool.get(
159                 PropsUtil.get(PropsUtil.LUCENE_FILE_EXTRACTOR));
160 
161         return fileExtractor.getFile(field, file, fileExt);
162     }
163 
164     public static Field getKeyword(String field, long keyword) {
165         return getKeyword(field, String.valueOf(keyword));
166     }
167 
168     public static Field getKeyword(String field, Long keyword) {
169         return getKeyword(field, keyword.longValue());
170     }
171 
172     public static Field getKeyword(String field, String keyword) {
173         //keyword = KeywordsUtil.escape(keyword);
174 
175         Field fieldObj = new Field(
176             field, keyword, Field.Store.YES, Field.Index.UN_TOKENIZED);
177 
178         //fieldObj.setBoost(0);
179 
180         return fieldObj;
181     }
182 
183     public static Field getText(String field, String text) {
184         return new Field(field, text, Field.Store.YES, Field.Index.TOKENIZED);
185     }
186 
187     public static Field getText(String field, StringMaker sm) {
188         return getText(field, sm.toString());
189     }
190 
191     private static final String _UID_PORTLET = "_PORTLET_";
192 
193     private static final String _UID_FIELD = "_FIELD_";
194 
195 }