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