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