1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.search.lucene;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.kernel.search.Document;
20  import com.liferay.portal.kernel.search.Field;
21  import com.liferay.portal.kernel.search.IndexWriter;
22  import com.liferay.portal.kernel.search.SearchException;
23  import com.liferay.portal.kernel.util.Validator;
24  
25  import java.io.IOException;
26  
27  import java.util.Collection;
28  
29  import org.apache.lucene.index.Term;
30  
31  /**
32   * <a href="LuceneIndexWriterImpl.java.html"><b><i>View Source</i></b></a>
33   *
34   * @author Bruno Farache
35   * @author Brian Wing Shun Chan
36   * @author Allen Chiang
37   * @author Alex Wallace
38   */
39  public class LuceneIndexWriterImpl implements IndexWriter {
40  
41      public void addDocument(long companyId, Document document)
42          throws SearchException {
43  
44          try {
45              LuceneHelperUtil.addDocument(
46                  companyId, _getLuceneDocument(document));
47  
48              if (_log.isDebugEnabled()) {
49                  _log.debug("Added document " + document.get(Field.UID));
50              }
51          }
52          catch (IOException ioe) {
53              throw new SearchException(ioe);
54          }
55      }
56  
57      public void addDocuments(long companyId, Collection<Document> documents)
58          throws SearchException {
59  
60          for (Document document : documents) {
61              addDocument(companyId, document);
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 deleteDocuments(long companyId, Collection<String> uids)
82          throws SearchException {
83  
84          for (String uid : uids) {
85              deleteDocument(companyId, uid);
86          }
87      }
88  
89      public void deletePortletDocuments(long companyId, String portletId)
90          throws SearchException {
91  
92          try {
93              LuceneHelperUtil.deleteDocuments(
94                  companyId, new Term(Field.PORTLET_ID, portletId));
95          }
96          catch (IOException ioe) {
97              throw new SearchException(ioe);
98          }
99      }
100 
101     public void updateDocument(long companyId, Document document)
102         throws SearchException {
103 
104         try {
105             LuceneHelperUtil.updateDocument(
106                 companyId, new Term(Field.UID, document.getUID()),
107                 _getLuceneDocument(document));
108 
109             if (_log.isDebugEnabled()) {
110                 _log.debug("Updated document " + document.get(Field.UID));
111             }
112         }
113         catch (IOException ioe) {
114             throw new SearchException(ioe);
115         }
116     }
117 
118     public void updateDocuments(long companyId, Collection<Document> documents)
119         throws SearchException {
120 
121         for (Document document : documents) {
122             updateDocument(companyId, document);
123         }
124     }
125 
126     private org.apache.lucene.document.Document _getLuceneDocument(
127         Document document) {
128 
129         org.apache.lucene.document.Document luceneDocument =
130             new org.apache.lucene.document.Document();
131 
132         Collection<Field> fields = document.getFields().values();
133 
134         for (Field field : fields) {
135             String name = field.getName();
136             boolean tokenized = field.isTokenized();
137             float boost = field.getBoost();
138 
139             for (String value : field.getValues()) {
140                 if (Validator.isNull(value)) {
141                     continue;
142                 }
143 
144                 org.apache.lucene.document.Field luceneField = null;
145 
146                 if (tokenized) {
147                     luceneField = LuceneFields.getText(name, value);
148                 }
149                 else {
150                     luceneField = LuceneFields.getKeyword(name, value);
151                 }
152 
153                 luceneField.setBoost(boost);
154 
155                 luceneDocument.add(luceneField);
156             }
157         }
158 
159         return luceneDocument;
160     }
161 
162     private static Log _log = LogFactoryUtil.getLog(
163         LuceneIndexWriterImpl.class);
164 
165 }