1
14
15 package com.liferay.portlet.softwarecatalog.util;
16
17 import com.liferay.portal.kernel.search.BooleanClauseOccur;
18 import com.liferay.portal.kernel.search.BooleanQuery;
19 import com.liferay.portal.kernel.search.BooleanQueryFactoryUtil;
20 import com.liferay.portal.kernel.search.Document;
21 import com.liferay.portal.kernel.search.DocumentImpl;
22 import com.liferay.portal.kernel.search.Field;
23 import com.liferay.portal.kernel.search.Indexer;
24 import com.liferay.portal.kernel.search.SearchContext;
25 import com.liferay.portal.kernel.search.SearchEngineUtil;
26 import com.liferay.portal.kernel.search.Summary;
27 import com.liferay.portal.kernel.util.GetterUtil;
28 import com.liferay.portal.kernel.util.HtmlUtil;
29 import com.liferay.portal.kernel.util.StringBundler;
30 import com.liferay.portal.kernel.util.StringPool;
31 import com.liferay.portal.kernel.util.StringUtil;
32 import com.liferay.portal.kernel.util.Validator;
33 import com.liferay.portal.search.BaseIndexer;
34 import com.liferay.portal.util.PortalUtil;
35 import com.liferay.portal.util.PortletKeys;
36 import com.liferay.portlet.expando.model.ExpandoBridge;
37 import com.liferay.portlet.expando.util.ExpandoBridgeIndexerUtil;
38 import com.liferay.portlet.softwarecatalog.model.SCProductEntry;
39 import com.liferay.portlet.softwarecatalog.model.SCProductVersion;
40 import com.liferay.portlet.softwarecatalog.service.SCProductEntryLocalServiceUtil;
41
42 import java.util.Date;
43 import java.util.List;
44
45 import javax.portlet.PortletURL;
46
47
56 public class SCIndexer extends BaseIndexer {
57
58 public static final String[] CLASS_NAMES = {SCProductEntry.class.getName()};
59
60 public static final String PORTLET_ID = PortletKeys.SOFTWARE_CATALOG;
61
62 public String[] getClassNames() {
63 return CLASS_NAMES;
64 }
65
66 public Summary getSummary(
67 Document document, String snippet, PortletURL portletURL) {
68
69 String title = document.get(Field.TITLE);
70
71 String content = snippet;
72
73 if (Validator.isNull(snippet)) {
74 content = StringUtil.shorten(document.get(Field.CONTENT), 200);
75 }
76
77 String productEntryId = document.get(Field.ENTRY_CLASS_PK);
78
79 portletURL.setParameter(
80 "struts_action", "/software_catalog/view_product_entry");
81 portletURL.setParameter("productEntryId", productEntryId);
82
83 return new Summary(title, content, portletURL);
84 }
85
86 protected void doDelete(Object obj) throws Exception {
87 SCProductEntry productEntry = (SCProductEntry)obj;
88
89 Document document = new DocumentImpl();
90
91 document.addUID(PORTLET_ID, productEntry.getProductEntryId());
92
93 SearchEngineUtil.deleteDocument(
94 productEntry.getCompanyId(), document.get(Field.UID));
95 }
96
97 protected Document doGetDocument(Object obj) throws Exception {
98 SCProductEntry productEntry = (SCProductEntry)obj;
99
100 long companyId = productEntry.getCompanyId();
101 long groupId = getParentGroupId(productEntry.getGroupId());
102 long scopeGroupId = productEntry.getGroupId();
103 long userId = productEntry.getUserId();
104 String userName = PortalUtil.getUserName(
105 userId, productEntry.getUserName());
106 long productEntryId = productEntry.getProductEntryId();
107 String name = productEntry.getName();
108 Date modifiedDate = productEntry.getModifiedDate();
109
110 String version = StringPool.BLANK;
111
112 SCProductVersion latestProductVersion = productEntry.getLatestVersion();
113
114 if (latestProductVersion != null) {
115 version = latestProductVersion.getVersion();
116 }
117
118 String type = productEntry.getType();
119 String shortDescription = HtmlUtil.extractText(
120 productEntry.getShortDescription());
121 String longDescription = HtmlUtil.extractText(
122 productEntry.getLongDescription());
123 String pageURL = productEntry.getPageURL();
124 String repoGroupId = productEntry.getRepoGroupId();
125 String repoArtifactId = productEntry.getRepoArtifactId();
126
127 ExpandoBridge expandoBridge = productEntry.getExpandoBridge();
128
129 StringBundler sb = new StringBundler(15);
130
131 sb.append(userId);
132 sb.append(StringPool.SPACE);
133 sb.append(userName);
134 sb.append(StringPool.SPACE);
135 sb.append(type);
136 sb.append(StringPool.SPACE);
137 sb.append(shortDescription);
138 sb.append(StringPool.SPACE);
139 sb.append(longDescription);
140 sb.append(StringPool.SPACE);
141 sb.append(pageURL);
142 sb.append(StringPool.SPACE);
143 sb.append(repoGroupId);
144 sb.append(StringPool.SPACE);
145 sb.append(repoArtifactId);
146
147 String content = sb.toString();
148
149 Document document = new DocumentImpl();
150
151 document.addUID(PORTLET_ID, productEntryId);
152
153 document.addModifiedDate(modifiedDate);
154
155 document.addKeyword(Field.COMPANY_ID, companyId);
156 document.addKeyword(Field.PORTLET_ID, PORTLET_ID);
157 document.addKeyword(Field.GROUP_ID, groupId);
158 document.addKeyword(Field.SCOPE_GROUP_ID, scopeGroupId);
159 document.addKeyword(Field.USER_ID, userId);
160 document.addText(Field.USER_NAME, userName);
161
162 document.addText(Field.TITLE, name);
163 document.addText(Field.CONTENT, content);
164
165 document.addKeyword(
166 Field.ENTRY_CLASS_NAME, SCProductEntry.class.getName());
167 document.addKeyword(Field.ENTRY_CLASS_PK, productEntryId);
168 document.addKeyword("version", version);
169 document.addKeyword("type", type);
170 document.addText("shortDescription", shortDescription);
171 document.addText("longDescription", longDescription);
172 document.addText("pageURL", pageURL);
173 document.addKeyword("repoGroupId", repoGroupId);
174 document.addKeyword("repoArtifactId", repoArtifactId);
175
176 ExpandoBridgeIndexerUtil.addAttributes(document, expandoBridge);
177
178 return document;
179 }
180
181 protected void doReindex(Object obj) throws Exception {
182 SCProductEntry productEntry = (SCProductEntry)obj;
183
184 Document document = getDocument(productEntry);
185
186 SearchEngineUtil.updateDocument(
187 productEntry.getCompanyId(), document.get(Field.UID), document);
188 }
189
190 protected void doReindex(String className, long classPK) throws Exception {
191 SCProductEntry productEntry =
192 SCProductEntryLocalServiceUtil.getProductEntry(classPK);
193
194 doReindex(productEntry);
195 }
196
197 protected void doReindex(String[] ids) throws Exception {
198 long companyId = GetterUtil.getLong(ids[0]);
199
200 reindexProductEntries(companyId);
201 }
202
203 protected String getPortletId(SearchContext searchContext) {
204 return PORTLET_ID;
205 }
206
207 protected void postProcessFullQuery(
208 BooleanQuery fullQuery, SearchContext searchContext)
209 throws Exception {
210
211 String type = (String)searchContext.getAttribute("type");
212
213 if (Validator.isNotNull(type)) {
214 BooleanQuery searchQuery = BooleanQueryFactoryUtil.create();
215
216 searchQuery.addRequiredTerm("type", type);
217
218 fullQuery.add(searchQuery, BooleanClauseOccur.MUST);
219 }
220 }
221
222 protected void reindexProductEntries(long companyId) throws Exception {
223 int count =
224 SCProductEntryLocalServiceUtil.getCompanyProductEntriesCount(
225 companyId);
226
227 int pages = count / Indexer.DEFAULT_INTERVAL;
228
229 for (int i = 0; i <= pages; i++) {
230 int start = (i * Indexer.DEFAULT_INTERVAL);
231 int end = start + Indexer.DEFAULT_INTERVAL;
232
233 reindexProductEntries(companyId, start, end);
234 }
235 }
236
237 protected void reindexProductEntries(long companyId, int start, int end)
238 throws Exception {
239
240 List<SCProductEntry> productEntries =
241 SCProductEntryLocalServiceUtil.getCompanyProductEntries(
242 companyId, start, end);
243
244 for (SCProductEntry productEntry : productEntries) {
245 reindex(productEntry);
246 }
247 }
248
249 }