1
22
23 package com.liferay.portlet.bookmarks.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.bookmarks.service.BookmarksFolderLocalServiceUtil;
32
33 import java.io.IOException;
34
35 import javax.portlet.PortletURL;
36
37 import org.apache.lucene.document.Document;
38 import org.apache.lucene.index.IndexWriter;
39 import org.apache.lucene.index.Term;
40
41
47 public class Indexer implements com.liferay.portal.kernel.search.Indexer {
48
49 public static final String PORTLET_ID = PortletKeys.BOOKMARKS;
50
51 public static void addEntry(
52 long companyId, long groupId, long folderId, long entryId,
53 String title, String content, String description,
54 String[] tagsEntries)
55 throws IOException {
56
57 Document doc = getAddEntryDocument(
58 companyId, groupId, folderId, entryId, title, content, description,
59 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 folderId, long entryId, String title,
86 String content, String description, String[] tagsEntries) {
87
88 Document doc = new Document();
89
90 LuceneUtil.addKeyword(
91 doc, LuceneFields.UID, LuceneFields.getUID(PORTLET_ID, entryId));
92
93 LuceneUtil.addKeyword(doc, LuceneFields.COMPANY_ID, companyId);
94 LuceneUtil.addKeyword(doc, LuceneFields.PORTLET_ID, PORTLET_ID);
95 LuceneUtil.addKeyword(doc, LuceneFields.GROUP_ID, groupId);
96
97 LuceneUtil.addText(doc, LuceneFields.TITLE, title);
98 LuceneUtil.addText(doc, LuceneFields.CONTENT, content);
99 LuceneUtil.addText(doc, LuceneFields.DESCRIPTION, description);
100
101 LuceneUtil.addModifiedDate(doc);
102
103 LuceneUtil.addKeyword(doc, "folderId", folderId);
104 LuceneUtil.addKeyword(doc, "entryId", entryId);
105
106 LuceneUtil.addKeyword(doc, LuceneFields.TAG_ENTRY, tagsEntries);
107
108 return doc;
109 }
110
111 public static void updateEntry(
112 long companyId, long groupId, long folderId, long entryId,
113 String title, String content, String description,
114 String[] tagsEntries)
115 throws IOException {
116
117 try {
118 deleteEntry(companyId, entryId);
119 }
120 catch (IOException ioe) {
121 }
122
123 addEntry(
124 companyId, groupId, folderId, entryId, title, content, description,
125 tagsEntries);
126 }
127
128 public DocumentSummary getDocumentSummary(
129 com.liferay.portal.kernel.search.Document doc, PortletURL portletURL) {
130
131
133 String title = doc.get(LuceneFields.TITLE);
134
135
137 String content = doc.get(LuceneFields.CONTENT);
138
139 content = StringUtil.shorten(content, 200);
140
141
143 String entryId = doc.get("entryId");
144
145 portletURL.setParameter("struts_action", "/bookmarks/edit_entry");
146 portletURL.setParameter("entryId", entryId);
147
148 return new DocumentSummary(title, content, portletURL);
149 }
150
151 public void reIndex(String[] ids) throws SearchException {
152 try {
153 BookmarksFolderLocalServiceUtil.reIndex(ids);
154 }
155 catch (Exception e) {
156 throw new SearchException(e);
157 }
158 }
159
160 }