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