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[tags.size()]));
161    
162                    List<String> types = pluginPackage.getTypes();
163    
164                    document.addKeyword("type", types.toArray(new String[types.size()]));
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(
202                            getSearchEngineId(), CompanyConstants.SYSTEM, document);
203            }
204    
205            @Override
206            protected void doReindex(String className, long classPK) throws Exception {
207            }
208    
209            @Override
210            protected void doReindex(String[] ids) throws Exception {
211                    SearchEngineUtil.deletePortletDocuments(
212                            getSearchEngineId(), CompanyConstants.SYSTEM, PORTLET_ID);
213    
214                    Collection<Document> documents = new ArrayList<Document>();
215    
216                    for (PluginPackage pluginPackage :
217                                    PluginPackageUtil.getAllAvailablePluginPackages()) {
218    
219                            Document document = getDocument(pluginPackage);
220    
221                            documents.add(document);
222                    }
223    
224                    SearchEngineUtil.updateDocuments(
225                            getSearchEngineId(), CompanyConstants.SYSTEM, documents);
226            }
227    
228            @Override
229            protected String getPortletId(SearchContext searchContext) {
230                    return PORTLET_ID;
231            }
232    
233            @Override
234            protected void postProcessFullQuery(
235                            BooleanQuery fullQuery, SearchContext searchContext)
236                    throws Exception {
237    
238                    String type = (String)searchContext.getAttribute("type");
239    
240                    if (Validator.isNotNull(type)) {
241                            BooleanQuery searchQuery = BooleanQueryFactoryUtil.create(
242                                    searchContext);
243    
244                            searchQuery.addRequiredTerm("type", type);
245    
246                            fullQuery.add(searchQuery, BooleanClauseOccur.MUST);
247                    }
248    
249                    String tag = (String)searchContext.getAttribute("tag");
250    
251                    if (Validator.isNotNull(tag)) {
252                            BooleanQuery searchQuery = BooleanQueryFactoryUtil.create(
253                                    searchContext);
254    
255                            searchQuery.addExactTerm("tag", tag);
256    
257                            fullQuery.add(searchQuery, BooleanClauseOccur.MUST);
258                    }
259    
260                    String repositoryURL = (String)searchContext.getAttribute(
261                            "repositoryURL");
262    
263                    if (Validator.isNotNull(repositoryURL)) {
264                            BooleanQuery searchQuery = BooleanQueryFactoryUtil.create(
265                                    searchContext);
266    
267                            Query query = TermQueryFactoryUtil.create(
268                                    searchContext, "repositoryURL", repositoryURL);
269    
270                            searchQuery.add(query, BooleanClauseOccur.SHOULD);
271    
272                            fullQuery.add(searchQuery, BooleanClauseOccur.MUST);
273                    }
274    
275                    String license = (String)searchContext.getAttribute("license");
276    
277                    if (Validator.isNotNull(license)) {
278                            BooleanQuery searchQuery = BooleanQueryFactoryUtil.create(
279                                    searchContext);
280    
281                            searchQuery.addExactTerm("license", license);
282    
283                            fullQuery.add(searchQuery, BooleanClauseOccur.MUST);
284                    }
285    
286                    String status = (String)searchContext.getAttribute(Field.STATUS);
287    
288                    if (Validator.isNotNull(status) && !status.equals("all")) {
289                            BooleanQuery searchQuery = BooleanQueryFactoryUtil.create(
290                                    searchContext);
291    
292                            if (status.equals(
293                                            PluginPackageImpl.
294                                                    STATUS_NOT_INSTALLED_OR_OLDER_VERSION_INSTALLED)) {
295    
296                                    searchQuery.addExactTerm(
297                                            Field.STATUS, PluginPackageImpl.STATUS_NOT_INSTALLED);
298                                    searchQuery.addExactTerm(
299                                            Field.STATUS,
300                                            PluginPackageImpl.STATUS_OLDER_VERSION_INSTALLED);
301                            }
302                            else {
303                                    searchQuery.addExactTerm(Field.STATUS, status);
304                            }
305    
306                            fullQuery.add(searchQuery, BooleanClauseOccur.MUST);
307                    }
308            }
309    
310    }