1   /**
2    * Copyright (c) 2000-2008 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.PropsValues;
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 final String TAG_ENTRY = "tag_entry";
73  
74      public static String getUID(String portletId, long field1) {
75          return getUID(portletId, String.valueOf(field1));
76      }
77  
78      public static String getUID(String portletId, Long field1) {
79          return getUID(portletId, field1.longValue());
80      }
81  
82      public static String getUID(String portletId, String field1) {
83          return getUID(portletId, field1, null);
84      }
85  
86      public static String getUID(
87          String portletId, long field1, String field2) {
88  
89          return getUID(portletId, String.valueOf(field1), field2);
90      }
91  
92      public static String getUID(
93          String portletId, Long field1, String field2) {
94  
95          return getUID(portletId, field1.longValue(), field2);
96      }
97  
98      public static String getUID(
99          String portletId, String field1, String field2) {
100 
101         return getUID(portletId, field1, field2, null);
102     }
103 
104     public static String getUID(
105         String portletId, String field1, String field2, String field3) {
106 
107         String uid = portletId + _UID_PORTLET + field1;
108 
109         if (field2 != null) {
110             uid += _UID_FIELD + field2;
111         }
112 
113         if (field3 != null) {
114             uid += _UID_FIELD + field3;
115         }
116 
117         return uid;
118     }
119 
120     public static Field getDate(String field) {
121         return getDate(field, new Date());
122     }
123 
124     public static Field getDate(String field, Date date) {
125         if (date == null) {
126             return getDate(field);
127         }
128         else {
129             return new Field(
130                 field,
131                 DateTools.dateToString(date, DateTools.Resolution.SECOND),
132                 Field.Store.YES, Field.Index.UN_TOKENIZED);
133         }
134     }
135 
136     public static Field getFile(String field, InputStream is, String fileExt)
137         throws IOException {
138 
139         LuceneFileExtractor fileExtractor =
140             (LuceneFileExtractor)InstancePool.get(
141                 PropsValues.LUCENE_FILE_EXTRACTOR);
142 
143         return fileExtractor.getFile(field, is, fileExt);
144     }
145 
146     public static Field getFile(String field, byte[] byteArray, String fileExt)
147         throws IOException {
148 
149         LuceneFileExtractor fileExtractor =
150             (LuceneFileExtractor)InstancePool.get(
151                 PropsValues.LUCENE_FILE_EXTRACTOR);
152 
153         return fileExtractor.getFile(field, byteArray, fileExt);
154     }
155 
156     public static Field getFile(String field, File file, String fileExt)
157         throws IOException {
158 
159         LuceneFileExtractor fileExtractor =
160             (LuceneFileExtractor)InstancePool.get(
161                 PropsValues.LUCENE_FILE_EXTRACTOR);
162 
163         return fileExtractor.getFile(field, file, fileExt);
164     }
165 
166     public static Field getKeyword(String field, double keyword) {
167         return getKeyword(field, String.valueOf(keyword));
168     }
169 
170     public static Field getKeyword(String field, long keyword) {
171         return getKeyword(field, String.valueOf(keyword));
172     }
173 
174     public static Field getKeyword(String field, Long keyword) {
175         return getKeyword(field, keyword.longValue());
176     }
177 
178     public static Field getKeyword(String field, String keyword) {
179         //keyword = KeywordsUtil.escape(keyword);
180 
181         Field fieldObj = new Field(
182             field, keyword, Field.Store.YES, Field.Index.UN_TOKENIZED);
183 
184         //fieldObj.setBoost(0);
185 
186         return fieldObj;
187     }
188 
189     public static Field getText(String field, String text) {
190         return new Field(field, text, Field.Store.YES, Field.Index.TOKENIZED);
191     }
192 
193     public static Field getText(String field, StringMaker sm) {
194         return getText(field, sm.toString());
195     }
196 
197     private static final String _UID_PORTLET = "_PORTLET_";
198 
199     private static final String _UID_FIELD = "_FIELD_";
200 
201 }