1
22
23 package com.liferay.portlet.imagegallery.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.imagegallery.service.IGFolderLocalServiceUtil;
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.IMAGE_GALLERY;
50
51 public static void addImage(
52 long companyId, long groupId, long folderId, long imageId,
53 String description, String[] tagsEntries)
54 throws IOException {
55
56 Document doc = getAddImageDocument(
57 companyId, groupId, folderId, imageId, description, tagsEntries);
58
59 IndexWriter writer = null;
60
61 try {
62 writer = LuceneUtil.getWriter(companyId);
63
64 writer.addDocument(doc);
65 }
66 finally {
67 if (writer != null) {
68 LuceneUtil.write(companyId);
69 }
70 }
71 }
72
73 public static void deleteImage(long companyId, long imageId)
74 throws IOException {
75
76 LuceneUtil.deleteDocuments(
77 companyId,
78 new Term(
79 LuceneFields.UID, LuceneFields.getUID(PORTLET_ID, imageId)));
80 }
81
82 public static Document getAddImageDocument(
83 long companyId, long groupId, long folderId, long imageId,
84 String description, String[] tagsEntries) {
85
86 Document doc = new Document();
87
88 LuceneUtil.addKeyword(
89 doc, LuceneFields.UID, LuceneFields.getUID(PORTLET_ID, imageId));
90
91 LuceneUtil.addKeyword(doc, LuceneFields.COMPANY_ID, companyId);
92 LuceneUtil.addKeyword(doc, LuceneFields.PORTLET_ID, PORTLET_ID);
93 LuceneUtil.addKeyword(doc, LuceneFields.GROUP_ID, groupId);
94
95 LuceneUtil.addText(doc, LuceneFields.DESCRIPTION, description);
96
97 LuceneUtil.addModifiedDate(doc);
98
99 LuceneUtil.addKeyword(doc, "folderId", folderId);
100 LuceneUtil.addKeyword(doc, "imageId", imageId);
101
102 LuceneUtil.addKeyword(doc, LuceneFields.TAG_ENTRY, tagsEntries);
103
104 return doc;
105 }
106
107 public static void updateImage(
108 long companyId, long groupId, long folderId, long imageId,
109 String description, String[] tagsEntries)
110 throws IOException {
111
112 try {
113 deleteImage(companyId, imageId);
114 }
115 catch (IOException ioe) {
116 }
117
118 addImage(
119 companyId, groupId, folderId, imageId, description, tagsEntries);
120 }
121
122 public DocumentSummary getDocumentSummary(
123 com.liferay.portal.kernel.search.Document doc, PortletURL portletURL) {
124
125
127 String title = doc.get(LuceneFields.TITLE);
128
129
131 String content = doc.get(LuceneFields.CONTENT);
132
133 content = StringUtil.shorten(content, 200);
134
135
137 String imageId = doc.get("imageId");
138
139 portletURL.setParameter("struts_action", "/image_gallery/edit_image");
140 portletURL.setParameter("imageId", imageId);
141
142 return new DocumentSummary(title, content, portletURL);
143 }
144
145 public void reIndex(String[] ids) throws SearchException {
146 try {
147 IGFolderLocalServiceUtil.reIndex(ids);
148 }
149 catch (Exception e) {
150 throw new SearchException(e);
151 }
152 }
153
154 }