001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
022    import com.liferay.portal.kernel.search.BaseIndexer;
023    import com.liferay.portal.kernel.search.BooleanClauseOccur;
024    import com.liferay.portal.kernel.search.BooleanQuery;
025    import com.liferay.portal.kernel.search.BooleanQueryFactoryUtil;
026    import com.liferay.portal.kernel.search.Document;
027    import com.liferay.portal.kernel.search.Field;
028    import com.liferay.portal.kernel.search.SearchContext;
029    import com.liferay.portal.kernel.search.SearchEngineUtil;
030    import com.liferay.portal.kernel.search.Summary;
031    import com.liferay.portal.kernel.util.GetterUtil;
032    import com.liferay.portal.kernel.util.HtmlUtil;
033    import com.liferay.portal.kernel.util.StringBundler;
034    import com.liferay.portal.kernel.util.StringPool;
035    import com.liferay.portal.kernel.util.Validator;
036    import com.liferay.portal.util.PortalUtil;
037    import com.liferay.portal.util.PortletKeys;
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    import com.liferay.portlet.softwarecatalog.service.persistence.SCProductEntryActionableDynamicQuery;
042    
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                    ActionableDynamicQuery actionableDynamicQuery =
213                            new SCProductEntryActionableDynamicQuery() {
214    
215                            @Override
216                            protected void performAction(Object object) {
217                                    SCProductEntry productEntry = (SCProductEntry)object;
218    
219                                    try {
220                                            Document document = getDocument(productEntry);
221    
222                                            addDocument(document);
223                                    }
224                                    catch (PortalException pe) {
225                                            if (_log.isWarnEnabled()) {
226                                                    _log.warn(
227                                                            "Unable to software catalog product entry " +
228                                                                    productEntry.getProductEntryId(),
229                                                            pe);
230                                            }
231                                    }
232                            }
233    
234                    };
235    
236                    actionableDynamicQuery.setCompanyId(companyId);
237                    actionableDynamicQuery.setSearchEngineId(getSearchEngineId());
238    
239                    actionableDynamicQuery.performActions();
240            }
241    
242            private static Log _log = LogFactoryUtil.getLog(SCIndexer.class);
243    
244    }