1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.plugin;
16  
17  import com.liferay.portal.kernel.plugin.License;
18  import com.liferay.portal.kernel.plugin.PluginPackage;
19  import com.liferay.portal.kernel.search.BaseIndexer;
20  import com.liferay.portal.kernel.search.BooleanClauseOccur;
21  import com.liferay.portal.kernel.search.BooleanQuery;
22  import com.liferay.portal.kernel.search.BooleanQueryFactoryUtil;
23  import com.liferay.portal.kernel.search.Document;
24  import com.liferay.portal.kernel.search.DocumentImpl;
25  import com.liferay.portal.kernel.search.Field;
26  import com.liferay.portal.kernel.search.Query;
27  import com.liferay.portal.kernel.search.SearchContext;
28  import com.liferay.portal.kernel.search.SearchEngineUtil;
29  import com.liferay.portal.kernel.search.Summary;
30  import com.liferay.portal.kernel.search.TermQueryFactoryUtil;
31  import com.liferay.portal.kernel.util.HtmlUtil;
32  import com.liferay.portal.kernel.util.StringBundler;
33  import com.liferay.portal.kernel.util.StringPool;
34  import com.liferay.portal.kernel.util.StringUtil;
35  import com.liferay.portal.kernel.util.Validator;
36  import com.liferay.portal.model.CompanyConstants;
37  
38  import java.util.ArrayList;
39  import java.util.Collection;
40  import java.util.Date;
41  import java.util.List;
42  
43  import javax.portlet.PortletURL;
44  
45  /**
46   * <a href="PluginPackageIndexer.java.html"><b><i>View Source</i></b></a>
47   *
48   * @author Jorge Ferrer
49   * @author Brian Wing Shun Chan
50   * @author Bruno Farache
51   * @author Raymond Augé
52   */
53  public class PluginPackageIndexer extends BaseIndexer {
54  
55      public static final String[] CLASS_NAMES = {PluginPackage.class.getName()};
56  
57      public static final String PORTLET_ID = "PluginPackageIndexer";
58  
59      public String[] getClassNames() {
60          return CLASS_NAMES;
61      }
62  
63      public Summary getSummary(
64          Document document, String snippet, PortletURL portletURL) {
65  
66          String title = document.get(Field.TITLE);
67  
68          String content = snippet;
69  
70          if (Validator.isNull(snippet)) {
71              content = StringUtil.shorten(document.get(Field.CONTENT), 200);
72          }
73  
74          String moduleId = document.get("moduleId");
75          String repositoryURL = document.get("repositoryURL");
76  
77          portletURL.setParameter(
78              "struts_action", "/admin/view");
79          portletURL.setParameter("tabs2", "repositories");
80          portletURL.setParameter("moduleId", moduleId);
81          portletURL.setParameter("repositoryURL", repositoryURL);
82  
83          return new Summary(title, content, portletURL);
84      }
85  
86      protected void doDelete(Object obj) throws Exception {
87          PluginPackage pluginPackage = (PluginPackage)obj;
88  
89          Document document = new DocumentImpl();
90  
91          document.addUID(PORTLET_ID, pluginPackage.getModuleId());
92  
93          SearchEngineUtil.deleteDocument(
94              CompanyConstants.SYSTEM, document.get(Field.UID));
95      }
96  
97      protected Document doGetDocument(Object obj) throws Exception {
98          PluginPackage pluginPackage = (PluginPackage)obj;
99  
100         String moduleId = pluginPackage.getModuleId();
101         String name = pluginPackage.getName();
102         String version = pluginPackage.getVersion();
103         Date modifiedDate = pluginPackage.getModifiedDate();
104         String author = pluginPackage.getAuthor();
105         List<String> types = pluginPackage.getTypes();
106         List<String> tags = pluginPackage.getTags();
107         List<License> licenses = pluginPackage.getLicenses();
108         //List<String> liferayVersions = pluginPackage.getLiferayVersions();
109         String shortDescription = HtmlUtil.extractText(
110             pluginPackage.getShortDescription());
111         String longDescription = HtmlUtil.extractText(
112             pluginPackage.getLongDescription());
113         String changeLog = pluginPackage.getChangeLog();
114         String pageURL = pluginPackage.getPageURL();
115         String repositoryURL = pluginPackage.getRepositoryURL();
116 
117         String[] statusAndInstalledVersion =
118             PluginPackageUtil.getStatusAndInstalledVersion(pluginPackage);
119 
120         String status = statusAndInstalledVersion[0];
121         String installedVersion = statusAndInstalledVersion[1];
122 
123         ModuleId moduleIdObj = ModuleId.getInstance(moduleId);
124 
125         StringBundler sb = new StringBundler(7);
126 
127         sb.append(name);
128         sb.append(StringPool.SPACE);
129         sb.append(author);
130         sb.append(StringPool.SPACE);
131         sb.append(shortDescription);
132         sb.append(StringPool.SPACE);
133         sb.append(longDescription);
134 
135         String content = sb.toString();
136 
137         Document document = new DocumentImpl();
138 
139         document.addUID(PORTLET_ID, moduleId);
140 
141         document.addModifiedDate(modifiedDate);
142 
143         document.addKeyword(Field.PORTLET_ID, PORTLET_ID);
144         document.addKeyword(Field.GROUP_ID, moduleIdObj.getGroupId());
145 
146         document.addText(Field.TITLE, name);
147         document.addText(Field.CONTENT, content);
148 
149         document.addKeyword("moduleId", moduleId);
150         document.addKeyword("artifactId", moduleIdObj.getArtifactId());
151         document.addKeyword("version", version);
152         document.addText("author", author);
153         document.addKeyword("type", types.toArray(new String[0]));
154         document.addKeyword("tag", tags.toArray(new String[0]));
155 
156         String[] licenseNames = new String[licenses.size()];
157 
158         boolean osiLicense = false;
159 
160         for (int i = 0; i < licenses.size(); i++) {
161             License license = licenses.get(i);
162 
163             licenseNames[i] = license.getName();
164 
165             if (license.isOsiApproved()) {
166                 osiLicense = true;
167             }
168         }
169 
170         document.addKeyword("license", licenseNames);
171         document.addKeyword("osi-approved-license", String.valueOf(osiLicense));
172         document.addText("shortDescription", shortDescription);
173         document.addText("longDescription", longDescription);
174         document.addText("changeLog", changeLog);
175         document.addText("pageURL", pageURL);
176         document.addKeyword("repositoryURL", repositoryURL);
177         document.addKeyword("status", status);
178         document.addKeyword("installedVersion", installedVersion);
179 
180         return document;
181     }
182 
183     protected void doReindex(Object obj) throws Exception {
184         PluginPackage pluginPackage = (PluginPackage)obj;
185 
186         Document document = getDocument(pluginPackage);
187 
188         SearchEngineUtil.updateDocument(CompanyConstants.SYSTEM, document);
189     }
190 
191     protected void doReindex(String className, long classPK) throws Exception {
192     }
193 
194     protected void doReindex(String[] ids) throws Exception {
195         SearchEngineUtil.deletePortletDocuments(
196             CompanyConstants.SYSTEM, PORTLET_ID);
197 
198         Collection<Document> documents = new ArrayList<Document>();
199 
200         for (PluginPackage pluginPackage :
201                 PluginPackageUtil.getAllAvailablePluginPackages()) {
202 
203             Document document = getDocument(pluginPackage);
204 
205             documents.add(document);
206         }
207 
208         SearchEngineUtil.updateDocuments(CompanyConstants.SYSTEM, documents);
209     }
210 
211     protected String getPortletId(SearchContext searchContext) {
212         return PORTLET_ID;
213     }
214 
215     protected void postProcessFullQuery(
216             BooleanQuery fullQuery, SearchContext searchContext)
217         throws Exception {
218 
219         String type = (String)searchContext.getAttribute("type");
220 
221         if (Validator.isNotNull(type)) {
222             BooleanQuery searchQuery = BooleanQueryFactoryUtil.create();
223 
224             searchQuery.addRequiredTerm("type", type);
225 
226             fullQuery.add(searchQuery, BooleanClauseOccur.MUST);
227         }
228 
229         String tag = (String)searchContext.getAttribute("tag");
230 
231         if (Validator.isNotNull(tag)) {
232             BooleanQuery searchQuery = BooleanQueryFactoryUtil.create();
233 
234             searchQuery.addExactTerm("tag", tag);
235 
236             fullQuery.add(searchQuery, BooleanClauseOccur.MUST);
237         }
238 
239         String repositoryURL = (String)searchContext.getAttribute(
240             "repositoryURL");
241 
242         if (Validator.isNotNull(repositoryURL)) {
243             BooleanQuery searchQuery = BooleanQueryFactoryUtil.create();
244 
245             Query query = TermQueryFactoryUtil.create(
246                 "repositoryURL", repositoryURL);
247 
248             searchQuery.add(query, BooleanClauseOccur.SHOULD);
249 
250             fullQuery.add(searchQuery, BooleanClauseOccur.MUST);
251         }
252 
253         String license = (String)searchContext.getAttribute("license");
254 
255         if (Validator.isNotNull(license)) {
256             BooleanQuery searchQuery = BooleanQueryFactoryUtil.create();
257 
258             searchQuery.addExactTerm("license", license);
259 
260             fullQuery.add(searchQuery, BooleanClauseOccur.MUST);
261         }
262 
263         String status = (String)searchContext.getAttribute("status");
264 
265         if (Validator.isNotNull(status) && !status.equals("all")) {
266             BooleanQuery searchQuery = BooleanQueryFactoryUtil.create();
267 
268             if (status.equals(
269                     PluginPackageImpl.
270                         STATUS_NOT_INSTALLED_OR_OLDER_VERSION_INSTALLED)) {
271 
272                 searchQuery.addExactTerm(
273                     "status", PluginPackageImpl.STATUS_NOT_INSTALLED);
274                 searchQuery.addExactTerm(
275                     "status", PluginPackageImpl.STATUS_OLDER_VERSION_INSTALLED);
276             }
277             else {
278                 searchQuery.addExactTerm("status", status);
279             }
280 
281             fullQuery.add(searchQuery, BooleanClauseOccur.MUST);
282         }
283     }
284 
285 }