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.search.lucene;
24  
25  import com.liferay.portal.kernel.search.Document;
26  import com.liferay.portal.kernel.search.Field;
27  import com.liferay.portal.kernel.search.IndexWriter;
28  import com.liferay.portal.kernel.search.SearchException;
29  
30  import java.io.IOException;
31  
32  import java.util.Collection;
33  
34  import org.apache.lucene.index.Term;
35  
36  /**
37   * <a href="LuceneIndexWriterImpl.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Bruno Farache
40   * @author Brian Wing Shun Chan
41   * @author Allen Chiang
42   *
43   */
44  public class LuceneIndexWriterImpl implements IndexWriter {
45  
46      public void addDocument(long companyId, Document doc)
47          throws SearchException {
48  
49          org.apache.lucene.index.IndexWriter writer = null;
50  
51          try {
52              writer = LuceneUtil.getWriter(companyId);
53  
54              writer.addDocument(_getLuceneDocument(doc));
55          }
56          catch (IOException ioe) {
57              throw new SearchException(ioe);
58          }
59          finally {
60              if (writer != null) {
61                  LuceneUtil.write(companyId);
62              }
63          }
64      }
65  
66      public void deleteDocument(long companyId, String uid)
67          throws SearchException {
68  
69          try {
70              LuceneUtil.deleteDocuments(companyId, new Term(Field.UID, uid));
71          }
72          catch (IOException ioe) {
73              throw new SearchException(ioe);
74          }
75      }
76  
77      public void deletePortletDocuments(long companyId, String portletId)
78          throws SearchException {
79  
80          try {
81              LuceneUtil.deleteDocuments(
82                  companyId, new Term(Field.PORTLET_ID, portletId));
83          }
84          catch (IOException ioe) {
85              throw new SearchException(ioe);
86          }
87      }
88  
89      public void updateDocument(long companyId, String uid, Document doc)
90          throws SearchException {
91  
92          deleteDocument(companyId, uid);
93  
94          addDocument(companyId, doc);
95      }
96  
97      private org.apache.lucene.document.Document _getLuceneDocument(
98          Document doc) {
99  
100         org.apache.lucene.document.Document luceneDoc =
101             new org.apache.lucene.document.Document();
102 
103         Collection<Field> values = doc.getFields().values();
104 
105         for (Field field : values) {
106             if (field.isTokenized()) {
107                 for (String value : field.getValues()) {
108                     LuceneUtil.addText(luceneDoc, field.getName(), value);
109                 }
110             }
111             else {
112                 for (String value : field.getValues()) {
113                     LuceneUtil.addKeyword(luceneDoc, field.getName(), value);
114                 }
115             }
116         }
117 
118         return luceneDoc;
119     }
120 
121 }