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