001    /**
002     * Copyright (c) 2000-2013 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.dao.orm.ActionableDynamicQuery;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.exception.SystemException;
020    import com.liferay.portal.kernel.search.BaseIndexer;
021    import com.liferay.portal.kernel.search.Document;
022    import com.liferay.portal.kernel.search.Field;
023    import com.liferay.portal.kernel.search.SearchContext;
024    import com.liferay.portal.kernel.search.SearchEngineUtil;
025    import com.liferay.portal.kernel.search.Summary;
026    import com.liferay.portal.kernel.util.GetterUtil;
027    import com.liferay.portal.kernel.util.HtmlUtil;
028    import com.liferay.portal.util.PortletKeys;
029    import com.liferay.portlet.calendar.model.CalEvent;
030    import com.liferay.portlet.calendar.service.CalEventLocalServiceUtil;
031    import com.liferay.portlet.calendar.service.persistence.CalEventActionableDynamicQuery;
032    
033    import java.util.ArrayList;
034    import java.util.Collection;
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 CalIndexer() {
049                    setPermissionAware(true);
050            }
051    
052            @Override
053            public String[] getClassNames() {
054                    return CLASS_NAMES;
055            }
056    
057            @Override
058            public String getPortletId() {
059                    return PORTLET_ID;
060            }
061    
062            @Override
063            protected void doDelete(Object obj) throws Exception {
064                    CalEvent event = (CalEvent)obj;
065    
066                    deleteDocument(event.getCompanyId(), event.getEventId());
067            }
068    
069            @Override
070            protected Document doGetDocument(Object obj) throws Exception {
071                    CalEvent event = (CalEvent)obj;
072    
073                    Document document = getBaseModelDocument(PORTLET_ID, event);
074    
075                    document.addText(
076                            Field.DESCRIPTION, HtmlUtil.extractText(event.getDescription()));
077                    document.addText(Field.TITLE, event.getTitle());
078                    document.addKeyword(Field.TYPE, event.getType());
079    
080                    return document;
081            }
082    
083            @Override
084            protected Summary doGetSummary(
085                    Document document, Locale locale, String snippet,
086                    PortletURL portletURL) {
087    
088                    String eventId = document.get(Field.ENTRY_CLASS_PK);
089    
090                    portletURL.setParameter("struts_action", "/calendar/view_event");
091                    portletURL.setParameter("eventId", eventId);
092    
093                    Summary summary = createSummary(
094                            document, Field.TITLE, Field.DESCRIPTION);
095    
096                    summary.setMaxContentLength(200);
097                    summary.setPortletURL(portletURL);
098    
099                    return summary;
100            }
101    
102            @Override
103            protected void doReindex(Object obj) throws Exception {
104                    CalEvent event = (CalEvent)obj;
105    
106                    Document document = getDocument(event);
107    
108                    SearchEngineUtil.updateDocument(
109                            getSearchEngineId(), 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)
132                    throws PortalException, SystemException {
133    
134                    final Collection<Document> documents = new ArrayList<Document>();
135    
136                    ActionableDynamicQuery actionableDynamicQuery =
137                            new CalEventActionableDynamicQuery() {
138    
139                            @Override
140                            protected void performAction(Object object) throws PortalException {
141                                    CalEvent event = (CalEvent)object;
142    
143                                    Document document = getDocument(event);
144    
145                                    documents.add(document);
146                            }
147    
148                    };
149    
150                    actionableDynamicQuery.setCompanyId(companyId);
151    
152                    actionableDynamicQuery.performActions();
153    
154                    SearchEngineUtil.updateDocuments(
155                            getSearchEngineId(), companyId, documents);
156            }
157    
158    }