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