1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.softwarecatalog.util;
16  
17  import com.liferay.portal.kernel.search.Document;
18  import com.liferay.portal.kernel.search.DocumentImpl;
19  import com.liferay.portal.kernel.search.DocumentSummary;
20  import com.liferay.portal.kernel.search.Field;
21  import com.liferay.portal.kernel.search.SearchEngineUtil;
22  import com.liferay.portal.kernel.search.SearchException;
23  import com.liferay.portal.kernel.util.HtmlUtil;
24  import com.liferay.portal.kernel.util.StringUtil;
25  import com.liferay.portal.util.PortalUtil;
26  import com.liferay.portal.util.PortletKeys;
27  import com.liferay.portlet.softwarecatalog.service.SCProductEntryLocalServiceUtil;
28  
29  import java.util.Date;
30  
31  import javax.portlet.PortletURL;
32  
33  /**
34   * <a href="Indexer.java.html"><b><i>View Source</i></b></a>
35   *
36   * @author Jorge Ferrer
37   * @author Brian Wing Shun Chan
38   * @author Harry Mark
39   * @author Bruno Farache
40   */
41  public class Indexer
42      implements com.liferay.portal.kernel.search.Indexer {
43  
44      public static final String PORTLET_ID = PortletKeys.SOFTWARE_CATALOG;
45  
46      public static void addProductEntry(
47              long companyId, long groupId, long userId, String userName,
48              long productEntryId, String name, Date modifiedDate, String version,
49              String type, String shortDescription, String longDescription,
50              String pageURL, String repoGroupId, String repoArtifactId)
51          throws SearchException {
52  
53          Document doc = getProductEntryDocument(
54              companyId, groupId, userId, userName, productEntryId, name,
55              modifiedDate, version, type, shortDescription, longDescription,
56              pageURL, repoGroupId, repoArtifactId);
57  
58          SearchEngineUtil.addDocument(companyId, doc);
59      }
60  
61      public static void deleteProductEntry(long companyId, long productEntryId)
62          throws SearchException {
63  
64          SearchEngineUtil.deleteDocument(companyId, getEntryUID(productEntryId));
65      }
66  
67      public static Document getProductEntryDocument(
68          long companyId, long groupId, long userId, String userName,
69          long productEntryId, String name, Date modifiedDate, String version,
70          String type, String shortDescription, String longDescription,
71          String pageURL, String repoGroupId, String repoArtifactId) {
72  
73          userName = PortalUtil.getUserName(userId, userName);
74          shortDescription = HtmlUtil.extractText(shortDescription);
75          longDescription = HtmlUtil.extractText(longDescription);
76  
77          String content =
78              userId + " " + userName + " " + type + " " + shortDescription +
79                  " " + longDescription + " " + pageURL + repoGroupId + " " +
80                      repoArtifactId;
81  
82          Document doc = new DocumentImpl();
83  
84          doc.addUID(PORTLET_ID, productEntryId);
85  
86          doc.addModifiedDate(modifiedDate);
87  
88          doc.addKeyword(Field.COMPANY_ID, companyId);
89          doc.addKeyword(Field.PORTLET_ID, PORTLET_ID);
90          doc.addKeyword(Field.GROUP_ID, groupId);
91          doc.addKeyword(Field.USER_ID, userId);
92          doc.addText(Field.USER_NAME, userName);
93  
94          doc.addText(Field.TITLE, name);
95          doc.addText(Field.CONTENT, content);
96  
97          doc.addKeyword(Field.ENTRY_CLASS_PK, productEntryId);
98          doc.addKeyword("version", version);
99          doc.addKeyword("type", type);
100         doc.addText("shortDescription", shortDescription);
101         doc.addText("longDescription", longDescription);
102         doc.addText("pageURL", pageURL);
103         doc.addKeyword("repoGroupId", repoGroupId);
104         doc.addKeyword("repoArtifactId", repoArtifactId);
105 
106         return doc;
107     }
108 
109     public static String getEntryUID(long productEntryId) {
110         Document doc = new DocumentImpl();
111 
112         doc.addUID(PORTLET_ID, productEntryId);
113 
114         return doc.get(Field.UID);
115     }
116 
117     public static void updateProductEntry(
118             long companyId, long groupId, long userId, String userName,
119             long productEntryId, String name, Date modifiedDate, String version,
120             String type, String shortDescription, String longDescription,
121             String pageURL, String repoGroupId, String repoArtifactId)
122         throws SearchException {
123 
124         Document doc = getProductEntryDocument(
125             companyId, groupId, userId, userName, productEntryId, name,
126             modifiedDate, version, type, shortDescription, longDescription,
127             pageURL, repoGroupId, repoArtifactId);
128 
129         SearchEngineUtil.updateDocument(companyId, doc.get(Field.UID), doc);
130     }
131 
132     public DocumentSummary getDocumentSummary(
133         com.liferay.portal.kernel.search.Document doc, PortletURL portletURL) {
134 
135         // Title
136 
137         String title = doc.get(Field.TITLE);
138 
139         // Content
140 
141         String content = doc.get(Field.CONTENT);
142 
143         content = StringUtil.shorten(content, 200);
144 
145         // Portlet URL
146 
147         String productEntryId = doc.get(Field.ENTRY_CLASS_PK);
148 
149         portletURL.setParameter(
150             "struts_action", "/software_catalog/view_product_entry");
151         portletURL.setParameter("productEntryId", productEntryId);
152 
153         return new DocumentSummary(title, content, portletURL);
154     }
155 
156     public void reIndex(String[] ids) throws SearchException {
157         try {
158             SCProductEntryLocalServiceUtil.reIndex(ids);
159         }
160         catch (Exception e) {
161             throw new SearchException(e);
162         }
163     }
164 
165 }