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