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.search.BaseIndexer;
020    import com.liferay.portal.kernel.search.Document;
021    import com.liferay.portal.kernel.search.Field;
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.util.PortletKeys;
028    import com.liferay.portlet.calendar.model.CalEvent;
029    import com.liferay.portlet.calendar.service.CalEventLocalServiceUtil;
030    
031    import java.util.Locale;
032    
033    import javax.portlet.PortletRequest;
034    import javax.portlet.PortletResponse;
035    import javax.portlet.PortletURL;
036    
037    /**
038     * @author Brett Swaim
039     */
040    public class CalIndexer extends BaseIndexer {
041    
042            public static final String[] CLASS_NAMES = {CalEvent.class.getName()};
043    
044            public static final String PORTLET_ID = PortletKeys.CALENDAR;
045    
046            public CalIndexer() {
047                    setPermissionAware(true);
048            }
049    
050            @Override
051            public String[] getClassNames() {
052                    return CLASS_NAMES;
053            }
054    
055            @Override
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, PortletURL portletURL,
084                    PortletRequest portletRequest, PortletResponse portletResponse) {
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                            isCommitImmediately());
109            }
110    
111            @Override
112            protected void doReindex(String className, long classPK) throws Exception {
113                    CalEvent event = CalEventLocalServiceUtil.getEvent(classPK);
114    
115                    doReindex(event);
116            }
117    
118            @Override
119            protected void doReindex(String[] ids) throws Exception {
120                    long companyId = GetterUtil.getLong(ids[0]);
121    
122                    reindexEvents(companyId);
123            }
124    
125            @Override
126            protected String getPortletId(SearchContext searchContext) {
127                    return PORTLET_ID;
128            }
129    
130            protected void reindexEvents(long companyId) throws PortalException {
131                    final ActionableDynamicQuery actionableDynamicQuery =
132                            CalEventLocalServiceUtil.getActionableDynamicQuery();
133    
134                    actionableDynamicQuery.setCompanyId(companyId);
135                    actionableDynamicQuery.setPerformActionMethod(
136                            new ActionableDynamicQuery.PerformActionMethod() {
137    
138                                    @Override
139                                    public void performAction(Object object)
140                                            throws PortalException {
141    
142                                            CalEvent event = (CalEvent)object;
143    
144                                            Document document = getDocument(event);
145    
146                                            actionableDynamicQuery.addDocument(document);
147                                    }
148    
149                            });
150                    actionableDynamicQuery.setSearchEngineId(getSearchEngineId());
151    
152                    actionableDynamicQuery.performActions();
153            }
154    
155    }