1
22
23 package com.liferay.portlet.softwarecatalog.util;
24
25 import com.liferay.portal.kernel.search.DocumentSummary;
26 import com.liferay.portal.kernel.search.SearchException;
27 import com.liferay.portal.kernel.util.HtmlUtil;
28 import com.liferay.portal.kernel.util.StringUtil;
29 import com.liferay.portal.lucene.LuceneFields;
30 import com.liferay.portal.lucene.LuceneUtil;
31 import com.liferay.portal.util.PortletKeys;
32 import com.liferay.portlet.softwarecatalog.service.SCProductEntryLocalServiceUtil;
33
34 import java.io.IOException;
35
36 import java.util.Date;
37
38 import javax.portlet.PortletURL;
39
40 import org.apache.lucene.document.Document;
41 import org.apache.lucene.index.IndexWriter;
42 import org.apache.lucene.index.Term;
43
44
52 public class Indexer
53 implements com.liferay.portal.kernel.search.Indexer {
54
55 public static final String PORTLET_ID = PortletKeys.SOFTWARE_CATALOG;
56
57 public static void addProductEntry(
58 long companyId, long groupId, long userId, String userName,
59 long productEntryId, String name, Date modifiedDate, String version,
60 String type, String shortDescription, String longDescription,
61 String pageURL, String repoGroupId, String repoArtifactId)
62 throws IOException {
63
64 Document doc = getAddProductEntryDocument(
65 companyId, groupId, userId, userName, productEntryId, name,
66 modifiedDate, version, type, shortDescription, longDescription,
67 pageURL, repoGroupId, repoArtifactId);
68
69 IndexWriter writer = null;
70
71 try {
72 writer = LuceneUtil.getWriter(companyId);
73
74 writer.addDocument(doc);
75 }
76 finally {
77 if (writer != null) {
78 LuceneUtil.write(companyId);
79 }
80 }
81 }
82
83 public static void deleteProductEntry(long companyId, long productEntryId)
84 throws IOException {
85
86 LuceneUtil.deleteDocuments(
87 companyId,
88 new Term(
89 LuceneFields.UID,
90 LuceneFields.getUID(PORTLET_ID, productEntryId)));
91 }
92
93 public static Document getAddProductEntryDocument(
94 long companyId, long groupId, long userId, String userName,
95 long productEntryId, String name, Date modifiedDate, String version,
96 String type, String shortDescription, String longDescription,
97 String pageURL, String repoGroupId, String repoArtifactId) {
98
99 shortDescription = HtmlUtil.extractText(shortDescription);
100 longDescription = HtmlUtil.extractText(longDescription);
101
102 String content =
103 userId + " " + userName + " " + type + " " + shortDescription +
104 " " + longDescription + " " + pageURL + repoGroupId + " " +
105 repoArtifactId;
106
107 Document doc = new Document();
108
109 doc.add(
110 LuceneFields.getKeyword(
111 LuceneFields.UID,
112 LuceneFields.getUID(PORTLET_ID, productEntryId)));
113
114 doc.add(LuceneFields.getKeyword(LuceneFields.COMPANY_ID, companyId));
115 doc.add(LuceneFields.getKeyword(LuceneFields.PORTLET_ID, PORTLET_ID));
116 doc.add(LuceneFields.getKeyword(LuceneFields.GROUP_ID, groupId));
117 doc.add(LuceneFields.getKeyword(LuceneFields.USER_ID, userId));
118
119 doc.add(LuceneFields.getText(LuceneFields.TITLE, name));
120 doc.add(LuceneFields.getText(LuceneFields.CONTENT, content));
121
122 doc.add(LuceneFields.getDate(LuceneFields.MODIFIED));
123
124 doc.add(LuceneFields.getKeyword("productEntryId", productEntryId));
125 doc.add(LuceneFields.getDate("modified-date", modifiedDate));
126 doc.add(LuceneFields.getText("version", version));
127 doc.add(LuceneFields.getKeyword("type", type));
128 doc.add(LuceneFields.getKeyword("repoGroupId", repoGroupId));
129 doc.add(LuceneFields.getKeyword("repoArtifactId", repoArtifactId));
130
131 return doc;
132 }
133
134 public static void updateProductEntry(
135 long companyId, long groupId, long userId, String userName,
136 long productEntryId, String name, Date modifiedDate, String version,
137 String type, String shortDescription, String longDescription,
138 String pageURL, String repoGroupId, String repoArtifactId)
139 throws IOException {
140
141 try {
142 deleteProductEntry(companyId, productEntryId);
143 }
144 catch (IOException ioe) {
145 }
146
147 addProductEntry(
148 companyId, groupId, userId, userName, productEntryId, name,
149 modifiedDate, version, type, shortDescription, longDescription,
150 pageURL, repoGroupId, repoArtifactId);
151 }
152
153 public DocumentSummary getDocumentSummary(
154 com.liferay.portal.kernel.search.Document doc, PortletURL portletURL) {
155
156
158 String title = doc.get(LuceneFields.TITLE);
159
160
162 String content = doc.get(LuceneFields.CONTENT);
163
164 content = StringUtil.shorten(content, 200);
165
166
168 String productEntryId = doc.get("productEntryId");
169
170 portletURL.setParameter(
171 "struts_action", "/software_catalog/view_product_entry");
172 portletURL.setParameter("productEntryId", productEntryId);
173
174 return new DocumentSummary(title, content, portletURL);
175 }
176
177 public void reIndex(String[] ids) throws SearchException {
178 try {
179 SCProductEntryLocalServiceUtil.reIndex(ids);
180 }
181 catch (Exception e) {
182 throw new SearchException(e);
183 }
184 }
185
186 }