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.Locale;
034    
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,
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                    ActionableDynamicQuery actionableDynamicQuery =
133                            new CalEventActionableDynamicQuery() {
134    
135                            @Override
136                            protected void performAction(Object object) throws PortalException {
137                                    CalEvent event = (CalEvent)object;
138    
139                                    Document document = getDocument(event);
140    
141                                    addDocument(document);
142                            }
143    
144                    };
145    
146                    actionableDynamicQuery.setCompanyId(companyId);
147                    actionableDynamicQuery.setSearchEngineId(getSearchEngineId());
148    
149                    actionableDynamicQuery.performActions();
150            }
151    
152    }