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.ContactLastNameComparator;
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, String keywords, int startPage,
54 int itemsPerPage)
55 throws SearchException {
56
57 try {
58 return _search(request, keywords, startPage, itemsPerPage);
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)
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 ContactLastNameComparator(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, themeDisplay);
86
87 Document doc = (Document)values[0];
88 Element root = (Element)values[1];
89
90 for (User user : results) {
91 String portletId = PortletKeys.DIRECTORY;
92
93
96 PortletURL portletURL = getPortletURL(request, portletId);
97
98 String title = user.getFullName();
99 String url = portletURL.toString();
100 Date modifedDate = user.getModifiedDate();
101 String content =
102 user.getFullName() + " <" + user.getEmailAddress() + ">";
103 double score = 1.0;
104
105 addSearchResult(root, title, url, modifedDate, content, score);
106 }
107
108 if (_log.isDebugEnabled()) {
109 _log.debug("Return\n" + doc.asXML());
110 }
111
112 return doc.asXML();
113 }
114
115 private static Log _log =
116 LogFactoryUtil.getLog(DirectoryOpenSearchImpl.class);
117
118 }