1
14
15 package com.liferay.portlet.documentlibrary.util;
16
17 import com.liferay.documentlibrary.model.FileModel;
18 import com.liferay.portal.kernel.portlet.LiferayPortletURL;
19 import com.liferay.portal.kernel.portlet.LiferayWindowState;
20 import com.liferay.portal.kernel.search.Document;
21 import com.liferay.portal.kernel.search.Field;
22 import com.liferay.portal.kernel.search.Indexer;
23 import com.liferay.portal.kernel.search.IndexerRegistryUtil;
24 import com.liferay.portal.kernel.search.SearchContext;
25 import com.liferay.portal.kernel.search.SearchEngineUtil;
26 import com.liferay.portal.kernel.search.Summary;
27 import com.liferay.portal.kernel.util.GetterUtil;
28 import com.liferay.portal.kernel.util.StringUtil;
29 import com.liferay.portal.kernel.util.Validator;
30 import com.liferay.portal.model.Group;
31 import com.liferay.portal.search.BaseIndexer;
32 import com.liferay.portal.service.GroupLocalServiceUtil;
33 import com.liferay.portal.util.PortletKeys;
34 import com.liferay.portlet.asset.service.AssetCategoryLocalServiceUtil;
35 import com.liferay.portlet.asset.service.AssetTagLocalServiceUtil;
36 import com.liferay.portlet.documentlibrary.model.DLFileEntry;
37 import com.liferay.portlet.documentlibrary.model.DLFolder;
38 import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
39 import com.liferay.portlet.documentlibrary.service.DLFolderLocalServiceUtil;
40
41 import java.util.Date;
42 import java.util.List;
43
44 import javax.portlet.PortletRequest;
45 import javax.portlet.PortletURL;
46 import javax.portlet.WindowStateException;
47
48
54 public class DLIndexer extends BaseIndexer {
55
56 public static final String[] CLASS_NAMES = {DLFileEntry.class.getName()};
57
58 public static final String PORTLET_ID = PortletKeys.DOCUMENT_LIBRARY;
59
60 public DLIndexer() {
61 IndexerRegistryUtil.register(
62 new com.liferay.documentlibrary.util.DLIndexer());
63 }
64
65 public String[] getClassNames() {
66 return CLASS_NAMES;
67 }
68
69 public Summary getSummary(
70 Document document, String snippet, PortletURL portletURL) {
71
72 LiferayPortletURL liferayPortletURL = (LiferayPortletURL)portletURL;
73
74 liferayPortletURL.setLifecycle(PortletRequest.ACTION_PHASE);
75
76 try {
77 liferayPortletURL.setWindowState(LiferayWindowState.EXCLUSIVE);
78 }
79 catch (WindowStateException wse) {
80 }
81
82 String repositoryId = document.get("repositoryId");
83 String fileName = document.get("path");
84
85 String title = fileName;
86
87 String content = snippet;
88
89 if (Validator.isNull(snippet)) {
90 content = StringUtil.shorten(document.get(Field.CONTENT), 200);
91 }
92
93 portletURL.setParameter("struts_action", "/document_library/get_file");
94 portletURL.setParameter("folderId", repositoryId);
95 portletURL.setParameter("name", fileName);
96
97 return new Summary(title, content, portletURL);
98 }
99
100 protected void doDelete(Object obj) throws Exception {
101 DLFileEntry fileEntry = (DLFileEntry)obj;
102
103 FileModel fileModel = new FileModel();
104
105 fileModel.setCompanyId(fileEntry.getCompanyId());
106 fileModel.setFileName(fileEntry.getName());
107 fileModel.setPortletId(PORTLET_ID);
108 fileModel.setRepositoryId(fileEntry.getRepositoryId());
109
110 Indexer indexer = IndexerRegistryUtil.getIndexer(FileModel.class);
111
112 indexer.delete(fileModel);
113 }
114
115 protected void doReindex(String[] ids) throws Exception {
116 long companyId = GetterUtil.getLong(ids[0]);
117
118 reindexFolders(companyId);
119 reindexRoot(companyId);
120 }
121
122 protected void doReindex(String className, long classPK) throws Exception {
123 DLFileEntry fileEntry = DLFileEntryLocalServiceUtil.getFileEntry(
124 classPK);
125
126 doReindex(fileEntry);
127 }
128
129 protected void doReindex(Object obj) throws Exception {
130 DLFileEntry fileEntry = (DLFileEntry)obj;
131
132 Document document = getDocument(fileEntry);
133
134 if (document != null) {
135 SearchEngineUtil.updateDocument(
136 fileEntry.getCompanyId(), document.get(Field.UID), document);
137 }
138 }
139
140 protected Document doGetDocument(Object obj) throws Exception {
141 DLFileEntry fileEntry = (DLFileEntry)obj;
142
143 DLFolder folder = fileEntry.getFolder();
144
145 long companyId = fileEntry.getCompanyId();
146 long groupId = folder.getGroupId();
147 long repositoryId = fileEntry.getRepositoryId();
148 String fileName = fileEntry.getName();
149 long fileEntryId = fileEntry.getFileEntryId();
150 String properties = fileEntry.getLuceneProperties();
151 Date modifiedDate = fileEntry.getModifiedDate();
152
153 long[] assetCategoryIds = AssetCategoryLocalServiceUtil.getCategoryIds(
154 DLFileEntry.class.getName(), fileEntryId);
155 String[] assetTagNames = AssetTagLocalServiceUtil.getTagNames(
156 DLFileEntry.class.getName(), fileEntryId);
157
158 FileModel fileModel = new FileModel();
159
160 fileModel.setAssetCategoryIds(assetCategoryIds);
161 fileModel.setAssetTagNames(assetTagNames);
162 fileModel.setCompanyId(companyId);
163 fileModel.setFileEntryId(fileEntryId);
164 fileModel.setFileName(fileName);
165 fileModel.setGroupId(groupId);
166 fileModel.setModifiedDate(modifiedDate);
167 fileModel.setPortletId(PORTLET_ID);
168 fileModel.setProperties(properties);
169 fileModel.setRepositoryId(repositoryId);
170
171 Indexer indexer = IndexerRegistryUtil.getIndexer(FileModel.class);
172
173 return indexer.getDocument(fileModel);
174 }
175
176 protected String getPortletId(SearchContext searchContext) {
177 return PORTLET_ID;
178 }
179
180 protected void reindexFolders(long companyId) throws Exception {
181 int folderCount = DLFolderLocalServiceUtil.getCompanyFoldersCount(
182 companyId);
183
184 int folderPages = folderCount / Indexer.DEFAULT_INTERVAL;
185
186 for (int i = 0; i <= folderPages; i++) {
187 int folderStart = (i * Indexer.DEFAULT_INTERVAL);
188 int folderEnd = folderStart + Indexer.DEFAULT_INTERVAL;
189
190 reindexFolders(companyId, folderStart, folderEnd);
191 }
192 }
193
194 protected void reindexFolders(
195 long companyId, int folderStart, int folderEnd)
196 throws Exception {
197
198 List<DLFolder> folders = DLFolderLocalServiceUtil.getCompanyFolders(
199 companyId, folderStart, folderEnd);
200
201 for (DLFolder folder : folders) {
202 String portletId = PortletKeys.DOCUMENT_LIBRARY;
203 long groupId = folder.getGroupId();
204 long folderId = folder.getFolderId();
205
206 String[] newIds = {
207 String.valueOf(companyId), portletId,
208 String.valueOf(groupId), String.valueOf(folderId)
209 };
210
211 Indexer indexer = IndexerRegistryUtil.getIndexer(FileModel.class);
212
213 indexer.reindex(newIds);
214 }
215 }
216
217 protected void reindexRoot(long companyId) throws Exception {
218 int groupCount = GroupLocalServiceUtil.getCompanyGroupsCount(companyId);
219
220 int groupPages = groupCount / Indexer.DEFAULT_INTERVAL;
221
222 for (int i = 0; i <= groupPages; i++) {
223 int groupStart = (i * Indexer.DEFAULT_INTERVAL);
224 int groupEnd = groupStart + Indexer.DEFAULT_INTERVAL;
225
226 reindexRoot(companyId, groupStart, groupEnd);
227 }
228 }
229
230 protected void reindexRoot(long companyId, int groupStart, int groupEnd)
231 throws Exception {
232
233 List<Group> groups = GroupLocalServiceUtil.getCompanyGroups(
234 companyId, groupStart, groupEnd);
235
236 for (Group group : groups) {
237 String portletId = PortletKeys.DOCUMENT_LIBRARY;
238 long groupId = group.getGroupId();
239 long folderId = groupId;
240
241 String[] newIds = {
242 String.valueOf(companyId), portletId,
243 String.valueOf(groupId), String.valueOf(folderId)
244 };
245
246 Indexer indexer = IndexerRegistryUtil.getIndexer(FileModel.class);
247
248 indexer.reindex(newIds);
249 }
250 }
251
252 }