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