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