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.util.StringPool;
29 import com.liferay.portal.kernel.util.StringUtil;
30 import com.liferay.portal.kernel.xml.Document;
31 import com.liferay.portal.kernel.xml.Element;
32 import com.liferay.portal.model.User;
33 import com.liferay.portal.search.BaseOpenSearchImpl;
34 import com.liferay.portal.service.UserLocalServiceUtil;
35 import com.liferay.portal.theme.ThemeDisplay;
36 import com.liferay.portal.util.PortletKeys;
37 import com.liferay.portal.util.WebKeys;
38 import com.liferay.portal.util.comparator.UserLastNameComparator;
39
40 import java.util.Date;
41 import java.util.List;
42
43 import javax.portlet.PortletURL;
44
45 import javax.servlet.http.HttpServletRequest;
46
47
52 public class DirectoryOpenSearchImpl extends BaseOpenSearchImpl {
53
54 public static final String SEARCH_PATH = "/c/directory/open_search";
55
56 public String search(
57 HttpServletRequest request, long groupId, long userId,
58 String keywords, int startPage, int itemsPerPage, String format)
59 throws SearchException {
60
61 try {
62 return _search(request, keywords, startPage, itemsPerPage, format);
63 }
64 catch (Exception e) {
65 throw new SearchException(e);
66 }
67 }
68
69 private String _search(
70 HttpServletRequest request, String keywords, int startPage,
71 int itemsPerPage, String format)
72 throws Exception {
73
74 ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
75 WebKeys.THEME_DISPLAY);
76
77 int start = (startPage * itemsPerPage) - itemsPerPage;
78 int end = startPage * itemsPerPage;
79
80 List<User> results = UserLocalServiceUtil.search(
81 themeDisplay.getCompanyId(), keywords, Boolean.TRUE, null, start,
82 end, new UserLastNameComparator(true));
83
84 String[] queryTerms = StringUtil.split(keywords, StringPool.SPACE);
85
86 int total = UserLocalServiceUtil.searchCount(
87 themeDisplay.getCompanyId(), keywords, Boolean.TRUE, null);
88
89 Object[] values = addSearchResults(
90 queryTerms, keywords, startPage, itemsPerPage, total, start,
91 "Liferay Directory Search: " + keywords, SEARCH_PATH, format,
92 themeDisplay);
93
94 Document doc = (Document)values[0];
95 Element root = (Element)values[1];
96
97 for (User user : results) {
98 String portletId = PortletKeys.DIRECTORY;
99
100
103 PortletURL portletURL = getPortletURL(
104 request, portletId, themeDisplay.getScopeGroupId());
105
106 portletURL.setParameter("struts_action", "/directory/view_user");
107 portletURL.setParameter(
108 "p_u_i_d", String.valueOf(user.getUserId()));
109
110 String title = user.getFullName();
111 String url = portletURL.toString();
112 Date modifedDate = user.getModifiedDate();
113 String content =
114 user.getFullName() + " <" + user.getEmailAddress() + ">";
115 double score = 1.0;
116
117 addSearchResult(
118 root, title, url, modifedDate, content, score, format);
119 }
120
121 if (_log.isDebugEnabled()) {
122 _log.debug("Return\n" + doc.asXML());
123 }
124
125 return doc.asXML();
126 }
127
128 private static Log _log =
129 LogFactoryUtil.getLog(DirectoryOpenSearchImpl.class);
130
131 }