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