1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.exoplatform.services.wcm.search.connector;
18
19 import java.text.DateFormat;
20 import java.text.Normalizer;
21 import java.text.SimpleDateFormat;
22 import java.util.*;
23
24 import javax.jcr.Node;
25 import javax.jcr.RepositoryException;
26
27 import org.exoplatform.commons.api.search.SearchServiceConnector;
28 import org.exoplatform.commons.api.search.data.SearchContext;
29 import org.exoplatform.commons.api.search.data.SearchResult;
30 import org.exoplatform.container.ExoContainerContext;
31 import org.exoplatform.container.xml.InitParams;
32 import org.exoplatform.portal.config.UserPortalConfig;
33 import org.exoplatform.portal.config.UserPortalConfigService;
34 import org.exoplatform.portal.mop.SiteKey;
35 import org.exoplatform.portal.mop.user.UserNavigation;
36 import org.exoplatform.portal.mop.user.UserPortalContext;
37 import org.exoplatform.services.cms.documents.DocumentService;
38 import org.exoplatform.services.cms.drives.DriveData;
39 import org.exoplatform.services.cms.drives.ManageDriveService;
40 import org.exoplatform.services.cms.impl.Utils;
41 import org.exoplatform.services.jcr.RepositoryService;
42 import org.exoplatform.services.log.ExoLogger;
43 import org.exoplatform.services.log.Log;
44 import org.exoplatform.services.security.ConversationState;
45 import org.exoplatform.services.security.IdentityConstants;
46 import org.exoplatform.services.wcm.core.NodetypeConstant;
47 import org.exoplatform.services.wcm.search.QueryCriteria;
48 import org.exoplatform.services.wcm.search.ResultNode;
49 import org.exoplatform.services.wcm.search.SiteSearchService;
50 import org.exoplatform.services.wcm.search.base.AbstractPageList;
51 import org.exoplatform.services.wcm.search.base.EcmsSearchResult;
52 import org.exoplatform.services.wcm.utils.WCMCoreUtils;
53
54
55
56
57 public abstract class BaseSearchServiceConnector extends SearchServiceConnector {
58
59 public static final String sortByDate = "date";
60 public static final String sortByRelevancy = "relevancy";
61 public static final String sortByTitle = "title";
62
63 protected SiteSearchService siteSearch_;
64 protected DocumentService documentService;
65 protected ManageDriveService driveService_;
66
67 private static final Log LOG = ExoLogger.getLogger(BaseSearchServiceConnector.class.getName());
68
69 public static final String DEFAULT_SITENAME = "intranet";
70 public static final String PAGE_NAGVIGATION = "documents";
71 public static final String NONE_NAGVIGATION = "#";
72 public static final String PORTLET_NAME = "FileExplorerPortlet";
73
74 public BaseSearchServiceConnector(InitParams initParams) throws Exception {
75 super(initParams);
76 siteSearch_ = WCMCoreUtils.getService(SiteSearchService.class);
77 documentService = WCMCoreUtils.getService(DocumentService.class);
78 driveService_ = WCMCoreUtils.getService(ManageDriveService.class);
79 }
80
81
82
83
84
85
86
87
88
89
90
91
92
93 @Override
94 public Collection<SearchResult> search(SearchContext context,
95 String query,
96 Collection<String> sites,
97 int offset,
98 int limit,
99 String sort,
100 String order) {
101 Collection<SearchResult> ret = new ArrayList<SearchResult>();
102
103 if (query != null) {
104 query = query.trim();
105 }
106 QueryCriteria criteria = createQueryCriteria(query, offset, limit, sort, order);
107
108 try {
109 criteria.setSiteName(getSitesStr(sites));
110 ret = convertResult(searchNodes(criteria, context), limit, offset, context);
111 } catch (Exception e) {
112 if (LOG.isErrorEnabled()) {
113 LOG.error(e.getMessage(), e);
114 }
115 }
116 return ret;
117 }
118
119
120
121
122
123
124 private String getSitesStr(Collection<String> sites) {
125 if (sites == null || sites.size() == 0) return null;
126 StringBuffer s = new StringBuffer();
127 for (String site : sites) {
128 s.append(site).append(',');
129 }
130 return s.substring(0, s.length() - 1);
131 }
132
133
134
135
136
137
138
139
140
141
142 protected abstract QueryCriteria createQueryCriteria(String query, long offset, long limit, String sort, String order);
143
144
145
146
147
148
149 protected abstract AbstractPageList<ResultNode> searchNodes(QueryCriteria criteria, SearchContext context) throws Exception;
150
151
152
153
154
155 protected abstract ResultNode filterNode(ResultNode node) throws RepositoryException;
156
157
158
159
160
161
162 protected List<SearchResult> convertResult(AbstractPageList<ResultNode> pageList, int limit, int offset, SearchContext context) {
163 List<SearchResult> ret = new ArrayList<SearchResult>();
164 try {
165 if (pageList != null) {
166 for (int i = 1; i <= pageList.getAvailablePage(); i++) {
167 List<ResultNode> list = pageList.getPageWithOffsetCare(i);
168 if (list == null || list.size() == 0) return ret;
169 for (Object obj : list) {
170 try {
171 if (obj instanceof ResultNode) {
172 ResultNode retNode = filterNode((ResultNode)obj);
173 if (retNode == null) {
174 continue;
175 }
176
177 Calendar date = getDate(retNode);
178 String url = getPath(retNode, context);
179 if (url == null) continue;
180
181 EcmsSearchResult result =
182
183 new EcmsSearchResult(url,
184 getPreviewUrl(retNode, context),
185 getTitleResult(retNode),
186 retNode.getExcerpt(),
187 getDetails(retNode, context),
188 getImageUrl(retNode),
189 date.getTimeInMillis(),
190 (long)retNode.getScore(),
191 getFileType(retNode),
192 retNode.getPath());
193 if (result != null) {
194 ret.add(result);
195 }
196 if (ret.size() >= limit) {
197 return ret;
198 }
199 }
200 } catch (Exception e) {
201 if (LOG.isErrorEnabled()) {
202 LOG.error("Failed to get result information.", e);
203 }
204 }
205 }
206 }
207 }
208 } catch (Exception e) {
209 if (LOG.isErrorEnabled()) {
210 LOG.error(e.getMessage(), e);
211 }
212 }
213 return ret;
214 }
215
216
217
218
219
220
221
222
223
224 protected String fileSize(Node node) throws Exception {
225 return Utils.fileSize(node);
226 }
227
228
229
230
231
232
233
234 protected Calendar getDate(Node node) throws Exception {
235 return node.hasProperty(NodetypeConstant.EXO_LAST_MODIFIED_DATE) ?
236 node.getProperty(NodetypeConstant.EXO_LAST_MODIFIED_DATE).getDate() :
237 node.hasProperty(NodetypeConstant.EXO_DATE_CREATED) ?
238 node.getProperty(NodetypeConstant.EXO_DATE_CREATED).getDate() :
239 Calendar.getInstance();
240 }
241
242
243
244
245
246
247 protected String formatDate(Calendar date) {
248 DateFormat format = SimpleDateFormat.getDateTimeInstance(SimpleDateFormat.FULL, SimpleDateFormat.SHORT);
249 return " - " + format.format(date.getTime());
250 }
251
252 protected String getDriveTitle(DriveData driveData) {
253 if (driveData == null) {
254 return "";
255 }
256 String id = driveData.getName();
257 String path = driveData.getResolvedHomePath();
258
259 try {
260 Class spaceServiceClass = Class.forName("org.exoplatform.social.core.space.spi.SpaceService");
261 Object spaceService = ExoContainerContext.getCurrentContainer().getComponentInstanceOfType(spaceServiceClass);
262
263 Class spaceClass = Class.forName("org.exoplatform.social.core.space.model.Space");
264 Object space = spaceServiceClass.getDeclaredMethod("getSpaceByGroupId", String.class)
265 .invoke(spaceService, id.replace(".", "/"));
266 if (space != null) {
267 return String.valueOf(spaceClass.getDeclaredMethod("getDisplayName").invoke(space));
268 }
269 } catch (Exception e) {
270
271 if (LOG.isInfoEnabled()) {
272 LOG.info("Can not find the space corresponding to drive " + id);
273 }
274 }
275
276 try {
277 RepositoryService repoService = WCMCoreUtils.getService(RepositoryService.class);
278 Node groupNode = (Node)WCMCoreUtils.getSystemSessionProvider().getSession(
279 repoService.getCurrentRepository().getConfiguration().getDefaultWorkspaceName(),
280 repoService.getCurrentRepository()).getItem(path);
281 while (groupNode.getParent() != null) {
282 if (groupNode.hasProperty(NodetypeConstant.EXO_LABEL)) {
283 return groupNode.getProperty(NodetypeConstant.EXO_LABEL).getString();
284 } else groupNode = groupNode.getParent();
285 }
286 return id.replace(".", " / ");
287 } catch(Exception e) {
288 return id.replace(".", " / ");
289 }
290 }
291
292
293
294
295
296
297
298 protected static String removeAccents(String query) {
299 query = Normalizer.normalize(query, Normalizer.Form.NFD);
300 query = query.replaceAll("[\\p{InCombiningDiacriticalMarks}]", "");
301 return query;
302 }
303
304
305
306
307
308
309
310 protected abstract String getPath(ResultNode node, SearchContext context) throws Exception;
311
312
313
314
315
316
317
318 protected String getPreviewUrl(ResultNode node, SearchContext context) throws Exception {
319
320 return getPath(node, context);
321 }
322
323
324
325
326
327
328 protected abstract String getFileType(ResultNode node) throws Exception;
329
330
331
332
333
334
335
336 protected abstract String getTitleResult(ResultNode node) throws Exception;
337
338
339
340
341
342 protected abstract String getImageUrl(Node node);
343
344
345
346
347
348
349 protected abstract String getDetails(ResultNode node, SearchContext context) throws Exception;
350
351 }