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