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.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.blogs.service.BlogsEntryLocalServiceUtil;
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 entryId,
55 String title, String content, String[] tagsEntries)
56 throws IOException {
57
58 Document doc = getAddEntryDocument(
59 companyId, groupId, userId, entryId, title, content, tagsEntries);
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 entryId, String title,
86 String content, String[] tagsEntries) {
87
88 content = HtmlUtil.extractText(content);
89
90 Document doc = new Document();
91
92 LuceneUtil.addKeyword(
93 doc, LuceneFields.UID, LuceneFields.getUID(PORTLET_ID, entryId));
94
95 LuceneUtil.addKeyword(doc, LuceneFields.COMPANY_ID, companyId);
96 LuceneUtil.addKeyword(doc, LuceneFields.PORTLET_ID, PORTLET_ID);
97 LuceneUtil.addKeyword(doc, LuceneFields.GROUP_ID, groupId);
98 LuceneUtil.addKeyword(doc, LuceneFields.USER_ID, userId);
99
100 LuceneUtil.addText(doc, LuceneFields.TITLE, title);
101 LuceneUtil.addText(doc, LuceneFields.CONTENT, content);
102
103 LuceneUtil.addModifiedDate(doc);
104
105 LuceneUtil.addKeyword(doc, "entryId", entryId);
106
107 LuceneUtil.addKeyword(doc, LuceneFields.TAG_ENTRY, tagsEntries);
108
109 return doc;
110 }
111
112 public static void updateEntry(
113 long companyId, long groupId, long userId, long entryId,
114 String title, String content, String[] tagsEntries)
115 throws IOException {
116
117 try {
118 deleteEntry(companyId, entryId);
119 }
120 catch (IOException ioe) {
121 }
122
123 addEntry(
124 companyId, groupId, userId, entryId, title, content, tagsEntries);
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 }