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.portal.plugin;
016    
017    import com.liferay.portal.kernel.plugin.License;
018    import com.liferay.portal.kernel.plugin.PluginPackage;
019    import com.liferay.portal.kernel.search.BaseIndexer;
020    import com.liferay.portal.kernel.search.BooleanClauseOccur;
021    import com.liferay.portal.kernel.search.BooleanQuery;
022    import com.liferay.portal.kernel.search.Document;
023    import com.liferay.portal.kernel.search.DocumentImpl;
024    import com.liferay.portal.kernel.search.Field;
025    import com.liferay.portal.kernel.search.IndexWriterHelperUtil;
026    import com.liferay.portal.kernel.search.SearchContext;
027    import com.liferay.portal.kernel.search.Summary;
028    import com.liferay.portal.kernel.search.background.task.ReindexStatusMessageSenderUtil;
029    import com.liferay.portal.kernel.search.filter.BooleanFilter;
030    import com.liferay.portal.kernel.spring.osgi.OSGiBeanProperties;
031    import com.liferay.portal.kernel.util.HtmlUtil;
032    import com.liferay.portal.kernel.util.ListUtil;
033    import com.liferay.portal.kernel.util.StringBundler;
034    import com.liferay.portal.kernel.util.StringPool;
035    import com.liferay.portal.kernel.util.StringUtil;
036    import com.liferay.portal.kernel.util.Validator;
037    import com.liferay.portal.model.CompanyConstants;
038    
039    import java.util.ArrayList;
040    import java.util.Collection;
041    import java.util.List;
042    import java.util.Locale;
043    
044    import javax.portlet.PortletRequest;
045    import javax.portlet.PortletResponse;
046    
047    /**
048     * @author Jorge Ferrer
049     * @author Brian Wing Shun Chan
050     * @author Bruno Farache
051     * @author Raymond Aug??
052     */
053    @OSGiBeanProperties
054    public class PluginPackageIndexer extends BaseIndexer<PluginPackage> {
055    
056            public static final String CLASS_NAME = PluginPackage.class.getName();
057    
058            public PluginPackageIndexer() {
059                    setDefaultSelectedFieldNames(
060                            Field.COMPANY_ID, Field.CONTENT, Field.ENTRY_CLASS_NAME,
061                            Field.ENTRY_CLASS_PK, Field.TITLE, Field.UID);
062                    setStagingAware(false);
063            }
064    
065            @Override
066            public String getClassName() {
067                    return CLASS_NAME;
068            }
069    
070            @Override
071            protected void doDelete(PluginPackage pluginPackage) throws Exception {
072                    deleteDocument(CompanyConstants.SYSTEM, pluginPackage.getModuleId());
073            }
074    
075            @Override
076            protected Document doGetDocument(PluginPackage pluginPackage)
077                    throws Exception {
078    
079                    Document document = new DocumentImpl();
080    
081                    document.addUID(CLASS_NAME, pluginPackage.getModuleId());
082    
083                    document.addKeyword(Field.COMPANY_ID, CompanyConstants.SYSTEM);
084    
085                    StringBundler sb = new StringBundler(7);
086    
087                    sb.append(pluginPackage.getAuthor());
088                    sb.append(StringPool.SPACE);
089    
090                    String longDescription = HtmlUtil.extractText(
091                            pluginPackage.getLongDescription());
092    
093                    sb.append(longDescription);
094    
095                    sb.append(StringPool.SPACE);
096                    sb.append(pluginPackage.getName());
097                    sb.append(StringPool.SPACE);
098    
099                    String shortDescription = HtmlUtil.extractText(
100                            pluginPackage.getShortDescription());
101    
102                    sb.append(shortDescription);
103    
104                    document.addText(Field.CONTENT, sb.toString());
105    
106                    document.addKeyword(
107                            Field.ENTRY_CLASS_NAME, PluginPackage.class.getName());
108    
109                    ModuleId moduleIdObj = ModuleId.getInstance(
110                            pluginPackage.getModuleId());
111    
112                    document.addKeyword(Field.GROUP_ID, moduleIdObj.getGroupId());
113    
114                    document.addDate(Field.MODIFIED_DATE, pluginPackage.getModifiedDate());
115    
116                    String[] statusAndInstalledVersion =
117                            PluginPackageUtil.getStatusAndInstalledVersion(pluginPackage);
118    
119                    document.addKeyword(Field.STATUS, statusAndInstalledVersion[0]);
120    
121                    document.addText(Field.TITLE, pluginPackage.getName());
122    
123                    document.addKeyword("artifactId", moduleIdObj.getArtifactId());
124                    document.addText("author", pluginPackage.getAuthor());
125                    document.addText("changeLog", pluginPackage.getChangeLog());
126                    document.addKeyword("installedVersion", statusAndInstalledVersion[1]);
127    
128                    List<License> licenses = pluginPackage.getLicenses();
129    
130                    document.addKeyword(
131                            "license",
132                            ListUtil.toArray(licenses, License.NAME_ACCESSOR));
133    
134                    document.addText("longDescription", longDescription);
135                    document.addKeyword("moduleId", pluginPackage.getModuleId());
136    
137                    boolean osiLicense = false;
138    
139                    for (int i = 0; i < licenses.size(); i++) {
140                            License license = licenses.get(i);
141    
142                            if (license.isOsiApproved()) {
143                                    osiLicense = true;
144    
145                                    break;
146                            }
147                    }
148    
149                    document.addKeyword("osi-approved-license", osiLicense);
150                    document.addText("pageURL", pluginPackage.getPageURL());
151                    document.addKeyword("repositoryURL", pluginPackage.getRepositoryURL());
152                    document.addText("shortDescription", shortDescription);
153    
154                    List<String> tags = pluginPackage.getTags();
155    
156                    document.addKeyword("tag", tags.toArray(new String[tags.size()]));
157    
158                    List<String> types = pluginPackage.getTypes();
159    
160                    document.addKeyword("type", types.toArray(new String[types.size()]));
161    
162                    document.addKeyword("version", pluginPackage.getVersion());
163    
164                    return document;
165            }
166    
167            @Override
168            protected Summary doGetSummary(
169                    Document document, Locale locale, String snippet,
170                    PortletRequest portletRequest, PortletResponse portletResponse) {
171    
172                    String title = document.get(Field.TITLE);
173    
174                    String content = snippet;
175    
176                    if (Validator.isNull(snippet)) {
177                            content = StringUtil.shorten(document.get(Field.CONTENT), 200);
178                    }
179    
180                    return new Summary(title, content);
181            }
182    
183            @Override
184            protected void doReindex(PluginPackage pluginPackage) throws Exception {
185                    Document document = getDocument(pluginPackage);
186    
187                    IndexWriterHelperUtil.updateDocument(
188                            getSearchEngineId(), CompanyConstants.SYSTEM, document,
189                            isCommitImmediately());
190            }
191    
192            @Override
193            protected void doReindex(String className, long classPK) throws Exception {
194            }
195    
196            @Override
197            protected void doReindex(String[] ids) throws Exception {
198                    IndexWriterHelperUtil.deleteEntityDocuments(
199                            getSearchEngineId(), CompanyConstants.SYSTEM, CLASS_NAME,
200                            isCommitImmediately());
201    
202                    Collection<Document> documents = new ArrayList<>();
203    
204                    List<PluginPackage> pluginPackages =
205                            PluginPackageUtil.getAllAvailablePluginPackages();
206    
207                    int total = pluginPackages.size();
208    
209                    ReindexStatusMessageSenderUtil.sendStatusMessage(
210                            getClassName(), 0, total);
211    
212                    for (PluginPackage pluginPackage : pluginPackages) {
213                            Document document = getDocument(pluginPackage);
214    
215                            documents.add(document);
216                    }
217    
218                    IndexWriterHelperUtil.updateDocuments(
219                            getSearchEngineId(), CompanyConstants.SYSTEM, documents,
220                            isCommitImmediately());
221    
222                    ReindexStatusMessageSenderUtil.sendStatusMessage(
223                            getClassName(), total, total);
224            }
225    
226            @Override
227            protected void postProcessFullQuery(
228                            BooleanQuery fullQuery, SearchContext searchContext)
229                    throws Exception {
230    
231                    BooleanFilter booleanFilter = fullQuery.getPreBooleanFilter();
232    
233                    if (booleanFilter == null) {
234                            booleanFilter = new BooleanFilter();
235                    }
236    
237                    String type = (String)searchContext.getAttribute("type");
238    
239                    if (Validator.isNotNull(type)) {
240                            booleanFilter.addRequiredTerm("type", type);
241                    }
242    
243                    String tag = (String)searchContext.getAttribute("tag");
244    
245                    if (Validator.isNotNull(tag)) {
246                            booleanFilter.addRequiredTerm("tag", tag);
247                    }
248    
249                    String repositoryURL = (String)searchContext.getAttribute(
250                            "repositoryURL");
251    
252                    if (Validator.isNotNull(repositoryURL)) {
253                            booleanFilter.addRequiredTerm("repositoryURL", repositoryURL);
254                    }
255    
256                    String license = (String)searchContext.getAttribute("license");
257    
258                    if (Validator.isNotNull(license)) {
259                            booleanFilter.addRequiredTerm("license", license);
260                    }
261    
262                    String status = (String)searchContext.getAttribute(Field.STATUS);
263    
264                    if (Validator.isNull(status) || status.equals("all")) {
265                            return;
266                    }
267    
268                    if (status.equals(
269                                    PluginPackageImpl.
270                                            STATUS_NOT_INSTALLED_OR_OLDER_VERSION_INSTALLED)) {
271    
272                            BooleanFilter statusBooleanFilter = new BooleanFilter();
273    
274                            statusBooleanFilter.addTerm(
275                                    Field.STATUS, PluginPackageImpl.STATUS_NOT_INSTALLED);
276                            statusBooleanFilter.addTerm(
277                                    Field.STATUS, PluginPackageImpl.STATUS_OLDER_VERSION_INSTALLED);
278    
279                            booleanFilter.add(statusBooleanFilter, BooleanClauseOccur.MUST);
280                    }
281                    else {
282                            booleanFilter.addRequiredTerm(Field.STATUS, status);
283                    }
284    
285                    if (booleanFilter.hasClauses()) {
286                            fullQuery.setPreBooleanFilter(booleanFilter);
287                    }
288            }
289    
290    }