1   /**
2    * Copyright (c) 2000-2009 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.Document;
26  import com.liferay.portal.kernel.search.DocumentImpl;
27  import com.liferay.portal.kernel.search.DocumentSummary;
28  import com.liferay.portal.kernel.search.Field;
29  import com.liferay.portal.kernel.search.SearchEngineUtil;
30  import com.liferay.portal.kernel.search.SearchException;
31  import com.liferay.portal.util.PortletKeys;
32  import com.liferay.portlet.bookmarks.service.BookmarksFolderLocalServiceUtil;
33  
34  import java.util.Date;
35  
36  import javax.portlet.PortletURL;
37  
38  /**
39   * <a href="Indexer.java.html"><b><i>View Source</i></b></a>
40   *
41   * @author Brian Wing Shun Chan
42   * @author Bruno Farache
43   *
44   */
45  public class Indexer implements com.liferay.portal.kernel.search.Indexer {
46  
47      public static final String PORTLET_ID = PortletKeys.BOOKMARKS;
48  
49      public static void addEntry(
50              long companyId, long groupId, long folderId, long entryId,
51              String name, String url, String comments, Date modifiedDate,
52              String[] tagsEntries)
53          throws SearchException {
54  
55          Document doc = getEntryDocument(
56              companyId, groupId, folderId, entryId, name, url, comments,
57              modifiedDate, tagsEntries);
58  
59          SearchEngineUtil.addDocument(companyId, doc);
60      }
61  
62      public static void deleteEntry(long companyId, long entryId)
63          throws SearchException {
64  
65          SearchEngineUtil.deleteDocument(companyId, getEntryUID(entryId));
66      }
67  
68      public static Document getEntryDocument(
69          long companyId, long groupId, long folderId, long entryId, String name,
70          String url, String comments, Date modifiedDate, String[] tagsEntries) {
71  
72          Document doc = new DocumentImpl();
73  
74          doc.addUID(PORTLET_ID, entryId);
75  
76          doc.addModifiedDate(modifiedDate);
77  
78          doc.addKeyword(Field.COMPANY_ID, companyId);
79          doc.addKeyword(Field.PORTLET_ID, PORTLET_ID);
80          doc.addKeyword(Field.GROUP_ID, groupId);
81  
82          doc.addText(Field.TITLE, name);
83          doc.addKeyword(Field.TAGS_ENTRIES, tagsEntries);
84  
85          doc.addKeyword("folderId", folderId);
86          doc.addKeyword(Field.ENTRY_CLASS_PK, entryId);
87          doc.addText(Field.URL, url);
88          doc.addText(Field.COMMENTS, comments);
89  
90          return doc;
91      }
92  
93      public static String getEntryUID(long entryId) {
94          Document doc = new DocumentImpl();
95  
96          doc.addUID(PORTLET_ID, entryId);
97  
98          return doc.get(Field.UID);
99      }
100 
101     public static void updateEntry(
102             long companyId, long groupId, long folderId, long entryId,
103             String name, String url, String comments, Date modifiedDate,
104             String[] tagsEntries)
105         throws SearchException {
106 
107         Document doc = getEntryDocument(
108             companyId, groupId, folderId, entryId, name, url, comments,
109             modifiedDate, tagsEntries);
110 
111         SearchEngineUtil.updateDocument(companyId, doc.get(Field.UID), doc);
112     }
113 
114     public DocumentSummary getDocumentSummary(
115         com.liferay.portal.kernel.search.Document doc, PortletURL portletURL) {
116 
117         // Title
118 
119         String title = doc.get(Field.TITLE);
120 
121         // URL
122 
123         String url = doc.get(Field.URL);
124 
125         // Portlet URL
126 
127         String entryId = doc.get(Field.ENTRY_CLASS_PK);
128 
129         portletURL.setParameter("struts_action", "/bookmarks/edit_entry");
130         portletURL.setParameter("entryId", entryId);
131 
132         return new DocumentSummary(title, url, portletURL);
133     }
134 
135     public void reIndex(String[] ids) throws SearchException {
136         try {
137             BookmarksFolderLocalServiceUtil.reIndex(ids);
138         }
139         catch (Exception e) {
140             throw new SearchException(e);
141         }
142     }
143 
144 }