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            public String[] getClassNames() {
053                    return CLASS_NAMES;
054            }
055    
056            public String getPortletId() {
057                    return PORTLET_ID;
058            }
059    
060            @Override
061            protected void doDelete(Object obj) throws Exception {
062                    CalEvent event = (CalEvent)obj;
063    
064                    deleteDocument(event.getCompanyId(), event.getEventId());
065            }
066    
067            @Override
068            protected Document doGetDocument(Object obj) throws Exception {
069                    CalEvent event = (CalEvent)obj;
070    
071                    Document document = getBaseModelDocument(PORTLET_ID, event);
072    
073                    document.addText(
074                            Field.DESCRIPTION, HtmlUtil.extractText(event.getDescription()));
075                    document.addText(Field.TITLE, event.getTitle());
076                    document.addKeyword(Field.TYPE, event.getType());
077    
078                    return document;
079            }
080    
081            @Override
082            protected Summary doGetSummary(
083                    Document document, Locale locale, String snippet,
084                    PortletURL portletURL) {
085    
086                    String eventId = document.get(Field.ENTRY_CLASS_PK);
087    
088                    portletURL.setParameter("struts_action", "/calendar/view_event");
089                    portletURL.setParameter("eventId", eventId);
090    
091                    Summary summary = createSummary(
092                            document, Field.TITLE, Field.DESCRIPTION);
093    
094                    summary.setMaxContentLength(200);
095                    summary.setPortletURL(portletURL);
096    
097                    return summary;
098            }
099    
100            @Override
101            protected void doReindex(Object obj) throws Exception {
102                    CalEvent event = (CalEvent)obj;
103    
104                    Document document = getDocument(event);
105    
106                    SearchEngineUtil.updateDocument(
107                            getSearchEngineId(), event.getCompanyId(), document);
108            }
109    
110            @Override
111            protected void doReindex(String className, long classPK) throws Exception {
112                    CalEvent event = CalEventLocalServiceUtil.getEvent(classPK);
113    
114                    doReindex(event);
115            }
116    
117            @Override
118            protected void doReindex(String[] ids) throws Exception {
119                    long companyId = GetterUtil.getLong(ids[0]);
120    
121                    reindexEvents(companyId);
122            }
123    
124            @Override
125            protected String getPortletId(SearchContext searchContext) {
126                    return PORTLET_ID;
127            }
128    
129            protected void reindexEvents(long companyId)
130                    throws PortalException, SystemException {
131    
132                    final Collection<Document> documents = new ArrayList<Document>();
133    
134                    ActionableDynamicQuery actionableDynamicQuery =
135                            new CalEventActionableDynamicQuery() {
136    
137                            @Override
138                            protected void performAction(Object object) throws PortalException {
139                                    CalEvent event = (CalEvent)object;
140    
141                                    Document document = getDocument(event);
142    
143                                    documents.add(document);
144                            }
145    
146                    };
147    
148                    actionableDynamicQuery.setCompanyId(companyId);
149    
150                    actionableDynamicQuery.performActions();
151    
152                    SearchEngineUtil.updateDocuments(
153                            getSearchEngineId(), companyId, documents);
154            }
155    
156    }