001    /**
002     * Copyright (c) 2000-2012 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.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    /**
040     * @author Brett Swaim
041     */
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            public boolean isPermissionAware() {
058                    return _PERMISSION_AWARE;
059            }
060    
061            @Override
062            protected void doDelete(Object obj) throws Exception {
063                    CalEvent event = (CalEvent)obj;
064    
065                    deleteDocument(event.getCompanyId(), event.getEventId());
066            }
067    
068            @Override
069            protected Document doGetDocument(Object obj) throws Exception {
070                    CalEvent event = (CalEvent)obj;
071    
072                    Document document = getBaseModelDocument(PORTLET_ID, event);
073    
074                    document.addText(
075                            Field.DESCRIPTION, HtmlUtil.extractText(event.getDescription()));
076                    document.addText(Field.TITLE, event.getTitle());
077                    document.addKeyword(Field.TYPE, event.getType());
078    
079                    return document;
080            }
081    
082            @Override
083            protected Summary doGetSummary(
084                    Document document, Locale locale, String snippet,
085                    PortletURL portletURL) {
086    
087                    String title = document.get(Field.TITLE);
088    
089                    String content = snippet;
090    
091                    if (Validator.isNull(snippet)) {
092                            content = StringUtil.shorten(document.get(Field.DESCRIPTION), 200);
093                    }
094    
095                    String eventId = document.get(Field.ENTRY_CLASS_PK);
096    
097                    portletURL.setParameter("struts_action", "/calendar/view_event");
098                    portletURL.setParameter("eventId", eventId);
099    
100                    return new Summary(title, content, portletURL);
101            }
102    
103            @Override
104            protected void doReindex(Object obj) throws Exception {
105                    CalEvent event = (CalEvent)obj;
106    
107                    Document document = getDocument(event);
108    
109                    SearchEngineUtil.updateDocument(event.getCompanyId(), document);
110            }
111    
112            @Override
113            protected void doReindex(String className, long classPK) throws Exception {
114                    CalEvent event = CalEventLocalServiceUtil.getEvent(classPK);
115    
116                    doReindex(event);
117            }
118    
119            @Override
120            protected void doReindex(String[] ids) throws Exception {
121                    long companyId = GetterUtil.getLong(ids[0]);
122    
123                    reindexEvents(companyId);
124            }
125    
126            @Override
127            protected String getPortletId(SearchContext searchContext) {
128                    return PORTLET_ID;
129            }
130    
131            protected void reindexEvents(long companyId) throws Exception {
132                    int count = CalEventLocalServiceUtil.getCompanyEventsCount(companyId);
133    
134                    int pages = count / Indexer.DEFAULT_INTERVAL;
135    
136                    for (int i = 0; i <= pages; i++) {
137                            int start = (i * Indexer.DEFAULT_INTERVAL);
138                            int end = start + Indexer.DEFAULT_INTERVAL;
139    
140                            reindexEvents(companyId, start, end);
141                    }
142            }
143    
144            protected void reindexEvents(long companyId, int start, int end)
145                    throws Exception {
146    
147                    List<CalEvent> events = CalEventLocalServiceUtil.getCompanyEvents(
148                            companyId, start, end);
149    
150                    if (events.isEmpty()) {
151                            return;
152                    }
153    
154                    Collection<Document> documents = new ArrayList<Document>();
155    
156                    for (CalEvent event : events) {
157                            Document document = getDocument(event);
158    
159                            documents.add(document);
160                    }
161    
162                    SearchEngineUtil.updateDocuments(companyId, documents);
163            }
164    
165            private static final boolean _PERMISSION_AWARE = true;
166    
167    }