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