1
19
20 package com.liferay.portlet.directory.util;
21
22 import com.liferay.portal.kernel.log.Log;
23 import com.liferay.portal.kernel.log.LogFactoryUtil;
24 import com.liferay.portal.kernel.search.SearchException;
25 import com.liferay.portal.kernel.xml.Document;
26 import com.liferay.portal.kernel.xml.Element;
27 import com.liferay.portal.model.User;
28 import com.liferay.portal.search.BaseOpenSearchImpl;
29 import com.liferay.portal.service.UserLocalServiceUtil;
30 import com.liferay.portal.theme.ThemeDisplay;
31 import com.liferay.portal.util.PortletKeys;
32 import com.liferay.portal.util.WebKeys;
33 import com.liferay.portal.util.comparator.UserLastNameComparator;
34
35 import java.util.Date;
36 import java.util.List;
37
38 import javax.portlet.PortletURL;
39
40 import javax.servlet.http.HttpServletRequest;
41
42
48 public class DirectoryOpenSearchImpl extends BaseOpenSearchImpl {
49
50 public static final String SEARCH_PATH = "/c/directory/open_search";
51
52 public String search(
53 HttpServletRequest request, long groupId, long userId,
54 String keywords, int startPage, int itemsPerPage, String format)
55 throws SearchException {
56
57 try {
58 return _search(request, keywords, startPage, itemsPerPage, format);
59 }
60 catch (Exception e) {
61 throw new SearchException(e);
62 }
63 }
64
65 private String _search(
66 HttpServletRequest request, String keywords, int startPage,
67 int itemsPerPage, String format)
68 throws Exception {
69
70 ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
71 WebKeys.THEME_DISPLAY);
72
73 int start = (startPage * itemsPerPage) - itemsPerPage;
74 int end = startPage * itemsPerPage;
75
76 List<User> results = UserLocalServiceUtil.search(
77 themeDisplay.getCompanyId(), keywords, Boolean.TRUE, null, start,
78 end, new UserLastNameComparator(true));
79
80 int total = UserLocalServiceUtil.searchCount(
81 themeDisplay.getCompanyId(), keywords, Boolean.TRUE, null);
82
83 Object[] values = addSearchResults(
84 keywords, startPage, itemsPerPage, total, start,
85 "Liferay Directory Search: " + keywords, SEARCH_PATH, format,
86 themeDisplay);
87
88 Document doc = (Document)values[0];
89 Element root = (Element)values[1];
90
91 for (User user : results) {
92 String portletId = PortletKeys.DIRECTORY;
93
94
97 PortletURL portletURL = getPortletURL(request, portletId);
98
99 String title = user.getFullName();
100 String url = portletURL.toString();
101 Date modifedDate = user.getModifiedDate();
102 String content =
103 user.getFullName() + " <" + user.getEmailAddress() + ">";
104 double score = 1.0;
105
106 addSearchResult(
107 root, title, url, modifedDate, content, score, format);
108 }
109
110 if (_log.isDebugEnabled()) {
111 _log.debug("Return\n" + doc.asXML());
112 }
113
114 return doc.asXML();
115 }
116
117 private static Log _log =
118 LogFactoryUtil.getLog(DirectoryOpenSearchImpl.class);
119
120 }