1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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.kernel.search;
24  
25  import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayInputStream;
26  import com.liferay.portal.kernel.util.ArrayUtil;
27  import com.liferay.portal.kernel.util.DateFormatFactoryUtil;
28  import com.liferay.portal.kernel.util.FileUtil;
29  import com.liferay.portal.kernel.util.StringPool;
30  import com.liferay.portal.kernel.util.Validator;
31  
32  import java.io.File;
33  import java.io.FileInputStream;
34  import java.io.IOException;
35  import java.io.InputStream;
36  
37  import java.text.DateFormat;
38  import java.text.ParseException;
39  
40  import java.util.Date;
41  import java.util.HashMap;
42  import java.util.Map;
43  
44  /**
45   * <a href="DocumentImpl.java.html"><b><i>View Source</i></b></a>
46   *
47   * @author Brian Wing Shun Chan
48   * @author Bruno Farache
49   */
50  public class DocumentImpl implements Document {
51  
52      public void add(Field field) {
53          _fields.put(field.getName(), field);
54      }
55  
56      public void addDate(String name, Date value) {
57          if (value == null) {
58              return;
59          }
60  
61          addKeyword(name, _dateFormat.format(value));
62      }
63  
64      public void addFile(String name, byte[] bytes, String fileExt) {
65          InputStream is = new UnsyncByteArrayInputStream(bytes);
66  
67          addFile(name, is, fileExt);
68      }
69  
70      public void addFile(String name, File file, String fileExt)
71          throws IOException {
72  
73          InputStream is = new FileInputStream(file);
74  
75          addFile(name, is, fileExt);
76      }
77  
78      public void addFile(String name, InputStream is, String fileExt) {
79          addText(name, FileUtil.extractText(is, fileExt));
80      }
81  
82      public void addKeyword(String name, boolean value) {
83          addKeyword(name, String.valueOf(value));
84      }
85  
86      public void addKeyword(String name, Boolean value) {
87          addKeyword(name, String.valueOf(value));
88      }
89  
90      public void addKeyword(String name, boolean[] values) {
91          if (values == null) {
92              return;
93          }
94  
95          addKeyword(name, ArrayUtil.toStringArray(values));
96      }
97  
98      public void addKeyword(String name, Boolean[] values) {
99          if (values == null) {
100             return;
101         }
102 
103         addKeyword(name, ArrayUtil.toStringArray(values));
104     }
105 
106     public void addKeyword(String name, double value) {
107         addKeyword(name, String.valueOf(value));
108     }
109 
110     public void addKeyword(String name, Double value) {
111         addKeyword(name, String.valueOf(value));
112     }
113 
114     public void addKeyword(String name, double[] values) {
115         if (values == null) {
116             return;
117         }
118 
119         addKeyword(name, ArrayUtil.toStringArray(values));
120     }
121 
122     public void addKeyword(String name, Double[] values) {
123         if (values == null) {
124             return;
125         }
126 
127         addKeyword(name, ArrayUtil.toStringArray(values));
128     }
129 
130     public void addKeyword(String name, int value) {
131         addKeyword(name, String.valueOf(value));
132     }
133 
134     public void addKeyword(String name, int[] values) {
135         if (values == null) {
136             return;
137         }
138 
139         addKeyword(name, ArrayUtil.toStringArray(values));
140     }
141 
142     public void addKeyword(String name, Integer value) {
143         addKeyword(name, String.valueOf(value));
144     }
145 
146     public void addKeyword(String name, Integer[] values) {
147         if (values == null) {
148             return;
149         }
150 
151         addKeyword(name, ArrayUtil.toStringArray(values));
152     }
153 
154     public void addKeyword(String name, long value) {
155         addKeyword(name, String.valueOf(value));
156     }
157 
158     public void addKeyword(String name, Long value) {
159         addKeyword(name, String.valueOf(value));
160     }
161 
162     public void addKeyword(String name, long[] values) {
163         if (values == null) {
164             return;
165         }
166 
167         addKeyword(name, ArrayUtil.toStringArray(values));
168     }
169 
170     public void addKeyword(String name, Long[] values) {
171         if (values == null) {
172             return;
173         }
174 
175         addKeyword(name, ArrayUtil.toStringArray(values));
176     }
177 
178     public void addKeyword(String name, short value) {
179         addKeyword(name, String.valueOf(value));
180     }
181 
182     public void addKeyword(String name, Short value) {
183         addKeyword(name, String.valueOf(value));
184     }
185 
186     public void addKeyword(String name, short[] values) {
187         if (values == null) {
188             return;
189         }
190 
191         addKeyword(name, ArrayUtil.toStringArray(values));
192     }
193 
194     public void addKeyword(String name, Short[] values) {
195         if (values == null) {
196             return;
197         }
198 
199         addKeyword(name, ArrayUtil.toStringArray(values));
200     }
201 
202     public void addKeyword(String name, String value) {
203         addKeyword(name, value, false);
204     }
205 
206     public void addKeyword(String name, String value, boolean lowerCase) {
207         if (lowerCase && Validator.isNotNull(value)) {
208             value = value.toLowerCase();
209         }
210 
211         _fields.put(name, new Field(name, value, false));
212     }
213 
214     public void addKeyword(String name, String[] values) {
215         if (values == null) {
216             return;
217         }
218 
219         _fields.put(name, new Field(name, values, false));
220     }
221 
222     public void addModifiedDate() {
223         addModifiedDate(new Date());
224     }
225 
226     public void addModifiedDate(Date modifiedDate) {
227         addDate(Field.MODIFIED, modifiedDate);
228     }
229 
230     public void addText(String name, String value) {
231         if (Validator.isNotNull(value)) {
232             _fields.put(name, new Field(name, value, true));
233         }
234     }
235 
236     public void addUID(String portletId, long field1) {
237         addUID(portletId, String.valueOf(field1));
238     }
239 
240     public void addUID(
241         String portletId, long field1, String field2) {
242 
243         addUID(portletId, String.valueOf(field1), field2);
244     }
245 
246     public void addUID(String portletId, Long field1) {
247         addUID(portletId, field1.longValue());
248     }
249 
250     public void addUID(
251         String portletId, Long field1, String field2) {
252 
253         addUID(portletId, field1.longValue(), field2);
254     }
255 
256     public void addUID(String portletId, String field1) {
257         addUID(portletId, field1, null);
258     }
259 
260     public void addUID(
261         String portletId, String field1, String field2) {
262 
263         addUID(portletId, field1, field2, null);
264     }
265 
266     public void addUID(
267         String portletId, String field1, String field2, String field3) {
268 
269         addUID(portletId, field1, field2, field3, null);
270     }
271 
272     public void addUID(
273         String portletId, String field1, String field2, String field3,
274         String field4) {
275 
276         String uid = portletId + _UID_PORTLET + field1;
277 
278         if (field2 != null) {
279             uid += _UID_FIELD + field2;
280         }
281 
282         if (field3 != null) {
283             uid += _UID_FIELD + field3;
284         }
285 
286         if (field4 != null) {
287             uid += _UID_FIELD + field4;
288         }
289 
290         addKeyword(Field.UID, uid);
291     }
292 
293     public String get(String name) {
294         Field field = _fields.get(name);
295 
296         if (field == null) {
297             return StringPool.BLANK;
298         }
299 
300         return field.getValue();
301     }
302 
303     public Date getDate(String name) throws ParseException {
304         return _dateFormat.parse(get(name));
305     }
306 
307     public Map<String, Field> getFields() {
308         return _fields;
309     }
310 
311     public String[] getValues(String name) {
312         Field field = _fields.get(name);
313 
314         if (field == null) {
315             return new String[] {StringPool.BLANK};
316         }
317 
318         return field.getValues();
319     }
320 
321     public void setFields(Map<String, Field> fields) {
322         _fields = fields;
323     }
324 
325     public String toString() {
326         StringBuilder sb = new StringBuilder();
327 
328         sb.append(StringPool.OPEN_CURLY_BRACE);
329 
330         int i = 0;
331 
332         for (Field field : _fields.values()) {
333             if (i > 0) {
334                 sb.append(StringPool.COMMA);
335                 sb.append(StringPool.SPACE);
336             }
337 
338             sb.append(field.getName());
339             sb.append(StringPool.EQUAL);
340             sb.append(field.getValues());
341         }
342 
343         sb.append(StringPool.CLOSE_CURLY_BRACE);
344 
345         return sb.toString();
346     }
347 
348     private static final String _DATE_FORMAT_PATTERN = "yyyyMMddHHmmss";
349 
350     private static final String _UID_FIELD = "_FIELD_";
351 
352     private static final String _UID_PORTLET = "_PORTLET_";
353 
354     private DateFormat _dateFormat = DateFormatFactoryUtil.getSimpleDateFormat(
355         _DATE_FORMAT_PATTERN);
356     private Map<String, Field> _fields = new HashMap<String, Field>();
357 
358 }