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.portlet.bookmarks.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.bookmarks.service.BookmarksFolderLocalServiceUtil;
32  
33  import java.io.IOException;
34  
35  import javax.portlet.PortletURL;
36  
37  import org.apache.lucene.document.Document;
38  import org.apache.lucene.index.IndexWriter;
39  import org.apache.lucene.index.Term;
40  
41  /**
42   * <a href="Indexer.java.html"><b><i>View Source</i></b></a>
43   *
44   * @author Brian Wing Shun Chan
45   *
46   */
47  public class Indexer implements com.liferay.portal.kernel.search.Indexer {
48  
49      public static final String PORTLET_ID = PortletKeys.BOOKMARKS;
50  
51      public static void addEntry(
52              long companyId, long groupId, long folderId, long entryId,
53              String title, String content, String description,
54              String[] tagsEntries)
55          throws IOException {
56  
57          Document doc = getAddEntryDocument(
58              companyId, groupId, folderId, entryId, title, content, description,
59              tagsEntries);
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 folderId, long entryId, String title,
86          String content, String description, String[] tagsEntries) {
87  
88          Document doc = new Document();
89  
90          LuceneUtil.addKeyword(
91              doc, LuceneFields.UID, LuceneFields.getUID(PORTLET_ID, entryId));
92  
93          LuceneUtil.addKeyword(doc, LuceneFields.COMPANY_ID, companyId);
94          LuceneUtil.addKeyword(doc, LuceneFields.PORTLET_ID, PORTLET_ID);
95          LuceneUtil.addKeyword(doc, LuceneFields.GROUP_ID, groupId);
96  
97          LuceneUtil.addText(doc, LuceneFields.TITLE, title);
98          LuceneUtil.addText(doc, LuceneFields.CONTENT, content);
99          LuceneUtil.addText(doc, LuceneFields.DESCRIPTION, description);
100 
101         LuceneUtil.addModifiedDate(doc);
102 
103         LuceneUtil.addKeyword(doc, "folderId", folderId);
104         LuceneUtil.addKeyword(doc, "entryId", entryId);
105 
106         LuceneUtil.addKeyword(doc, LuceneFields.TAG_ENTRY, tagsEntries);
107 
108         return doc;
109     }
110 
111     public static void updateEntry(
112             long companyId, long groupId, long folderId, long entryId,
113             String title, String content, String description,
114             String[] tagsEntries)
115         throws IOException {
116 
117         try {
118             deleteEntry(companyId, entryId);
119         }
120         catch (IOException ioe) {
121         }
122 
123         addEntry(
124             companyId, groupId, folderId, entryId, title, content, description,
125             tagsEntries);
126     }
127 
128     public DocumentSummary getDocumentSummary(
129         com.liferay.portal.kernel.search.Document doc, PortletURL portletURL) {
130 
131         // Title
132 
133         String title = doc.get(LuceneFields.TITLE);
134 
135         // Content
136 
137         String content = doc.get(LuceneFields.CONTENT);
138 
139         content = StringUtil.shorten(content, 200);
140 
141         // URL
142 
143         String entryId = doc.get("entryId");
144 
145         portletURL.setParameter("struts_action", "/bookmarks/edit_entry");
146         portletURL.setParameter("entryId", entryId);
147 
148         return new DocumentSummary(title, content, portletURL);
149     }
150 
151     public void reIndex(String[] ids) throws SearchException {
152         try {
153             BookmarksFolderLocalServiceUtil.reIndex(ids);
154         }
155         catch (Exception e) {
156             throw new SearchException(e);
157         }
158     }
159 
160 }