001    /**
002     * Copyright (c) 2000-2013 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.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.log.Log;
024    import com.liferay.portal.kernel.log.LogFactoryUtil;
025    import com.liferay.portal.kernel.search.BaseIndexer;
026    import com.liferay.portal.kernel.search.BooleanQuery;
027    import com.liferay.portal.kernel.search.Document;
028    import com.liferay.portal.kernel.search.Field;
029    import com.liferay.portal.kernel.search.SearchContext;
030    import com.liferay.portal.kernel.search.SearchEngineUtil;
031    import com.liferay.portal.kernel.search.Summary;
032    import com.liferay.portal.kernel.util.GetterUtil;
033    import com.liferay.portal.kernel.util.HtmlUtil;
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.Date;
044    import java.util.Locale;
045    
046    import javax.portlet.PortletURL;
047    
048    /**
049     * @author Brian Wing Shun Chan
050     * @author Harry Mark
051     * @author Bruno Farache
052     * @author Raymond Aug??
053     */
054    public class BlogsIndexer extends BaseIndexer {
055    
056            public static final String[] CLASS_NAMES = {BlogsEntry.class.getName()};
057    
058            public static final String PORTLET_ID = PortletKeys.BLOGS;
059    
060            public BlogsIndexer() {
061                    setFilterSearch(true);
062                    setPermissionAware(true);
063            }
064    
065            @Override
066            public void addRelatedEntryFields(Document document, Object obj) {
067                    document.addKeyword(Field.RELATED_ENTRY, true);
068            }
069    
070            @Override
071            public String[] getClassNames() {
072                    return CLASS_NAMES;
073            }
074    
075            @Override
076            public String getPortletId() {
077                    return PORTLET_ID;
078            }
079    
080            @Override
081            public boolean hasPermission(
082                            PermissionChecker permissionChecker, String entryClassName,
083                            long entryClassPK, String actionId)
084                    throws Exception {
085    
086                    return BlogsEntryPermission.contains(
087                            permissionChecker, entryClassPK, ActionKeys.VIEW);
088            }
089    
090            @Override
091            public boolean isVisible(long classPK, int status) throws Exception {
092                    BlogsEntry entry = BlogsEntryLocalServiceUtil.getEntry(classPK);
093    
094                    return isVisible(entry.getStatus(), status);
095            }
096    
097            @Override
098            public void postProcessContextQuery(
099                            BooleanQuery contextQuery, SearchContext searchContext)
100                    throws Exception {
101    
102                    addStatus(contextQuery, searchContext);
103            }
104    
105            @Override
106            protected void doDelete(Object obj) throws Exception {
107                    BlogsEntry entry = (BlogsEntry)obj;
108    
109                    deleteDocument(entry.getCompanyId(), entry.getEntryId());
110            }
111    
112            @Override
113            protected Document doGetDocument(Object obj) throws Exception {
114                    BlogsEntry entry = (BlogsEntry)obj;
115    
116                    Document document = getBaseModelDocument(PORTLET_ID, entry);
117    
118                    document.addText(
119                            Field.CONTENT, HtmlUtil.extractText(entry.getContent()));
120                    document.addText(Field.DESCRIPTION, entry.getDescription());
121                    document.addDate(Field.MODIFIED_DATE, entry.getModifiedDate());
122                    document.addText(Field.TITLE, entry.getTitle());
123    
124                    return document;
125            }
126    
127            @Override
128            protected Summary doGetSummary(
129                    Document document, Locale locale, String snippet,
130                    PortletURL portletURL) {
131    
132                    String entryId = document.get(Field.ENTRY_CLASS_PK);
133    
134                    portletURL.setParameter("struts_action", "/blogs/view_entry");
135                    portletURL.setParameter("entryId", entryId);
136    
137                    Summary summary = createSummary(document);
138    
139                    summary.setMaxContentLength(200);
140                    summary.setPortletURL(portletURL);
141    
142                    return summary;
143            }
144    
145            @Override
146            protected void doReindex(Object obj) throws Exception {
147                    BlogsEntry entry = (BlogsEntry)obj;
148    
149                    Document document = getDocument(entry);
150    
151                    SearchEngineUtil.updateDocument(
152                            getSearchEngineId(), entry.getCompanyId(), document);
153            }
154    
155            @Override
156            protected void doReindex(String className, long classPK) throws Exception {
157                    BlogsEntry entry = BlogsEntryLocalServiceUtil.getEntry(classPK);
158    
159                    doReindex(entry);
160            }
161    
162            @Override
163            protected void doReindex(String[] ids) throws Exception {
164                    long companyId = GetterUtil.getLong(ids[0]);
165    
166                    reindexEntries(companyId);
167            }
168    
169            @Override
170            protected String getPortletId(SearchContext searchContext) {
171                    return PORTLET_ID;
172            }
173    
174            protected void reindexEntries(long companyId)
175                    throws PortalException, SystemException {
176    
177                    ActionableDynamicQuery actionableDynamicQuery =
178                            new BlogsEntryActionableDynamicQuery() {
179    
180                            @Override
181                            protected void addCriteria(DynamicQuery dynamicQuery) {
182                                    Property displayDateProperty = PropertyFactoryUtil.forName(
183                                            "displayDate");
184    
185                                    dynamicQuery.add(displayDateProperty.lt(new Date()));
186    
187                                    Property statusProperty = PropertyFactoryUtil.forName("status");
188    
189                                    Integer[] statuses = {
190                                            WorkflowConstants.STATUS_APPROVED,
191                                            WorkflowConstants.STATUS_IN_TRASH
192                                    };
193    
194                                    dynamicQuery.add(statusProperty.in(statuses));
195                            }
196    
197                            @Override
198                            protected void performAction(Object object) {
199                                    BlogsEntry entry = (BlogsEntry)object;
200    
201                                    try {
202                                            Document document = getDocument(entry);
203    
204                                            addDocument(document);
205                                    }
206                                    catch (PortalException pe) {
207                                            if (_log.isWarnEnabled()) {
208                                                    _log.warn(
209                                                            "Unable to index blogs entry " +
210                                                                    entry.getEntryId(),
211                                                            pe);
212                                            }
213                                    }
214                            }
215    
216                    };
217    
218                    actionableDynamicQuery.setCompanyId(companyId);
219                    actionableDynamicQuery.setSearchEngineId(getSearchEngineId());
220    
221                    actionableDynamicQuery.performActions();
222            }
223    
224            private static Log _log = LogFactoryUtil.getLog(BlogsIndexer.class);
225    
226    }