1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   * 
13   */
14  
15  package com.liferay.portlet.bookmarks.util;
16  
17  import com.liferay.portal.kernel.search.Document;
18  import com.liferay.portal.kernel.search.DocumentImpl;
19  import com.liferay.portal.kernel.search.DocumentSummary;
20  import com.liferay.portal.kernel.search.Field;
21  import com.liferay.portal.kernel.search.SearchEngineUtil;
22  import com.liferay.portal.kernel.search.SearchException;
23  import com.liferay.portal.util.PortletKeys;
24  import com.liferay.portlet.bookmarks.service.BookmarksFolderLocalServiceUtil;
25  
26  import java.util.Date;
27  
28  import javax.portlet.PortletURL;
29  
30  /**
31   * <a href="Indexer.java.html"><b><i>View Source</i></b></a>
32   *
33   * @author Brian Wing Shun Chan
34   * @author Bruno Farache
35   */
36  public class Indexer implements com.liferay.portal.kernel.search.Indexer {
37  
38      public static final String PORTLET_ID = PortletKeys.BOOKMARKS;
39  
40      public static void addEntry(
41              long companyId, long groupId, long folderId, long entryId,
42              String name, String url, String comments, Date modifiedDate,
43              String[] tagsEntries)
44          throws SearchException {
45  
46          Document doc = getEntryDocument(
47              companyId, groupId, folderId, entryId, name, url, comments,
48              modifiedDate, tagsEntries);
49  
50          SearchEngineUtil.addDocument(companyId, doc);
51      }
52  
53      public static void deleteEntry(long companyId, long entryId)
54          throws SearchException {
55  
56          SearchEngineUtil.deleteDocument(companyId, getEntryUID(entryId));
57      }
58  
59      public static Document getEntryDocument(
60          long companyId, long groupId, long folderId, long entryId, String name,
61          String url, String comments, Date modifiedDate, String[] tagsEntries) {
62  
63          Document doc = new DocumentImpl();
64  
65          doc.addUID(PORTLET_ID, entryId);
66  
67          doc.addModifiedDate(modifiedDate);
68  
69          doc.addKeyword(Field.COMPANY_ID, companyId);
70          doc.addKeyword(Field.PORTLET_ID, PORTLET_ID);
71          doc.addKeyword(Field.GROUP_ID, groupId);
72  
73          doc.addText(Field.TITLE, name);
74          doc.addKeyword(Field.TAGS_ENTRIES, tagsEntries);
75  
76          doc.addKeyword("folderId", folderId);
77          doc.addKeyword(Field.ENTRY_CLASS_PK, entryId);
78          doc.addText(Field.URL, url);
79          doc.addText(Field.COMMENTS, comments);
80  
81          return doc;
82      }
83  
84      public static String getEntryUID(long entryId) {
85          Document doc = new DocumentImpl();
86  
87          doc.addUID(PORTLET_ID, entryId);
88  
89          return doc.get(Field.UID);
90      }
91  
92      public static void updateEntry(
93              long companyId, long groupId, long folderId, long entryId,
94              String name, String url, String comments, Date modifiedDate,
95              String[] tagsEntries)
96          throws SearchException {
97  
98          Document doc = getEntryDocument(
99              companyId, groupId, folderId, entryId, name, url, comments,
100             modifiedDate, tagsEntries);
101 
102         SearchEngineUtil.updateDocument(companyId, doc.get(Field.UID), doc);
103     }
104 
105     public DocumentSummary getDocumentSummary(
106         com.liferay.portal.kernel.search.Document doc, PortletURL portletURL) {
107 
108         // Title
109 
110         String title = doc.get(Field.TITLE);
111 
112         // URL
113 
114         String url = doc.get(Field.URL);
115 
116         // Portlet URL
117 
118         String entryId = doc.get(Field.ENTRY_CLASS_PK);
119 
120         portletURL.setParameter("struts_action", "/bookmarks/edit_entry");
121         portletURL.setParameter("entryId", entryId);
122 
123         return new DocumentSummary(title, url, portletURL);
124     }
125 
126     public void reIndex(String[] ids) throws SearchException {
127         try {
128             BookmarksFolderLocalServiceUtil.reIndex(ids);
129         }
130         catch (Exception e) {
131             throw new SearchException(e);
132         }
133     }
134 
135 }