001
014
015 package com.liferay.portlet.calendar.util;
016
017 import com.liferay.portal.kernel.search.BaseIndexer;
018 import com.liferay.portal.kernel.search.Document;
019 import com.liferay.portal.kernel.search.Field;
020 import com.liferay.portal.kernel.search.Indexer;
021 import com.liferay.portal.kernel.search.SearchContext;
022 import com.liferay.portal.kernel.search.SearchEngineUtil;
023 import com.liferay.portal.kernel.search.Summary;
024 import com.liferay.portal.kernel.util.GetterUtil;
025 import com.liferay.portal.kernel.util.HtmlUtil;
026 import com.liferay.portal.kernel.util.StringUtil;
027 import com.liferay.portal.kernel.util.Validator;
028 import com.liferay.portal.util.PortletKeys;
029 import com.liferay.portlet.calendar.model.CalEvent;
030 import com.liferay.portlet.calendar.service.CalEventLocalServiceUtil;
031
032 import java.util.ArrayList;
033 import java.util.Collection;
034 import java.util.List;
035 import java.util.Locale;
036
037 import javax.portlet.PortletURL;
038
039
042 public class CalIndexer extends BaseIndexer {
043
044 public static final String[] CLASS_NAMES = {CalEvent.class.getName()};
045
046 public static final String PORTLET_ID = PortletKeys.CALENDAR;
047
048 public String[] getClassNames() {
049 return CLASS_NAMES;
050 }
051
052 public String getPortletId() {
053 return PORTLET_ID;
054 }
055
056 @Override
057 protected void doDelete(Object obj) throws Exception {
058 CalEvent event = (CalEvent)obj;
059
060 deleteDocument(event.getCompanyId(), event.getEventId());
061 }
062
063 @Override
064 protected Document doGetDocument(Object obj) throws Exception {
065 CalEvent event = (CalEvent)obj;
066
067 Document document = getBaseModelDocument(PORTLET_ID, event);
068
069 document.addText(
070 Field.DESCRIPTION, HtmlUtil.extractText(event.getDescription()));
071 document.addText(Field.TITLE, event.getTitle());
072 document.addKeyword(Field.TYPE, event.getType());
073
074 return document;
075 }
076
077 @Override
078 protected Summary doGetSummary(
079 Document document, Locale locale, String snippet,
080 PortletURL portletURL) {
081
082 String title = document.get(Field.TITLE);
083
084 String content = snippet;
085
086 if (Validator.isNull(snippet)) {
087 content = StringUtil.shorten(document.get(Field.DESCRIPTION), 200);
088 }
089
090 String eventId = document.get(Field.ENTRY_CLASS_PK);
091
092 portletURL.setParameter("struts_action", "/calendar/view_event");
093 portletURL.setParameter("eventId", eventId);
094
095 return new Summary(title, content, portletURL);
096 }
097
098 @Override
099 protected void doReindex(Object obj) throws Exception {
100 CalEvent event = (CalEvent)obj;
101
102 Document document = getDocument(event);
103
104 SearchEngineUtil.updateDocument(event.getCompanyId(), document);
105 }
106
107 @Override
108 protected void doReindex(String className, long classPK) throws Exception {
109 CalEvent event = CalEventLocalServiceUtil.getEvent(classPK);
110
111 doReindex(event);
112 }
113
114 @Override
115 protected void doReindex(String[] ids) throws Exception {
116 long companyId = GetterUtil.getLong(ids[0]);
117
118 reindexEvents(companyId);
119 }
120
121 @Override
122 protected String getPortletId(SearchContext searchContext) {
123 return PORTLET_ID;
124 }
125
126 protected void reindexEvents(long companyId) throws Exception {
127 int count = CalEventLocalServiceUtil.getCompanyEventsCount(companyId);
128
129 int pages = count / Indexer.DEFAULT_INTERVAL;
130
131 for (int i = 0; i <= pages; i++) {
132 int start = (i * Indexer.DEFAULT_INTERVAL);
133 int end = start + Indexer.DEFAULT_INTERVAL;
134
135 reindexEvents(companyId, start, end);
136 }
137 }
138
139 protected void reindexEvents(long companyId, int start, int end)
140 throws Exception {
141
142 List<CalEvent> events = CalEventLocalServiceUtil.getCompanyEvents(
143 companyId, start, end);
144
145 if (events.isEmpty()) {
146 return;
147 }
148
149 Collection<Document> documents = new ArrayList<Document>();
150
151 for (CalEvent event : events) {
152 Document document = getDocument(event);
153
154 documents.add(document);
155 }
156
157 SearchEngineUtil.updateDocuments(companyId, documents);
158 }
159
160 }