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