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