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