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.DocumentSummary;
26  import com.liferay.portal.kernel.search.Indexer;
27  import com.liferay.portal.kernel.search.SearchException;
28  import com.liferay.portal.kernel.util.StringMaker;
29  import com.liferay.portal.kernel.util.StringPool;
30  import com.liferay.portal.kernel.util.StringUtil;
31  import com.liferay.portal.lucene.LuceneFields;
32  import com.liferay.portal.lucene.LuceneUtil;
33  import com.liferay.portal.model.impl.CompanyImpl;
34  import com.liferay.util.Html;
35  import com.liferay.util.License;
36  
37  import java.io.IOException;
38  
39  import java.util.Date;
40  import java.util.Iterator;
41  import java.util.List;
42  
43  import javax.portlet.PortletURL;
44  
45  import org.apache.lucene.document.Document;
46  import org.apache.lucene.index.IndexWriter;
47  import org.apache.lucene.index.Term;
48  
49  /**
50   * <a href="PluginPackageIndexer.java.html"><b><i>View Source</i></b></a>
51   *
52   * @author Jorge Ferrer
53   * @author Brian Wing Shun Chan
54   *
55   */
56  public class PluginPackageIndexer implements Indexer {
57  
58      public static final String PORTLET_ID = "PluginPackageIndexer";
59  
60      public static void addPluginPackage(
61              String moduleId, String name, String version, Date modifiedDate,
62              String author, List types, List tags, List licenses,
63              List liferayVersions, String shortDescription,
64              String longDescription, String changeLog, String pageURL,
65              String repositoryURL, String status, String installedVersion)
66          throws IOException {
67  
68          Document doc = getAddPluginPackageDocument(
69              moduleId, name, version, modifiedDate, author, types, tags,
70              licenses, liferayVersions, shortDescription, longDescription,
71              changeLog, pageURL, repositoryURL, status, installedVersion);
72  
73          IndexWriter writer = null;
74  
75          try {
76              writer = LuceneUtil.getWriter(CompanyImpl.SYSTEM);
77  
78              writer.addDocument(doc);
79          }
80          finally {
81              if (writer != null) {
82                  LuceneUtil.write(CompanyImpl.SYSTEM);
83              }
84          }
85      }
86  
87      public static void cleanIndex() throws IOException {
88          LuceneUtil.deleteDocuments(
89              CompanyImpl.SYSTEM, new Term(LuceneFields.PORTLET_ID, PORTLET_ID));
90      }
91  
92      public static Document getAddPluginPackageDocument(
93          String moduleId, String name, String version, Date modifiedDate,
94          String author, List types, List tags, List licenses,
95          List liferayVersions, String shortDescription,
96          String longDescription, String changeLog, String pageURL,
97          String repositoryURL, String status, String installedVersion) {
98  
99          ModuleId moduleIdObj = ModuleId.getInstance(moduleId);
100 
101         shortDescription = Html.stripHtml(shortDescription);
102         longDescription = Html.stripHtml(longDescription);
103 
104         String content =
105             name + " " + author + " " + shortDescription + " " +
106                 longDescription;
107 
108         Document doc = new Document();
109 
110         doc.add(
111             LuceneFields.getKeyword(
112                 LuceneFields.UID, LuceneFields.getUID(PORTLET_ID, moduleId)));
113 
114         doc.add(LuceneFields.getKeyword(LuceneFields.PORTLET_ID, PORTLET_ID));
115 
116         doc.add(LuceneFields.getText(LuceneFields.TITLE, name));
117         doc.add(LuceneFields.getText(LuceneFields.CONTENT, content));
118 
119         doc.add(LuceneFields.getDate(LuceneFields.MODIFIED));
120 
121         doc.add(LuceneFields.getKeyword("moduleId", moduleId));
122         doc.add(LuceneFields.getKeyword("groupId", moduleIdObj.getGroupId()));
123         doc.add(
124             LuceneFields.getKeyword("artifactId", moduleIdObj.getArtifactId()));
125         doc.add(LuceneFields.getKeyword("version", version));
126         doc.add(LuceneFields.getDate("modified-date", modifiedDate));
127         doc.add(LuceneFields.getKeyword("shortDescription", shortDescription));
128         doc.add(LuceneFields.getKeyword("changeLog", changeLog));
129         doc.add(LuceneFields.getKeyword("repositoryURL", repositoryURL));
130 
131         StringMaker sm = new StringMaker();
132 
133         Iterator itr = types.iterator();
134 
135         while (itr.hasNext()) {
136             String type = (String)itr.next();
137 
138             doc.add(LuceneFields.getKeyword("type", type));
139 
140             sm.append(type);
141 
142             if (itr.hasNext()) {
143                 sm.append(StringPool.COMMA);
144                 sm.append(StringPool.SPACE);
145             }
146         }
147 
148         doc.add(LuceneFields.getKeyword("types", sm.toString()));
149 
150         sm = new StringMaker();
151 
152         itr = tags.iterator();
153 
154         while (itr.hasNext()) {
155             String tag = (String)itr.next();
156 
157             doc.add(LuceneFields.getKeyword("tag", tag));
158 
159             sm.append(tag);
160 
161             if (itr.hasNext()) {
162                 sm.append(StringPool.COMMA);
163                 sm.append(StringPool.SPACE);
164             }
165         }
166 
167         doc.add(LuceneFields.getKeyword("tags", sm.toString()));
168 
169         boolean osiLicense = false;
170 
171         itr = licenses.iterator();
172 
173         while (itr.hasNext()) {
174             License license = (License)itr.next();
175 
176             doc.add(LuceneFields.getKeyword("license", license.getName()));
177 
178             if (license.isOsiApproved()) {
179                 osiLicense = true;
180             }
181         }
182 
183         doc.add(
184             LuceneFields.getKeyword(
185                 "osi-approved-license", String.valueOf(osiLicense)));
186 
187         doc.add(LuceneFields.getKeyword("status", status));
188 
189         if (installedVersion != null) {
190             doc.add(
191                 LuceneFields.getKeyword("installedVersion", installedVersion));
192         }
193 
194         return doc;
195     }
196 
197     public static void removePluginPackage(String moduleId)
198         throws IOException {
199 
200         LuceneUtil.deleteDocuments(
201             CompanyImpl.SYSTEM,
202             new Term(
203                 LuceneFields.UID, LuceneFields.getUID(PORTLET_ID, moduleId)));
204     }
205 
206     public static void updatePluginPackage(
207             String moduleId, String name, String version, Date modifiedDate,
208             String author, List types, List tags, List licenses,
209             List liferayVersions, String shortDescription,
210             String longDescription, String changeLog, String pageURL,
211             String repositoryURL, String status, String installedVersion)
212         throws IOException {
213 
214         try {
215             removePluginPackage(moduleId);
216         }
217         catch (IOException ioe) {
218         }
219 
220         addPluginPackage(
221             moduleId, name, version, modifiedDate, author, types, tags,
222             licenses, liferayVersions, shortDescription, longDescription,
223             changeLog, pageURL, repositoryURL, status, installedVersion);
224     }
225 
226     public DocumentSummary getDocumentSummary(
227         com.liferay.portal.kernel.search.Document doc, PortletURL portletURL) {
228 
229         // Title
230 
231         String title = doc.get(LuceneFields.TITLE);
232 
233         // Content
234 
235         String content = doc.get(LuceneFields.CONTENT);
236 
237         content = StringUtil.shorten(content, 200);
238 
239         // URL
240 
241         String moduleId = doc.get("moduleId");
242         String repositoryURL = doc.get("repositoryURL");
243 
244         portletURL.setParameter(
245             "struts_action", "/admin/view");
246         portletURL.setParameter("tabs2", "repositories");
247         portletURL.setParameter("moduleId", moduleId);
248         portletURL.setParameter("repositoryURL", repositoryURL);
249 
250         return new DocumentSummary(title, content, portletURL);
251     }
252 
253     public void reIndex(String[] ids) throws SearchException {
254         try {
255             PluginPackageUtil.reIndex();
256         }
257         catch (Exception e) {
258             throw new SearchException(e);
259         }
260     }
261 
262 }