1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   * 
13   */
14  
15  package com.liferay.portal.plugin;
16  
17  import com.liferay.portal.kernel.plugin.License;
18  import com.liferay.portal.kernel.search.Document;
19  import com.liferay.portal.kernel.search.DocumentImpl;
20  import com.liferay.portal.kernel.search.DocumentSummary;
21  import com.liferay.portal.kernel.search.Field;
22  import com.liferay.portal.kernel.search.Indexer;
23  import com.liferay.portal.kernel.search.SearchEngineUtil;
24  import com.liferay.portal.kernel.search.SearchException;
25  import com.liferay.portal.kernel.util.HtmlUtil;
26  import com.liferay.portal.kernel.util.StringUtil;
27  import com.liferay.portal.model.CompanyConstants;
28  
29  import java.util.Date;
30  import java.util.List;
31  
32  import javax.portlet.PortletURL;
33  
34  /**
35   * <a href="PluginPackageIndexer.java.html"><b><i>View Source</i></b></a>
36   *
37   * @author Jorge Ferrer
38   * @author Brian Wing Shun Chan
39   * @author Bruno Farache
40   */
41  public class PluginPackageIndexer implements Indexer {
42  
43      public static final String PORTLET_ID = "PluginPackageIndexer";
44  
45      public static void addPluginPackage(
46              String moduleId, String name, String version, Date modifiedDate,
47              String author, List<String> types, List<String> tags,
48              List<License> licenses, List<String> liferayVersions,
49              String shortDescription, String longDescription, String changeLog,
50              String pageURL, String repositoryURL, String status,
51              String installedVersion)
52          throws SearchException {
53  
54          Document doc = getPluginPackageDocument(
55              moduleId, name, version, modifiedDate, author, types, tags,
56              licenses, liferayVersions, shortDescription, longDescription,
57              changeLog, pageURL, repositoryURL, status, installedVersion);
58  
59          SearchEngineUtil.addDocument(CompanyConstants.SYSTEM, doc);
60      }
61  
62      public static void cleanIndex() throws SearchException {
63          SearchEngineUtil.deletePortletDocuments(
64              CompanyConstants.SYSTEM, PORTLET_ID);
65      }
66  
67      public static Document getPluginPackageDocument(
68          String moduleId, String name, String version, Date modifiedDate,
69          String author, List<String> types, List<String> tags,
70          List<License> licenses, List<String> liferayVersions,
71          String shortDescription, String longDescription, String changeLog,
72          String pageURL, String repositoryURL, String status,
73          String installedVersion) {
74  
75          ModuleId moduleIdObj = ModuleId.getInstance(moduleId);
76  
77          shortDescription = HtmlUtil.extractText(shortDescription);
78          longDescription = HtmlUtil.extractText(longDescription);
79  
80          String content =
81              name + " " + author + " " + shortDescription + " " +
82                  longDescription;
83  
84          Document doc = new DocumentImpl();
85  
86          doc.addUID(PORTLET_ID, moduleId);
87  
88          doc.addModifiedDate(modifiedDate);
89  
90          doc.addKeyword(Field.PORTLET_ID, PORTLET_ID);
91          doc.addKeyword(Field.GROUP_ID, moduleIdObj.getGroupId());
92  
93          doc.addText(Field.TITLE, name);
94          doc.addText(Field.CONTENT, content);
95  
96          doc.addKeyword("moduleId", moduleId);
97          doc.addKeyword("artifactId", moduleIdObj.getArtifactId());
98          doc.addKeyword("version", version);
99          doc.addText("author", author);
100         doc.addKeyword("type", types.toArray(new String[0]));
101         doc.addKeyword("tag", tags.toArray(new String[0]));
102 
103         String[] licenseNames = new String[licenses.size()];
104 
105         boolean osiLicense = false;
106 
107         for (int i = 0; i < licenses.size(); i++) {
108             License license = licenses.get(i);
109 
110             licenseNames[i] = license.getName();
111 
112             if (license.isOsiApproved()) {
113                 osiLicense = true;
114             }
115         }
116 
117         doc.addKeyword("license", licenseNames);
118         doc.addKeyword("osi-approved-license", String.valueOf(osiLicense));
119         doc.addText("shortDescription", shortDescription);
120         doc.addText("longDescription", longDescription);
121         doc.addText("changeLog", changeLog);
122         doc.addText("pageURL", pageURL);
123         doc.addKeyword("repositoryURL", repositoryURL);
124         doc.addKeyword("status", status);
125         doc.addKeyword("installedVersion", installedVersion);
126 
127         return doc;
128     }
129 
130     public static String getPluginPackagerUID(String moduleId) {
131         Document doc = new DocumentImpl();
132 
133         doc.addUID(PORTLET_ID, moduleId);
134 
135         return doc.get(Field.UID);
136     }
137 
138     public static void removePluginPackage(String moduleId)
139         throws SearchException {
140 
141         SearchEngineUtil.deleteDocument(
142             CompanyConstants.SYSTEM, getPluginPackagerUID(moduleId));
143     }
144 
145     public static void updatePluginPackage(
146             String moduleId, String name, String version, Date modifiedDate,
147             String author, List<String> types, List<String> tags,
148             List<License> licenses, List<String> liferayVersions,
149             String shortDescription, String longDescription, String changeLog,
150             String pageURL, String repositoryURL, String status,
151             String installedVersion)
152         throws SearchException {
153 
154         Document doc = getPluginPackageDocument(
155             moduleId, name, version, modifiedDate, author, types, tags,
156             licenses, liferayVersions, shortDescription, longDescription,
157             changeLog, pageURL, repositoryURL, status, installedVersion);
158 
159         SearchEngineUtil.updateDocument(
160             CompanyConstants.SYSTEM, doc.get(Field.UID), doc);
161     }
162 
163     public DocumentSummary getDocumentSummary(
164         com.liferay.portal.kernel.search.Document doc, PortletURL portletURL) {
165 
166         // Title
167 
168         String title = doc.get(Field.TITLE);
169 
170         // Content
171 
172         String content = doc.get(Field.CONTENT);
173 
174         content = StringUtil.shorten(content, 200);
175 
176         // Portlet URL
177 
178         String moduleId = doc.get("moduleId");
179         String repositoryURL = doc.get("repositoryURL");
180 
181         portletURL.setParameter(
182             "struts_action", "/admin/view");
183         portletURL.setParameter("tabs2", "repositories");
184         portletURL.setParameter("moduleId", moduleId);
185         portletURL.setParameter("repositoryURL", repositoryURL);
186 
187         return new DocumentSummary(title, content, portletURL);
188     }
189 
190     public void reIndex(String[] ids) throws SearchException {
191         try {
192             PluginPackageUtil.reIndex();
193         }
194         catch (Exception e) {
195             throw new SearchException(e);
196         }
197     }
198 
199 }