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