001
014
015 package com.liferay.portlet.softwarecatalog.util;
016
017 import com.liferay.portal.kernel.dao.orm.DynamicQuery;
018 import com.liferay.portal.kernel.dao.orm.DynamicQueryFactoryUtil;
019 import com.liferay.portal.kernel.dao.orm.Projection;
020 import com.liferay.portal.kernel.dao.orm.ProjectionFactoryUtil;
021 import com.liferay.portal.kernel.dao.orm.ProjectionList;
022 import com.liferay.portal.kernel.dao.orm.Property;
023 import com.liferay.portal.kernel.dao.orm.PropertyFactoryUtil;
024 import com.liferay.portal.kernel.search.BaseIndexer;
025 import com.liferay.portal.kernel.search.BooleanClauseOccur;
026 import com.liferay.portal.kernel.search.BooleanQuery;
027 import com.liferay.portal.kernel.search.BooleanQueryFactoryUtil;
028 import com.liferay.portal.kernel.search.Document;
029 import com.liferay.portal.kernel.search.Field;
030 import com.liferay.portal.kernel.search.SearchContext;
031 import com.liferay.portal.kernel.search.SearchEngineUtil;
032 import com.liferay.portal.kernel.search.Summary;
033 import com.liferay.portal.kernel.util.GetterUtil;
034 import com.liferay.portal.kernel.util.HtmlUtil;
035 import com.liferay.portal.kernel.util.StringBundler;
036 import com.liferay.portal.kernel.util.StringPool;
037 import com.liferay.portal.kernel.util.StringUtil;
038 import com.liferay.portal.kernel.util.Validator;
039 import com.liferay.portal.security.pacl.PACLClassLoaderUtil;
040 import com.liferay.portal.util.PortalUtil;
041 import com.liferay.portal.util.PortletKeys;
042 import com.liferay.portlet.softwarecatalog.model.SCProductEntry;
043 import com.liferay.portlet.softwarecatalog.model.SCProductVersion;
044 import com.liferay.portlet.softwarecatalog.service.SCProductEntryLocalServiceUtil;
045
046 import java.util.ArrayList;
047 import java.util.Collection;
048 import java.util.List;
049 import java.util.Locale;
050
051 import javax.portlet.PortletURL;
052
053
060 public class SCIndexer extends BaseIndexer {
061
062 public static final String[] CLASS_NAMES = {SCProductEntry.class.getName()};
063
064 public static final String PORTLET_ID = PortletKeys.SOFTWARE_CATALOG;
065
066 public SCIndexer() {
067 setStagingAware(false);
068 }
069
070 public String[] getClassNames() {
071 return CLASS_NAMES;
072 }
073
074 public String getPortletId() {
075 return PORTLET_ID;
076 }
077
078 protected void addReindexCriteria(
079 DynamicQuery dynamicQuery, long companyId) {
080
081 Property property = PropertyFactoryUtil.forName("companyId");
082
083 dynamicQuery.add(property.eq(companyId));
084 }
085
086 @Override
087 protected void doDelete(Object obj) throws Exception {
088 SCProductEntry productEntry = (SCProductEntry)obj;
089
090 deleteDocument(
091 productEntry.getCompanyId(), productEntry.getProductEntryId());
092 }
093
094 @Override
095 protected Document doGetDocument(Object obj) throws Exception {
096 SCProductEntry productEntry = (SCProductEntry)obj;
097
098 Document document = getBaseModelDocument(PORTLET_ID, productEntry);
099
100 StringBundler sb = new StringBundler(15);
101
102 String longDescription = HtmlUtil.extractText(
103 productEntry.getLongDescription());
104
105 sb.append(longDescription);
106
107 sb.append(StringPool.SPACE);
108 sb.append(productEntry.getPageURL());
109 sb.append(StringPool.SPACE);
110 sb.append(productEntry.getRepoArtifactId());
111 sb.append(StringPool.SPACE);
112 sb.append(productEntry.getRepoGroupId());
113 sb.append(StringPool.SPACE);
114
115 String shortDescription = HtmlUtil.extractText(
116 productEntry.getShortDescription());
117
118 sb.append(shortDescription);
119
120 sb.append(StringPool.SPACE);
121 sb.append(productEntry.getType());
122 sb.append(StringPool.SPACE);
123 sb.append(productEntry.getUserId());
124 sb.append(StringPool.SPACE);
125
126 String userName = PortalUtil.getUserName(
127 productEntry.getUserId(), productEntry.getUserName());
128
129 sb.append(userName);
130
131 document.addText(Field.CONTENT, sb.toString());
132
133 document.addText(Field.TITLE, productEntry.getName());
134 document.addKeyword(Field.TYPE, productEntry.getType());
135
136 String version = StringPool.BLANK;
137
138 SCProductVersion latestProductVersion = productEntry.getLatestVersion();
139
140 if (latestProductVersion != null) {
141 version = latestProductVersion.getVersion();
142 }
143
144 document.addKeyword(Field.VERSION, version);
145
146 document.addText("longDescription", longDescription);
147 document.addText("pageURL", productEntry.getPageURL());
148 document.addKeyword("repoArtifactId", productEntry.getRepoArtifactId());
149 document.addKeyword("repoGroupId", productEntry.getRepoGroupId());
150 document.addText("shortDescription", shortDescription);
151
152 return document;
153 }
154
155 @Override
156 protected Summary doGetSummary(
157 Document document, Locale locale, String snippet,
158 PortletURL portletURL) {
159
160 String title = document.get(Field.TITLE);
161
162 String content = snippet;
163
164 if (Validator.isNull(snippet)) {
165 content = StringUtil.shorten(document.get(Field.CONTENT), 200);
166 }
167
168 String productEntryId = document.get(Field.ENTRY_CLASS_PK);
169
170 portletURL.setParameter(
171 "struts_action", "/software_catalog/view_product_entry");
172 portletURL.setParameter("productEntryId", productEntryId);
173
174 return new Summary(title, content, portletURL);
175 }
176
177 @Override
178 protected void doReindex(Object obj) throws Exception {
179 SCProductEntry productEntry = (SCProductEntry)obj;
180
181 Document document = getDocument(productEntry);
182
183 SearchEngineUtil.updateDocument(
184 getSearchEngineId(), productEntry.getCompanyId(), document);
185 }
186
187 @Override
188 protected void doReindex(String className, long classPK) throws Exception {
189 SCProductEntry productEntry =
190 SCProductEntryLocalServiceUtil.getProductEntry(classPK);
191
192 doReindex(productEntry);
193 }
194
195 @Override
196 protected void doReindex(String[] ids) throws Exception {
197 long companyId = GetterUtil.getLong(ids[0]);
198
199 reindexProductEntries(companyId);
200 }
201
202 @Override
203 protected String getPortletId(SearchContext searchContext) {
204 return PORTLET_ID;
205 }
206
207 @Override
208 protected void postProcessFullQuery(
209 BooleanQuery fullQuery, SearchContext searchContext)
210 throws Exception {
211
212 String type = (String)searchContext.getAttribute("type");
213
214 if (Validator.isNotNull(type)) {
215 BooleanQuery searchQuery = BooleanQueryFactoryUtil.create(
216 searchContext);
217
218 searchQuery.addRequiredTerm("type", type);
219
220 fullQuery.add(searchQuery, BooleanClauseOccur.MUST);
221 }
222 }
223
224 protected void reindexProductEntries(long companyId) throws Exception {
225 DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(
226 SCProductEntry.class, PACLClassLoaderUtil.getPortalClassLoader());
227
228 Projection minProductEntryIdProjection = ProjectionFactoryUtil.min(
229 "productEntryId");
230 Projection maxProductEntryIdProjection = ProjectionFactoryUtil.max(
231 "productEntryId");
232
233 ProjectionList projectionList = ProjectionFactoryUtil.projectionList();
234
235 projectionList.add(minProductEntryIdProjection);
236 projectionList.add(maxProductEntryIdProjection);
237
238 dynamicQuery.setProjection(projectionList);
239
240 addReindexCriteria(dynamicQuery, companyId);
241
242 List<Object[]> results = SCProductEntryLocalServiceUtil.dynamicQuery(
243 dynamicQuery);
244
245 Object[] minAndMaxProductEntryIds = results.get(0);
246
247 if ((minAndMaxProductEntryIds[0] == null) ||
248 (minAndMaxProductEntryIds[1] == null)) {
249
250 return;
251 }
252
253 long minProductEntryId = (Long)minAndMaxProductEntryIds[0];
254 long maxProductEntryId = (Long)minAndMaxProductEntryIds[1];
255
256 long startProductEntryId = minProductEntryId;
257 long endProductEntryId = startProductEntryId + DEFAULT_INTERVAL;
258
259 while (startProductEntryId <= maxProductEntryId) {
260 reindexProductEntries(
261 companyId, startProductEntryId, endProductEntryId);
262
263 startProductEntryId = endProductEntryId;
264 endProductEntryId += DEFAULT_INTERVAL;
265 }
266 }
267
268 protected void reindexProductEntries(
269 long companyId, long startProductEntryId, long endProductEntryId)
270 throws Exception {
271
272 DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(
273 SCProductEntry.class, PACLClassLoaderUtil.getPortalClassLoader());
274
275 Property property = PropertyFactoryUtil.forName("productEntryId");
276
277 dynamicQuery.add(property.ge(startProductEntryId));
278 dynamicQuery.add(property.lt(endProductEntryId));
279
280 addReindexCriteria(dynamicQuery, companyId);
281
282 List<SCProductEntry> productEntries =
283 SCProductEntryLocalServiceUtil.dynamicQuery(dynamicQuery);
284
285 if (productEntries.isEmpty()) {
286 return;
287 }
288
289 Collection<Document> documents = new ArrayList<Document>(
290 productEntries.size());
291
292 for (SCProductEntry productEntry : productEntries) {
293 Document document = getDocument(productEntry);
294
295 documents.add(document);
296 }
297
298 SearchEngineUtil.updateDocuments(
299 getSearchEngineId(), companyId, documents);
300 }
301
302 }