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.model.BookmarksEntry;
33  import com.liferay.portlet.bookmarks.service.BookmarksEntryLocalServiceUtil;
34  import com.liferay.portlet.bookmarks.service.BookmarksFolderLocalServiceUtil;
35  import com.liferay.portlet.expando.model.ExpandoBridge;
36  import com.liferay.portlet.expando.util.ExpandoBridgeIndexerUtil;
37  
38  import java.util.Date;
39  
40  import javax.portlet.PortletURL;
41  
42  /**
43   * <a href="Indexer.java.html"><b><i>View Source</i></b></a>
44   *
45   * @author Brian Wing Shun Chan
46   * @author Bruno Farache
47   * @author Raymond Augé
48   *
49   */
50  public class Indexer implements com.liferay.portal.kernel.search.Indexer {
51  
52      public static final String PORTLET_ID = PortletKeys.BOOKMARKS;
53  
54      public static void addEntry(
55              long companyId, long groupId, long folderId, long entryId,
56              String name, String url, String comments, Date modifiedDate,
57              String[] tagsEntries, ExpandoBridge expandoBridge)
58          throws SearchException {
59  
60          Document doc = getEntryDocument(
61              companyId, groupId, folderId, entryId, name, url, comments,
62              modifiedDate, tagsEntries, expandoBridge);
63  
64          SearchEngineUtil.addDocument(companyId, doc);
65      }
66  
67      public static void deleteEntry(long companyId, long entryId)
68          throws SearchException {
69  
70          SearchEngineUtil.deleteDocument(companyId, getEntryUID(entryId));
71      }
72  
73      public static Document getEntryDocument(
74          long companyId, long groupId, long folderId, long entryId, String name,
75          String url, String comments, Date modifiedDate, String[] tagsEntries,
76          ExpandoBridge expandoBridge) {
77  
78          Document doc = new DocumentImpl();
79  
80          doc.addUID(PORTLET_ID, entryId);
81  
82          doc.addModifiedDate(modifiedDate);
83  
84          doc.addKeyword(Field.COMPANY_ID, companyId);
85          doc.addKeyword(Field.PORTLET_ID, PORTLET_ID);
86          doc.addKeyword(Field.GROUP_ID, groupId);
87  
88          doc.addText(Field.TITLE, name);
89          doc.addKeyword(Field.TAGS_ENTRIES, tagsEntries);
90  
91          doc.addKeyword("folderId", folderId);
92          doc.addKeyword(Field.ENTRY_CLASS_NAME, BookmarksEntry.class.getName());
93          doc.addKeyword(Field.ENTRY_CLASS_PK, entryId);
94          doc.addText(Field.URL, url);
95          doc.addText(Field.COMMENTS, comments);
96  
97          ExpandoBridgeIndexerUtil.addAttributes(doc, expandoBridge);
98  
99          return doc;
100     }
101 
102     public static String getEntryUID(long entryId) {
103         Document doc = new DocumentImpl();
104 
105         doc.addUID(PORTLET_ID, entryId);
106 
107         return doc.get(Field.UID);
108     }
109 
110     public static void updateEntry(
111             long companyId, long groupId, long folderId, long entryId,
112             String name, String url, String comments, Date modifiedDate,
113             String[] tagsEntries, ExpandoBridge expandoBridge)
114         throws SearchException {
115 
116         Document doc = getEntryDocument(
117             companyId, groupId, folderId, entryId, name, url, comments,
118             modifiedDate, tagsEntries, expandoBridge);
119 
120         SearchEngineUtil.updateDocument(companyId, doc.get(Field.UID), doc);
121     }
122 
123     public String[] getClassNames() {
124         return _CLASS_NAMES;
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(Field.TITLE);
133 
134         // URL
135 
136         String url = doc.get(Field.URL);
137 
138         // Portlet URL
139 
140         String entryId = doc.get(Field.ENTRY_CLASS_PK);
141 
142         portletURL.setParameter("struts_action", "/bookmarks/edit_entry");
143         portletURL.setParameter("entryId", entryId);
144 
145         return new DocumentSummary(title, url, portletURL);
146     }
147 
148     public void reIndex(String className, long classPK) throws SearchException {
149         try {
150             BookmarksEntryLocalServiceUtil.reIndex(classPK);
151         }
152         catch (Exception e) {
153             throw new SearchException(e);
154         }
155     }
156 
157     public void reIndex(String[] ids) throws SearchException {
158         try {
159             BookmarksFolderLocalServiceUtil.reIndex(ids);
160         }
161         catch (Exception e) {
162             throw new SearchException(e);
163         }
164     }
165 
166     private static final String[] _CLASS_NAMES = new String[] {
167         BookmarksEntry.class.getName()
168     };
169 
170 }