001    /**
002     * Copyright (c) 2000-present 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.softwarecatalog.util;
016    
017    import com.liferay.portal.kernel.dao.orm.ActionableDynamicQuery;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.search.BaseIndexer;
022    import com.liferay.portal.kernel.search.BooleanQuery;
023    import com.liferay.portal.kernel.search.Document;
024    import com.liferay.portal.kernel.search.Field;
025    import com.liferay.portal.kernel.search.SearchContext;
026    import com.liferay.portal.kernel.search.SearchEngineUtil;
027    import com.liferay.portal.kernel.search.Summary;
028    import com.liferay.portal.kernel.search.filter.BooleanFilter;
029    import com.liferay.portal.kernel.spring.osgi.OSGiBeanProperties;
030    import com.liferay.portal.kernel.util.GetterUtil;
031    import com.liferay.portal.kernel.util.HtmlUtil;
032    import com.liferay.portal.kernel.util.StringBundler;
033    import com.liferay.portal.kernel.util.StringPool;
034    import com.liferay.portal.kernel.util.Validator;
035    import com.liferay.portal.util.PortalUtil;
036    import com.liferay.portlet.softwarecatalog.model.SCProductEntry;
037    import com.liferay.portlet.softwarecatalog.model.SCProductVersion;
038    import com.liferay.portlet.softwarecatalog.service.SCProductEntryLocalServiceUtil;
039    
040    import java.util.Locale;
041    
042    import javax.portlet.PortletRequest;
043    import javax.portlet.PortletResponse;
044    
045    /**
046     * @author Jorge Ferrer
047     * @author Brian Wing Shun Chan
048     * @author Harry Mark
049     * @author Bruno Farache
050     * @author Raymond Aug??
051     */
052    @OSGiBeanProperties
053    public class SCIndexer extends BaseIndexer<SCProductEntry> {
054    
055            public static final String CLASS_NAME = SCProductEntry.class.getName();
056    
057            public SCIndexer() {
058                    setDefaultSelectedFieldNames(
059                            Field.COMPANY_ID, Field.CONTENT, Field.ENTRY_CLASS_NAME,
060                            Field.ENTRY_CLASS_PK, Field.TITLE, Field.UID);
061                    setStagingAware(false);
062            }
063    
064            @Override
065            public String getClassName() {
066                    return CLASS_NAME;
067            }
068    
069            @Override
070            protected void doDelete(SCProductEntry scProductEntry) throws Exception {
071                    deleteDocument(
072                            scProductEntry.getCompanyId(), scProductEntry.getProductEntryId());
073            }
074    
075            @Override
076            protected Document doGetDocument(SCProductEntry scProductEntry)
077                    throws Exception {
078    
079                    Document document = getBaseModelDocument(CLASS_NAME, scProductEntry);
080    
081                    StringBundler sb = new StringBundler(15);
082    
083                    String longDescription = HtmlUtil.extractText(
084                            scProductEntry.getLongDescription());
085    
086                    sb.append(longDescription);
087    
088                    sb.append(StringPool.SPACE);
089                    sb.append(scProductEntry.getPageURL());
090                    sb.append(StringPool.SPACE);
091                    sb.append(scProductEntry.getRepoArtifactId());
092                    sb.append(StringPool.SPACE);
093                    sb.append(scProductEntry.getRepoGroupId());
094                    sb.append(StringPool.SPACE);
095    
096                    String shortDescription = HtmlUtil.extractText(
097                            scProductEntry.getShortDescription());
098    
099                    sb.append(shortDescription);
100    
101                    sb.append(StringPool.SPACE);
102                    sb.append(scProductEntry.getType());
103                    sb.append(StringPool.SPACE);
104                    sb.append(scProductEntry.getUserId());
105                    sb.append(StringPool.SPACE);
106    
107                    String userName = PortalUtil.getUserName(
108                            scProductEntry.getUserId(), scProductEntry.getUserName());
109    
110                    sb.append(userName);
111    
112                    document.addText(Field.CONTENT, sb.toString());
113    
114                    document.addText(Field.TITLE, scProductEntry.getName());
115                    document.addKeyword(Field.TYPE, scProductEntry.getType());
116    
117                    String version = StringPool.BLANK;
118    
119                    SCProductVersion latestProductVersion =
120                            scProductEntry.getLatestVersion();
121    
122                    if (latestProductVersion != null) {
123                            version = latestProductVersion.getVersion();
124                    }
125    
126                    document.addKeyword(Field.VERSION, version);
127    
128                    document.addText("longDescription", longDescription);
129                    document.addText("pageURL", scProductEntry.getPageURL());
130                    document.addKeyword(
131                            "repoArtifactId", scProductEntry.getRepoArtifactId());
132                    document.addKeyword("repoGroupId", scProductEntry.getRepoGroupId());
133                    document.addText("shortDescription", shortDescription);
134    
135                    return document;
136            }
137    
138            @Override
139            protected Summary doGetSummary(
140                    Document document, Locale locale, String snippet,
141                    PortletRequest portletRequest, PortletResponse portletResponse) {
142    
143                    Summary summary = createSummary(document, Field.TITLE, Field.CONTENT);
144    
145                    summary.setMaxContentLength(200);
146    
147                    return summary;
148            }
149    
150            @Override
151            protected void doReindex(SCProductEntry scProductEntry) throws Exception {
152                    Document document = getDocument(scProductEntry);
153    
154                    SearchEngineUtil.updateDocument(
155                            getSearchEngineId(), scProductEntry.getCompanyId(), document,
156                            isCommitImmediately());
157            }
158    
159            @Override
160            protected void doReindex(String className, long classPK) throws Exception {
161                    SCProductEntry productEntry =
162                            SCProductEntryLocalServiceUtil.getProductEntry(classPK);
163    
164                    doReindex(productEntry);
165            }
166    
167            @Override
168            protected void doReindex(String[] ids) throws Exception {
169                    long companyId = GetterUtil.getLong(ids[0]);
170    
171                    reindexProductEntries(companyId);
172            }
173    
174            @Override
175            protected void postProcessFullQuery(
176                            BooleanQuery fullQuery, SearchContext searchContext)
177                    throws Exception {
178    
179                    BooleanFilter booleanFilter = fullQuery.getPreBooleanFilter();
180    
181                    if (booleanFilter == null) {
182                            booleanFilter = new BooleanFilter();
183                    }
184    
185                    String type = (String)searchContext.getAttribute("type");
186    
187                    if (Validator.isNotNull(type)) {
188                            booleanFilter.addRequiredTerm("type", type);
189                    }
190    
191                    if (booleanFilter.hasClauses()) {
192                            fullQuery.setPreBooleanFilter(booleanFilter);
193                    }
194            }
195    
196            protected void reindexProductEntries(long companyId)
197                    throws PortalException {
198    
199                    final ActionableDynamicQuery actionableDynamicQuery =
200                            SCProductEntryLocalServiceUtil.getActionableDynamicQuery();
201    
202                    actionableDynamicQuery.setCompanyId(companyId);
203                    actionableDynamicQuery.setPerformActionMethod(
204                            new ActionableDynamicQuery.PerformActionMethod<SCProductEntry>() {
205    
206                                    @Override
207                                    public void performAction(SCProductEntry productEntry) {
208                                            try {
209                                                    Document document = getDocument(productEntry);
210    
211                                                    actionableDynamicQuery.addDocument(document);
212                                            }
213                                            catch (PortalException pe) {
214                                                    if (_log.isWarnEnabled()) {
215                                                            _log.warn(
216                                                                    "Unable to software catalog product entry " +
217                                                                            productEntry.getProductEntryId(),
218                                                                    pe);
219                                                    }
220                                            }
221                                    }
222    
223                            });
224                    actionableDynamicQuery.setSearchEngineId(getSearchEngineId());
225    
226                    actionableDynamicQuery.performActions();
227            }
228    
229            private static final Log _log = LogFactoryUtil.getLog(SCIndexer.class);
230    
231    }