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.blogs.util;
016    
017    import com.liferay.portal.kernel.dao.orm.ActionableDynamicQuery;
018    import com.liferay.portal.kernel.dao.orm.DynamicQuery;
019    import com.liferay.portal.kernel.dao.orm.Property;
020    import com.liferay.portal.kernel.dao.orm.PropertyFactoryUtil;
021    import com.liferay.portal.kernel.exception.PortalException;
022    import com.liferay.portal.kernel.log.Log;
023    import com.liferay.portal.kernel.log.LogFactoryUtil;
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.search.filter.BooleanFilter;
031    import com.liferay.portal.kernel.spring.osgi.OSGiBeanProperties;
032    import com.liferay.portal.kernel.util.GetterUtil;
033    import com.liferay.portal.kernel.util.HtmlUtil;
034    import com.liferay.portal.kernel.workflow.WorkflowConstants;
035    import com.liferay.portal.security.permission.ActionKeys;
036    import com.liferay.portal.security.permission.PermissionChecker;
037    import com.liferay.portlet.blogs.model.BlogsEntry;
038    import com.liferay.portlet.blogs.service.BlogsEntryLocalServiceUtil;
039    import com.liferay.portlet.blogs.service.permission.BlogsEntryPermission;
040    
041    import java.util.Date;
042    import java.util.Locale;
043    
044    import javax.portlet.PortletRequest;
045    import javax.portlet.PortletResponse;
046    
047    /**
048     * @author Brian Wing Shun Chan
049     * @author Harry Mark
050     * @author Bruno Farache
051     * @author Raymond Aug??
052     */
053    @OSGiBeanProperties
054    public class BlogsIndexer extends BaseIndexer {
055    
056            public static final String CLASS_NAME = BlogsEntry.class.getName();
057    
058            public BlogsIndexer() {
059                    setDefaultSelectedFieldNames(
060                            Field.ASSET_TAG_NAMES, Field.COMPANY_ID, Field.CONTENT,
061                            Field.ENTRY_CLASS_NAME, Field.ENTRY_CLASS_PK, Field.GROUP_ID,
062                            Field.MODIFIED_DATE, Field.SCOPE_GROUP_ID, Field.TITLE, Field.UID);
063                    setFilterSearch(true);
064                    setPermissionAware(true);
065            }
066    
067            @Override
068            public String getClassName() {
069                    return CLASS_NAME;
070            }
071    
072            @Override
073            public boolean hasPermission(
074                            PermissionChecker permissionChecker, String entryClassName,
075                            long entryClassPK, String actionId)
076                    throws Exception {
077    
078                    return BlogsEntryPermission.contains(
079                            permissionChecker, entryClassPK, ActionKeys.VIEW);
080            }
081    
082            @Override
083            public boolean isVisible(long classPK, int status) throws Exception {
084                    BlogsEntry entry = BlogsEntryLocalServiceUtil.getEntry(classPK);
085    
086                    return isVisible(entry.getStatus(), status);
087            }
088    
089            @Override
090            public void postProcessContextBooleanFilter(
091                            BooleanFilter contextBooleanFilter, SearchContext searchContext)
092                    throws Exception {
093    
094                    addStatus(contextBooleanFilter, searchContext);
095            }
096    
097            @Override
098            protected void doDelete(Object obj) throws Exception {
099                    BlogsEntry entry = (BlogsEntry)obj;
100    
101                    deleteDocument(entry.getCompanyId(), entry.getEntryId());
102            }
103    
104            @Override
105            protected Document doGetDocument(Object obj) throws Exception {
106                    BlogsEntry entry = (BlogsEntry)obj;
107    
108                    Document document = getBaseModelDocument(CLASS_NAME, entry);
109    
110                    document.addText(Field.CAPTION, entry.getCoverImageCaption());
111                    document.addText(
112                            Field.CONTENT, HtmlUtil.extractText(entry.getContent()));
113                    document.addText(Field.DESCRIPTION, entry.getDescription());
114                    document.addDate(Field.MODIFIED_DATE, entry.getModifiedDate());
115                    document.addText(Field.SUBTITLE, entry.getSubtitle());
116                    document.addText(Field.TITLE, entry.getTitle());
117    
118                    return document;
119            }
120    
121            @Override
122            protected Summary doGetSummary(
123                    Document document, Locale locale, String snippet,
124                    PortletRequest portletRequest, PortletResponse portletResponse) {
125    
126                    Summary summary = createSummary(document);
127    
128                    summary.setMaxContentLength(200);
129    
130                    return summary;
131            }
132    
133            @Override
134            protected void doReindex(Object obj) throws Exception {
135                    BlogsEntry entry = (BlogsEntry)obj;
136    
137                    Document document = getDocument(entry);
138    
139                    SearchEngineUtil.updateDocument(
140                            getSearchEngineId(), entry.getCompanyId(), document,
141                            isCommitImmediately());
142            }
143    
144            @Override
145            protected void doReindex(String className, long classPK) throws Exception {
146                    BlogsEntry entry = BlogsEntryLocalServiceUtil.getEntry(classPK);
147    
148                    doReindex(entry);
149            }
150    
151            @Override
152            protected void doReindex(String[] ids) throws Exception {
153                    long companyId = GetterUtil.getLong(ids[0]);
154    
155                    reindexEntries(companyId);
156            }
157    
158            protected void reindexEntries(long companyId) throws PortalException {
159                    final ActionableDynamicQuery actionableDynamicQuery =
160                            BlogsEntryLocalServiceUtil.getActionableDynamicQuery();
161    
162                    actionableDynamicQuery.setAddCriteriaMethod(
163                            new ActionableDynamicQuery.AddCriteriaMethod() {
164    
165                                    @Override
166                                    public void addCriteria(DynamicQuery dynamicQuery) {
167                                            Property displayDateProperty = PropertyFactoryUtil.forName(
168                                                    "displayDate");
169    
170                                            dynamicQuery.add(displayDateProperty.lt(new Date()));
171    
172                                            Property statusProperty = PropertyFactoryUtil.forName(
173                                                    "status");
174    
175                                            Integer[] statuses = {
176                                                    WorkflowConstants.STATUS_APPROVED,
177                                                    WorkflowConstants.STATUS_IN_TRASH
178                                            };
179    
180                                            dynamicQuery.add(statusProperty.in(statuses));
181                                    }
182    
183                            });
184                    actionableDynamicQuery.setCompanyId(companyId);
185                    actionableDynamicQuery.setPerformActionMethod(
186                            new ActionableDynamicQuery.PerformActionMethod() {
187    
188                                    @Override
189                                    public void performAction(Object object) {
190                                            BlogsEntry entry = (BlogsEntry)object;
191    
192                                            try {
193                                                    Document document = getDocument(entry);
194    
195                                                    actionableDynamicQuery.addDocument(document);
196                                            }
197                                            catch (PortalException pe) {
198                                                    if (_log.isWarnEnabled()) {
199                                                            _log.warn(
200                                                                    "Unable to index blogs entry " +
201                                                                            entry.getEntryId(),
202                                                                    pe);
203                                                    }
204                                            }
205                                    }
206    
207                            });
208                    actionableDynamicQuery.setSearchEngineId(getSearchEngineId());
209    
210                    actionableDynamicQuery.performActions();
211            }
212    
213            private static final Log _log = LogFactoryUtil.getLog(BlogsIndexer.class);
214    
215    }