1
22
23 package com.liferay.portlet.directory.util;
24
25 import com.liferay.portal.kernel.search.SearchException;
26 import com.liferay.portal.model.User;
27 import com.liferay.portal.search.BaseOpenSearchImpl;
28 import com.liferay.portal.service.UserLocalServiceUtil;
29 import com.liferay.portal.theme.ThemeDisplay;
30 import com.liferay.portal.util.PortletKeys;
31 import com.liferay.portal.util.WebKeys;
32 import com.liferay.portal.util.comparator.ContactLastNameComparator;
33
34 import java.util.Date;
35 import java.util.List;
36
37 import javax.portlet.PortletURL;
38
39 import javax.servlet.http.HttpServletRequest;
40
41 import org.apache.commons.logging.Log;
42 import org.apache.commons.logging.LogFactory;
43
44 import org.dom4j.Element;
45
46
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 req, String keywords, int startPage,
58 int itemsPerPage)
59 throws SearchException {
60
61 try {
62 return _search(req, keywords, startPage, itemsPerPage);
63 }
64 catch (Exception e) {
65 throw new SearchException(e);
66 }
67 }
68
69 private String _search(
70 HttpServletRequest req, String keywords, int startPage,
71 int itemsPerPage)
72 throws Exception {
73
74 ThemeDisplay themeDisplay =
75 (ThemeDisplay)req.getAttribute(WebKeys.THEME_DISPLAY);
76
77 int begin = (startPage * itemsPerPage) - itemsPerPage;
78 int end = startPage * itemsPerPage;
79
80 List results = UserLocalServiceUtil.search(
81 themeDisplay.getCompanyId(), keywords, Boolean.TRUE, null, begin,
82 end, new ContactLastNameComparator(true));
83
84 int total = UserLocalServiceUtil.searchCount(
85 themeDisplay.getCompanyId(), keywords, Boolean.TRUE, null);
86
87 Object[] values = addSearchResults(
88 keywords, startPage, itemsPerPage, total, null,
89 "Liferay Directory Search: " + keywords, SEARCH_PATH, themeDisplay);
90
91 org.dom4j.Document doc = (org.dom4j.Document)values[1];
92 Element root = (Element)values[2];
93
94 for (int i = 0; i < results.size(); i++) {
95 User user = (User)results.get(i);
96
97 String portletId = PortletKeys.DIRECTORY;
98
99
102 PortletURL portletURL = getPortletURL(req, portletId);
103
104 String title = user.getFullName();
105 String url = portletURL.toString();
106 Date modifedDate = user.getModifiedDate();
107 String content =
108 user.getFullName() + " <" + user.getEmailAddress() + ">";
109 double score = 1.0;
110
111 addSearchResult(root, title, url, modifedDate, content, score);
112 }
113
114 if (_log.isDebugEnabled()) {
115 _log.debug("Return\n" + doc.asXML());
116 }
117
118 return doc.asXML();
119 }
120
121 private static Log _log = LogFactory.getLog(DirectoryOpenSearchImpl.class);
122
123 }