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                    Document document = getDocument(entry);
140    
141                    SearchEngineUtil.updateDocument(
142                            getSearchEngineId(), entry.getCompanyId(), document);
143            }
144    
145            @Override
146            protected void doReindex(String className, long classPK) throws Exception {
147                    BlogsEntry entry = BlogsEntryLocalServiceUtil.getEntry(classPK);
148    
149                    doReindex(entry);
150            }
151    
152            @Override
153            protected void doReindex(String[] ids) throws Exception {
154                    long companyId = GetterUtil.getLong(ids[0]);
155    
156                    reindexEntries(companyId);
157            }
158    
159            @Override
160            protected String getPortletId(SearchContext searchContext) {
161                    return PORTLET_ID;
162            }
163    
164            protected void reindexEntries(long companyId)
165                    throws PortalException, SystemException {
166    
167                    ActionableDynamicQuery actionableDynamicQuery =
168                            new BlogsEntryActionableDynamicQuery() {
169    
170                            @Override
171                            protected void addCriteria(DynamicQuery dynamicQuery) {
172                                    Property displayDateProperty = PropertyFactoryUtil.forName(
173                                            "displayDate");
174    
175                                    dynamicQuery.add(displayDateProperty.lt(new Date()));
176    
177                                    Property statusProperty = PropertyFactoryUtil.forName("status");
178    
179                                    Integer[] statuses = {
180                                            WorkflowConstants.STATUS_APPROVED,
181                                            WorkflowConstants.STATUS_IN_TRASH
182                                    };
183    
184                                    dynamicQuery.add(statusProperty.in(statuses));
185                            }
186    
187                            @Override
188                            protected void performAction(Object object) throws PortalException {
189                                    BlogsEntry entry = (BlogsEntry)object;
190    
191                                    Document document = getDocument(entry);
192    
193                                    addDocument(document);
194                            }
195    
196                    };
197    
198                    actionableDynamicQuery.setCompanyId(companyId);
199                    actionableDynamicQuery.setSearchEngineId(getSearchEngineId());
200    
201                    actionableDynamicQuery.performActions();
202            }
203    
204    }