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