001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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.DocumentImpl;
020    import com.liferay.portal.kernel.search.Field;
021    import com.liferay.portal.kernel.search.Indexer;
022    import com.liferay.portal.kernel.search.SearchContext;
023    import com.liferay.portal.kernel.search.SearchEngineUtil;
024    import com.liferay.portal.kernel.search.Summary;
025    import com.liferay.portal.kernel.util.GetterUtil;
026    import com.liferay.portal.kernel.util.HtmlUtil;
027    import com.liferay.portal.kernel.util.StringUtil;
028    import com.liferay.portal.kernel.util.Validator;
029    import com.liferay.portal.util.PortalUtil;
030    import com.liferay.portal.util.PortletKeys;
031    import com.liferay.portlet.asset.service.AssetCategoryLocalServiceUtil;
032    import com.liferay.portlet.asset.service.AssetTagLocalServiceUtil;
033    import com.liferay.portlet.calendar.model.CalEvent;
034    import com.liferay.portlet.calendar.service.CalEventLocalServiceUtil;
035    import com.liferay.portlet.expando.model.ExpandoBridge;
036    import com.liferay.portlet.expando.util.ExpandoBridgeIndexerUtil;
037    
038    import java.util.ArrayList;
039    import java.util.Collection;
040    import java.util.Date;
041    import java.util.List;
042    
043    import javax.portlet.PortletURL;
044    
045    /**
046     * @author Brett Swaim
047     */
048    public class CalIndexer extends BaseIndexer {
049    
050            public static final String[] CLASS_NAMES = {CalEvent.class.getName()};
051    
052            public static final String PORTLET_ID = PortletKeys.CALENDAR;
053    
054            public String[] getClassNames() {
055                    return CLASS_NAMES;
056            }
057    
058            public Summary getSummary(
059                    Document document, String snippet, PortletURL portletURL) {
060    
061                    String title = document.get(Field.TITLE);
062    
063                    String content = snippet;
064    
065                    if (Validator.isNull(snippet)) {
066                            content = StringUtil.shorten(document.get(Field.DESCRIPTION), 200);
067                    }
068    
069                    String eventId = document.get(Field.ENTRY_CLASS_PK);
070    
071                    portletURL.setParameter("struts_action", "/calendar/view_event");
072                    portletURL.setParameter("eventId", eventId);
073    
074                    return new Summary(title, content, portletURL);
075            }
076    
077            protected void doDelete(Object obj) throws Exception {
078                    CalEvent event = (CalEvent)obj;
079    
080                    Document document = new DocumentImpl();
081    
082                    document.addUID(PORTLET_ID, event.getEventId());
083    
084                    SearchEngineUtil.deleteDocument(
085                            event.getCompanyId(), document.get(Field.UID));
086            }
087    
088            protected Document doGetDocument(Object obj) throws Exception {
089                    CalEvent event = (CalEvent)obj;
090    
091                    long companyId = event.getCompanyId();
092                    long groupId = getParentGroupId(event.getGroupId());
093                    long scopeGroupId = event.getGroupId();
094                    long userId = event.getUserId();
095                    long eventId = event.getEventId();
096                    String userName = PortalUtil.getUserName(userId, event.getUserName());
097                    String title = event.getTitle();
098                    String description = HtmlUtil.extractText(event.getDescription());
099                    Date modifiedDate = event.getModifiedDate();
100    
101                    long[] assetCategoryIds = AssetCategoryLocalServiceUtil.getCategoryIds(
102                            CalEvent.class.getName(), eventId);
103                    String[] assetTagNames = AssetTagLocalServiceUtil.getTagNames(
104                            CalEvent.class.getName(), eventId);
105    
106                    ExpandoBridge expandoBridge = event.getExpandoBridge();
107    
108                    Document document = new DocumentImpl();
109    
110                    document.addUID(PORTLET_ID, eventId);
111    
112                    document.addModifiedDate(modifiedDate);
113    
114                    document.addKeyword(Field.COMPANY_ID, companyId);
115                    document.addKeyword(Field.PORTLET_ID, PORTLET_ID);
116                    document.addKeyword(Field.GROUP_ID, groupId);
117                    document.addKeyword(Field.SCOPE_GROUP_ID, scopeGroupId);
118                    document.addKeyword(Field.USER_ID, userId);
119                    document.addText(Field.USER_NAME, userName);
120    
121                    document.addText(Field.TITLE, title);
122                    document.addText(Field.DESCRIPTION, description);
123                    document.addKeyword(Field.ASSET_CATEGORY_IDS, assetCategoryIds);
124                    document.addKeyword(Field.ASSET_TAG_NAMES, assetTagNames);
125    
126                    document.addKeyword(Field.ENTRY_CLASS_NAME, CalEvent.class.getName());
127                    document.addKeyword(Field.ENTRY_CLASS_PK, eventId);
128    
129                    ExpandoBridgeIndexerUtil.addAttributes(document, expandoBridge);
130    
131                    return document;
132            }
133    
134            protected void doReindex(Object obj) throws Exception {
135                    CalEvent event = (CalEvent)obj;
136    
137                    Document document = getDocument(event);
138    
139                    SearchEngineUtil.updateDocument(event.getCompanyId(), document);
140            }
141    
142            protected void doReindex(String className, long classPK) throws Exception {
143                    CalEvent event = CalEventLocalServiceUtil.getEvent(classPK);
144    
145                    doReindex(event);
146            }
147    
148            protected void doReindex(String[] ids) throws Exception {
149                    long companyId = GetterUtil.getLong(ids[0]);
150    
151                    reindexEvents(companyId);
152            }
153    
154            protected String getPortletId(SearchContext searchContext) {
155                    return PORTLET_ID;
156            }
157    
158            protected void reindexEvents(long companyId) throws Exception {
159                    int count = CalEventLocalServiceUtil.getCompanyEventsCount(companyId);
160    
161                    int pages = count / Indexer.DEFAULT_INTERVAL;
162    
163                    for (int i = 0; i <= pages; i++) {
164                            int start = (i * Indexer.DEFAULT_INTERVAL);
165                            int end = start + Indexer.DEFAULT_INTERVAL;
166    
167                            reindexEvents(companyId, start, end);
168                    }
169            }
170    
171            protected void reindexEvents(long companyId, int start, int end)
172                    throws Exception {
173    
174                    List<CalEvent> events = CalEventLocalServiceUtil.getCompanyEvents(
175                            companyId, start, end);
176    
177                    if (events.isEmpty()) {
178                            return;
179                    }
180    
181                    Collection<Document> documents = new ArrayList<Document>();
182    
183                    for (CalEvent event : events) {
184                            Document document = getDocument(event);
185    
186                            documents.add(document);
187                    }
188    
189                    SearchEngineUtil.updateDocuments(companyId, documents);
190            }
191    
192    }