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.BaseIndexer;
18  import com.liferay.portal.kernel.search.BooleanClauseOccur;
19  import com.liferay.portal.kernel.search.BooleanQuery;
20  import com.liferay.portal.kernel.search.BooleanQueryFactoryUtil;
21  import com.liferay.portal.kernel.search.Document;
22  import com.liferay.portal.kernel.search.DocumentImpl;
23  import com.liferay.portal.kernel.search.Field;
24  import com.liferay.portal.kernel.search.Indexer;
25  import com.liferay.portal.kernel.search.SearchContext;
26  import com.liferay.portal.kernel.search.SearchEngineUtil;
27  import com.liferay.portal.kernel.search.Summary;
28  import com.liferay.portal.kernel.util.GetterUtil;
29  import com.liferay.portal.kernel.util.HtmlUtil;
30  import com.liferay.portal.kernel.util.StringBundler;
31  import com.liferay.portal.kernel.util.StringPool;
32  import com.liferay.portal.kernel.util.StringUtil;
33  import com.liferay.portal.kernel.util.Validator;
34  import com.liferay.portal.util.PortalUtil;
35  import com.liferay.portal.util.PortletKeys;
36  import com.liferay.portlet.expando.model.ExpandoBridge;
37  import com.liferay.portlet.expando.util.ExpandoBridgeIndexerUtil;
38  import com.liferay.portlet.softwarecatalog.model.SCProductEntry;
39  import com.liferay.portlet.softwarecatalog.model.SCProductVersion;
40  import com.liferay.portlet.softwarecatalog.service.SCProductEntryLocalServiceUtil;
41  
42  import java.util.ArrayList;
43  import java.util.Collection;
44  import java.util.Date;
45  import java.util.List;
46  
47  import javax.portlet.PortletURL;
48  
49  /**
50   * <a href="SCIndexer.java.html"><b><i>View Source</i></b></a>
51   *
52   * @author Jorge Ferrer
53   * @author Brian Wing Shun Chan
54   * @author Harry Mark
55   * @author Bruno Farache
56   * @author Raymond Augé
57   */
58  public class SCIndexer extends BaseIndexer {
59  
60      public static final String[] CLASS_NAMES = {SCProductEntry.class.getName()};
61  
62      public static final String PORTLET_ID = PortletKeys.SOFTWARE_CATALOG;
63  
64      public String[] getClassNames() {
65          return CLASS_NAMES;
66      }
67  
68      public Summary getSummary(
69          Document document, String snippet, PortletURL portletURL) {
70  
71          String title = document.get(Field.TITLE);
72  
73          String content = snippet;
74  
75          if (Validator.isNull(snippet)) {
76              content = StringUtil.shorten(document.get(Field.CONTENT), 200);
77          }
78  
79          String productEntryId = document.get(Field.ENTRY_CLASS_PK);
80  
81          portletURL.setParameter(
82              "struts_action", "/software_catalog/view_product_entry");
83          portletURL.setParameter("productEntryId", productEntryId);
84  
85          return new Summary(title, content, portletURL);
86      }
87  
88      protected void doDelete(Object obj) throws Exception {
89          SCProductEntry productEntry = (SCProductEntry)obj;
90  
91          Document document = new DocumentImpl();
92  
93          document.addUID(PORTLET_ID, productEntry.getProductEntryId());
94  
95          SearchEngineUtil.deleteDocument(
96              productEntry.getCompanyId(), document.get(Field.UID));
97      }
98  
99      protected Document doGetDocument(Object obj) throws Exception {
100         SCProductEntry productEntry = (SCProductEntry)obj;
101 
102         long companyId = productEntry.getCompanyId();
103         long groupId = getParentGroupId(productEntry.getGroupId());
104         long scopeGroupId = productEntry.getGroupId();
105         long userId = productEntry.getUserId();
106         String userName = PortalUtil.getUserName(
107             userId, productEntry.getUserName());
108         long productEntryId = productEntry.getProductEntryId();
109         String name = productEntry.getName();
110         Date modifiedDate = productEntry.getModifiedDate();
111 
112         String version = StringPool.BLANK;
113 
114         SCProductVersion latestProductVersion = productEntry.getLatestVersion();
115 
116         if (latestProductVersion != null) {
117             version = latestProductVersion.getVersion();
118         }
119 
120         String type = productEntry.getType();
121         String shortDescription = HtmlUtil.extractText(
122             productEntry.getShortDescription());
123         String longDescription = HtmlUtil.extractText(
124             productEntry.getLongDescription());
125         String pageURL = productEntry.getPageURL();
126         String repoGroupId = productEntry.getRepoGroupId();
127         String repoArtifactId = productEntry.getRepoArtifactId();
128 
129         ExpandoBridge expandoBridge = productEntry.getExpandoBridge();
130 
131         StringBundler sb = new StringBundler(15);
132 
133         sb.append(userId);
134         sb.append(StringPool.SPACE);
135         sb.append(userName);
136         sb.append(StringPool.SPACE);
137         sb.append(type);
138         sb.append(StringPool.SPACE);
139         sb.append(shortDescription);
140         sb.append(StringPool.SPACE);
141         sb.append(longDescription);
142         sb.append(StringPool.SPACE);
143         sb.append(pageURL);
144         sb.append(StringPool.SPACE);
145         sb.append(repoGroupId);
146         sb.append(StringPool.SPACE);
147         sb.append(repoArtifactId);
148 
149         String content = sb.toString();
150 
151         Document document = new DocumentImpl();
152 
153         document.addUID(PORTLET_ID, productEntryId);
154 
155         document.addModifiedDate(modifiedDate);
156 
157         document.addKeyword(Field.COMPANY_ID, companyId);
158         document.addKeyword(Field.PORTLET_ID, PORTLET_ID);
159         document.addKeyword(Field.GROUP_ID, groupId);
160         document.addKeyword(Field.SCOPE_GROUP_ID, scopeGroupId);
161         document.addKeyword(Field.USER_ID, userId);
162         document.addText(Field.USER_NAME, userName);
163 
164         document.addText(Field.TITLE, name);
165         document.addText(Field.CONTENT, content);
166 
167         document.addKeyword(
168             Field.ENTRY_CLASS_NAME, SCProductEntry.class.getName());
169         document.addKeyword(Field.ENTRY_CLASS_PK, productEntryId);
170         document.addKeyword("version", version);
171         document.addKeyword("type", type);
172         document.addText("shortDescription", shortDescription);
173         document.addText("longDescription", longDescription);
174         document.addText("pageURL", pageURL);
175         document.addKeyword("repoGroupId", repoGroupId);
176         document.addKeyword("repoArtifactId", repoArtifactId);
177 
178         ExpandoBridgeIndexerUtil.addAttributes(document, expandoBridge);
179 
180         return document;
181     }
182 
183     protected void doReindex(Object obj) throws Exception {
184         SCProductEntry productEntry = (SCProductEntry)obj;
185 
186         Document document = getDocument(productEntry);
187 
188         SearchEngineUtil.updateDocument(productEntry.getCompanyId(), document);
189     }
190 
191     protected void doReindex(String className, long classPK) throws Exception {
192         SCProductEntry productEntry =
193             SCProductEntryLocalServiceUtil.getProductEntry(classPK);
194 
195         doReindex(productEntry);
196     }
197 
198     protected void doReindex(String[] ids) throws Exception {
199         long companyId = GetterUtil.getLong(ids[0]);
200 
201         reindexProductEntries(companyId);
202     }
203 
204     protected String getPortletId(SearchContext searchContext) {
205         return PORTLET_ID;
206     }
207 
208     protected void postProcessFullQuery(
209             BooleanQuery fullQuery, SearchContext searchContext)
210         throws Exception {
211 
212         String type = (String)searchContext.getAttribute("type");
213 
214         if (Validator.isNotNull(type)) {
215             BooleanQuery searchQuery = BooleanQueryFactoryUtil.create();
216 
217             searchQuery.addRequiredTerm("type", type);
218 
219             fullQuery.add(searchQuery, BooleanClauseOccur.MUST);
220         }
221     }
222 
223     protected void reindexProductEntries(long companyId) throws Exception {
224         int count =
225             SCProductEntryLocalServiceUtil.getCompanyProductEntriesCount(
226                 companyId);
227 
228         int pages = count / Indexer.DEFAULT_INTERVAL;
229 
230         for (int i = 0; i <= pages; i++) {
231             int start = (i * Indexer.DEFAULT_INTERVAL);
232             int end = start + Indexer.DEFAULT_INTERVAL;
233 
234             reindexProductEntries(companyId, start, end);
235         }
236     }
237 
238     protected void reindexProductEntries(long companyId, int start, int end)
239         throws Exception {
240 
241         List<SCProductEntry> productEntries =
242             SCProductEntryLocalServiceUtil.getCompanyProductEntries(
243                 companyId, start, end);
244 
245         if (productEntries.isEmpty()) {
246             return;
247         }
248 
249         Collection<Document> documents = new ArrayList<Document>();
250 
251         for (SCProductEntry productEntry : productEntries) {
252             Document document = getDocument(productEntry);
253 
254             documents.add(document);
255         }
256 
257         SearchEngineUtil.updateDocuments(companyId, documents);
258     }
259 
260 }