1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class Indexer implements com.liferay.portal.kernel.search.Indexer {
45  
46      public static final String PORTLET_ID = PortletKeys.BOOKMARKS;
47  
48      public static void addEntry(
49              long companyId, long groupId, long folderId, long entryId,
50              String name, String url, String comments, Date modifiedDate,
51              String[] tagsEntries)
52          throws SearchException {
53  
54          Document doc = getEntryDocument(
55              companyId, groupId, folderId, entryId, name, url, comments,
56              modifiedDate, tagsEntries);
57  
58          SearchEngineUtil.addDocument(companyId, doc);
59      }
60  
61      public static void deleteEntry(long companyId, long entryId)
62          throws SearchException {
63  
64          SearchEngineUtil.deleteDocument(companyId, getEntryUID(entryId));
65      }
66  
67      public static Document getEntryDocument(
68          long companyId, long groupId, long folderId, long entryId, String name,
69          String url, String comments, Date modifiedDate, String[] tagsEntries) {
70  
71          Document doc = new DocumentImpl();
72  
73          doc.addUID(PORTLET_ID, entryId);
74  
75          doc.addModifiedDate(modifiedDate);
76  
77          doc.addKeyword(Field.COMPANY_ID, companyId);
78          doc.addKeyword(Field.PORTLET_ID, PORTLET_ID);
79          doc.addKeyword(Field.GROUP_ID, groupId);
80  
81          doc.addText(Field.TITLE, name);
82          doc.addKeyword(Field.TAGS_ENTRIES, tagsEntries);
83  
84          doc.addKeyword("folderId", folderId);
85          doc.addKeyword(Field.ENTRY_CLASS_PK, entryId);
86          doc.addText(Field.URL, url);
87          doc.addText(Field.COMMENTS, comments);
88  
89          return doc;
90      }
91  
92      public static String getEntryUID(long entryId) {
93          Document doc = new DocumentImpl();
94  
95          doc.addUID(PORTLET_ID, entryId);
96  
97          return doc.get(Field.UID);
98      }
99  
100     public static void updateEntry(
101             long companyId, long groupId, long folderId, long entryId,
102             String name, String url, String comments, Date modifiedDate,
103             String[] tagsEntries)
104         throws SearchException {
105 
106         Document doc = getEntryDocument(
107             companyId, groupId, folderId, entryId, name, url, comments,
108             modifiedDate, tagsEntries);
109 
110         SearchEngineUtil.updateDocument(companyId, doc.get(Field.UID), doc);
111     }
112 
113     public DocumentSummary getDocumentSummary(
114         com.liferay.portal.kernel.search.Document doc, PortletURL portletURL) {
115 
116         // Title
117 
118         String title = doc.get(Field.TITLE);
119 
120         // URL
121 
122         String url = doc.get(Field.URL);
123 
124         // Portlet URL
125 
126         String entryId = doc.get(Field.ENTRY_CLASS_PK);
127 
128         portletURL.setParameter("struts_action", "/bookmarks/edit_entry");
129         portletURL.setParameter("entryId", entryId);
130 
131         return new DocumentSummary(title, url, portletURL);
132     }
133 
134     public void reIndex(String[] ids) throws SearchException {
135         try {
136             BookmarksFolderLocalServiceUtil.reIndex(ids);
137         }
138         catch (Exception e) {
139             throw new SearchException(e);
140         }
141     }
142 
143 }