001    /**
002     * Copyright (c) 2000-present 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.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.search.BaseIndexer;
022    import com.liferay.portal.kernel.search.Document;
023    import com.liferay.portal.kernel.search.Field;
024    import com.liferay.portal.kernel.search.SearchEngineUtil;
025    import com.liferay.portal.kernel.search.Summary;
026    import com.liferay.portal.kernel.spring.osgi.OSGiBeanProperties;
027    import com.liferay.portal.kernel.util.GetterUtil;
028    import com.liferay.portal.kernel.util.HtmlUtil;
029    import com.liferay.portlet.calendar.model.CalEvent;
030    import com.liferay.portlet.calendar.service.CalEventLocalServiceUtil;
031    
032    import java.util.Locale;
033    
034    import javax.portlet.PortletRequest;
035    import javax.portlet.PortletResponse;
036    
037    /**
038     * @author Brett Swaim
039     */
040    @OSGiBeanProperties
041    public class CalIndexer extends BaseIndexer {
042    
043            public static final String CLASS_NAME = CalEvent.class.getName();
044    
045            public CalIndexer() {
046                    setPermissionAware(true);
047            }
048    
049            @Override
050            public String getClassName() {
051                    return CLASS_NAME;
052            }
053    
054            @Override
055            protected void doDelete(Object obj) throws Exception {
056                    CalEvent event = (CalEvent)obj;
057    
058                    deleteDocument(event.getCompanyId(), event.getEventId());
059            }
060    
061            @Override
062            protected Document doGetDocument(Object obj) throws Exception {
063                    CalEvent event = (CalEvent)obj;
064    
065                    Document document = getBaseModelDocument(CLASS_NAME, event);
066    
067                    document.addText(
068                            Field.DESCRIPTION, HtmlUtil.extractText(event.getDescription()));
069                    document.addText(Field.TITLE, event.getTitle());
070                    document.addKeyword(Field.TYPE, event.getType());
071    
072                    return document;
073            }
074    
075            @Override
076            protected Summary doGetSummary(
077                    Document document, Locale locale, String snippet,
078                    PortletRequest portletRequest, PortletResponse portletResponse) {
079    
080                    Summary summary = createSummary(
081                            document, Field.TITLE, Field.DESCRIPTION);
082    
083                    summary.setMaxContentLength(200);
084    
085                    return summary;
086            }
087    
088            @Override
089            protected void doReindex(Object obj) throws Exception {
090                    CalEvent event = (CalEvent)obj;
091    
092                    Document document = getDocument(event);
093    
094                    SearchEngineUtil.updateDocument(
095                            getSearchEngineId(), event.getCompanyId(), document,
096                            isCommitImmediately());
097            }
098    
099            @Override
100            protected void doReindex(String className, long classPK) throws Exception {
101                    CalEvent event = CalEventLocalServiceUtil.getEvent(classPK);
102    
103                    doReindex(event);
104            }
105    
106            @Override
107            protected void doReindex(String[] ids) throws Exception {
108                    long companyId = GetterUtil.getLong(ids[0]);
109    
110                    reindexEvents(companyId);
111            }
112    
113            protected void reindexEvents(long companyId) throws PortalException {
114                    final ActionableDynamicQuery actionableDynamicQuery =
115                            CalEventLocalServiceUtil.getActionableDynamicQuery();
116    
117                    actionableDynamicQuery.setCompanyId(companyId);
118                    actionableDynamicQuery.setPerformActionMethod(
119                            new ActionableDynamicQuery.PerformActionMethod() {
120    
121                                    @Override
122                                    public void performAction(Object object) {
123                                            CalEvent event = (CalEvent)object;
124    
125                                            try {
126                                                    Document document = getDocument(event);
127    
128                                                    actionableDynamicQuery.addDocument(document);
129                                            }
130                                            catch (PortalException pe) {
131                                                    if (_log.isWarnEnabled()) {
132                                                            _log.warn(
133                                                                    "Unable to index calendar event " +
134                                                                            event.getEventId(),
135                                                                    pe);
136                                                    }
137                                            }
138                                    }
139    
140                            });
141                    actionableDynamicQuery.setSearchEngineId(getSearchEngineId());
142    
143                    actionableDynamicQuery.performActions();
144            }
145    
146            private static final Log _log = LogFactoryUtil.getLog(CalIndexer.class);
147    
148    }