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