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.kernel.util.Validator;
28  import com.liferay.portal.model.CompanyConstants;
29  
30  import java.util.Date;
31  import java.util.List;
32  
33  import javax.portlet.PortletURL;
34  
35  /**
36   * <a href="PluginPackageIndexer.java.html"><b><i>View Source</i></b></a>
37   *
38   * @author Jorge Ferrer
39   * @author Brian Wing Shun Chan
40   * @author Bruno Farache
41   * @author Raymond Augé
42   */
43  public class PluginPackageIndexer implements Indexer {
44  
45      public static final String PORTLET_ID = "PluginPackageIndexer";
46  
47      public static void addPluginPackage(
48              String moduleId, String name, String version, Date modifiedDate,
49              String author, List<String> types, List<String> tags,
50              List<License> licenses, List<String> liferayVersions,
51              String shortDescription, String longDescription, String changeLog,
52              String pageURL, String repositoryURL, String status,
53              String installedVersion)
54          throws SearchException {
55  
56          Document doc = getPluginPackageDocument(
57              moduleId, name, version, modifiedDate, author, types, tags,
58              licenses, liferayVersions, shortDescription, longDescription,
59              changeLog, pageURL, repositoryURL, status, installedVersion);
60  
61          SearchEngineUtil.addDocument(CompanyConstants.SYSTEM, doc);
62      }
63  
64      public static void cleanIndex() throws SearchException {
65          SearchEngineUtil.deletePortletDocuments(
66              CompanyConstants.SYSTEM, PORTLET_ID);
67      }
68  
69      public static Document getPluginPackageDocument(
70          String moduleId, String name, String version, Date modifiedDate,
71          String author, List<String> types, List<String> tags,
72          List<License> licenses, List<String> liferayVersions,
73          String shortDescription, String longDescription, String changeLog,
74          String pageURL, String repositoryURL, String status,
75          String installedVersion) {
76  
77          ModuleId moduleIdObj = ModuleId.getInstance(moduleId);
78  
79          shortDescription = HtmlUtil.extractText(shortDescription);
80          longDescription = HtmlUtil.extractText(longDescription);
81  
82          String content =
83              name + " " + author + " " + shortDescription + " " +
84                  longDescription;
85  
86          Document doc = new DocumentImpl();
87  
88          doc.addUID(PORTLET_ID, moduleId);
89  
90          doc.addModifiedDate(modifiedDate);
91  
92          doc.addKeyword(Field.PORTLET_ID, PORTLET_ID);
93          doc.addKeyword(Field.GROUP_ID, moduleIdObj.getGroupId());
94  
95          doc.addText(Field.TITLE, name);
96          doc.addText(Field.CONTENT, content);
97  
98          doc.addKeyword("moduleId", moduleId);
99          doc.addKeyword("artifactId", moduleIdObj.getArtifactId());
100         doc.addKeyword("version", version);
101         doc.addText("author", author);
102         doc.addKeyword("type", types.toArray(new String[0]));
103         doc.addKeyword("tag", tags.toArray(new String[0]));
104 
105         String[] licenseNames = new String[licenses.size()];
106 
107         boolean osiLicense = false;
108 
109         for (int i = 0; i < licenses.size(); i++) {
110             License license = licenses.get(i);
111 
112             licenseNames[i] = license.getName();
113 
114             if (license.isOsiApproved()) {
115                 osiLicense = true;
116             }
117         }
118 
119         doc.addKeyword("license", licenseNames);
120         doc.addKeyword("osi-approved-license", String.valueOf(osiLicense));
121         doc.addText("shortDescription", shortDescription);
122         doc.addText("longDescription", longDescription);
123         doc.addText("changeLog", changeLog);
124         doc.addText("pageURL", pageURL);
125         doc.addKeyword("repositoryURL", repositoryURL);
126         doc.addKeyword("status", status);
127         doc.addKeyword("installedVersion", installedVersion);
128 
129         return doc;
130     }
131 
132     public static String getPluginPackagerUID(String moduleId) {
133         Document doc = new DocumentImpl();
134 
135         doc.addUID(PORTLET_ID, moduleId);
136 
137         return doc.get(Field.UID);
138     }
139 
140     public static void removePluginPackage(String moduleId)
141         throws SearchException {
142 
143         SearchEngineUtil.deleteDocument(
144             CompanyConstants.SYSTEM, getPluginPackagerUID(moduleId));
145     }
146 
147     public static void updatePluginPackage(
148             String moduleId, String name, String version, Date modifiedDate,
149             String author, List<String> types, List<String> tags,
150             List<License> licenses, List<String> liferayVersions,
151             String shortDescription, String longDescription, String changeLog,
152             String pageURL, String repositoryURL, String status,
153             String installedVersion)
154         throws SearchException {
155 
156         Document doc = getPluginPackageDocument(
157             moduleId, name, version, modifiedDate, author, types, tags,
158             licenses, liferayVersions, shortDescription, longDescription,
159             changeLog, pageURL, repositoryURL, status, installedVersion);
160 
161         SearchEngineUtil.updateDocument(
162             CompanyConstants.SYSTEM, doc.get(Field.UID), doc);
163     }
164 
165     public String[] getClassNames() {
166         return _CLASS_NAMES;
167     }
168 
169     public DocumentSummary getDocumentSummary(
170         Document doc, String snippet, PortletURL portletURL) {
171 
172         // Title
173 
174         String title = doc.get(Field.TITLE);
175 
176         // Content
177 
178         String content = snippet;
179 
180         if (Validator.isNull(snippet)) {
181             content = StringUtil.shorten(doc.get(Field.CONTENT), 200);
182         }
183 
184         // Portlet URL
185 
186         String moduleId = doc.get("moduleId");
187         String repositoryURL = doc.get("repositoryURL");
188 
189         portletURL.setParameter(
190             "struts_action", "/admin/view");
191         portletURL.setParameter("tabs2", "repositories");
192         portletURL.setParameter("moduleId", moduleId);
193         portletURL.setParameter("repositoryURL", repositoryURL);
194 
195         return new DocumentSummary(title, content, portletURL);
196     }
197 
198     public void reIndex(String className, long classPK) {
199     }
200 
201     public void reIndex(String[] ids) throws SearchException {
202         try {
203             PluginPackageUtil.reIndex();
204         }
205         catch (Exception e) {
206             throw new SearchException(e);
207         }
208     }
209 
210     private static final String[] _CLASS_NAMES = new String[0];
211 
212 }