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