1   /**
2    * Copyright (c) 2000-2008 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   *
49   */
50  public class PluginPackageIndexer implements Indexer {
51  
52      public static final String PORTLET_ID = "PluginPackageIndexer";
53  
54      public static void addPluginPackage(
55              String moduleId, String name, String version, Date modifiedDate,
56              String author, List<String> types, List<String> tags, List licenses,
57              List liferayVersions, String shortDescription,
58              String longDescription, String changeLog, String pageURL,
59              String repositoryURL, String status, String installedVersion)
60          throws SearchException {
61  
62          Document doc = getPluginPackageDocument(
63              moduleId, name, version, modifiedDate, author, types, tags,
64              licenses, liferayVersions, shortDescription, longDescription,
65              changeLog, pageURL, repositoryURL, status, installedVersion);
66  
67          SearchEngineUtil.addDocument(CompanyConstants.SYSTEM, doc);
68      }
69  
70      public static void cleanIndex() throws SearchException {
71          SearchEngineUtil.deletePortletDocuments(
72              CompanyConstants.SYSTEM, PORTLET_ID);
73      }
74  
75      public static Document getPluginPackageDocument(
76          String moduleId, String name, String version, Date modifiedDate,
77          String author, List<String> types, List<String> tags, List licenses,
78          List liferayVersions, String shortDescription,
79          String longDescription, String changeLog, String pageURL,
80          String repositoryURL, String status, String installedVersion) {
81  
82          ModuleId moduleIdObj = ModuleId.getInstance(moduleId);
83  
84          shortDescription = HtmlUtil.extractText(shortDescription);
85          longDescription = HtmlUtil.extractText(longDescription);
86  
87          String content =
88              name + " " + author + " " + shortDescription + " " +
89                  longDescription;
90  
91          Document doc = new DocumentImpl();
92  
93          doc.addUID(PORTLET_ID, moduleId);
94  
95          doc.addKeyword(Field.PORTLET_ID, PORTLET_ID);
96          doc.addKeyword(Field.GROUP_ID, moduleIdObj.getGroupId());
97  
98          doc.addText(Field.TITLE, name);
99          doc.addText(Field.CONTENT, content);
100 
101         doc.addModifiedDate();
102 
103         doc.addKeyword("moduleId", moduleId);
104         doc.addKeyword("artifactId", moduleIdObj.getArtifactId());
105         doc.addKeyword("version", version);
106         doc.addDate("modified-date", modifiedDate);
107         doc.addKeyword("shortDescription", shortDescription);
108         doc.addKeyword("changeLog", changeLog);
109         doc.addKeyword("repositoryURL", repositoryURL);
110         doc.addKeyword("type", types.toArray(new String[0]));
111         doc.addKeyword("tag", tags.toArray(new String[0]));
112 
113         String[] licenseNames = new String[licenses.size()];
114 
115         boolean osiLicense = false;
116 
117         for (int i = 0; i < licenses.size(); i++) {
118             License license = (License)licenses.get(i);
119 
120             licenseNames[i] = license.getName();
121 
122             if (license.isOsiApproved()) {
123                 osiLicense = true;
124             }
125         }
126 
127         doc.addKeyword("license", licenseNames);
128         doc.addKeyword("osi-approved-license", String.valueOf(osiLicense));
129         doc.addKeyword("status", status);
130 
131         if (installedVersion != null) {
132             doc.addKeyword("installedVersion", installedVersion);
133         }
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 DocumentSummary getDocumentSummary(
171         com.liferay.portal.kernel.search.Document doc, PortletURL portletURL) {
172 
173         // Title
174 
175         String title = doc.get(Field.TITLE);
176 
177         // Content
178 
179         String content = doc.get(Field.CONTENT);
180 
181         content = StringUtil.shorten(content, 200);
182 
183         // Portlet URL
184 
185         String moduleId = doc.get("moduleId");
186         String repositoryURL = doc.get("repositoryURL");
187 
188         portletURL.setParameter(
189             "struts_action", "/admin/view");
190         portletURL.setParameter("tabs2", "repositories");
191         portletURL.setParameter("moduleId", moduleId);
192         portletURL.setParameter("repositoryURL", repositoryURL);
193 
194         return new DocumentSummary(title, content, portletURL);
195     }
196 
197     public void reIndex(String[] ids) throws SearchException {
198         try {
199             PluginPackageUtil.reIndex();
200         }
201         catch (Exception e) {
202             throw new SearchException(e);
203         }
204     }
205 
206 }