1   /**
2    * Copyright (c) 2000-2007 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.portlet.blogs.util;
24  
25  import com.liferay.portal.kernel.search.DocumentSummary;
26  import com.liferay.portal.kernel.search.SearchException;
27  import com.liferay.portal.kernel.util.StringUtil;
28  import com.liferay.portal.lucene.LuceneFields;
29  import com.liferay.portal.lucene.LuceneUtil;
30  import com.liferay.portal.util.PortletKeys;
31  import com.liferay.portlet.blogs.service.BlogsEntryLocalServiceUtil;
32  import com.liferay.util.Html;
33  
34  import java.io.IOException;
35  
36  import javax.portlet.PortletURL;
37  
38  import org.apache.lucene.document.Document;
39  import org.apache.lucene.index.IndexWriter;
40  import org.apache.lucene.index.Term;
41  
42  /**
43   * <a href="Indexer.java.html"><b><i>View Source</i></b></a>
44   *
45   * @author Brian Wing Shun Chan
46   * @author Harry Mark
47   *
48   */
49  public class Indexer implements com.liferay.portal.kernel.search.Indexer {
50  
51      public static final String PORTLET_ID = PortletKeys.BLOGS;
52  
53      public static void addEntry(
54              long companyId, long groupId, long userId, long categoryId,
55              long entryId, String title, String content)
56          throws IOException {
57  
58          Document doc = getAddEntryDocument(
59              companyId, groupId, userId, categoryId, entryId, title, content);
60  
61          IndexWriter writer = null;
62  
63          try {
64              writer = LuceneUtil.getWriter(companyId);
65  
66              writer.addDocument(doc);
67          }
68          finally {
69              if (writer != null) {
70                  LuceneUtil.write(companyId);
71              }
72          }
73      }
74  
75      public static void deleteEntry(long companyId, long entryId)
76          throws IOException {
77  
78          LuceneUtil.deleteDocuments(
79              companyId,
80              new Term(
81                  LuceneFields.UID, LuceneFields.getUID(PORTLET_ID, entryId)));
82      }
83  
84      public static Document getAddEntryDocument(
85          long companyId, long groupId, long userId, long categoryId,
86          long entryId, String title, String content) {
87  
88          content = Html.stripHtml(content);
89  
90          Document doc = new Document();
91  
92          doc.add(
93              LuceneFields.getKeyword(
94                  LuceneFields.UID, LuceneFields.getUID(PORTLET_ID, entryId)));
95  
96          doc.add(LuceneFields.getKeyword(LuceneFields.COMPANY_ID, companyId));
97          doc.add(LuceneFields.getKeyword(LuceneFields.PORTLET_ID, PORTLET_ID));
98          doc.add(LuceneFields.getKeyword(LuceneFields.GROUP_ID, groupId));
99          doc.add(LuceneFields.getKeyword(LuceneFields.USER_ID, userId));
100 
101         doc.add(LuceneFields.getText(LuceneFields.TITLE, title));
102         doc.add(LuceneFields.getText(LuceneFields.CONTENT, content));
103 
104         doc.add(LuceneFields.getDate(LuceneFields.MODIFIED));
105 
106         doc.add(LuceneFields.getKeyword("categoryId", categoryId));
107         doc.add(LuceneFields.getKeyword("entryId", entryId));
108 
109         return doc;
110     }
111 
112     public static void updateEntry(
113             long companyId, long groupId, long userId, long categoryId,
114             long entryId, String title, String content)
115         throws IOException {
116 
117         try {
118             deleteEntry(companyId, entryId);
119         }
120         catch (IOException ioe) {
121         }
122 
123         addEntry(
124             companyId, groupId, userId, categoryId, entryId, title, content);
125     }
126 
127     public DocumentSummary getDocumentSummary(
128         com.liferay.portal.kernel.search.Document doc, PortletURL portletURL) {
129 
130         // Title
131 
132         String title = doc.get(LuceneFields.TITLE);
133 
134         // Content
135 
136         String content = doc.get(LuceneFields.CONTENT);
137 
138         content = StringUtil.shorten(content, 200);
139 
140         // URL
141 
142         String entryId = doc.get("entryId");
143 
144         portletURL.setParameter("struts_action", "/blogs/view_entry");
145         portletURL.setParameter("entryId", entryId);
146 
147         return new DocumentSummary(title, content, portletURL);
148     }
149 
150     public void reIndex(String[] ids) throws SearchException {
151         try {
152             BlogsEntryLocalServiceUtil.reIndex(ids);
153         }
154         catch (Exception e) {
155             throw new SearchException(e);
156         }
157     }
158 
159 }