001    /**
002     * Copyright (c) 2000-2013 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.dao.orm.ActionableDynamicQuery;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.exception.SystemException;
020    import com.liferay.portal.kernel.search.BaseIndexer;
021    import com.liferay.portal.kernel.search.BooleanClauseOccur;
022    import com.liferay.portal.kernel.search.BooleanQuery;
023    import com.liferay.portal.kernel.search.BooleanQueryFactoryUtil;
024    import com.liferay.portal.kernel.search.Document;
025    import com.liferay.portal.kernel.search.Field;
026    import com.liferay.portal.kernel.search.SearchContext;
027    import com.liferay.portal.kernel.search.SearchEngineUtil;
028    import com.liferay.portal.kernel.search.Summary;
029    import com.liferay.portal.kernel.util.GetterUtil;
030    import com.liferay.portal.kernel.util.HtmlUtil;
031    import com.liferay.portal.kernel.util.StringBundler;
032    import com.liferay.portal.kernel.util.StringPool;
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.softwarecatalog.model.SCProductEntry;
037    import com.liferay.portlet.softwarecatalog.model.SCProductVersion;
038    import com.liferay.portlet.softwarecatalog.service.SCProductEntryLocalServiceUtil;
039    import com.liferay.portlet.softwarecatalog.service.persistence.SCProductEntryActionableDynamicQuery;
040    
041    import java.util.ArrayList;
042    import java.util.Collection;
043    import java.util.Locale;
044    
045    import javax.portlet.PortletURL;
046    
047    /**
048     * @author Jorge Ferrer
049     * @author Brian Wing Shun Chan
050     * @author Harry Mark
051     * @author Bruno Farache
052     * @author Raymond Aug??
053     */
054    public class SCIndexer extends BaseIndexer {
055    
056            public static final String[] CLASS_NAMES = {SCProductEntry.class.getName()};
057    
058            public static final String PORTLET_ID = PortletKeys.SOFTWARE_CATALOG;
059    
060            public SCIndexer() {
061                    setStagingAware(false);
062            }
063    
064            @Override
065            public String[] getClassNames() {
066                    return CLASS_NAMES;
067            }
068    
069            @Override
070            public String getPortletId() {
071                    return PORTLET_ID;
072            }
073    
074            @Override
075            protected void doDelete(Object obj) throws Exception {
076                    SCProductEntry productEntry = (SCProductEntry)obj;
077    
078                    deleteDocument(
079                            productEntry.getCompanyId(), productEntry.getProductEntryId());
080            }
081    
082            @Override
083            protected Document doGetDocument(Object obj) throws Exception {
084                    SCProductEntry productEntry = (SCProductEntry)obj;
085    
086                    Document document = getBaseModelDocument(PORTLET_ID, productEntry);
087    
088                    StringBundler sb = new StringBundler(15);
089    
090                    String longDescription = HtmlUtil.extractText(
091                            productEntry.getLongDescription());
092    
093                    sb.append(longDescription);
094    
095                    sb.append(StringPool.SPACE);
096                    sb.append(productEntry.getPageURL());
097                    sb.append(StringPool.SPACE);
098                    sb.append(productEntry.getRepoArtifactId());
099                    sb.append(StringPool.SPACE);
100                    sb.append(productEntry.getRepoGroupId());
101                    sb.append(StringPool.SPACE);
102    
103                    String shortDescription = HtmlUtil.extractText(
104                            productEntry.getShortDescription());
105    
106                    sb.append(shortDescription);
107    
108                    sb.append(StringPool.SPACE);
109                    sb.append(productEntry.getType());
110                    sb.append(StringPool.SPACE);
111                    sb.append(productEntry.getUserId());
112                    sb.append(StringPool.SPACE);
113    
114                    String userName = PortalUtil.getUserName(
115                            productEntry.getUserId(), productEntry.getUserName());
116    
117                    sb.append(userName);
118    
119                    document.addText(Field.CONTENT, sb.toString());
120    
121                    document.addText(Field.TITLE, productEntry.getName());
122                    document.addKeyword(Field.TYPE, productEntry.getType());
123    
124                    String version = StringPool.BLANK;
125    
126                    SCProductVersion latestProductVersion = productEntry.getLatestVersion();
127    
128                    if (latestProductVersion != null) {
129                            version = latestProductVersion.getVersion();
130                    }
131    
132                    document.addKeyword(Field.VERSION, version);
133    
134                    document.addText("longDescription", longDescription);
135                    document.addText("pageURL", productEntry.getPageURL());
136                    document.addKeyword("repoArtifactId", productEntry.getRepoArtifactId());
137                    document.addKeyword("repoGroupId", productEntry.getRepoGroupId());
138                    document.addText("shortDescription", shortDescription);
139    
140                    return document;
141            }
142    
143            @Override
144            protected Summary doGetSummary(
145                    Document document, Locale locale, String snippet,
146                    PortletURL portletURL) {
147    
148                    String productEntryId = document.get(Field.ENTRY_CLASS_PK);
149    
150                    portletURL.setParameter(
151                            "struts_action", "/software_catalog/view_product_entry");
152                    portletURL.setParameter("productEntryId", productEntryId);
153    
154                    Summary summary = createSummary(document, Field.TITLE, Field.CONTENT);
155    
156                    summary.setMaxContentLength(200);
157                    summary.setPortletURL(portletURL);
158    
159                    return summary;
160            }
161    
162            @Override
163            protected void doReindex(Object obj) throws Exception {
164                    SCProductEntry productEntry = (SCProductEntry)obj;
165    
166                    Document document = getDocument(productEntry);
167    
168                    SearchEngineUtil.updateDocument(
169                            getSearchEngineId(), productEntry.getCompanyId(), document);
170            }
171    
172            @Override
173            protected void doReindex(String className, long classPK) throws Exception {
174                    SCProductEntry productEntry =
175                            SCProductEntryLocalServiceUtil.getProductEntry(classPK);
176    
177                    doReindex(productEntry);
178            }
179    
180            @Override
181            protected void doReindex(String[] ids) throws Exception {
182                    long companyId = GetterUtil.getLong(ids[0]);
183    
184                    reindexProductEntries(companyId);
185            }
186    
187            @Override
188            protected String getPortletId(SearchContext searchContext) {
189                    return PORTLET_ID;
190            }
191    
192            @Override
193            protected void postProcessFullQuery(
194                            BooleanQuery fullQuery, SearchContext searchContext)
195                    throws Exception {
196    
197                    String type = (String)searchContext.getAttribute("type");
198    
199                    if (Validator.isNotNull(type)) {
200                            BooleanQuery searchQuery = BooleanQueryFactoryUtil.create(
201                                    searchContext);
202    
203                            searchQuery.addRequiredTerm("type", type);
204    
205                            fullQuery.add(searchQuery, BooleanClauseOccur.MUST);
206                    }
207            }
208    
209            protected void reindexProductEntries(long companyId)
210                    throws PortalException, SystemException {
211    
212                    final Collection<Document> documents = new ArrayList<Document>();
213    
214                    ActionableDynamicQuery actionableDynamicQuery =
215                            new SCProductEntryActionableDynamicQuery() {
216    
217                            @Override
218                            protected void performAction(Object object) throws PortalException {
219                                    SCProductEntry productEntry = (SCProductEntry)object;
220    
221                                    Document document = getDocument(productEntry);
222    
223                                    documents.add(document);
224                            }
225    
226                    };
227    
228                    actionableDynamicQuery.setCompanyId(companyId);
229    
230                    actionableDynamicQuery.performActions();
231    
232                    SearchEngineUtil.updateDocuments(
233                            getSearchEngineId(), companyId, documents);
234            }
235    
236    }