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            @Override
070            public String[] getClassNames() {
071                    return CLASS_NAMES;
072            }
073    
074            @Override
075            public String getPortletId() {
076                    return PORTLET_ID;
077            }
078    
079            @Override
080            public boolean hasPermission(
081                            PermissionChecker permissionChecker, String entryClassName,
082                            long entryClassPK, String actionId)
083                    throws Exception {
084    
085                    return BlogsEntryPermission.contains(
086                            permissionChecker, entryClassPK, ActionKeys.VIEW);
087            }
088    
089            @Override
090            public void postProcessContextQuery(
091                            BooleanQuery contextQuery, SearchContext searchContext)
092                    throws Exception {
093    
094                    addStatus(contextQuery, 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(PORTLET_ID, entry);
109    
110                    document.addText(
111                            Field.CONTENT, HtmlUtil.extractText(entry.getContent()));
112                    document.addText(Field.DESCRIPTION, entry.getDescription());
113                    document.addDate(Field.MODIFIED_DATE, entry.getModifiedDate());
114                    document.addText(Field.TITLE, entry.getTitle());
115    
116                    return document;
117            }
118    
119            @Override
120            protected Summary doGetSummary(
121                    Document document, Locale locale, String snippet,
122                    PortletURL portletURL) {
123    
124                    String entryId = document.get(Field.ENTRY_CLASS_PK);
125    
126                    portletURL.setParameter("struts_action", "/blogs/view_entry");
127                    portletURL.setParameter("entryId", entryId);
128    
129                    Summary summary = createSummary(document);
130    
131                    summary.setMaxContentLength(200);
132                    summary.setPortletURL(portletURL);
133    
134                    return summary;
135            }
136    
137            @Override
138            protected void doReindex(Object obj) throws Exception {
139                    BlogsEntry entry = (BlogsEntry)obj;
140    
141                    if (!entry.isApproved() && !entry.isInTrash()) {
142                            return;
143                    }
144    
145                    Document document = getDocument(entry);
146    
147                    SearchEngineUtil.updateDocument(
148                            getSearchEngineId(), entry.getCompanyId(), document);
149            }
150    
151            @Override
152            protected void doReindex(String className, long classPK) throws Exception {
153                    BlogsEntry entry = BlogsEntryLocalServiceUtil.getEntry(classPK);
154    
155                    doReindex(entry);
156            }
157    
158            @Override
159            protected void doReindex(String[] ids) throws Exception {
160                    long companyId = GetterUtil.getLong(ids[0]);
161    
162                    reindexEntries(companyId);
163            }
164    
165            @Override
166            protected String getPortletId(SearchContext searchContext) {
167                    return PORTLET_ID;
168            }
169    
170            protected void reindexEntries(long companyId)
171                    throws PortalException, SystemException {
172    
173                    final Collection<Document> documents = new ArrayList<Document>();
174    
175                    ActionableDynamicQuery actionableDynamicQuery =
176                            new BlogsEntryActionableDynamicQuery() {
177    
178                            @Override
179                            protected void addCriteria(DynamicQuery dynamicQuery) {
180                                    Property displayDateProperty = PropertyFactoryUtil.forName(
181                                            "displayDate");
182    
183                                    dynamicQuery.add(displayDateProperty.lt(new Date()));
184    
185                                    Property statusProperty = PropertyFactoryUtil.forName("status");
186    
187                                    Integer[] statuses = {
188                                            WorkflowConstants.STATUS_APPROVED,
189                                            WorkflowConstants.STATUS_IN_TRASH
190                                    };
191    
192                                    dynamicQuery.add(statusProperty.in(statuses));
193                            }
194    
195                            @Override
196                            protected void performAction(Object object) throws PortalException {
197                                    BlogsEntry entry = (BlogsEntry)object;
198    
199                                    Document document = getDocument(entry);
200    
201                                    documents.add(document);
202                            }
203    
204                    };
205    
206                    actionableDynamicQuery.setCompanyId(companyId);
207    
208                    actionableDynamicQuery.performActions();
209    
210                    SearchEngineUtil.updateDocuments(
211                            getSearchEngineId(), companyId, documents);
212            }
213    
214    }