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.ContactLastNameComparator;
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
51 public class DirectoryOpenSearchImpl extends BaseOpenSearchImpl {
52
53 public static final String SEARCH_PATH = "/c/directory/open_search";
54
55 public String search(
56 HttpServletRequest request, String keywords, int startPage,
57 int itemsPerPage, String format)
58 throws SearchException {
59
60 try {
61 return _search(request, keywords, startPage, itemsPerPage, format);
62 }
63 catch (Exception e) {
64 throw new SearchException(e);
65 }
66 }
67
68 private String _search(
69 HttpServletRequest request, String keywords, int startPage,
70 int itemsPerPage, String format)
71 throws Exception {
72
73 ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
74 WebKeys.THEME_DISPLAY);
75
76 int start = (startPage * itemsPerPage) - itemsPerPage;
77 int end = startPage * itemsPerPage;
78
79 List<User> results = UserLocalServiceUtil.search(
80 themeDisplay.getCompanyId(), keywords, Boolean.TRUE, null, start,
81 end, new ContactLastNameComparator(true));
82
83 int total = UserLocalServiceUtil.searchCount(
84 themeDisplay.getCompanyId(), keywords, Boolean.TRUE, null);
85
86 Object[] values = addSearchResults(
87 keywords, startPage, itemsPerPage, total, start,
88 "Liferay Directory Search: " + keywords, SEARCH_PATH, format,
89 themeDisplay);
90
91 Document doc = (Document)values[0];
92 Element root = (Element)values[1];
93
94 for (User user : results) {
95 String portletId = PortletKeys.DIRECTORY;
96
97
100 PortletURL portletURL = getPortletURL(request, portletId);
101
102 String title = user.getFullName();
103 String url = portletURL.toString();
104 Date modifedDate = user.getModifiedDate();
105 String content =
106 user.getFullName() + " <" + user.getEmailAddress() + ">";
107 double score = 1.0;
108
109 addSearchResult(
110 root, title, url, modifedDate, content, score, format);
111 }
112
113 if (_log.isDebugEnabled()) {
114 _log.debug("Return\n" + doc.asXML());
115 }
116
117 return doc.asXML();
118 }
119
120 private static Log _log =
121 LogFactoryUtil.getLog(DirectoryOpenSearchImpl.class);
122
123 }