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