1   /**
2    * Copyright (c) 2000-2009 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.portlet.softwarecatalog.util;
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.SearchEngineUtil;
30  import com.liferay.portal.kernel.search.SearchException;
31  import com.liferay.portal.kernel.util.HtmlUtil;
32  import com.liferay.portal.kernel.util.StringUtil;
33  import com.liferay.portal.util.PortalUtil;
34  import com.liferay.portal.util.PortletKeys;
35  import com.liferay.portlet.softwarecatalog.service.SCProductEntryLocalServiceUtil;
36  
37  import java.util.Date;
38  
39  import javax.portlet.PortletURL;
40  
41  /**
42   * <a href="Indexer.java.html"><b><i>View Source</i></b></a>
43   *
44   * @author Jorge Ferrer
45   * @author Brian Wing Shun Chan
46   * @author Harry Mark
47   * @author Bruno Farache
48   */
49  public class Indexer
50      implements com.liferay.portal.kernel.search.Indexer {
51  
52      public static final String PORTLET_ID = PortletKeys.SOFTWARE_CATALOG;
53  
54      public static void addProductEntry(
55              long companyId, long groupId, long userId, String userName,
56              long productEntryId, String name, Date modifiedDate, String version,
57              String type, String shortDescription, String longDescription,
58              String pageURL, String repoGroupId, String repoArtifactId)
59          throws SearchException {
60  
61          Document doc = getProductEntryDocument(
62              companyId, groupId, userId, userName, productEntryId, name,
63              modifiedDate, version, type, shortDescription, longDescription,
64              pageURL, repoGroupId, repoArtifactId);
65  
66          SearchEngineUtil.addDocument(companyId, doc);
67      }
68  
69      public static void deleteProductEntry(long companyId, long productEntryId)
70          throws SearchException {
71  
72          SearchEngineUtil.deleteDocument(companyId, getEntryUID(productEntryId));
73      }
74  
75      public static Document getProductEntryDocument(
76          long companyId, long groupId, long userId, String userName,
77          long productEntryId, String name, Date modifiedDate, String version,
78          String type, String shortDescription, String longDescription,
79          String pageURL, String repoGroupId, String repoArtifactId) {
80  
81          userName = PortalUtil.getUserName(userId, userName);
82          shortDescription = HtmlUtil.extractText(shortDescription);
83          longDescription = HtmlUtil.extractText(longDescription);
84  
85          String content =
86              userId + " " + userName + " " + type + " " + shortDescription +
87                  " " + longDescription + " " + pageURL + repoGroupId + " " +
88                      repoArtifactId;
89  
90          Document doc = new DocumentImpl();
91  
92          doc.addUID(PORTLET_ID, productEntryId);
93  
94          doc.addModifiedDate(modifiedDate);
95  
96          doc.addKeyword(Field.COMPANY_ID, companyId);
97          doc.addKeyword(Field.PORTLET_ID, PORTLET_ID);
98          doc.addKeyword(Field.GROUP_ID, groupId);
99          doc.addKeyword(Field.USER_ID, userId);
100         doc.addText(Field.USER_NAME, userName);
101 
102         doc.addText(Field.TITLE, name);
103         doc.addText(Field.CONTENT, content);
104 
105         doc.addKeyword(Field.ENTRY_CLASS_PK, productEntryId);
106         doc.addKeyword("version", version);
107         doc.addKeyword("type", type);
108         doc.addText("shortDescription", shortDescription);
109         doc.addText("longDescription", longDescription);
110         doc.addText("pageURL", pageURL);
111         doc.addKeyword("repoGroupId", repoGroupId);
112         doc.addKeyword("repoArtifactId", repoArtifactId);
113 
114         return doc;
115     }
116 
117     public static String getEntryUID(long productEntryId) {
118         Document doc = new DocumentImpl();
119 
120         doc.addUID(PORTLET_ID, productEntryId);
121 
122         return doc.get(Field.UID);
123     }
124 
125     public static void updateProductEntry(
126             long companyId, long groupId, long userId, String userName,
127             long productEntryId, String name, Date modifiedDate, String version,
128             String type, String shortDescription, String longDescription,
129             String pageURL, String repoGroupId, String repoArtifactId)
130         throws SearchException {
131 
132         Document doc = getProductEntryDocument(
133             companyId, groupId, userId, userName, productEntryId, name,
134             modifiedDate, version, type, shortDescription, longDescription,
135             pageURL, repoGroupId, repoArtifactId);
136 
137         SearchEngineUtil.updateDocument(companyId, doc.get(Field.UID), doc);
138     }
139 
140     public DocumentSummary getDocumentSummary(
141         com.liferay.portal.kernel.search.Document doc, PortletURL portletURL) {
142 
143         // Title
144 
145         String title = doc.get(Field.TITLE);
146 
147         // Content
148 
149         String content = doc.get(Field.CONTENT);
150 
151         content = StringUtil.shorten(content, 200);
152 
153         // Portlet URL
154 
155         String productEntryId = doc.get(Field.ENTRY_CLASS_PK);
156 
157         portletURL.setParameter(
158             "struts_action", "/software_catalog/view_product_entry");
159         portletURL.setParameter("productEntryId", productEntryId);
160 
161         return new DocumentSummary(title, content, portletURL);
162     }
163 
164     public void reIndex(String[] ids) throws SearchException {
165         try {
166             SCProductEntryLocalServiceUtil.reIndex(ids);
167         }
168         catch (Exception e) {
169             throw new SearchException(e);
170         }
171     }
172 
173 }