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