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.util.StringUtil;
033    import com.liferay.portal.kernel.util.Validator;
034    import com.liferay.portal.kernel.workflow.WorkflowConstants;
035    import com.liferay.portal.security.permission.ActionKeys;
036    import com.liferay.portal.security.permission.PermissionChecker;
037    import com.liferay.portal.util.PortletKeys;
038    import com.liferay.portlet.blogs.model.BlogsEntry;
039    import com.liferay.portlet.blogs.service.BlogsEntryLocalServiceUtil;
040    import com.liferay.portlet.blogs.service.permission.BlogsEntryPermission;
041    import com.liferay.portlet.blogs.service.persistence.BlogsEntryActionableDynamicQuery;
042    
043    import java.util.ArrayList;
044    import java.util.Collection;
045    import java.util.Date;
046    import java.util.Locale;
047    
048    import javax.portlet.PortletURL;
049    
050    /**
051     * @author Brian Wing Shun Chan
052     * @author Harry Mark
053     * @author Bruno Farache
054     * @author Raymond Aug??
055     */
056    public class BlogsIndexer extends BaseIndexer {
057    
058            public static final String[] CLASS_NAMES = {BlogsEntry.class.getName()};
059    
060            public static final String PORTLET_ID = PortletKeys.BLOGS;
061    
062            @Override
063            public String[] getClassNames() {
064                    return CLASS_NAMES;
065            }
066    
067            @Override
068            public String getPortletId() {
069                    return PORTLET_ID;
070            }
071    
072            @Override
073            public boolean hasPermission(
074                            PermissionChecker permissionChecker, long entryClassPK,
075                            String actionId)
076                    throws Exception {
077    
078                    return BlogsEntryPermission.contains(
079                            permissionChecker, entryClassPK, ActionKeys.VIEW);
080            }
081    
082            @Override
083            public boolean isPermissionAware() {
084                    return _PERMISSION_AWARE;
085            }
086    
087            @Override
088            public void postProcessContextQuery(
089                            BooleanQuery contextQuery, SearchContext searchContext)
090                    throws Exception {
091    
092                    int status = GetterUtil.getInteger(
093                            searchContext.getAttribute(Field.STATUS),
094                            WorkflowConstants.STATUS_ANY);
095    
096                    if (status != WorkflowConstants.STATUS_ANY) {
097                            contextQuery.addRequiredTerm(Field.STATUS, status);
098                    }
099            }
100    
101            @Override
102            protected void doDelete(Object obj) throws Exception {
103                    BlogsEntry entry = (BlogsEntry)obj;
104    
105                    deleteDocument(entry.getCompanyId(), entry.getEntryId());
106            }
107    
108            @Override
109            protected Document doGetDocument(Object obj) throws Exception {
110                    BlogsEntry entry = (BlogsEntry)obj;
111    
112                    Document document = getBaseModelDocument(PORTLET_ID, entry);
113    
114                    document.addText(
115                            Field.CONTENT, HtmlUtil.extractText(entry.getContent()));
116                    document.addText(Field.DESCRIPTION, entry.getDescription());
117                    document.addDate(Field.MODIFIED_DATE, entry.getDisplayDate());
118                    document.addText(Field.TITLE, entry.getTitle());
119    
120                    return document;
121            }
122    
123            @Override
124            protected Summary doGetSummary(
125                    Document document, Locale locale, String snippet,
126                    PortletURL portletURL) {
127    
128                    String title = document.get(Field.TITLE);
129    
130                    String content = snippet;
131    
132                    if (Validator.isNull(snippet)) {
133                            content = StringUtil.shorten(document.get(Field.CONTENT), 200);
134                    }
135    
136                    String entryId = document.get(Field.ENTRY_CLASS_PK);
137    
138                    portletURL.setParameter("struts_action", "/blogs/view_entry");
139                    portletURL.setParameter("entryId", entryId);
140    
141                    return new Summary(title, content, portletURL);
142            }
143    
144            @Override
145            protected void doReindex(Object obj) throws Exception {
146                    BlogsEntry entry = (BlogsEntry)obj;
147    
148                    if (!entry.isApproved()) {
149                            return;
150                    }
151    
152                    Document document = getDocument(entry);
153    
154                    SearchEngineUtil.updateDocument(
155                            getSearchEngineId(), entry.getCompanyId(), document);
156            }
157    
158            @Override
159            protected void doReindex(String className, long classPK) throws Exception {
160                    BlogsEntry entry = BlogsEntryLocalServiceUtil.getEntry(classPK);
161    
162                    doReindex(entry);
163            }
164    
165            @Override
166            protected void doReindex(String[] ids) throws Exception {
167                    long companyId = GetterUtil.getLong(ids[0]);
168    
169                    reindexEntries(companyId);
170            }
171    
172            @Override
173            protected String getPortletId(SearchContext searchContext) {
174                    return PORTLET_ID;
175            }
176    
177            protected void reindexEntries(long companyId)
178                    throws PortalException, SystemException {
179    
180                    final Collection<Document> documents = new ArrayList<Document>();
181    
182                    ActionableDynamicQuery actionableDynamicQuery =
183                            new BlogsEntryActionableDynamicQuery() {
184    
185                            @Override
186                            protected void addCriteria(DynamicQuery dynamicQuery) {
187                                    Property displayDateProperty = PropertyFactoryUtil.forName(
188                                            "displayDate");
189    
190                                    dynamicQuery.add(displayDateProperty.lt(new Date()));
191    
192                                    Property statusProperty = PropertyFactoryUtil.forName("status");
193    
194                                    dynamicQuery.add(
195                                            statusProperty.eq(WorkflowConstants.STATUS_APPROVED));
196                            }
197    
198                            @Override
199                            protected void performAction(Object object) throws PortalException {
200                                    BlogsEntry entry = (BlogsEntry)object;
201    
202                                    Document document = getDocument(entry);
203    
204                                    documents.add(document);
205                            }
206    
207                    };
208    
209                    actionableDynamicQuery.setCompanyId(companyId);
210    
211                    actionableDynamicQuery.performActions();
212    
213                    SearchEngineUtil.updateDocuments(
214                            getSearchEngineId(), companyId, documents);
215            }
216    
217            private static final boolean _PERMISSION_AWARE = true;
218    
219    }