001    /**
002     * Copyright (c) 2000-2012 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            public String[] getClassNames() {
063                    return CLASS_NAMES;
064            }
065    
066            public String getPortletId() {
067                    return PORTLET_ID;
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(PORTLET_ID, 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                    document.addKeyword(Field.PORTLET_ID, PORTLET_ID);
118    
119                    String[] statusAndInstalledVersion =
120                            PluginPackageUtil.getStatusAndInstalledVersion(pluginPackage);
121    
122                    document.addKeyword(Field.STATUS, statusAndInstalledVersion[0]);
123    
124                    document.addText(Field.TITLE, pluginPackage.getName());
125    
126                    document.addKeyword("artifactId", moduleIdObj.getArtifactId());
127                    document.addText("author", pluginPackage.getAuthor());
128                    document.addText("changeLog", pluginPackage.getChangeLog());
129                    document.addKeyword("installedVersion", statusAndInstalledVersion[1]);
130    
131                    List<License> licenses = pluginPackage.getLicenses();
132    
133                    document.addKeyword(
134                            "license",
135                            StringUtil.split(
136                                    ListUtil.toString(licenses, License.NAME_ACCESSOR)));
137    
138                    document.addText("longDescription", longDescription);
139                    document.addKeyword("moduleId", pluginPackage.getModuleId());
140    
141                    boolean osiLicense = false;
142    
143                    for (int i = 0; i < licenses.size(); i++) {
144                            License license = licenses.get(i);
145    
146                            if (license.isOsiApproved()) {
147                                    osiLicense = true;
148    
149                                    break;
150                            }
151                    }
152    
153                    document.addKeyword("osi-approved-license", osiLicense);
154                    document.addText("pageURL", pluginPackage.getPageURL());
155                    document.addKeyword("repositoryURL", pluginPackage.getRepositoryURL());
156                    document.addText("shortDescription", shortDescription);
157    
158                    List<String> tags = pluginPackage.getTags();
159    
160                    document.addKeyword("tag", tags.toArray(new String[0]));
161    
162                    List<String> types = pluginPackage.getTypes();
163    
164                    document.addKeyword("type", types.toArray(new String[0]));
165    
166                    document.addKeyword("version", pluginPackage.getVersion());
167    
168                    return document;
169            }
170    
171            @Override
172            protected Summary doGetSummary(
173                    Document document, Locale locale, String snippet,
174                    PortletURL portletURL) {
175    
176                    String title = document.get(Field.TITLE);
177    
178                    String content = snippet;
179    
180                    if (Validator.isNull(snippet)) {
181                            content = StringUtil.shorten(document.get(Field.CONTENT), 200);
182                    }
183    
184                    String moduleId = document.get("moduleId");
185                    String repositoryURL = document.get("repositoryURL");
186    
187                    portletURL.setParameter("struts_action", "/admin/view");
188                    portletURL.setParameter("tabs2", "repositories");
189                    portletURL.setParameter("moduleId", moduleId);
190                    portletURL.setParameter("repositoryURL", repositoryURL);
191    
192                    return new Summary(title, content, portletURL);
193            }
194    
195            @Override
196            protected void doReindex(Object obj) throws Exception {
197                    PluginPackage pluginPackage = (PluginPackage)obj;
198    
199                    Document document = getDocument(pluginPackage);
200    
201                    SearchEngineUtil.updateDocument(CompanyConstants.SYSTEM, document);
202            }
203    
204            @Override
205            protected void doReindex(String className, long classPK) throws Exception {
206            }
207    
208            @Override
209            protected void doReindex(String[] ids) throws Exception {
210                    SearchEngineUtil.deletePortletDocuments(
211                            CompanyConstants.SYSTEM, PORTLET_ID);
212    
213                    Collection<Document> documents = new ArrayList<Document>();
214    
215                    for (PluginPackage pluginPackage :
216                                    PluginPackageUtil.getAllAvailablePluginPackages()) {
217    
218                            Document document = getDocument(pluginPackage);
219    
220                            documents.add(document);
221                    }
222    
223                    SearchEngineUtil.updateDocuments(CompanyConstants.SYSTEM, documents);
224            }
225    
226            @Override
227            protected String getPortletId(SearchContext searchContext) {
228                    return PORTLET_ID;
229            }
230    
231            @Override
232            protected void postProcessFullQuery(
233                            BooleanQuery fullQuery, SearchContext searchContext)
234                    throws Exception {
235    
236                    String type = (String)searchContext.getAttribute("type");
237    
238                    if (Validator.isNotNull(type)) {
239                            BooleanQuery searchQuery = BooleanQueryFactoryUtil.create(
240                                    searchContext);
241    
242                            searchQuery.addRequiredTerm("type", type);
243    
244                            fullQuery.add(searchQuery, BooleanClauseOccur.MUST);
245                    }
246    
247                    String tag = (String)searchContext.getAttribute("tag");
248    
249                    if (Validator.isNotNull(tag)) {
250                            BooleanQuery searchQuery = BooleanQueryFactoryUtil.create(
251                                    searchContext);
252    
253                            searchQuery.addExactTerm("tag", tag);
254    
255                            fullQuery.add(searchQuery, BooleanClauseOccur.MUST);
256                    }
257    
258                    String repositoryURL = (String)searchContext.getAttribute(
259                            "repositoryURL");
260    
261                    if (Validator.isNotNull(repositoryURL)) {
262                            BooleanQuery searchQuery = BooleanQueryFactoryUtil.create(
263                                    searchContext);
264    
265                            Query query = TermQueryFactoryUtil.create(
266                                    searchContext, "repositoryURL", repositoryURL);
267    
268                            searchQuery.add(query, BooleanClauseOccur.SHOULD);
269    
270                            fullQuery.add(searchQuery, BooleanClauseOccur.MUST);
271                    }
272    
273                    String license = (String)searchContext.getAttribute("license");
274    
275                    if (Validator.isNotNull(license)) {
276                            BooleanQuery searchQuery = BooleanQueryFactoryUtil.create(
277                                    searchContext);
278    
279                            searchQuery.addExactTerm("license", license);
280    
281                            fullQuery.add(searchQuery, BooleanClauseOccur.MUST);
282                    }
283    
284                    String status = (String)searchContext.getAttribute(Field.STATUS);
285    
286                    if (Validator.isNotNull(status) && !status.equals("all")) {
287                            BooleanQuery searchQuery = BooleanQueryFactoryUtil.create(
288                                    searchContext);
289    
290                            if (status.equals(
291                                            PluginPackageImpl.
292                                                    STATUS_NOT_INSTALLED_OR_OLDER_VERSION_INSTALLED)) {
293    
294                                    searchQuery.addExactTerm(
295                                            Field.STATUS, PluginPackageImpl.STATUS_NOT_INSTALLED);
296                                    searchQuery.addExactTerm(
297                                            Field.STATUS,
298                                            PluginPackageImpl.STATUS_OLDER_VERSION_INSTALLED);
299                            }
300                            else {
301                                    searchQuery.addExactTerm(Field.STATUS, status);
302                            }
303    
304                            fullQuery.add(searchQuery, BooleanClauseOccur.MUST);
305                    }
306            }
307    
308    }