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.kernel.util.Validator;
35  import com.liferay.portal.model.CompanyConstants;
36  import com.liferay.util.License;
37  
38  import java.util.Date;
39  import java.util.List;
40  
41  import javax.portlet.PortletURL;
42  
43  /**
44   * <a href="PluginPackageIndexer.java.html"><b><i>View Source</i></b></a>
45   *
46   * @author Jorge Ferrer
47   * @author Brian Wing Shun Chan
48   * @author Bruno Farache
49   * @author Raymond Augé
50   */
51  public class PluginPackageIndexer implements Indexer {
52  
53      public static final String PORTLET_ID = "PluginPackageIndexer";
54  
55      public static void addPluginPackage(
56              String moduleId, String name, String version, Date modifiedDate,
57              String author, List<String> types, List<String> tags, List licenses,
58              List liferayVersions, String shortDescription,
59              String longDescription, String changeLog, String pageURL,
60              String repositoryURL, String status, String installedVersion)
61          throws SearchException {
62  
63          Document doc = getPluginPackageDocument(
64              moduleId, name, version, modifiedDate, author, types, tags,
65              licenses, liferayVersions, shortDescription, longDescription,
66              changeLog, pageURL, repositoryURL, status, installedVersion);
67  
68          SearchEngineUtil.addDocument(CompanyConstants.SYSTEM, doc);
69      }
70  
71      public static void cleanIndex() throws SearchException {
72          SearchEngineUtil.deletePortletDocuments(
73              CompanyConstants.SYSTEM, PORTLET_ID);
74      }
75  
76      public static Document getPluginPackageDocument(
77          String moduleId, String name, String version, Date modifiedDate,
78          String author, List<String> types, List<String> tags, List licenses,
79          List liferayVersions, String shortDescription,
80          String longDescription, String changeLog, String pageURL,
81          String repositoryURL, String status, String installedVersion) {
82  
83          ModuleId moduleIdObj = ModuleId.getInstance(moduleId);
84  
85          shortDescription = HtmlUtil.extractText(shortDescription);
86          longDescription = HtmlUtil.extractText(longDescription);
87  
88          String content =
89              name + " " + author + " " + shortDescription + " " +
90                  longDescription;
91  
92          Document doc = new DocumentImpl();
93  
94          doc.addUID(PORTLET_ID, moduleId);
95  
96          doc.addModifiedDate(modifiedDate);
97  
98          doc.addKeyword(Field.PORTLET_ID, PORTLET_ID);
99          doc.addKeyword(Field.GROUP_ID, moduleIdObj.getGroupId());
100 
101         doc.addText(Field.TITLE, name);
102         doc.addText(Field.CONTENT, content);
103 
104         doc.addKeyword("moduleId", moduleId);
105         doc.addKeyword("artifactId", moduleIdObj.getArtifactId());
106         doc.addKeyword("version", version);
107         doc.addText("author", author);
108         doc.addKeyword("type", types.toArray(new String[0]));
109         doc.addKeyword("tag", tags.toArray(new String[0]));
110 
111         String[] licenseNames = new String[licenses.size()];
112 
113         boolean osiLicense = false;
114 
115         for (int i = 0; i < licenses.size(); i++) {
116             License license = (License)licenses.get(i);
117 
118             licenseNames[i] = license.getName();
119 
120             if (license.isOsiApproved()) {
121                 osiLicense = true;
122             }
123         }
124 
125         doc.addKeyword("license", licenseNames);
126         doc.addKeyword("osi-approved-license", String.valueOf(osiLicense));
127         doc.addText("shortDescription", shortDescription);
128         doc.addText("longDescription", longDescription);
129         doc.addText("changeLog", changeLog);
130         doc.addText("pageURL", pageURL);
131         doc.addKeyword("repositoryURL", repositoryURL);
132         doc.addKeyword("status", status);
133         doc.addKeyword("installedVersion", installedVersion);
134 
135         return doc;
136     }
137 
138     public static String getPluginPackagerUID(String moduleId) {
139         Document doc = new DocumentImpl();
140 
141         doc.addUID(PORTLET_ID, moduleId);
142 
143         return doc.get(Field.UID);
144     }
145 
146     public static void removePluginPackage(String moduleId)
147         throws SearchException {
148 
149         SearchEngineUtil.deleteDocument(
150             CompanyConstants.SYSTEM, getPluginPackagerUID(moduleId));
151     }
152 
153     public static void updatePluginPackage(
154             String moduleId, String name, String version, Date modifiedDate,
155             String author, List<String> types, List<String> tags, List licenses,
156             List liferayVersions, String shortDescription,
157             String longDescription, String changeLog, String pageURL,
158             String repositoryURL, String status, String installedVersion)
159         throws SearchException {
160 
161         Document doc = getPluginPackageDocument(
162             moduleId, name, version, modifiedDate, author, types, tags,
163             licenses, liferayVersions, shortDescription, longDescription,
164             changeLog, pageURL, repositoryURL, status, installedVersion);
165 
166         SearchEngineUtil.updateDocument(
167             CompanyConstants.SYSTEM, doc.get(Field.UID), doc);
168     }
169 
170     public String[] getClassNames() {
171         return _CLASS_NAMES;
172     }
173 
174     public DocumentSummary getDocumentSummary(
175         Document doc, String snippet, PortletURL portletURL) {
176 
177         // Title
178 
179         String title = doc.get(Field.TITLE);
180 
181         // Content
182 
183         String content = snippet;
184 
185         if (Validator.isNull(snippet)) {
186             content = StringUtil.shorten(doc.get(Field.CONTENT), 200);
187         }
188 
189         // Portlet URL
190 
191         String moduleId = doc.get("moduleId");
192         String repositoryURL = doc.get("repositoryURL");
193 
194         portletURL.setParameter(
195             "struts_action", "/admin/view");
196         portletURL.setParameter("tabs2", "repositories");
197         portletURL.setParameter("moduleId", moduleId);
198         portletURL.setParameter("repositoryURL", repositoryURL);
199 
200         return new DocumentSummary(title, content, portletURL);
201     }
202 
203     public void reIndex(String className, long classPK) {
204     }
205 
206     public void reIndex(String[] ids) throws SearchException {
207         try {
208             PluginPackageUtil.reIndex();
209         }
210         catch (Exception e) {
211             throw new SearchException(e);
212         }
213     }
214 
215     private static final String[] _CLASS_NAMES = new String[0];
216 
217 }