1
22
23 package com.liferay.portlet.imagegallery.util;
24
25 import com.liferay.portal.kernel.search.Document;
26 import com.liferay.portal.kernel.search.DocumentImpl;
27 import com.liferay.portal.kernel.search.DocumentSummary;
28 import com.liferay.portal.kernel.search.Field;
29 import com.liferay.portal.kernel.search.SearchEngineUtil;
30 import com.liferay.portal.kernel.search.SearchException;
31 import com.liferay.portal.kernel.util.StringUtil;
32 import com.liferay.portal.util.PortletKeys;
33 import com.liferay.portlet.expando.model.ExpandoBridge;
34 import com.liferay.portlet.expando.util.ExpandoBridgeIndexerUtil;
35 import com.liferay.portlet.imagegallery.model.IGImage;
36 import com.liferay.portlet.imagegallery.service.IGFolderLocalServiceUtil;
37 import com.liferay.portlet.imagegallery.service.IGImageLocalServiceUtil;
38
39 import java.util.Date;
40
41 import javax.portlet.PortletURL;
42
43
51 public class Indexer implements com.liferay.portal.kernel.search.Indexer {
52
53 public static final String PORTLET_ID = PortletKeys.IMAGE_GALLERY;
54
55 public static void addImage(
56 long companyId, long groupId, long folderId, long imageId,
57 String name, String description, Date modifiedDate,
58 String[] tagsCategories, String[] tagsEntries,
59 ExpandoBridge expandoBridge)
60 throws SearchException {
61
62 Document doc = getImageDocument(
63 companyId, groupId, folderId, imageId, name, description,
64 modifiedDate, tagsCategories, tagsEntries, expandoBridge);
65
66 SearchEngineUtil.addDocument(companyId, doc);
67 }
68
69 public static void deleteImage(long companyId, long imageId)
70 throws SearchException {
71
72 SearchEngineUtil.deleteDocument(companyId, getImageUID(imageId));
73 }
74
75 public static Document getImageDocument(
76 long companyId, long groupId, long folderId, long imageId,
77 String name, String description, Date modifiedDate,
78 String[] tagsCategories, String[] tagsEntries,
79 ExpandoBridge expandoBridge) {
80
81 Document doc = new DocumentImpl();
82
83 doc.addUID(PORTLET_ID, imageId);
84
85 doc.addModifiedDate(modifiedDate);
86
87 doc.addKeyword(Field.COMPANY_ID, companyId);
88 doc.addKeyword(Field.PORTLET_ID, PORTLET_ID);
89 doc.addKeyword(Field.GROUP_ID, groupId);
90
91 doc.addText(Field.TITLE, name);
92 doc.addText(Field.DESCRIPTION, description);
93 doc.addKeyword(Field.TAGS_CATEGORIES, tagsCategories);
94 doc.addKeyword(Field.TAGS_ENTRIES, tagsEntries);
95
96 doc.addKeyword("folderId", folderId);
97 doc.addKeyword(Field.ENTRY_CLASS_NAME, IGImage.class.getName());
98 doc.addKeyword(Field.ENTRY_CLASS_PK, imageId);
99
100 ExpandoBridgeIndexerUtil.addAttributes(doc, expandoBridge);
101
102 return doc;
103 }
104
105 public static String getImageUID(long imageId) {
106 Document doc = new DocumentImpl();
107
108 doc.addUID(PORTLET_ID, imageId);
109
110 return doc.get(Field.UID);
111 }
112
113 public static void updateImage(
114 long companyId, long groupId, long folderId, long imageId,
115 String name, String description, Date modifiedDate,
116 String[] tagsCategories, String[] tagsEntries,
117 ExpandoBridge expandoBridge)
118 throws SearchException {
119
120 Document doc = getImageDocument(
121 companyId, groupId, folderId, imageId, name, description,
122 modifiedDate, tagsCategories, tagsEntries, expandoBridge);
123
124 SearchEngineUtil.updateDocument(companyId, doc.get(Field.UID), doc);
125 }
126
127 public String[] getClassNames() {
128 return _CLASS_NAMES;
129 }
130
131 public DocumentSummary getDocumentSummary(
132 com.liferay.portal.kernel.search.Document doc, PortletURL portletURL) {
133
134
136 String title = doc.get(Field.TITLE);
137
138
140 String content = doc.get(Field.DESCRIPTION);
141
142 content = StringUtil.shorten(content, 200);
143
144
146 String imageId = doc.get(Field.ENTRY_CLASS_PK);
147
148 portletURL.setParameter("struts_action", "/image_gallery/edit_image");
149 portletURL.setParameter(Field.ENTRY_CLASS_PK, imageId);
150
151 return new DocumentSummary(title, content, portletURL);
152 }
153
154 public void reIndex(String className, long classPK) throws SearchException {
155 try {
156 IGImageLocalServiceUtil.reIndex(classPK);
157 }
158 catch (Exception e) {
159 throw new SearchException(e);
160 }
161 }
162
163 public void reIndex(String[] ids) throws SearchException {
164 try {
165 IGFolderLocalServiceUtil.reIndex(ids);
166 }
167 catch (Exception e) {
168 throw new SearchException(e);
169 }
170 }
171
172 private static final String[] _CLASS_NAMES = new String[] {
173 IGImage.class.getName()
174 };
175
176 }