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