001
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.log.Log;
020 import com.liferay.portal.kernel.log.LogFactoryUtil;
021 import com.liferay.portal.kernel.search.BaseIndexer;
022 import com.liferay.portal.kernel.search.BooleanQuery;
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.search.filter.BooleanFilter;
029 import com.liferay.portal.kernel.spring.osgi.OSGiBeanProperties;
030 import com.liferay.portal.kernel.util.GetterUtil;
031 import com.liferay.portal.kernel.util.HtmlUtil;
032 import com.liferay.portal.kernel.util.StringBundler;
033 import com.liferay.portal.kernel.util.StringPool;
034 import com.liferay.portal.kernel.util.Validator;
035 import com.liferay.portal.util.PortalUtil;
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
040 import java.util.Locale;
041
042 import javax.portlet.PortletRequest;
043 import javax.portlet.PortletResponse;
044
045
052 @OSGiBeanProperties
053 public class SCIndexer extends BaseIndexer {
054
055 public static final String CLASS_NAME = SCProductEntry.class.getName();
056
057 public SCIndexer() {
058 setDefaultSelectedFieldNames(
059 Field.COMPANY_ID, Field.CONTENT, Field.ENTRY_CLASS_NAME,
060 Field.ENTRY_CLASS_PK, Field.TITLE, Field.UID);
061 setStagingAware(false);
062 }
063
064 @Override
065 public String getClassName() {
066 return CLASS_NAME;
067 }
068
069 @Override
070 protected void doDelete(Object obj) throws Exception {
071 SCProductEntry productEntry = (SCProductEntry)obj;
072
073 deleteDocument(
074 productEntry.getCompanyId(), productEntry.getProductEntryId());
075 }
076
077 @Override
078 protected Document doGetDocument(Object obj) throws Exception {
079 SCProductEntry productEntry = (SCProductEntry)obj;
080
081 Document document = getBaseModelDocument(CLASS_NAME, productEntry);
082
083 StringBundler sb = new StringBundler(15);
084
085 String longDescription = HtmlUtil.extractText(
086 productEntry.getLongDescription());
087
088 sb.append(longDescription);
089
090 sb.append(StringPool.SPACE);
091 sb.append(productEntry.getPageURL());
092 sb.append(StringPool.SPACE);
093 sb.append(productEntry.getRepoArtifactId());
094 sb.append(StringPool.SPACE);
095 sb.append(productEntry.getRepoGroupId());
096 sb.append(StringPool.SPACE);
097
098 String shortDescription = HtmlUtil.extractText(
099 productEntry.getShortDescription());
100
101 sb.append(shortDescription);
102
103 sb.append(StringPool.SPACE);
104 sb.append(productEntry.getType());
105 sb.append(StringPool.SPACE);
106 sb.append(productEntry.getUserId());
107 sb.append(StringPool.SPACE);
108
109 String userName = PortalUtil.getUserName(
110 productEntry.getUserId(), productEntry.getUserName());
111
112 sb.append(userName);
113
114 document.addText(Field.CONTENT, sb.toString());
115
116 document.addText(Field.TITLE, productEntry.getName());
117 document.addKeyword(Field.TYPE, productEntry.getType());
118
119 String version = StringPool.BLANK;
120
121 SCProductVersion latestProductVersion = productEntry.getLatestVersion();
122
123 if (latestProductVersion != null) {
124 version = latestProductVersion.getVersion();
125 }
126
127 document.addKeyword(Field.VERSION, version);
128
129 document.addText("longDescription", longDescription);
130 document.addText("pageURL", productEntry.getPageURL());
131 document.addKeyword("repoArtifactId", productEntry.getRepoArtifactId());
132 document.addKeyword("repoGroupId", productEntry.getRepoGroupId());
133 document.addText("shortDescription", shortDescription);
134
135 return document;
136 }
137
138 @Override
139 protected Summary doGetSummary(
140 Document document, Locale locale, String snippet,
141 PortletRequest portletRequest, PortletResponse portletResponse) {
142
143 Summary summary = createSummary(document, Field.TITLE, Field.CONTENT);
144
145 summary.setMaxContentLength(200);
146
147 return summary;
148 }
149
150 @Override
151 protected void doReindex(Object obj) throws Exception {
152 SCProductEntry productEntry = (SCProductEntry)obj;
153
154 Document document = getDocument(productEntry);
155
156 SearchEngineUtil.updateDocument(
157 getSearchEngineId(), productEntry.getCompanyId(), document,
158 isCommitImmediately());
159 }
160
161 @Override
162 protected void doReindex(String className, long classPK) throws Exception {
163 SCProductEntry productEntry =
164 SCProductEntryLocalServiceUtil.getProductEntry(classPK);
165
166 doReindex(productEntry);
167 }
168
169 @Override
170 protected void doReindex(String[] ids) throws Exception {
171 long companyId = GetterUtil.getLong(ids[0]);
172
173 reindexProductEntries(companyId);
174 }
175
176 @Override
177 protected void postProcessFullQuery(
178 BooleanQuery fullQuery, SearchContext searchContext)
179 throws Exception {
180
181 BooleanFilter booleanFilter = fullQuery.getPreBooleanFilter();
182
183 if (booleanFilter == null) {
184 booleanFilter = new BooleanFilter();
185 }
186
187 String type = (String)searchContext.getAttribute("type");
188
189 if (Validator.isNotNull(type)) {
190 booleanFilter.addRequiredTerm("type", type);
191 }
192
193 if (booleanFilter.hasClauses()) {
194 fullQuery.setPreBooleanFilter(booleanFilter);
195 }
196 }
197
198 protected void reindexProductEntries(long companyId)
199 throws PortalException {
200
201 final ActionableDynamicQuery actionableDynamicQuery =
202 SCProductEntryLocalServiceUtil.getActionableDynamicQuery();
203
204 actionableDynamicQuery.setCompanyId(companyId);
205 actionableDynamicQuery.setPerformActionMethod(
206 new ActionableDynamicQuery.PerformActionMethod() {
207
208 @Override
209 public void performAction(Object object) {
210 SCProductEntry productEntry = (SCProductEntry)object;
211
212 try {
213 Document document = getDocument(productEntry);
214
215 actionableDynamicQuery.addDocument(document);
216 }
217 catch (PortalException pe) {
218 if (_log.isWarnEnabled()) {
219 _log.warn(
220 "Unable to software catalog product entry " +
221 productEntry.getProductEntryId(),
222 pe);
223 }
224 }
225 }
226
227 });
228 actionableDynamicQuery.setSearchEngineId(getSearchEngineId());
229
230 actionableDynamicQuery.performActions();
231 }
232
233 private static final Log _log = LogFactoryUtil.getLog(SCIndexer.class);
234
235 }