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