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.search.lucene;
24  
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.kernel.search.Document;
28  import com.liferay.portal.kernel.search.Field;
29  import com.liferay.portal.kernel.search.IndexWriter;
30  import com.liferay.portal.kernel.search.SearchException;
31  import com.liferay.portal.kernel.util.Validator;
32  
33  import java.io.IOException;
34  
35  import java.util.Collection;
36  
37  import org.apache.lucene.index.Term;
38  
39  /**
40   * <a href="LuceneIndexWriterImpl.java.html"><b><i>View Source</i></b></a>
41   *
42   * @author Bruno Farache
43   * @author Brian Wing Shun Chan
44   * @author Allen Chiang
45   * @author Alex Wallace
46   */
47  public class LuceneIndexWriterImpl implements IndexWriter {
48  
49      public void addDocument(long companyId, Document document)
50          throws SearchException {
51  
52          try {
53              LuceneHelperUtil.addDocument(
54                  companyId, _getLuceneDocument(document));
55  
56              if (_log.isDebugEnabled()) {
57                  _log.debug("Added document " + document.get(Field.UID));
58              }
59          }
60          catch (IOException ioe) {
61              throw new SearchException(ioe);
62          }
63      }
64  
65      public void deleteDocument(long companyId, String uid)
66          throws SearchException {
67  
68          try {
69              LuceneHelperUtil.deleteDocuments(
70                  companyId, new Term(Field.UID, uid));
71  
72              if (_log.isDebugEnabled()) {
73                  _log.debug("Deleted document " + uid);
74              }
75          }
76          catch (IOException ioe) {
77              throw new SearchException(ioe);
78          }
79      }
80  
81      public void deletePortletDocuments(long companyId, String portletId)
82          throws SearchException {
83  
84          try {
85              LuceneHelperUtil.deleteDocuments(
86                  companyId, new Term(Field.PORTLET_ID, portletId));
87          }
88          catch (IOException ioe) {
89              throw new SearchException(ioe);
90          }
91      }
92  
93      public void updateDocument(long companyId, String uid, Document document)
94          throws SearchException {
95  
96          try {
97              LuceneHelperUtil.updateDocument(
98                  companyId, new Term(Field.UID, uid),
99                  _getLuceneDocument(document));
100 
101             if (_log.isDebugEnabled()) {
102                 _log.debug("Updated document " + document.get(Field.UID));
103             }
104         }
105         catch (IOException ioe) {
106             throw new SearchException(ioe);
107         }
108     }
109 
110     private org.apache.lucene.document.Document _getLuceneDocument(
111         Document document) {
112 
113         org.apache.lucene.document.Document luceneDocument =
114             new org.apache.lucene.document.Document();
115 
116         Collection<Field> fields = document.getFields().values();
117 
118         for (Field field : fields) {
119             String name = field.getName();
120             boolean tokenized = field.isTokenized();
121             float boost = field.getBoost();
122 
123             for (String value : field.getValues()) {
124                 if (Validator.isNull(value)) {
125                     continue;
126                 }
127 
128                 org.apache.lucene.document.Field luceneField = null;
129 
130                 if (tokenized) {
131                     luceneField = LuceneFields.getText(name, value);
132                 }
133                 else {
134                     luceneField = LuceneFields.getKeyword(name, value);
135                 }
136 
137                 luceneField.setBoost(boost);
138 
139                 luceneDocument.add(luceneField);
140             }
141         }
142 
143         return luceneDocument;
144     }
145 
146     private static Log _log =
147         LogFactoryUtil.getLog(LuceneIndexWriterImpl.class);
148 
149 }