1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.calendar.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.Field;
20  import com.liferay.portal.kernel.search.Indexer;
21  import com.liferay.portal.kernel.search.SearchContext;
22  import com.liferay.portal.kernel.search.SearchEngineUtil;
23  import com.liferay.portal.kernel.search.Summary;
24  import com.liferay.portal.kernel.util.GetterUtil;
25  import com.liferay.portal.kernel.util.HtmlUtil;
26  import com.liferay.portal.kernel.util.StringUtil;
27  import com.liferay.portal.kernel.util.Validator;
28  import com.liferay.portal.search.BaseIndexer;
29  import com.liferay.portal.util.PortalUtil;
30  import com.liferay.portal.util.PortletKeys;
31  import com.liferay.portlet.asset.service.AssetTagLocalServiceUtil;
32  import com.liferay.portlet.calendar.model.CalEvent;
33  import com.liferay.portlet.calendar.service.CalEventLocalServiceUtil;
34  import com.liferay.portlet.expando.model.ExpandoBridge;
35  import com.liferay.portlet.expando.util.ExpandoBridgeIndexerUtil;
36  
37  import java.util.ArrayList;
38  import java.util.Collection;
39  import java.util.Date;
40  import java.util.List;
41  
42  import javax.portlet.PortletURL;
43  
44  /**
45   * <a href="CalIndexer.java.html"><b><i>View Source</i></b></a>
46   *
47   * @author Brett Swaim
48   */
49  public class CalIndexer extends BaseIndexer {
50  
51      public static final String[] CLASS_NAMES = {CalEvent.class.getName()};
52  
53      public static final String PORTLET_ID = PortletKeys.CALENDAR;
54  
55      public String[] getClassNames() {
56          return CLASS_NAMES;
57      }
58  
59      public Summary getSummary(
60          Document document, String snippet, PortletURL portletURL) {
61  
62          String title = document.get(Field.TITLE);
63  
64          String content = snippet;
65  
66          if (Validator.isNull(snippet)) {
67              content = StringUtil.shorten(document.get(Field.DESCRIPTION), 200);
68          }
69  
70          String eventId = document.get(Field.ENTRY_CLASS_PK);
71  
72          portletURL.setParameter("struts_action", "/calendar/view_event");
73          portletURL.setParameter("eventId", eventId);
74  
75          return new Summary(title, content, portletURL);
76      }
77  
78      protected void doDelete(Object obj) throws Exception {
79          CalEvent event = (CalEvent)obj;
80  
81          Document document = new DocumentImpl();
82  
83          document.addUID(PORTLET_ID, event.getEventId());
84  
85          SearchEngineUtil.deleteDocument(
86              event.getCompanyId(), document.get(Field.UID));
87      }
88  
89      protected Document doGetDocument(Object obj) throws Exception {
90          CalEvent event = (CalEvent)obj;
91  
92          long companyId = event.getCompanyId();
93          long groupId = getParentGroupId(event.getGroupId());
94          long scopeGroupId = event.getGroupId();
95          long userId = event.getUserId();
96          long eventId = event.getEventId();
97          String userName = PortalUtil.getUserName(userId, event.getUserName());
98          String title = event.getTitle();
99          String description = HtmlUtil.extractText(event.getDescription());
100         Date modifiedDate = event.getModifiedDate();
101 
102         String[] assetTagNames = AssetTagLocalServiceUtil.getTagNames(
103             CalEvent.class.getName(), eventId);
104 
105         ExpandoBridge expandoBridge = event.getExpandoBridge();
106 
107         Document document = new DocumentImpl();
108 
109         document.addUID(PORTLET_ID, eventId);
110 
111         document.addModifiedDate(modifiedDate);
112 
113         document.addKeyword(Field.COMPANY_ID, companyId);
114         document.addKeyword(Field.PORTLET_ID, PORTLET_ID);
115         document.addKeyword(Field.GROUP_ID, groupId);
116         document.addKeyword(Field.SCOPE_GROUP_ID, scopeGroupId);
117         document.addKeyword(Field.USER_ID, userId);
118         document.addText(Field.USER_NAME, userName);
119 
120         document.addText(Field.TITLE, title);
121         document.addText(Field.DESCRIPTION, description);
122         document.addKeyword(Field.ASSET_TAG_NAMES, assetTagNames);
123 
124         document.addKeyword(Field.ENTRY_CLASS_NAME, CalEvent.class.getName());
125         document.addKeyword(Field.ENTRY_CLASS_PK, eventId);
126 
127         ExpandoBridgeIndexerUtil.addAttributes(document, expandoBridge);
128 
129         return document;
130     }
131 
132     protected void doReindex(Object obj) throws Exception {
133         CalEvent event = (CalEvent)obj;
134 
135         Document document = getDocument(event);
136 
137         SearchEngineUtil.updateDocument(event.getCompanyId(), document);
138     }
139 
140     protected void doReindex(String className, long classPK) throws Exception {
141         CalEvent event = CalEventLocalServiceUtil.getEvent(classPK);
142 
143         doReindex(event);
144     }
145 
146     protected void doReindex(String[] ids) throws Exception {
147         long companyId = GetterUtil.getLong(ids[0]);
148 
149         reindexEvents(companyId);
150     }
151 
152     protected String getPortletId(SearchContext searchContext) {
153         return PORTLET_ID;
154     }
155 
156     protected void reindexEvents(long companyId) throws Exception {
157         int count = CalEventLocalServiceUtil.getCompanyEventsCount(companyId);
158 
159         int pages = count / Indexer.DEFAULT_INTERVAL;
160 
161         for (int i = 0; i <= pages; i++) {
162             int start = (i * Indexer.DEFAULT_INTERVAL);
163             int end = start + Indexer.DEFAULT_INTERVAL;
164 
165             reindexEvents(companyId, start, end);
166         }
167     }
168 
169     protected void reindexEvents(long companyId, int start, int end)
170         throws Exception {
171 
172         List<CalEvent> events = CalEventLocalServiceUtil.getCompanyEvents(
173             companyId, start, end);
174 
175         if (events.isEmpty()) {
176             return;
177         }
178 
179         Collection<Document> documents = new ArrayList<Document>();
180 
181         for (CalEvent event : events) {
182             Document document = getDocument(event);
183 
184             documents.add(document);
185         }
186 
187         SearchEngineUtil.updateDocuments(companyId, documents);
188     }
189 
190 }