001    /**
002     * Copyright (c) 2000-2010 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.Document;
019    import com.liferay.portal.kernel.search.DocumentImpl;
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.util.PortalUtil;
031    import com.liferay.portal.util.PortletKeys;
032    import com.liferay.portlet.asset.service.AssetCategoryLocalServiceUtil;
033    import com.liferay.portlet.asset.service.AssetTagLocalServiceUtil;
034    import com.liferay.portlet.blogs.model.BlogsEntry;
035    import com.liferay.portlet.blogs.service.BlogsEntryLocalServiceUtil;
036    import com.liferay.portlet.expando.model.ExpandoBridge;
037    import com.liferay.portlet.expando.util.ExpandoBridgeIndexerUtil;
038    
039    import java.util.ArrayList;
040    import java.util.Collection;
041    import java.util.Date;
042    import java.util.List;
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 String[] getClassNames() {
059                    return CLASS_NAMES;
060            }
061    
062            public Summary getSummary(
063                    Document document, String snippet, PortletURL portletURL) {
064    
065                    String title = document.get(Field.TITLE);
066    
067                    String content = snippet;
068    
069                    if (Validator.isNull(snippet)) {
070                            content = StringUtil.shorten(document.get(Field.CONTENT), 200);
071                    }
072    
073                    String entryId = document.get(Field.ENTRY_CLASS_PK);
074    
075                    portletURL.setParameter("struts_action", "/blogs/view_entry");
076                    portletURL.setParameter("entryId", entryId);
077    
078                    return new Summary(title, content, portletURL);
079            }
080    
081            protected void doDelete(Object obj) throws Exception {
082                    BlogsEntry entry = (BlogsEntry)obj;
083    
084                    Document document = new DocumentImpl();
085    
086                    document.addUID(PORTLET_ID, entry.getEntryId());
087    
088                    SearchEngineUtil.deleteDocument(
089                            entry.getCompanyId(), document.get(Field.UID));
090            }
091    
092            protected Document doGetDocument(Object obj) throws Exception {
093                    BlogsEntry entry = (BlogsEntry)obj;
094    
095                    long companyId = entry.getCompanyId();
096                    long groupId = getParentGroupId(entry.getGroupId());
097                    long scopeGroupId = entry.getGroupId();
098                    long userId = entry.getUserId();
099                    String userName = PortalUtil.getUserName(userId, entry.getUserName());
100                    long entryId = entry.getEntryId();
101                    String title = entry.getTitle();
102                    String content = HtmlUtil.extractText(entry.getContent());
103                    Date displayDate = entry.getDisplayDate();
104    
105                    long[] assetCategoryIds = AssetCategoryLocalServiceUtil.getCategoryIds(
106                            BlogsEntry.class.getName(), entryId);
107                    String[] assetTagNames = AssetTagLocalServiceUtil.getTagNames(
108                            BlogsEntry.class.getName(), entryId);
109    
110                    ExpandoBridge expandoBridge = entry.getExpandoBridge();
111    
112                    Document document = new DocumentImpl();
113    
114                    document.addUID(PORTLET_ID, entryId);
115    
116                    document.addModifiedDate(displayDate);
117    
118                    document.addKeyword(Field.COMPANY_ID, companyId);
119                    document.addKeyword(Field.PORTLET_ID, PORTLET_ID);
120                    document.addKeyword(Field.GROUP_ID, groupId);
121                    document.addKeyword(Field.SCOPE_GROUP_ID, scopeGroupId);
122                    document.addKeyword(Field.USER_ID, userId);
123                    document.addText(Field.USER_NAME, userName);
124    
125                    document.addText(Field.TITLE, title);
126                    document.addText(Field.CONTENT, content);
127                    document.addKeyword(Field.ASSET_CATEGORY_IDS, assetCategoryIds);
128                    document.addKeyword(Field.ASSET_TAG_NAMES, assetTagNames);
129    
130                    document.addKeyword(Field.ENTRY_CLASS_NAME, BlogsEntry.class.getName());
131                    document.addKeyword(Field.ENTRY_CLASS_PK, entryId);
132    
133                    ExpandoBridgeIndexerUtil.addAttributes(document, expandoBridge);
134    
135                    return document;
136            }
137    
138            protected void doReindex(Object obj) throws Exception {
139                    BlogsEntry entry = (BlogsEntry)obj;
140    
141                    if (!entry.isApproved()) {
142                            return;
143                    }
144    
145                    Document document = getDocument(entry);
146    
147                    SearchEngineUtil.updateDocument(entry.getCompanyId(), document);
148            }
149    
150            protected void doReindex(String className, long classPK) throws Exception {
151                    BlogsEntry entry = BlogsEntryLocalServiceUtil.getEntry(classPK);
152    
153                    doReindex(entry);
154            }
155    
156            protected void doReindex(String[] ids) throws Exception {
157                    long companyId = GetterUtil.getLong(ids[0]);
158    
159                    reindexEntries(companyId);
160            }
161    
162            protected String getPortletId(SearchContext searchContext) {
163                    return PORTLET_ID;
164            }
165    
166            protected void reindexEntries(long companyId) throws Exception {
167                    int count = BlogsEntryLocalServiceUtil.getCompanyEntriesCount(
168                            companyId, WorkflowConstants.STATUS_APPROVED);
169    
170                    int pages = count / Indexer.DEFAULT_INTERVAL;
171    
172                    for (int i = 0; i <= pages; i++) {
173                            int start = (i * Indexer.DEFAULT_INTERVAL);
174                            int end = start + Indexer.DEFAULT_INTERVAL;
175    
176                            reindexEntries(companyId, start, end);
177                    }
178            }
179    
180            protected void reindexEntries(long companyId, int start, int end)
181                    throws Exception {
182    
183                    List<BlogsEntry> entries = BlogsEntryLocalServiceUtil.getCompanyEntries(
184                            companyId, WorkflowConstants.STATUS_APPROVED, start, end);
185    
186                    if (entries.isEmpty()) {
187                            return;
188                    }
189    
190                    Collection<Document> documents = new ArrayList<Document>();
191    
192                    for (BlogsEntry entry : entries) {
193                            Document document = getDocument(entry);
194    
195                            documents.add(document);
196                    }
197    
198                    SearchEngineUtil.updateDocuments(companyId, documents);
199            }
200    
201    }