1
22
23 package com.liferay.portlet.blogs.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.StringUtil;
28 import com.liferay.portal.lucene.LuceneFields;
29 import com.liferay.portal.lucene.LuceneUtil;
30 import com.liferay.portal.util.PortletKeys;
31 import com.liferay.portlet.blogs.service.BlogsEntryLocalServiceUtil;
32 import com.liferay.util.Html;
33
34 import java.io.IOException;
35
36 import javax.portlet.PortletURL;
37
38 import org.apache.lucene.document.Document;
39 import org.apache.lucene.index.IndexWriter;
40 import org.apache.lucene.index.Term;
41
42
49 public class Indexer implements com.liferay.portal.kernel.search.Indexer {
50
51 public static final String PORTLET_ID = PortletKeys.BLOGS;
52
53 public static void addEntry(
54 long companyId, long groupId, long userId, long categoryId,
55 long entryId, String title, String content)
56 throws IOException {
57
58 Document doc = getAddEntryDocument(
59 companyId, groupId, userId, categoryId, entryId, title, content);
60
61 IndexWriter writer = null;
62
63 try {
64 writer = LuceneUtil.getWriter(companyId);
65
66 writer.addDocument(doc);
67 }
68 finally {
69 if (writer != null) {
70 LuceneUtil.write(companyId);
71 }
72 }
73 }
74
75 public static void deleteEntry(long companyId, long entryId)
76 throws IOException {
77
78 LuceneUtil.deleteDocuments(
79 companyId,
80 new Term(
81 LuceneFields.UID, LuceneFields.getUID(PORTLET_ID, entryId)));
82 }
83
84 public static Document getAddEntryDocument(
85 long companyId, long groupId, long userId, long categoryId,
86 long entryId, String title, String content) {
87
88 content = Html.stripHtml(content);
89
90 Document doc = new Document();
91
92 doc.add(
93 LuceneFields.getKeyword(
94 LuceneFields.UID, LuceneFields.getUID(PORTLET_ID, entryId)));
95
96 doc.add(LuceneFields.getKeyword(LuceneFields.COMPANY_ID, companyId));
97 doc.add(LuceneFields.getKeyword(LuceneFields.PORTLET_ID, PORTLET_ID));
98 doc.add(LuceneFields.getKeyword(LuceneFields.GROUP_ID, groupId));
99 doc.add(LuceneFields.getKeyword(LuceneFields.USER_ID, userId));
100
101 doc.add(LuceneFields.getText(LuceneFields.TITLE, title));
102 doc.add(LuceneFields.getText(LuceneFields.CONTENT, content));
103
104 doc.add(LuceneFields.getDate(LuceneFields.MODIFIED));
105
106 doc.add(LuceneFields.getKeyword("categoryId", categoryId));
107 doc.add(LuceneFields.getKeyword("entryId", entryId));
108
109 return doc;
110 }
111
112 public static void updateEntry(
113 long companyId, long groupId, long userId, long categoryId,
114 long entryId, String title, String content)
115 throws IOException {
116
117 try {
118 deleteEntry(companyId, entryId);
119 }
120 catch (IOException ioe) {
121 }
122
123 addEntry(
124 companyId, groupId, userId, categoryId, entryId, title, content);
125 }
126
127 public DocumentSummary getDocumentSummary(
128 com.liferay.portal.kernel.search.Document doc, PortletURL portletURL) {
129
130
132 String title = doc.get(LuceneFields.TITLE);
133
134
136 String content = doc.get(LuceneFields.CONTENT);
137
138 content = StringUtil.shorten(content, 200);
139
140
142 String entryId = doc.get("entryId");
143
144 portletURL.setParameter("struts_action", "/blogs/view_entry");
145 portletURL.setParameter("entryId", entryId);
146
147 return new DocumentSummary(title, content, portletURL);
148 }
149
150 public void reIndex(String[] ids) throws SearchException {
151 try {
152 BlogsEntryLocalServiceUtil.reIndex(ids);
153 }
154 catch (Exception e) {
155 throw new SearchException(e);
156 }
157 }
158
159 }