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                    setFilterSearch(true);
060                    setPermissionAware(true);
061            }
062    
063            @Override
064            public void addRelatedEntryFields(Document document, Object obj) {
065                    document.addKeyword(Field.RELATED_ENTRY, true);
066            }
067    
068            @Override
069            public String[] getClassNames() {
070                    return CLASS_NAMES;
071            }
072    
073            @Override
074            public String getPortletId() {
075                    return PORTLET_ID;
076            }
077    
078            @Override
079            public boolean hasPermission(
080                            PermissionChecker permissionChecker, String entryClassName,
081                            long entryClassPK, String actionId)
082                    throws Exception {
083    
084                    return BlogsEntryPermission.contains(
085                            permissionChecker, entryClassPK, ActionKeys.VIEW);
086            }
087    
088            @Override
089            public boolean isVisible(long classPK, int status) throws Exception {
090                    BlogsEntry entry = BlogsEntryLocalServiceUtil.getEntry(classPK);
091    
092                    return isVisible(entry.getStatus(), status);
093            }
094    
095            @Override
096            public void postProcessContextQuery(
097                            BooleanQuery contextQuery, SearchContext searchContext)
098                    throws Exception {
099    
100                    addStatus(contextQuery, searchContext);
101            }
102    
103            @Override
104            protected void doDelete(Object obj) throws Exception {
105                    BlogsEntry entry = (BlogsEntry)obj;
106    
107                    deleteDocument(entry.getCompanyId(), entry.getEntryId());
108            }
109    
110            @Override
111            protected Document doGetDocument(Object obj) throws Exception {
112                    BlogsEntry entry = (BlogsEntry)obj;
113    
114                    Document document = getBaseModelDocument(PORTLET_ID, entry);
115    
116                    document.addText(
117                            Field.CONTENT, HtmlUtil.extractText(entry.getContent()));
118                    document.addText(Field.DESCRIPTION, entry.getDescription());
119                    document.addDate(Field.MODIFIED_DATE, entry.getModifiedDate());
120                    document.addText(Field.TITLE, entry.getTitle());
121    
122                    return document;
123            }
124    
125            @Override
126            protected Summary doGetSummary(
127                    Document document, Locale locale, String snippet,
128                    PortletURL portletURL) {
129    
130                    String entryId = document.get(Field.ENTRY_CLASS_PK);
131    
132                    portletURL.setParameter("struts_action", "/blogs/view_entry");
133                    portletURL.setParameter("entryId", entryId);
134    
135                    Summary summary = createSummary(document);
136    
137                    summary.setMaxContentLength(200);
138                    summary.setPortletURL(portletURL);
139    
140                    return summary;
141            }
142    
143            @Override
144            protected void doReindex(Object obj) throws Exception {
145                    BlogsEntry entry = (BlogsEntry)obj;
146    
147                    Document document = getDocument(entry);
148    
149                    SearchEngineUtil.updateDocument(
150                            getSearchEngineId(), entry.getCompanyId(), document);
151            }
152    
153            @Override
154            protected void doReindex(String className, long classPK) throws Exception {
155                    BlogsEntry entry = BlogsEntryLocalServiceUtil.getEntry(classPK);
156    
157                    doReindex(entry);
158            }
159    
160            @Override
161            protected void doReindex(String[] ids) throws Exception {
162                    long companyId = GetterUtil.getLong(ids[0]);
163    
164                    reindexEntries(companyId);
165            }
166    
167            @Override
168            protected String getPortletId(SearchContext searchContext) {
169                    return PORTLET_ID;
170            }
171    
172            protected void reindexEntries(long companyId)
173                    throws PortalException, SystemException {
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                                    addDocument(document);
202                            }
203    
204                    };
205    
206                    actionableDynamicQuery.setCompanyId(companyId);
207                    actionableDynamicQuery.setSearchEngineId(getSearchEngineId());
208    
209                    actionableDynamicQuery.performActions();
210            }
211    
212    }