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.Date;
38  import java.util.List;
39  
40  import javax.portlet.PortletURL;
41  
42  /**
43   * <a href="CalIndexer.java.html"><b><i>View Source</i></b></a>
44   *
45   * @author Brett Swaim
46   */
47  public class CalIndexer extends BaseIndexer {
48  
49      public static final String[] CLASS_NAMES = {CalEvent.class.getName()};
50  
51      public static final String PORTLET_ID = PortletKeys.CALENDAR;
52  
53      public String[] getClassNames() {
54          return CLASS_NAMES;
55      }
56  
57      public Summary getSummary(
58          Document document, String snippet, PortletURL portletURL) {
59  
60          String title = document.get(Field.TITLE);
61  
62          String content = snippet;
63  
64          if (Validator.isNull(snippet)) {
65              content = StringUtil.shorten(document.get(Field.DESCRIPTION), 200);
66          }
67  
68          String eventId = document.get(Field.ENTRY_CLASS_PK);
69  
70          portletURL.setParameter("struts_action", "/calendar/view_event");
71          portletURL.setParameter("eventId", eventId);
72  
73          return new Summary(title, content, portletURL);
74      }
75  
76      protected void doDelete(Object obj) throws Exception {
77          CalEvent event = (CalEvent)obj;
78  
79          Document document = new DocumentImpl();
80  
81          document.addUID(PORTLET_ID, event.getEventId());
82  
83          SearchEngineUtil.deleteDocument(
84              event.getCompanyId(), document.get(Field.UID));
85      }
86  
87      protected Document doGetDocument(Object obj) throws Exception {
88          CalEvent event = (CalEvent)obj;
89  
90          long companyId = event.getCompanyId();
91          long groupId = getParentGroupId(event.getGroupId());
92          long scopeGroupId = event.getGroupId();
93          long userId = event.getUserId();
94          long eventId = event.getEventId();
95          String userName = PortalUtil.getUserName(userId, event.getUserName());
96          String title = event.getTitle();
97          String description = HtmlUtil.extractText(event.getDescription());
98          Date modifiedDate = event.getModifiedDate();
99  
100         String[] assetTagNames = AssetTagLocalServiceUtil.getTagNames(
101             CalEvent.class.getName(), eventId);
102 
103         ExpandoBridge expandoBridge = event.getExpandoBridge();
104 
105         Document document = new DocumentImpl();
106 
107         document.addUID(PORTLET_ID, eventId);
108 
109         document.addModifiedDate(modifiedDate);
110 
111         document.addKeyword(Field.COMPANY_ID, companyId);
112         document.addKeyword(Field.PORTLET_ID, PORTLET_ID);
113         document.addKeyword(Field.GROUP_ID, groupId);
114         document.addKeyword(Field.SCOPE_GROUP_ID, scopeGroupId);
115         document.addKeyword(Field.USER_ID, userId);
116         document.addText(Field.USER_NAME, userName);
117 
118         document.addText(Field.TITLE, title);
119         document.addText(Field.DESCRIPTION, description);
120         document.addKeyword(Field.ASSET_TAG_NAMES, assetTagNames);
121 
122         document.addKeyword(Field.ENTRY_CLASS_NAME, CalEvent.class.getName());
123         document.addKeyword(Field.ENTRY_CLASS_PK, eventId);
124 
125         ExpandoBridgeIndexerUtil.addAttributes(document, expandoBridge);
126 
127         return document;
128     }
129 
130     protected void doReindex(Object obj) throws Exception {
131         CalEvent event = (CalEvent)obj;
132 
133         Document document = getDocument(event);
134 
135         SearchEngineUtil.updateDocument(
136             event.getCompanyId(), document.get(Field.UID), document);
137     }
138 
139     protected void doReindex(String className, long classPK) throws Exception {
140         CalEvent event = CalEventLocalServiceUtil.getEvent(classPK);
141 
142         doReindex(event);
143     }
144 
145     protected void doReindex(String[] ids) throws Exception {
146         long companyId = GetterUtil.getLong(ids[0]);
147 
148         reindexEvents(companyId);
149     }
150 
151     protected String getPortletId(SearchContext searchContext) {
152         return PORTLET_ID;
153     }
154 
155     protected void reindexEvents(long companyId) throws Exception {
156         int count = CalEventLocalServiceUtil.getCompanyEventsCount(companyId);
157 
158         int pages = count / Indexer.DEFAULT_INTERVAL;
159 
160         for (int i = 0; i <= pages; i++) {
161             int start = (i * Indexer.DEFAULT_INTERVAL);
162             int end = start + Indexer.DEFAULT_INTERVAL;
163 
164             reindexEvents(companyId, start, end);
165         }
166     }
167 
168     protected void reindexEvents(long companyId, int start, int end)
169         throws Exception {
170 
171         List<CalEvent> events = CalEventLocalServiceUtil.getCompanyEvents(
172             companyId, start, end);
173 
174         for (CalEvent event : events) {
175             reindex(event);
176         }
177     }
178 
179 }