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.Locale;
042    
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                    setStagingAware(false);
060            }
061    
062            @Override
063            public String[] getClassNames() {
064                    return CLASS_NAMES;
065            }
066    
067            @Override
068            public String getPortletId() {
069                    return PORTLET_ID;
070            }
071    
072            @Override
073            protected void doDelete(Object obj) throws Exception {
074                    SCProductEntry productEntry = (SCProductEntry)obj;
075    
076                    deleteDocument(
077                            productEntry.getCompanyId(), productEntry.getProductEntryId());
078            }
079    
080            @Override
081            protected Document doGetDocument(Object obj) throws Exception {
082                    SCProductEntry productEntry = (SCProductEntry)obj;
083    
084                    Document document = getBaseModelDocument(PORTLET_ID, productEntry);
085    
086                    StringBundler sb = new StringBundler(15);
087    
088                    String longDescription = HtmlUtil.extractText(
089                            productEntry.getLongDescription());
090    
091                    sb.append(longDescription);
092    
093                    sb.append(StringPool.SPACE);
094                    sb.append(productEntry.getPageURL());
095                    sb.append(StringPool.SPACE);
096                    sb.append(productEntry.getRepoArtifactId());
097                    sb.append(StringPool.SPACE);
098                    sb.append(productEntry.getRepoGroupId());
099                    sb.append(StringPool.SPACE);
100    
101                    String shortDescription = HtmlUtil.extractText(
102                            productEntry.getShortDescription());
103    
104                    sb.append(shortDescription);
105    
106                    sb.append(StringPool.SPACE);
107                    sb.append(productEntry.getType());
108                    sb.append(StringPool.SPACE);
109                    sb.append(productEntry.getUserId());
110                    sb.append(StringPool.SPACE);
111    
112                    String userName = PortalUtil.getUserName(
113                            productEntry.getUserId(), productEntry.getUserName());
114    
115                    sb.append(userName);
116    
117                    document.addText(Field.CONTENT, sb.toString());
118    
119                    document.addText(Field.TITLE, productEntry.getName());
120                    document.addKeyword(Field.TYPE, productEntry.getType());
121    
122                    String version = StringPool.BLANK;
123    
124                    SCProductVersion latestProductVersion = productEntry.getLatestVersion();
125    
126                    if (latestProductVersion != null) {
127                            version = latestProductVersion.getVersion();
128                    }
129    
130                    document.addKeyword(Field.VERSION, version);
131    
132                    document.addText("longDescription", longDescription);
133                    document.addText("pageURL", productEntry.getPageURL());
134                    document.addKeyword("repoArtifactId", productEntry.getRepoArtifactId());
135                    document.addKeyword("repoGroupId", productEntry.getRepoGroupId());
136                    document.addText("shortDescription", shortDescription);
137    
138                    return document;
139            }
140    
141            @Override
142            protected Summary doGetSummary(
143                    Document document, Locale locale, String snippet,
144                    PortletURL portletURL) {
145    
146                    String productEntryId = document.get(Field.ENTRY_CLASS_PK);
147    
148                    portletURL.setParameter(
149                            "struts_action", "/software_catalog/view_product_entry");
150                    portletURL.setParameter("productEntryId", productEntryId);
151    
152                    Summary summary = createSummary(document, Field.TITLE, Field.CONTENT);
153    
154                    summary.setMaxContentLength(200);
155                    summary.setPortletURL(portletURL);
156    
157                    return summary;
158            }
159    
160            @Override
161            protected void doReindex(Object obj) throws Exception {
162                    SCProductEntry productEntry = (SCProductEntry)obj;
163    
164                    Document document = getDocument(productEntry);
165    
166                    SearchEngineUtil.updateDocument(
167                            getSearchEngineId(), productEntry.getCompanyId(), document);
168            }
169    
170            @Override
171            protected void doReindex(String className, long classPK) throws Exception {
172                    SCProductEntry productEntry =
173                            SCProductEntryLocalServiceUtil.getProductEntry(classPK);
174    
175                    doReindex(productEntry);
176            }
177    
178            @Override
179            protected void doReindex(String[] ids) throws Exception {
180                    long companyId = GetterUtil.getLong(ids[0]);
181    
182                    reindexProductEntries(companyId);
183            }
184    
185            @Override
186            protected String getPortletId(SearchContext searchContext) {
187                    return PORTLET_ID;
188            }
189    
190            @Override
191            protected void postProcessFullQuery(
192                            BooleanQuery fullQuery, SearchContext searchContext)
193                    throws Exception {
194    
195                    String type = (String)searchContext.getAttribute("type");
196    
197                    if (Validator.isNotNull(type)) {
198                            BooleanQuery searchQuery = BooleanQueryFactoryUtil.create(
199                                    searchContext);
200    
201                            searchQuery.addRequiredTerm("type", type);
202    
203                            fullQuery.add(searchQuery, BooleanClauseOccur.MUST);
204                    }
205            }
206    
207            protected void reindexProductEntries(long companyId)
208                    throws PortalException, SystemException {
209    
210                    ActionableDynamicQuery actionableDynamicQuery =
211                            new SCProductEntryActionableDynamicQuery() {
212    
213                            @Override
214                            protected void performAction(Object object) throws PortalException {
215                                    SCProductEntry productEntry = (SCProductEntry)object;
216    
217                                    Document document = getDocument(productEntry);
218    
219                                    addDocument(document);
220                            }
221    
222                    };
223    
224                    actionableDynamicQuery.setCompanyId(companyId);
225                    actionableDynamicQuery.setSearchEngineId(getSearchEngineId());
226    
227                    actionableDynamicQuery.performActions();
228            }
229    
230    }