001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.List;
040    import java.util.Locale;
041    
042    import javax.portlet.PortletURL;
043    
044    /**
045     * @author Brian Wing Shun Chan
046     * @author Harry Mark
047     * @author Bruno Farache
048     * @author Raymond Augé
049     */
050    public class BlogsIndexer extends BaseIndexer {
051    
052            public static final String[] CLASS_NAMES = {BlogsEntry.class.getName()};
053    
054            public static final String PORTLET_ID = PortletKeys.BLOGS;
055    
056            public String[] getClassNames() {
057                    return CLASS_NAMES;
058            }
059    
060            public String getPortletId() {
061                    return PORTLET_ID;
062            }
063    
064            @Override
065            public boolean hasPermission(
066                            PermissionChecker permissionChecker, long entryClassPK,
067                            String actionId)
068                    throws Exception {
069    
070                    return BlogsEntryPermission.contains(
071                            permissionChecker, entryClassPK, ActionKeys.VIEW);
072            }
073    
074            @Override
075            public boolean isFilterSearch() {
076                    return _FILTER_SEARCH;
077            }
078    
079            @Override
080            public void postProcessContextQuery(
081                            BooleanQuery contextQuery, SearchContext searchContext)
082                    throws Exception {
083    
084                    int status = GetterUtil.getInteger(
085                            searchContext.getAttribute(Field.STATUS),
086                            WorkflowConstants.STATUS_ANY);
087    
088                    if (status != WorkflowConstants.STATUS_ANY) {
089                            contextQuery.addRequiredTerm(Field.STATUS, status);
090                    }
091            }
092    
093            @Override
094            protected void doDelete(Object obj) throws Exception {
095                    BlogsEntry entry = (BlogsEntry)obj;
096    
097                    deleteDocument(entry.getCompanyId(), entry.getEntryId());
098            }
099    
100            @Override
101            protected Document doGetDocument(Object obj) throws Exception {
102                    BlogsEntry entry = (BlogsEntry)obj;
103    
104                    Document document = getBaseModelDocument(PORTLET_ID, entry);
105    
106                    document.addText(
107                            Field.CONTENT, HtmlUtil.extractText(entry.getContent()));
108                    document.addDate(Field.MODIFIED_DATE, entry.getDisplayDate());
109                    document.addText(Field.TITLE, entry.getTitle());
110    
111                    return document;
112            }
113    
114            @Override
115            protected Summary doGetSummary(
116                    Document document, Locale locale, String snippet,
117                    PortletURL portletURL) {
118    
119                    String title = document.get(Field.TITLE);
120    
121                    String content = snippet;
122    
123                    if (Validator.isNull(snippet)) {
124                            content = StringUtil.shorten(document.get(Field.CONTENT), 200);
125                    }
126    
127                    String entryId = document.get(Field.ENTRY_CLASS_PK);
128    
129                    portletURL.setParameter("struts_action", "/blogs/view_entry");
130                    portletURL.setParameter("entryId", entryId);
131    
132                    return new Summary(title, content, portletURL);
133            }
134    
135            @Override
136            protected void doReindex(Object obj) throws Exception {
137                    BlogsEntry entry = (BlogsEntry)obj;
138    
139                    if (!entry.isApproved()) {
140                            return;
141                    }
142    
143                    Document document = getDocument(entry);
144    
145                    SearchEngineUtil.updateDocument(entry.getCompanyId(), document);
146            }
147    
148            @Override
149            protected void doReindex(String className, long classPK) throws Exception {
150                    BlogsEntry entry = BlogsEntryLocalServiceUtil.getEntry(classPK);
151    
152                    doReindex(entry);
153            }
154    
155            @Override
156            protected void doReindex(String[] ids) throws Exception {
157                    long companyId = GetterUtil.getLong(ids[0]);
158    
159                    reindexEntries(companyId);
160            }
161    
162            @Override
163            protected String getPortletId(SearchContext searchContext) {
164                    return PORTLET_ID;
165            }
166    
167            protected void reindexEntries(long companyId) throws Exception {
168                    int count = BlogsEntryLocalServiceUtil.getCompanyEntriesCount(
169                            companyId, WorkflowConstants.STATUS_APPROVED);
170    
171                    int pages = count / Indexer.DEFAULT_INTERVAL;
172    
173                    for (int i = 0; i <= pages; i++) {
174                            int start = (i * Indexer.DEFAULT_INTERVAL);
175                            int end = start + Indexer.DEFAULT_INTERVAL;
176    
177                            reindexEntries(companyId, start, end);
178                    }
179            }
180    
181            protected void reindexEntries(long companyId, int start, int end)
182                    throws Exception {
183    
184                    List<BlogsEntry> entries = BlogsEntryLocalServiceUtil.getCompanyEntries(
185                            companyId, WorkflowConstants.STATUS_APPROVED, start, end);
186    
187                    if (entries.isEmpty()) {
188                            return;
189                    }
190    
191                    Collection<Document> documents = new ArrayList<Document>();
192    
193                    for (BlogsEntry entry : entries) {
194                            Document document = getDocument(entry);
195    
196                            documents.add(document);
197                    }
198    
199                    SearchEngineUtil.updateDocuments(companyId, documents);
200            }
201    
202            private static final boolean _FILTER_SEARCH = true;
203    
204    }