001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.lucene;
022:
023: import com.liferay.portal.kernel.util.InstancePool;
024: import com.liferay.portal.kernel.util.StringMaker;
025: import com.liferay.portal.util.PropsValues;
026:
027: import java.io.File;
028: import java.io.IOException;
029: import java.io.InputStream;
030:
031: import java.util.Date;
032:
033: import org.apache.lucene.document.DateTools;
034: import org.apache.lucene.document.Field;
035:
036: /**
037: * <a href="LuceneFields.java.html"><b><i>View Source</i></b></a>
038: *
039: * @author Brian Wing Shun Chan
040: *
041: */
042: public class LuceneFields {
043:
044: public static final String UID = "uid";
045:
046: public static final String COMPANY_ID = "companyId";
047:
048: public static final String PORTLET_ID = "portletId";
049:
050: public static final String GROUP_ID = "groupId";
051:
052: public static final String USER_ID = "userId";
053:
054: public static final String USER_NAME = "userName";
055:
056: public static final String TYPE = "type";
057:
058: public static final String TITLE = "title";
059:
060: public static final String DESCRIPTION = "description";
061:
062: public static final String CONTENT = "content";
063:
064: public static final String PROPERTIES = "properties";
065:
066: public static final String MODIFIED = "modified";
067:
068: public static final String VERSION_LABEL = "versionLabel";
069:
070: public static final String TAG_ENTRY = "tag_entry";
071:
072: public static String getUID(String portletId, long field1) {
073: return getUID(portletId, String.valueOf(field1));
074: }
075:
076: public static String getUID(String portletId, Long field1) {
077: return getUID(portletId, field1.longValue());
078: }
079:
080: public static String getUID(String portletId, String field1) {
081: return getUID(portletId, field1, null);
082: }
083:
084: public static String getUID(String portletId, long field1,
085: String field2) {
086:
087: return getUID(portletId, String.valueOf(field1), field2);
088: }
089:
090: public static String getUID(String portletId, Long field1,
091: String field2) {
092:
093: return getUID(portletId, field1.longValue(), field2);
094: }
095:
096: public static String getUID(String portletId, String field1,
097: String field2) {
098:
099: return getUID(portletId, field1, field2, null);
100: }
101:
102: public static String getUID(String portletId, String field1,
103: 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: } else {
126: return new Field(field, DateTools.dateToString(date,
127: DateTools.Resolution.SECOND), Field.Store.YES,
128: Field.Index.UN_TOKENIZED);
129: }
130: }
131:
132: public static Field getFile(String field, InputStream is,
133: String fileExt) throws IOException {
134:
135: LuceneFileExtractor fileExtractor = (LuceneFileExtractor) InstancePool
136: .get(PropsValues.LUCENE_FILE_EXTRACTOR);
137:
138: return fileExtractor.getFile(field, is, fileExt);
139: }
140:
141: public static Field getFile(String field, byte[] byteArray,
142: String fileExt) throws IOException {
143:
144: LuceneFileExtractor fileExtractor = (LuceneFileExtractor) InstancePool
145: .get(PropsValues.LUCENE_FILE_EXTRACTOR);
146:
147: return fileExtractor.getFile(field, byteArray, fileExt);
148: }
149:
150: public static Field getFile(String field, File file, String fileExt)
151: throws IOException {
152:
153: LuceneFileExtractor fileExtractor = (LuceneFileExtractor) InstancePool
154: .get(PropsValues.LUCENE_FILE_EXTRACTOR);
155:
156: return fileExtractor.getFile(field, file, fileExt);
157: }
158:
159: public static Field getKeyword(String field, double keyword) {
160: return getKeyword(field, String.valueOf(keyword));
161: }
162:
163: public static Field getKeyword(String field, long keyword) {
164: return getKeyword(field, String.valueOf(keyword));
165: }
166:
167: public static Field getKeyword(String field, Long keyword) {
168: return getKeyword(field, keyword.longValue());
169: }
170:
171: public static Field getKeyword(String field, String keyword) {
172: //keyword = KeywordsUtil.escape(keyword);
173:
174: Field fieldObj = new Field(field, keyword, Field.Store.YES,
175: Field.Index.UN_TOKENIZED);
176:
177: //fieldObj.setBoost(0);
178:
179: return fieldObj;
180: }
181:
182: public static Field getText(String field, String text) {
183: return new Field(field, text, Field.Store.YES,
184: 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: }
|