001    /**
002     * Copyright (c) 2000-2011 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.search.BaseIndexer;
018    import com.liferay.portal.kernel.search.BooleanQuery;
019    import com.liferay.portal.kernel.search.Document;
020    import com.liferay.portal.kernel.search.Field;
021    import com.liferay.portal.kernel.search.Indexer;
022    import com.liferay.portal.kernel.search.SearchContext;
023    import com.liferay.portal.kernel.search.SearchEngineUtil;
024    import com.liferay.portal.kernel.search.Summary;
025    import com.liferay.portal.kernel.util.GetterUtil;
026    import com.liferay.portal.kernel.util.HtmlUtil;
027    import com.liferay.portal.kernel.util.StringUtil;
028    import com.liferay.portal.kernel.util.Validator;
029    import com.liferay.portal.kernel.workflow.WorkflowConstants;
030    import com.liferay.portal.security.permission.ActionKeys;
031    import com.liferay.portal.security.permission.PermissionChecker;
032    import com.liferay.portal.util.PortletKeys;
033    import com.liferay.portlet.blogs.model.BlogsEntry;
034    import com.liferay.portlet.blogs.service.BlogsEntryLocalServiceUtil;
035    import com.liferay.portlet.blogs.service.permission.BlogsEntryPermission;
036    
037    import java.util.ArrayList;
038    import java.util.Collection;
039    import java.util.Date;
040    import java.util.List;
041    import java.util.Locale;
042    
043    import javax.portlet.PortletURL;
044    
045    /**
046     * @author Brian Wing Shun Chan
047     * @author Harry Mark
048     * @author Bruno Farache
049     * @author Raymond Augé
050     */
051    public class BlogsIndexer extends BaseIndexer {
052    
053            public static final String[] CLASS_NAMES = {BlogsEntry.class.getName()};
054    
055            public static final String PORTLET_ID = PortletKeys.BLOGS;
056    
057            public String[] getClassNames() {
058                    return CLASS_NAMES;
059            }
060    
061            public String getPortletId() {
062                    return PORTLET_ID;
063            }
064    
065            @Override
066            public boolean hasPermission(
067                            PermissionChecker permissionChecker, long entryClassPK,
068                            String actionId)
069                    throws Exception {
070    
071                    return BlogsEntryPermission.contains(
072                            permissionChecker, entryClassPK, ActionKeys.VIEW);
073            }
074    
075            @Override
076            public boolean isFilterSearch() {
077                    return _FILTER_SEARCH;
078            }
079    
080            @Override
081            public void postProcessContextQuery(
082                            BooleanQuery contextQuery, SearchContext searchContext)
083                    throws Exception {
084    
085                    int status = GetterUtil.getInteger(
086                            searchContext.getAttribute(Field.STATUS),
087                            WorkflowConstants.STATUS_ANY);
088    
089                    if (status != WorkflowConstants.STATUS_ANY) {
090                            contextQuery.addRequiredTerm(Field.STATUS, status);
091                    }
092            }
093    
094            @Override
095            protected void doDelete(Object obj) throws Exception {
096                    BlogsEntry entry = (BlogsEntry)obj;
097    
098                    deleteDocument(entry.getCompanyId(), entry.getEntryId());
099            }
100    
101            @Override
102            protected Document doGetDocument(Object obj) throws Exception {
103                    BlogsEntry entry = (BlogsEntry)obj;
104    
105                    Document document = getBaseModelDocument(PORTLET_ID, entry);
106    
107                    document.addText(
108                            Field.CONTENT, HtmlUtil.extractText(entry.getContent()));
109                    document.addDate(Field.MODIFIED_DATE, entry.getDisplayDate());
110                    document.addText(Field.TITLE, entry.getTitle());
111    
112                    return document;
113            }
114    
115            @Override
116            protected Summary doGetSummary(
117                    Document document, Locale locale, String snippet,
118                    PortletURL portletURL) {
119    
120                    String title = document.get(Field.TITLE);
121    
122                    String content = snippet;
123    
124                    if (Validator.isNull(snippet)) {
125                            content = StringUtil.shorten(document.get(Field.CONTENT), 200);
126                    }
127    
128                    String entryId = document.get(Field.ENTRY_CLASS_PK);
129    
130                    portletURL.setParameter("struts_action", "/blogs/view_entry");
131                    portletURL.setParameter("entryId", entryId);
132    
133                    return new Summary(title, content, portletURL);
134            }
135    
136            @Override
137            protected void doReindex(Object obj) throws Exception {
138                    BlogsEntry entry = (BlogsEntry)obj;
139    
140                    if (!entry.isApproved()) {
141                            return;
142                    }
143    
144                    Document document = getDocument(entry);
145    
146                    SearchEngineUtil.updateDocument(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) throws Exception {
169                    int count = BlogsEntryLocalServiceUtil.getCompanyEntriesCount(
170                            companyId, new Date(), WorkflowConstants.STATUS_APPROVED);
171    
172                    int pages = count / Indexer.DEFAULT_INTERVAL;
173    
174                    for (int i = 0; i <= pages; i++) {
175                            int start = (i * Indexer.DEFAULT_INTERVAL);
176                            int end = start + Indexer.DEFAULT_INTERVAL;
177    
178                            reindexEntries(companyId, start, end);
179                    }
180            }
181    
182            protected void reindexEntries(long companyId, int start, int end)
183                    throws Exception {
184    
185                    List<BlogsEntry> entries = BlogsEntryLocalServiceUtil.getCompanyEntries(
186                            companyId, new Date(), WorkflowConstants.STATUS_APPROVED, start,
187                            end);
188    
189                    if (entries.isEmpty()) {
190                            return;
191                    }
192    
193                    Collection<Document> documents = new ArrayList<Document>();
194    
195                    for (BlogsEntry entry : entries) {
196                            Document document = getDocument(entry);
197    
198                            documents.add(document);
199                    }
200    
201                    SearchEngineUtil.updateDocuments(companyId, documents);
202            }
203    
204            private static final boolean _FILTER_SEARCH = true;
205    
206    }