1
22
23 package com.liferay.portlet.softwarecatalog.util;
24
25 import com.liferay.portal.kernel.search.Document;
26 import com.liferay.portal.kernel.search.DocumentImpl;
27 import com.liferay.portal.kernel.search.DocumentSummary;
28 import com.liferay.portal.kernel.search.Field;
29 import com.liferay.portal.kernel.search.SearchEngineUtil;
30 import com.liferay.portal.kernel.search.SearchException;
31 import com.liferay.portal.kernel.util.HtmlUtil;
32 import com.liferay.portal.kernel.util.StringUtil;
33 import com.liferay.portal.util.PortletKeys;
34 import com.liferay.portlet.softwarecatalog.service.SCProductEntryLocalServiceUtil;
35
36 import java.util.Date;
37
38 import javax.portlet.PortletURL;
39
40
49 public class Indexer
50 implements com.liferay.portal.kernel.search.Indexer {
51
52 public static final String PORTLET_ID = PortletKeys.SOFTWARE_CATALOG;
53
54 public static void addProductEntry(
55 long companyId, long groupId, long userId, String userName,
56 long productEntryId, String name, Date modifiedDate, String version,
57 String type, String shortDescription, String longDescription,
58 String pageURL, String repoGroupId, String repoArtifactId)
59 throws SearchException {
60
61 Document doc = getProductEntryDocument(
62 companyId, groupId, userId, userName, productEntryId, name,
63 modifiedDate, version, type, shortDescription, longDescription,
64 pageURL, repoGroupId, repoArtifactId);
65
66 SearchEngineUtil.addDocument(companyId, doc);
67 }
68
69 public static void deleteProductEntry(long companyId, long productEntryId)
70 throws SearchException {
71
72 SearchEngineUtil.deleteDocument(companyId, getEntryUID(productEntryId));
73 }
74
75 public static Document getProductEntryDocument(
76 long companyId, long groupId, long userId, String userName,
77 long productEntryId, String name, Date modifiedDate, String version,
78 String type, String shortDescription, String longDescription,
79 String pageURL, String repoGroupId, String repoArtifactId) {
80
81 shortDescription = HtmlUtil.extractText(shortDescription);
82 longDescription = HtmlUtil.extractText(longDescription);
83
84 String content =
85 userId + " " + userName + " " + type + " " + shortDescription +
86 " " + longDescription + " " + pageURL + repoGroupId + " " +
87 repoArtifactId;
88
89 Document doc = new DocumentImpl();
90
91 doc.addUID(PORTLET_ID, productEntryId);
92
93 doc.addKeyword(Field.COMPANY_ID, companyId);
94 doc.addKeyword(Field.PORTLET_ID, PORTLET_ID);
95 doc.addKeyword(Field.GROUP_ID, groupId);
96 doc.addKeyword(Field.USER_ID, userId);
97
98 doc.addText(Field.NAME, name);
99 doc.addText(Field.CONTENT, content);
100
101 doc.addModifiedDate();
102
103 doc.addKeyword(Field.ENTRY_CLASS_PK, productEntryId);
104 doc.addDate("modified-date", modifiedDate);
105 doc.addText("version", version);
106 doc.addKeyword("type", type);
107 doc.addKeyword("repoGroupId", repoGroupId);
108 doc.addKeyword("repoArtifactId", repoArtifactId);
109
110 return doc;
111 }
112
113 public static String getEntryUID(long productEntryId) {
114 Document doc = new DocumentImpl();
115
116 doc.addUID(PORTLET_ID, productEntryId);
117
118 return doc.get(Field.UID);
119 }
120
121 public static void updateProductEntry(
122 long companyId, long groupId, long userId, String userName,
123 long productEntryId, String name, Date modifiedDate, String version,
124 String type, String shortDescription, String longDescription,
125 String pageURL, String repoGroupId, String repoArtifactId)
126 throws SearchException {
127
128 Document doc = getProductEntryDocument(
129 companyId, groupId, userId, userName, productEntryId, name,
130 modifiedDate, version, type, shortDescription, longDescription,
131 pageURL, repoGroupId, repoArtifactId);
132
133 SearchEngineUtil.updateDocument(companyId, doc.get(Field.UID), doc);
134 }
135
136 public DocumentSummary getDocumentSummary(
137 com.liferay.portal.kernel.search.Document doc, PortletURL portletURL) {
138
139
141 String title = doc.get(Field.TITLE);
142
143
145 String content = doc.get(Field.CONTENT);
146
147 content = StringUtil.shorten(content, 200);
148
149
151 String productEntryId = doc.get(Field.ENTRY_CLASS_PK);
152
153 portletURL.setParameter(
154 "struts_action", "/software_catalog/view_product_entry");
155 portletURL.setParameter("productEntryId", productEntryId);
156
157 return new DocumentSummary(title, content, portletURL);
158 }
159
160 public void reIndex(String[] ids) throws SearchException {
161 try {
162 SCProductEntryLocalServiceUtil.reIndex(ids);
163 }
164 catch (Exception e) {
165 throw new SearchException(e);
166 }
167 }
168
169 }