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.kernel.util.StringUtil;
029    import com.liferay.portal.kernel.util.Validator;
030    import com.liferay.portal.util.PortletKeys;
031    import com.liferay.portlet.calendar.model.CalEvent;
032    import com.liferay.portlet.calendar.service.CalEventLocalServiceUtil;
033    import com.liferay.portlet.calendar.service.persistence.CalEventActionableDynamicQuery;
034    
035    import java.util.ArrayList;
036    import java.util.Collection;
037    import java.util.Locale;
038    
039    import javax.portlet.PortletURL;
040    
041    /**
042     * @author Brett Swaim
043     */
044    public class CalIndexer extends BaseIndexer {
045    
046            public static final String[] CLASS_NAMES = {CalEvent.class.getName()};
047    
048            public static final String PORTLET_ID = PortletKeys.CALENDAR;
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            public boolean isPermissionAware() {
062                    return _PERMISSION_AWARE;
063            }
064    
065            @Override
066            protected void doDelete(Object obj) throws Exception {
067                    CalEvent event = (CalEvent)obj;
068    
069                    deleteDocument(event.getCompanyId(), event.getEventId());
070            }
071    
072            @Override
073            protected Document doGetDocument(Object obj) throws Exception {
074                    CalEvent event = (CalEvent)obj;
075    
076                    Document document = getBaseModelDocument(PORTLET_ID, event);
077    
078                    document.addText(
079                            Field.DESCRIPTION, HtmlUtil.extractText(event.getDescription()));
080                    document.addText(Field.TITLE, event.getTitle());
081                    document.addKeyword(Field.TYPE, event.getType());
082    
083                    return document;
084            }
085    
086            @Override
087            protected Summary doGetSummary(
088                    Document document, Locale locale, String snippet,
089                    PortletURL portletURL) {
090    
091                    String title = document.get(Field.TITLE);
092    
093                    String content = snippet;
094    
095                    if (Validator.isNull(snippet)) {
096                            content = StringUtil.shorten(document.get(Field.DESCRIPTION), 200);
097                    }
098    
099                    String eventId = document.get(Field.ENTRY_CLASS_PK);
100    
101                    portletURL.setParameter("struts_action", "/calendar/view_event");
102                    portletURL.setParameter("eventId", eventId);
103    
104                    return new Summary(title, content, portletURL);
105            }
106    
107            @Override
108            protected void doReindex(Object obj) throws Exception {
109                    CalEvent event = (CalEvent)obj;
110    
111                    Document document = getDocument(event);
112    
113                    SearchEngineUtil.updateDocument(
114                            getSearchEngineId(), event.getCompanyId(), document);
115            }
116    
117            @Override
118            protected void doReindex(String className, long classPK) throws Exception {
119                    CalEvent event = CalEventLocalServiceUtil.getEvent(classPK);
120    
121                    doReindex(event);
122            }
123    
124            @Override
125            protected void doReindex(String[] ids) throws Exception {
126                    long companyId = GetterUtil.getLong(ids[0]);
127    
128                    reindexEvents(companyId);
129            }
130    
131            @Override
132            protected String getPortletId(SearchContext searchContext) {
133                    return PORTLET_ID;
134            }
135    
136            protected void reindexEvents(long companyId)
137                    throws PortalException, SystemException {
138    
139                    final Collection<Document> documents = new ArrayList<Document>();
140    
141                    ActionableDynamicQuery actionableDynamicQuery =
142                            new CalEventActionableDynamicQuery() {
143    
144                            @Override
145                            protected void performAction(Object object) throws PortalException {
146                                    CalEvent event = (CalEvent)object;
147    
148                                    Document document = getDocument(event);
149    
150                                    documents.add(document);
151                            }
152    
153                    };
154    
155                    actionableDynamicQuery.setCompanyId(companyId);
156    
157                    actionableDynamicQuery.performActions();
158    
159                    SearchEngineUtil.updateDocuments(
160                            getSearchEngineId(), companyId, documents);
161            }
162    
163            private static final boolean _PERMISSION_AWARE = true;
164    
165    }