001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.softwarecatalog.util;
016    
017    import com.liferay.portal.kernel.search.BaseIndexer;
018    import com.liferay.portal.kernel.search.BooleanClauseOccur;
019    import com.liferay.portal.kernel.search.BooleanQuery;
020    import com.liferay.portal.kernel.search.BooleanQueryFactoryUtil;
021    import com.liferay.portal.kernel.search.Document;
022    import com.liferay.portal.kernel.search.DocumentImpl;
023    import com.liferay.portal.kernel.search.Field;
024    import com.liferay.portal.kernel.search.Indexer;
025    import com.liferay.portal.kernel.search.SearchContext;
026    import com.liferay.portal.kernel.search.SearchEngineUtil;
027    import com.liferay.portal.kernel.search.Summary;
028    import com.liferay.portal.kernel.util.GetterUtil;
029    import com.liferay.portal.kernel.util.HtmlUtil;
030    import com.liferay.portal.kernel.util.StringBundler;
031    import com.liferay.portal.kernel.util.StringPool;
032    import com.liferay.portal.kernel.util.StringUtil;
033    import com.liferay.portal.kernel.util.Validator;
034    import com.liferay.portal.util.PortalUtil;
035    import com.liferay.portal.util.PortletKeys;
036    import com.liferay.portlet.expando.model.ExpandoBridge;
037    import com.liferay.portlet.expando.util.ExpandoBridgeIndexerUtil;
038    import com.liferay.portlet.softwarecatalog.model.SCProductEntry;
039    import com.liferay.portlet.softwarecatalog.model.SCProductVersion;
040    import com.liferay.portlet.softwarecatalog.service.SCProductEntryLocalServiceUtil;
041    
042    import java.util.ArrayList;
043    import java.util.Collection;
044    import java.util.Date;
045    import java.util.List;
046    
047    import javax.portlet.PortletURL;
048    
049    /**
050     * @author Jorge Ferrer
051     * @author Brian Wing Shun Chan
052     * @author Harry Mark
053     * @author Bruno Farache
054     * @author Raymond Augé
055     */
056    public class SCIndexer extends BaseIndexer {
057    
058            public static final String[] CLASS_NAMES = {SCProductEntry.class.getName()};
059    
060            public static final String PORTLET_ID = PortletKeys.SOFTWARE_CATALOG;
061    
062            public String[] getClassNames() {
063                    return CLASS_NAMES;
064            }
065    
066            public Summary getSummary(
067                    Document document, String snippet, PortletURL portletURL) {
068    
069                    String title = document.get(Field.TITLE);
070    
071                    String content = snippet;
072    
073                    if (Validator.isNull(snippet)) {
074                            content = StringUtil.shorten(document.get(Field.CONTENT), 200);
075                    }
076    
077                    String productEntryId = document.get(Field.ENTRY_CLASS_PK);
078    
079                    portletURL.setParameter(
080                            "struts_action", "/software_catalog/view_product_entry");
081                    portletURL.setParameter("productEntryId", productEntryId);
082    
083                    return new Summary(title, content, portletURL);
084            }
085    
086            protected void doDelete(Object obj) throws Exception {
087                    SCProductEntry productEntry = (SCProductEntry)obj;
088    
089                    Document document = new DocumentImpl();
090    
091                    document.addUID(PORTLET_ID, productEntry.getProductEntryId());
092    
093                    SearchEngineUtil.deleteDocument(
094                            productEntry.getCompanyId(), document.get(Field.UID));
095            }
096    
097            protected Document doGetDocument(Object obj) throws Exception {
098                    SCProductEntry productEntry = (SCProductEntry)obj;
099    
100                    long companyId = productEntry.getCompanyId();
101                    long groupId = getParentGroupId(productEntry.getGroupId());
102                    long scopeGroupId = productEntry.getGroupId();
103                    long userId = productEntry.getUserId();
104                    String userName = PortalUtil.getUserName(
105                            userId, productEntry.getUserName());
106                    long productEntryId = productEntry.getProductEntryId();
107                    String name = productEntry.getName();
108                    Date modifiedDate = productEntry.getModifiedDate();
109    
110                    String version = StringPool.BLANK;
111    
112                    SCProductVersion latestProductVersion = productEntry.getLatestVersion();
113    
114                    if (latestProductVersion != null) {
115                            version = latestProductVersion.getVersion();
116                    }
117    
118                    String type = productEntry.getType();
119                    String shortDescription = HtmlUtil.extractText(
120                            productEntry.getShortDescription());
121                    String longDescription = HtmlUtil.extractText(
122                            productEntry.getLongDescription());
123                    String pageURL = productEntry.getPageURL();
124                    String repoGroupId = productEntry.getRepoGroupId();
125                    String repoArtifactId = productEntry.getRepoArtifactId();
126    
127                    ExpandoBridge expandoBridge = productEntry.getExpandoBridge();
128    
129                    StringBundler sb = new StringBundler(15);
130    
131                    sb.append(userId);
132                    sb.append(StringPool.SPACE);
133                    sb.append(userName);
134                    sb.append(StringPool.SPACE);
135                    sb.append(type);
136                    sb.append(StringPool.SPACE);
137                    sb.append(shortDescription);
138                    sb.append(StringPool.SPACE);
139                    sb.append(longDescription);
140                    sb.append(StringPool.SPACE);
141                    sb.append(pageURL);
142                    sb.append(StringPool.SPACE);
143                    sb.append(repoGroupId);
144                    sb.append(StringPool.SPACE);
145                    sb.append(repoArtifactId);
146    
147                    String content = sb.toString();
148    
149                    Document document = new DocumentImpl();
150    
151                    document.addUID(PORTLET_ID, productEntryId);
152    
153                    document.addModifiedDate(modifiedDate);
154    
155                    document.addKeyword(Field.COMPANY_ID, companyId);
156                    document.addKeyword(Field.PORTLET_ID, PORTLET_ID);
157                    document.addKeyword(Field.GROUP_ID, groupId);
158                    document.addKeyword(Field.SCOPE_GROUP_ID, scopeGroupId);
159                    document.addKeyword(Field.USER_ID, userId);
160                    document.addText(Field.USER_NAME, userName);
161    
162                    document.addText(Field.TITLE, name);
163                    document.addText(Field.CONTENT, content);
164    
165                    document.addKeyword(
166                            Field.ENTRY_CLASS_NAME, SCProductEntry.class.getName());
167                    document.addKeyword(Field.ENTRY_CLASS_PK, productEntryId);
168                    document.addKeyword("version", version);
169                    document.addKeyword("type", type);
170                    document.addText("shortDescription", shortDescription);
171                    document.addText("longDescription", longDescription);
172                    document.addText("pageURL", pageURL);
173                    document.addKeyword("repoGroupId", repoGroupId);
174                    document.addKeyword("repoArtifactId", repoArtifactId);
175    
176                    ExpandoBridgeIndexerUtil.addAttributes(document, expandoBridge);
177    
178                    return document;
179            }
180    
181            protected void doReindex(Object obj) throws Exception {
182                    SCProductEntry productEntry = (SCProductEntry)obj;
183    
184                    Document document = getDocument(productEntry);
185    
186                    SearchEngineUtil.updateDocument(productEntry.getCompanyId(), document);
187            }
188    
189            protected void doReindex(String className, long classPK) throws Exception {
190                    SCProductEntry productEntry =
191                            SCProductEntryLocalServiceUtil.getProductEntry(classPK);
192    
193                    doReindex(productEntry);
194            }
195    
196            protected void doReindex(String[] ids) throws Exception {
197                    long companyId = GetterUtil.getLong(ids[0]);
198    
199                    reindexProductEntries(companyId);
200            }
201    
202            protected String getPortletId(SearchContext searchContext) {
203                    return PORTLET_ID;
204            }
205    
206            protected void postProcessFullQuery(
207                            BooleanQuery fullQuery, SearchContext searchContext)
208                    throws Exception {
209    
210                    String type = (String)searchContext.getAttribute("type");
211    
212                    if (Validator.isNotNull(type)) {
213                            BooleanQuery searchQuery = BooleanQueryFactoryUtil.create();
214    
215                            searchQuery.addRequiredTerm("type", type);
216    
217                            fullQuery.add(searchQuery, BooleanClauseOccur.MUST);
218                    }
219            }
220    
221            protected void reindexProductEntries(long companyId) throws Exception {
222                    int count =
223                            SCProductEntryLocalServiceUtil.getCompanyProductEntriesCount(
224                                    companyId);
225    
226                    int pages = count / Indexer.DEFAULT_INTERVAL;
227    
228                    for (int i = 0; i <= pages; i++) {
229                            int start = (i * Indexer.DEFAULT_INTERVAL);
230                            int end = start + Indexer.DEFAULT_INTERVAL;
231    
232                            reindexProductEntries(companyId, start, end);
233                    }
234            }
235    
236            protected void reindexProductEntries(long companyId, int start, int end)
237                    throws Exception {
238    
239                    List<SCProductEntry> productEntries =
240                            SCProductEntryLocalServiceUtil.getCompanyProductEntries(
241                                    companyId, start, end);
242    
243                    if (productEntries.isEmpty()) {
244                            return;
245                    }
246    
247                    Collection<Document> documents = new ArrayList<Document>();
248    
249                    for (SCProductEntry productEntry : productEntries) {
250                            Document document = getDocument(productEntry);
251    
252                            documents.add(document);
253                    }
254    
255                    SearchEngineUtil.updateDocuments(companyId, documents);
256            }
257    
258    }