001    /**
002     * Copyright (c) 2000-2012 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            public String[] getClassNames() {
065                    return CLASS_NAMES;
066            }
067    
068            public String getPortletId() {
069                    return PORTLET_ID;
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 void postProcessContextQuery(
084                            BooleanQuery contextQuery, SearchContext searchContext)
085                    throws Exception {
086    
087                    int status = GetterUtil.getInteger(
088                            searchContext.getAttribute(Field.STATUS),
089                            WorkflowConstants.STATUS_APPROVED);
090    
091                    if (status != WorkflowConstants.STATUS_ANY) {
092                            contextQuery.addRequiredTerm(Field.STATUS, status);
093                    }
094            }
095    
096            @Override
097            protected void doDelete(Object obj) throws Exception {
098                    BlogsEntry entry = (BlogsEntry)obj;
099    
100                    deleteDocument(entry.getCompanyId(), entry.getEntryId());
101            }
102    
103            @Override
104            protected Document doGetDocument(Object obj) throws Exception {
105                    BlogsEntry entry = (BlogsEntry)obj;
106    
107                    Document document = getBaseModelDocument(PORTLET_ID, entry);
108    
109                    document.addText(
110                            Field.CONTENT, HtmlUtil.extractText(entry.getContent()));
111                    document.addText(Field.DESCRIPTION, entry.getDescription());
112                    document.addDate(Field.MODIFIED_DATE, entry.getDisplayDate());
113                    document.addText(Field.TITLE, entry.getTitle());
114    
115                    return document;
116            }
117    
118            @Override
119            protected Summary doGetSummary(
120                    Document document, Locale locale, String snippet,
121                    PortletURL portletURL) {
122    
123                    String entryId = document.get(Field.ENTRY_CLASS_PK);
124    
125                    portletURL.setParameter("struts_action", "/blogs/view_entry");
126                    portletURL.setParameter("entryId", entryId);
127    
128                    Summary summary = createSummary(document);
129    
130                    summary.setMaxContentLength(200);
131                    summary.setPortletURL(portletURL);
132    
133                    return summary;
134            }
135    
136            @Override
137            protected void doReindex(Object obj) throws Exception {
138                    BlogsEntry entry = (BlogsEntry)obj;
139    
140                    if (!entry.isApproved() && !entry.isInTrash()) {
141                            return;
142                    }
143    
144                    Document document = getDocument(entry);
145    
146                    SearchEngineUtil.updateDocument(
147                            getSearchEngineId(), entry.getCompanyId(), document);
148            }
149    
150            @Override
151            protected void doReindex(String className, long classPK) throws Exception {
152                    BlogsEntry entry = BlogsEntryLocalServiceUtil.getEntry(classPK);
153    
154                    doReindex(entry);
155            }
156    
157            @Override
158            protected void doReindex(String[] ids) throws Exception {
159                    long companyId = GetterUtil.getLong(ids[0]);
160    
161                    reindexEntries(companyId);
162            }
163    
164            @Override
165            protected String getPortletId(SearchContext searchContext) {
166                    return PORTLET_ID;
167            }
168    
169            protected void reindexEntries(long companyId)
170                    throws PortalException, SystemException {
171    
172                    final Collection<Document> documents = new ArrayList<Document>();
173    
174                    ActionableDynamicQuery actionableDynamicQuery =
175                            new BlogsEntryActionableDynamicQuery() {
176    
177                            @Override
178                            protected void addCriteria(DynamicQuery dynamicQuery) {
179                                    Property displayDateProperty = PropertyFactoryUtil.forName(
180                                            "displayDate");
181    
182                                    dynamicQuery.add(displayDateProperty.lt(new Date()));
183    
184                                    Property statusProperty = PropertyFactoryUtil.forName("status");
185    
186                                    Integer[] statuses = {
187                                            WorkflowConstants.STATUS_APPROVED,
188                                            WorkflowConstants.STATUS_IN_TRASH
189                                    };
190    
191                                    dynamicQuery.add(statusProperty.in(statuses));
192                            }
193    
194                            @Override
195                            protected void performAction(Object object) throws PortalException {
196                                    BlogsEntry entry = (BlogsEntry)object;
197    
198                                    Document document = getDocument(entry);
199    
200                                    documents.add(document);
201                            }
202    
203                    };
204    
205                    actionableDynamicQuery.setCompanyId(companyId);
206    
207                    actionableDynamicQuery.performActions();
208    
209                    SearchEngineUtil.updateDocuments(
210                            getSearchEngineId(), companyId, documents);
211            }
212    
213    }