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