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.kernel.search;
24  
25  import com.liferay.portal.kernel.util.FileUtil;
26  import com.liferay.portal.kernel.util.StringPool;
27  import com.liferay.portal.kernel.util.Validator;
28  
29  import java.io.BufferedInputStream;
30  import java.io.ByteArrayInputStream;
31  import java.io.File;
32  import java.io.FileInputStream;
33  import java.io.IOException;
34  import java.io.InputStream;
35  
36  import java.text.ParseException;
37  import java.text.SimpleDateFormat;
38  
39  import java.util.Date;
40  import java.util.HashMap;
41  import java.util.Map;
42  
43  /**
44   * <a href="DocumentImpl.java.html"><b><i>View Source</i></b></a>
45   *
46   * @author Brian Wing Shun Chan
47   * @author Bruno Farache
48   *
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, _sdf.format(value));
62      }
63  
64      public void addFile(String name, InputStream is, String fileExt) {
65          addText(name, FileUtil.extractText(is, fileExt));
66      }
67  
68      public void addFile(String name, byte[] bytes, String fileExt) {
69          InputStream is = new BufferedInputStream(
70              new ByteArrayInputStream(bytes));
71  
72          addFile(name, is, fileExt);
73      }
74  
75      public void addFile(String name, File file, String fileExt)
76          throws IOException {
77  
78          InputStream is = new FileInputStream(file);
79  
80          addFile(name, is, fileExt);
81      }
82  
83      public void addKeyword(String name, double value) {
84          addKeyword(name, String.valueOf(value));
85      }
86  
87      public void addKeyword(String name, int value) {
88          addKeyword(name, String.valueOf(value));
89      }
90  
91      public void addKeyword(String name, long value) {
92          addKeyword(name, String.valueOf(value));
93      }
94  
95      public void addKeyword(String name, String value) {
96          _fields.put(name, new Field(name, value, false));
97      }
98  
99      public void addKeyword(String name, String[] values) {
100         if (values == null) {
101             return;
102         }
103 
104         _fields.put(name, new Field(name, values, false));
105     }
106 
107     public void addModifiedDate() {
108         addDate(Field.MODIFIED, new Date());
109     }
110 
111     public void addText(String name, String value) {
112         if (Validator.isNotNull(value)) {
113             _fields.put(name, new Field(name, value, true));
114         }
115     }
116 
117     public void addUID(String portletId, long field1) {
118         addUID(portletId, String.valueOf(field1));
119     }
120 
121     public void addUID(String portletId, Long field1) {
122         addUID(portletId, field1.longValue());
123     }
124 
125     public void addUID(String portletId, String field1) {
126         addUID(portletId, field1, null);
127     }
128 
129     public void addUID(
130         String portletId, long field1, String field2) {
131 
132         addUID(portletId, String.valueOf(field1), field2);
133     }
134 
135     public void addUID(
136         String portletId, Long field1, String field2) {
137 
138         addUID(portletId, field1.longValue(), field2);
139     }
140 
141     public void addUID(
142         String portletId, String field1, String field2) {
143 
144         addUID(portletId, field1, field2, null);
145     }
146 
147     public void addUID(
148         String portletId, String field1, String field2, String field3) {
149 
150         String uid = portletId + _UID_PORTLET + field1;
151 
152         if (field2 != null) {
153             uid += _UID_FIELD + field2;
154         }
155 
156         if (field3 != null) {
157             uid += _UID_FIELD + field3;
158         }
159 
160         addKeyword(Field.UID, uid);
161     }
162 
163     public String get(String name) {
164         Field field = _fields.get(name);
165 
166         if (field == null) {
167             return StringPool.BLANK;
168         }
169 
170         return field.getValue();
171     }
172 
173     public Date getDate(String name) throws ParseException {
174         return _sdf.parse(get(name));
175     }
176 
177     public String[] getValues(String name) {
178         Field field = _fields.get(name);
179 
180         if (field == null) {
181             return new String[] {StringPool.BLANK};
182         }
183 
184         return field.getValues();
185     }
186 
187     public Map<String, Field> getFields() {
188         return _fields;
189     }
190 
191     public void setFields(Map<String, Field> fields) {
192         _fields = fields;
193     }
194 
195     public String toString() {
196         StringBuilder sb = new StringBuilder();
197 
198         sb.append(StringPool.OPEN_CURLY_BRACE);
199 
200         int i = 0;
201 
202         for (Field field : _fields.values()) {
203             if (i > 0) {
204                 sb.append(StringPool.COMMA);
205                 sb.append(StringPool.SPACE);
206             }
207 
208             sb.append(field.getName());
209             sb.append(StringPool.EQUAL);
210             sb.append(field.getValues());
211         }
212 
213         sb.append(StringPool.CLOSE_CURLY_BRACE);
214 
215         return sb.toString();
216     }
217 
218     private static final String _DATE_FORMAT_PATTERN = "yyyyMMddHHmmss";
219 
220     private static final String _UID_PORTLET = "_PORTLET_";
221 
222     private static final String _UID_FIELD = "_FIELD_";
223 
224     private Map<String, Field> _fields = new HashMap<String, Field>();
225     private SimpleDateFormat _sdf = new SimpleDateFormat(_DATE_FORMAT_PATTERN);
226 
227 }