1
14
15 package com.liferay.portlet.calendar.util;
16
17 import com.liferay.portal.kernel.search.BaseIndexer;
18 import com.liferay.portal.kernel.search.Document;
19 import com.liferay.portal.kernel.search.DocumentImpl;
20 import com.liferay.portal.kernel.search.Field;
21 import com.liferay.portal.kernel.search.Indexer;
22 import com.liferay.portal.kernel.search.SearchContext;
23 import com.liferay.portal.kernel.search.SearchEngineUtil;
24 import com.liferay.portal.kernel.search.Summary;
25 import com.liferay.portal.kernel.util.GetterUtil;
26 import com.liferay.portal.kernel.util.HtmlUtil;
27 import com.liferay.portal.kernel.util.StringUtil;
28 import com.liferay.portal.kernel.util.Validator;
29 import com.liferay.portal.util.PortalUtil;
30 import com.liferay.portal.util.PortletKeys;
31 import com.liferay.portlet.asset.service.AssetCategoryLocalServiceUtil;
32 import com.liferay.portlet.asset.service.AssetTagLocalServiceUtil;
33 import com.liferay.portlet.calendar.model.CalEvent;
34 import com.liferay.portlet.calendar.service.CalEventLocalServiceUtil;
35 import com.liferay.portlet.expando.model.ExpandoBridge;
36 import com.liferay.portlet.expando.util.ExpandoBridgeIndexerUtil;
37
38 import java.util.ArrayList;
39 import java.util.Collection;
40 import java.util.Date;
41 import java.util.List;
42
43 import javax.portlet.PortletURL;
44
45
50 public class CalIndexer extends BaseIndexer {
51
52 public static final String[] CLASS_NAMES = {CalEvent.class.getName()};
53
54 public static final String PORTLET_ID = PortletKeys.CALENDAR;
55
56 public String[] getClassNames() {
57 return CLASS_NAMES;
58 }
59
60 public Summary getSummary(
61 Document document, String snippet, PortletURL portletURL) {
62
63 String title = document.get(Field.TITLE);
64
65 String content = snippet;
66
67 if (Validator.isNull(snippet)) {
68 content = StringUtil.shorten(document.get(Field.DESCRIPTION), 200);
69 }
70
71 String eventId = document.get(Field.ENTRY_CLASS_PK);
72
73 portletURL.setParameter("struts_action", "/calendar/view_event");
74 portletURL.setParameter("eventId", eventId);
75
76 return new Summary(title, content, portletURL);
77 }
78
79 protected void doDelete(Object obj) throws Exception {
80 CalEvent event = (CalEvent)obj;
81
82 Document document = new DocumentImpl();
83
84 document.addUID(PORTLET_ID, event.getEventId());
85
86 SearchEngineUtil.deleteDocument(
87 event.getCompanyId(), document.get(Field.UID));
88 }
89
90 protected Document doGetDocument(Object obj) throws Exception {
91 CalEvent event = (CalEvent)obj;
92
93 long companyId = event.getCompanyId();
94 long groupId = getParentGroupId(event.getGroupId());
95 long scopeGroupId = event.getGroupId();
96 long userId = event.getUserId();
97 long eventId = event.getEventId();
98 String userName = PortalUtil.getUserName(userId, event.getUserName());
99 String title = event.getTitle();
100 String description = HtmlUtil.extractText(event.getDescription());
101 Date modifiedDate = event.getModifiedDate();
102
103 long[] assetCategoryIds = AssetCategoryLocalServiceUtil.getCategoryIds(
104 CalEvent.class.getName(), eventId);
105 String[] assetTagNames = AssetTagLocalServiceUtil.getTagNames(
106 CalEvent.class.getName(), eventId);
107
108 ExpandoBridge expandoBridge = event.getExpandoBridge();
109
110 Document document = new DocumentImpl();
111
112 document.addUID(PORTLET_ID, eventId);
113
114 document.addModifiedDate(modifiedDate);
115
116 document.addKeyword(Field.COMPANY_ID, companyId);
117 document.addKeyword(Field.PORTLET_ID, PORTLET_ID);
118 document.addKeyword(Field.GROUP_ID, groupId);
119 document.addKeyword(Field.SCOPE_GROUP_ID, scopeGroupId);
120 document.addKeyword(Field.USER_ID, userId);
121 document.addText(Field.USER_NAME, userName);
122
123 document.addText(Field.TITLE, title);
124 document.addText(Field.DESCRIPTION, description);
125 document.addKeyword(Field.ASSET_CATEGORY_IDS, assetCategoryIds);
126 document.addKeyword(Field.ASSET_TAG_NAMES, assetTagNames);
127
128 document.addKeyword(Field.ENTRY_CLASS_NAME, CalEvent.class.getName());
129 document.addKeyword(Field.ENTRY_CLASS_PK, eventId);
130
131 ExpandoBridgeIndexerUtil.addAttributes(document, expandoBridge);
132
133 return document;
134 }
135
136 protected void doReindex(Object obj) throws Exception {
137 CalEvent event = (CalEvent)obj;
138
139 Document document = getDocument(event);
140
141 SearchEngineUtil.updateDocument(event.getCompanyId(), document);
142 }
143
144 protected void doReindex(String className, long classPK) throws Exception {
145 CalEvent event = CalEventLocalServiceUtil.getEvent(classPK);
146
147 doReindex(event);
148 }
149
150 protected void doReindex(String[] ids) throws Exception {
151 long companyId = GetterUtil.getLong(ids[0]);
152
153 reindexEvents(companyId);
154 }
155
156 protected String getPortletId(SearchContext searchContext) {
157 return PORTLET_ID;
158 }
159
160 protected void reindexEvents(long companyId) throws Exception {
161 int count = CalEventLocalServiceUtil.getCompanyEventsCount(companyId);
162
163 int pages = count / Indexer.DEFAULT_INTERVAL;
164
165 for (int i = 0; i <= pages; i++) {
166 int start = (i * Indexer.DEFAULT_INTERVAL);
167 int end = start + Indexer.DEFAULT_INTERVAL;
168
169 reindexEvents(companyId, start, end);
170 }
171 }
172
173 protected void reindexEvents(long companyId, int start, int end)
174 throws Exception {
175
176 List<CalEvent> events = CalEventLocalServiceUtil.getCompanyEvents(
177 companyId, start, end);
178
179 if (events.isEmpty()) {
180 return;
181 }
182
183 Collection<Document> documents = new ArrayList<Document>();
184
185 for (CalEvent event : events) {
186 Document document = getDocument(event);
187
188 documents.add(document);
189 }
190
191 SearchEngineUtil.updateDocuments(companyId, documents);
192 }
193
194 }