1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.plugin;
24  
25  import com.liferay.portal.kernel.search.Document;
26  import com.liferay.portal.kernel.search.DocumentImpl;
27  import com.liferay.portal.kernel.search.DocumentSummary;
28  import com.liferay.portal.kernel.search.Field;
29  import com.liferay.portal.kernel.search.Indexer;
30  import com.liferay.portal.kernel.search.SearchEngineUtil;
31  import com.liferay.portal.kernel.search.SearchException;
32  import com.liferay.portal.kernel.util.HtmlUtil;
33  import com.liferay.portal.kernel.util.StringUtil;
34  import com.liferay.portal.model.CompanyConstants;
35  import com.liferay.util.License;
36  
37  import java.util.Date;
38  import java.util.List;
39  
40  import javax.portlet.PortletURL;
41  
42  /**
43   * <a href="PluginPackageIndexer.java.html"><b><i>View Source</i></b></a>
44   *
45   * @author Jorge Ferrer
46   * @author Brian Wing Shun Chan
47   * @author Bruno Farache
48   * @author Raymond Augé
49   *
50   */
51  public class PluginPackageIndexer implements Indexer {
52  
53      public static final String PORTLET_ID = "PluginPackageIndexer";
54  
55      public static void addPluginPackage(
56              String moduleId, String name, String version, Date modifiedDate,
57              String author, List<String> types, List<String> tags, List licenses,
58              List liferayVersions, String shortDescription,
59              String longDescription, String changeLog, String pageURL,
60              String repositoryURL, String status, String installedVersion)
61          throws SearchException {
62  
63          Document doc = getPluginPackageDocument(
64              moduleId, name, version, modifiedDate, author, types, tags,
65              licenses, liferayVersions, shortDescription, longDescription,
66              changeLog, pageURL, repositoryURL, status, installedVersion);
67  
68          SearchEngineUtil.addDocument(CompanyConstants.SYSTEM, doc);
69      }
70  
71      public static void cleanIndex() throws SearchException {
72          SearchEngineUtil.deletePortletDocuments(
73              CompanyConstants.SYSTEM, PORTLET_ID);
74      }
75  
76      public static Document getPluginPackageDocument(
77          String moduleId, String name, String version, Date modifiedDate,
78          String author, List<String> types, List<String> tags, List licenses,
79          List liferayVersions, String shortDescription,
80          String longDescription, String changeLog, String pageURL,
81          String repositoryURL, String status, String installedVersion) {
82  
83          ModuleId moduleIdObj = ModuleId.getInstance(moduleId);
84  
85          shortDescription = HtmlUtil.extractText(shortDescription);
86          longDescription = HtmlUtil.extractText(longDescription);
87  
88          String content =
89              name + " " + author + " " + shortDescription + " " +
90                  longDescription;
91  
92          Document doc = new DocumentImpl();
93  
94          doc.addUID(PORTLET_ID, moduleId);
95  
96          doc.addModifiedDate(modifiedDate);
97  
98          doc.addKeyword(Field.PORTLET_ID, PORTLET_ID);
99          doc.addKeyword(Field.GROUP_ID, moduleIdObj.getGroupId());
100 
101         doc.addText(Field.TITLE, name);
102         doc.addText(Field.CONTENT, content);
103 
104         doc.addKeyword("moduleId", moduleId);
105         doc.addKeyword("artifactId", moduleIdObj.getArtifactId());
106         doc.addKeyword("version", version);
107         doc.addText("author", author);
108         doc.addKeyword("type", types.toArray(new String[0]));
109         doc.addKeyword("tag", tags.toArray(new String[0]));
110 
111         String[] licenseNames = new String[licenses.size()];
112 
113         boolean osiLicense = false;
114 
115         for (int i = 0; i < licenses.size(); i++) {
116             License license = (License)licenses.get(i);
117 
118             licenseNames[i] = license.getName();
119 
120             if (license.isOsiApproved()) {
121                 osiLicense = true;
122             }
123         }
124 
125         doc.addKeyword("license", licenseNames);
126         doc.addKeyword("osi-approved-license", String.valueOf(osiLicense));
127         doc.addText("shortDescription", shortDescription);
128         doc.addText("longDescription", longDescription);
129         doc.addText("changeLog", changeLog);
130         doc.addText("pageURL", pageURL);
131         doc.addKeyword("repositoryURL", repositoryURL);
132         doc.addKeyword("status", status);
133         doc.addKeyword("installedVersion", installedVersion);
134 
135         return doc;
136     }
137 
138     public static String getPluginPackagerUID(String moduleId) {
139         Document doc = new DocumentImpl();
140 
141         doc.addUID(PORTLET_ID, moduleId);
142 
143         return doc.get(Field.UID);
144     }
145 
146     public static void removePluginPackage(String moduleId)
147         throws SearchException {
148 
149         SearchEngineUtil.deleteDocument(
150             CompanyConstants.SYSTEM, getPluginPackagerUID(moduleId));
151     }
152 
153     public static void updatePluginPackage(
154             String moduleId, String name, String version, Date modifiedDate,
155             String author, List<String> types, List<String> tags, List licenses,
156             List liferayVersions, String shortDescription,
157             String longDescription, String changeLog, String pageURL,
158             String repositoryURL, String status, String installedVersion)
159         throws SearchException {
160 
161         Document doc = getPluginPackageDocument(
162             moduleId, name, version, modifiedDate, author, types, tags,
163             licenses, liferayVersions, shortDescription, longDescription,
164             changeLog, pageURL, repositoryURL, status, installedVersion);
165 
166         SearchEngineUtil.updateDocument(
167             CompanyConstants.SYSTEM, doc.get(Field.UID), doc);
168     }
169 
170     public String[] getClassNames() {
171         return _CLASS_NAMES;
172     }
173 
174     public DocumentSummary getDocumentSummary(
175         com.liferay.portal.kernel.search.Document doc, PortletURL portletURL) {
176 
177         // Title
178 
179         String title = doc.get(Field.TITLE);
180 
181         // Content
182 
183         String content = doc.get(Field.CONTENT);
184 
185         content = StringUtil.shorten(content, 200);
186 
187         // Portlet URL
188 
189         String moduleId = doc.get("moduleId");
190         String repositoryURL = doc.get("repositoryURL");
191 
192         portletURL.setParameter(
193             "struts_action", "/admin/view");
194         portletURL.setParameter("tabs2", "repositories");
195         portletURL.setParameter("moduleId", moduleId);
196         portletURL.setParameter("repositoryURL", repositoryURL);
197 
198         return new DocumentSummary(title, content, portletURL);
199     }
200 
201     public void reIndex(String className, long classPK) {
202     }
203 
204     public void reIndex(String[] ids) throws SearchException {
205         try {
206             PluginPackageUtil.reIndex();
207         }
208         catch (Exception e) {
209             throw new SearchException(e);
210         }
211     }
212 
213     private static final String[] _CLASS_NAMES = new String[0];
214 
215 }