001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portlet.calendar.util;
016    
017    import com.liferay.portal.kernel.dao.orm.DynamicQuery;
018    import com.liferay.portal.kernel.dao.orm.DynamicQueryFactoryUtil;
019    import com.liferay.portal.kernel.dao.orm.Projection;
020    import com.liferay.portal.kernel.dao.orm.ProjectionFactoryUtil;
021    import com.liferay.portal.kernel.dao.orm.ProjectionList;
022    import com.liferay.portal.kernel.dao.orm.Property;
023    import com.liferay.portal.kernel.dao.orm.PropertyFactoryUtil;
024    import com.liferay.portal.kernel.search.BaseIndexer;
025    import com.liferay.portal.kernel.search.Document;
026    import com.liferay.portal.kernel.search.Field;
027    import com.liferay.portal.kernel.search.SearchContext;
028    import com.liferay.portal.kernel.search.SearchEngineUtil;
029    import com.liferay.portal.kernel.search.Summary;
030    import com.liferay.portal.kernel.util.GetterUtil;
031    import com.liferay.portal.kernel.util.HtmlUtil;
032    import com.liferay.portal.kernel.util.StringUtil;
033    import com.liferay.portal.kernel.util.Validator;
034    import com.liferay.portal.security.pacl.PACLClassLoaderUtil;
035    import com.liferay.portal.util.PortletKeys;
036    import com.liferay.portlet.calendar.model.CalEvent;
037    import com.liferay.portlet.calendar.service.CalEventLocalServiceUtil;
038    
039    import java.util.ArrayList;
040    import java.util.Collection;
041    import java.util.List;
042    import java.util.Locale;
043    
044    import javax.portlet.PortletURL;
045    
046    /**
047     * @author Brett Swaim
048     */
049    public class CalIndexer extends BaseIndexer {
050    
051            public static final String[] CLASS_NAMES = {CalEvent.class.getName()};
052    
053            public static final String PORTLET_ID = PortletKeys.CALENDAR;
054    
055            public String[] getClassNames() {
056                    return CLASS_NAMES;
057            }
058    
059            public String getPortletId() {
060                    return PORTLET_ID;
061            }
062    
063            @Override
064            public boolean isPermissionAware() {
065                    return _PERMISSION_AWARE;
066            }
067    
068            protected void addReindexCriteria(
069                    DynamicQuery dynamicQuery, long companyId) {
070    
071                    Property property = PropertyFactoryUtil.forName("companyId");
072    
073                    dynamicQuery.add(property.eq(companyId));
074            }
075    
076            @Override
077            protected void doDelete(Object obj) throws Exception {
078                    CalEvent event = (CalEvent)obj;
079    
080                    deleteDocument(event.getCompanyId(), event.getEventId());
081            }
082    
083            @Override
084            protected Document doGetDocument(Object obj) throws Exception {
085                    CalEvent event = (CalEvent)obj;
086    
087                    Document document = getBaseModelDocument(PORTLET_ID, event);
088    
089                    document.addText(
090                            Field.DESCRIPTION, HtmlUtil.extractText(event.getDescription()));
091                    document.addText(Field.TITLE, event.getTitle());
092                    document.addKeyword(Field.TYPE, event.getType());
093    
094                    return document;
095            }
096    
097            @Override
098            protected Summary doGetSummary(
099                    Document document, Locale locale, String snippet,
100                    PortletURL portletURL) {
101    
102                    String title = document.get(Field.TITLE);
103    
104                    String content = snippet;
105    
106                    if (Validator.isNull(snippet)) {
107                            content = StringUtil.shorten(document.get(Field.DESCRIPTION), 200);
108                    }
109    
110                    String eventId = document.get(Field.ENTRY_CLASS_PK);
111    
112                    portletURL.setParameter("struts_action", "/calendar/view_event");
113                    portletURL.setParameter("eventId", eventId);
114    
115                    return new Summary(title, content, portletURL);
116            }
117    
118            @Override
119            protected void doReindex(Object obj) throws Exception {
120                    CalEvent event = (CalEvent)obj;
121    
122                    Document document = getDocument(event);
123    
124                    SearchEngineUtil.updateDocument(
125                            getSearchEngineId(), event.getCompanyId(), document);
126            }
127    
128            @Override
129            protected void doReindex(String className, long classPK) throws Exception {
130                    CalEvent event = CalEventLocalServiceUtil.getEvent(classPK);
131    
132                    doReindex(event);
133            }
134    
135            @Override
136            protected void doReindex(String[] ids) throws Exception {
137                    long companyId = GetterUtil.getLong(ids[0]);
138    
139                    reindexEvents(companyId);
140            }
141    
142            @Override
143            protected String getPortletId(SearchContext searchContext) {
144                    return PORTLET_ID;
145            }
146    
147            protected void reindexEvents(long companyId) throws Exception {
148                    DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(
149                            CalEvent.class, PACLClassLoaderUtil.getPortalClassLoader());
150    
151                    Projection minEventIdProjection = ProjectionFactoryUtil.min("eventId");
152                    Projection maxEventIdProjection = ProjectionFactoryUtil.max("eventId");
153    
154                    ProjectionList projectionList = ProjectionFactoryUtil.projectionList();
155    
156                    projectionList.add(minEventIdProjection);
157                    projectionList.add(maxEventIdProjection);
158    
159                    dynamicQuery.setProjection(projectionList);
160    
161                    addReindexCriteria(dynamicQuery, companyId);
162    
163                    List<Object[]> results = CalEventLocalServiceUtil.dynamicQuery(
164                            dynamicQuery);
165    
166                    Object[] minAndMaxEventIds = results.get(0);
167    
168                    if ((minAndMaxEventIds[0] == null) || (minAndMaxEventIds[1] == null)) {
169                            return;
170                    }
171    
172                    long minEventId = (Long)minAndMaxEventIds[0];
173                    long maxEventId = (Long)minAndMaxEventIds[1];
174    
175                    long startEventId = minEventId;
176                    long endEventId = startEventId + DEFAULT_INTERVAL;
177    
178                    while (startEventId <= maxEventId) {
179                            reindexEvents(companyId, startEventId, endEventId);
180    
181                            startEventId = endEventId;
182                            endEventId += DEFAULT_INTERVAL;
183                    }
184            }
185    
186            protected void reindexEvents(
187                            long companyId, long startEventId, long endEventId)
188                    throws Exception {
189    
190                    DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(
191                            CalEvent.class, PACLClassLoaderUtil.getPortalClassLoader());
192    
193                    Property property = PropertyFactoryUtil.forName("eventId");
194    
195                    dynamicQuery.add(property.ge(startEventId));
196                    dynamicQuery.add(property.lt(endEventId));
197    
198                    addReindexCriteria(dynamicQuery, companyId);
199    
200                    List<CalEvent> events = CalEventLocalServiceUtil.dynamicQuery(
201                            dynamicQuery);
202    
203                    if (events.isEmpty()) {
204                            return;
205                    }
206    
207                    Collection<Document> documents = new ArrayList<Document>(events.size());
208    
209                    for (CalEvent event : events) {
210                            Document document = getDocument(event);
211    
212                            documents.add(document);
213                    }
214    
215                    SearchEngineUtil.updateDocuments(
216                            getSearchEngineId(), companyId, documents);
217            }
218    
219            private static final boolean _PERMISSION_AWARE = true;
220    
221    }