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.model.Group;
24  import com.liferay.portal.service.GroupLocalServiceUtil;
25  import com.liferay.portal.util.PortletKeys;
26  import com.liferay.portlet.bookmarks.model.BookmarksEntry;
27  import com.liferay.portlet.bookmarks.service.BookmarksEntryLocalServiceUtil;
28  import com.liferay.portlet.bookmarks.service.BookmarksFolderLocalServiceUtil;
29  import com.liferay.portlet.expando.model.ExpandoBridge;
30  import com.liferay.portlet.expando.util.ExpandoBridgeIndexerUtil;
31  
32  import java.util.Date;
33  
34  import javax.portlet.PortletURL;
35  
36  /**
37   * <a href="Indexer.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Brian Wing Shun Chan
40   * @author Bruno Farache
41   * @author Raymond Augé
42   */
43  public class Indexer implements com.liferay.portal.kernel.search.Indexer {
44  
45      public static final String PORTLET_ID = PortletKeys.BOOKMARKS;
46  
47      public static void addEntry(
48              long companyId, long groupId, long userId, long folderId,
49              long entryId, String name, String url, String comments,
50              Date modifiedDate, String[] tagsEntries,
51              ExpandoBridge expandoBridge)
52          throws SearchException {
53  
54          Document doc = getEntryDocument(
55              companyId, groupId, userId, folderId, entryId, name, url, comments,
56              modifiedDate, tagsEntries, expandoBridge);
57  
58          SearchEngineUtil.addDocument(companyId, doc);
59      }
60  
61      /**
62       * @deprecated
63       */
64      public static void addEntry(
65              long companyId, long groupId, long folderId, long entryId,
66              String name, String url, String comments, Date modifiedDate,
67              String[] tagsEntries, ExpandoBridge expandoBridge)
68          throws SearchException {
69  
70          addEntry(
71              companyId, groupId, 0, folderId, entryId, name, url, comments,
72              modifiedDate, tagsEntries, expandoBridge);
73      }
74  
75      public static void deleteEntry(long companyId, long entryId)
76          throws SearchException {
77  
78          SearchEngineUtil.deleteDocument(companyId, getEntryUID(entryId));
79      }
80  
81      public static Document getEntryDocument(
82          long companyId, long groupId, long userId, long folderId, long entryId,
83          String name, String url, String comments, Date modifiedDate,
84          String[] tagsEntries, ExpandoBridge expandoBridge) {
85  
86          long scopeGroupId = groupId;
87  
88          try {
89              Group group = GroupLocalServiceUtil.getGroup(groupId);
90  
91              if (group.isLayout()) {
92                  groupId = group.getParentGroupId();
93              }
94          }
95          catch (Exception e) {
96          }
97  
98          Document doc = new DocumentImpl();
99  
100         doc.addUID(PORTLET_ID, entryId);
101 
102         doc.addModifiedDate(modifiedDate);
103 
104         doc.addKeyword(Field.COMPANY_ID, companyId);
105         doc.addKeyword(Field.PORTLET_ID, PORTLET_ID);
106         doc.addKeyword(Field.GROUP_ID, groupId);
107         doc.addKeyword(Field.SCOPE_GROUP_ID, scopeGroupId);
108         doc.addKeyword(Field.USER_ID, userId);
109 
110         doc.addText(Field.TITLE, name);
111         doc.addKeyword(Field.TAGS_ENTRIES, tagsEntries);
112 
113         doc.addKeyword("folderId", folderId);
114         doc.addKeyword(Field.ENTRY_CLASS_NAME, BookmarksEntry.class.getName());
115         doc.addKeyword(Field.ENTRY_CLASS_PK, entryId);
116         doc.addText(Field.URL, url);
117         doc.addText(Field.COMMENTS, comments);
118 
119         ExpandoBridgeIndexerUtil.addAttributes(doc, expandoBridge);
120 
121         return doc;
122     }
123 
124     /**
125      * @deprecated
126      */
127     public static Document getEntryDocument(
128         long companyId, long groupId, long folderId, long entryId, String name,
129         String url, String comments, Date modifiedDate, String[] tagsEntries,
130         ExpandoBridge expandoBridge) {
131 
132         return getEntryDocument(
133             companyId, groupId, 0, folderId, entryId, name, url, comments,
134             modifiedDate, tagsEntries, expandoBridge);
135     }
136 
137     public static String getEntryUID(long entryId) {
138         Document doc = new DocumentImpl();
139 
140         doc.addUID(PORTLET_ID, entryId);
141 
142         return doc.get(Field.UID);
143     }
144 
145     public static void updateEntry(
146             long companyId, long groupId, long userId, long folderId,
147             long entryId, String name, String url, String comments,
148             Date modifiedDate, String[] tagsEntries,
149             ExpandoBridge expandoBridge)
150         throws SearchException {
151 
152         Document doc = getEntryDocument(
153             companyId, groupId, userId, folderId, entryId, name, url, comments,
154             modifiedDate, tagsEntries, expandoBridge);
155 
156         SearchEngineUtil.updateDocument(companyId, doc.get(Field.UID), doc);
157     }
158 
159     /**
160      * @deprecated
161      */
162     public static void updateEntry(
163             long companyId, long groupId, long folderId, long entryId,
164             String name, String url, String comments, Date modifiedDate,
165             String[] tagsEntries, ExpandoBridge expandoBridge)
166         throws SearchException {
167 
168         updateEntry(
169             companyId, groupId, 0, folderId, entryId, name, url, comments,
170             modifiedDate, tagsEntries, expandoBridge);
171     }
172 
173     public String[] getClassNames() {
174         return _CLASS_NAMES;
175     }
176 
177     public DocumentSummary getDocumentSummary(
178         Document doc, String snippet, PortletURL portletURL) {
179 
180         // Title
181 
182         String title = doc.get(Field.TITLE);
183 
184         // URL
185 
186         String url = doc.get(Field.URL);
187 
188         // Portlet URL
189 
190         String entryId = doc.get(Field.ENTRY_CLASS_PK);
191 
192         portletURL.setParameter("struts_action", "/bookmarks/edit_entry");
193         portletURL.setParameter("entryId", entryId);
194 
195         return new DocumentSummary(title, url, portletURL);
196     }
197 
198     public void reIndex(String className, long classPK) throws SearchException {
199         try {
200             BookmarksEntryLocalServiceUtil.reIndex(classPK);
201         }
202         catch (Exception e) {
203             throw new SearchException(e);
204         }
205     }
206 
207     public void reIndex(String[] ids) throws SearchException {
208         try {
209             BookmarksFolderLocalServiceUtil.reIndex(ids);
210         }
211         catch (Exception e) {
212             throw new SearchException(e);
213         }
214     }
215 
216     private static final String[] _CLASS_NAMES = new String[] {
217         BookmarksEntry.class.getName()
218     };
219 
220 }