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