1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.directory.util;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.kernel.search.SearchException;
20  import com.liferay.portal.kernel.util.StringPool;
21  import com.liferay.portal.kernel.util.StringUtil;
22  import com.liferay.portal.kernel.xml.Document;
23  import com.liferay.portal.kernel.xml.Element;
24  import com.liferay.portal.model.User;
25  import com.liferay.portal.search.BaseOpenSearchImpl;
26  import com.liferay.portal.service.UserLocalServiceUtil;
27  import com.liferay.portal.theme.ThemeDisplay;
28  import com.liferay.portal.util.PortletKeys;
29  import com.liferay.portal.util.WebKeys;
30  import com.liferay.portal.util.comparator.UserLastNameComparator;
31  
32  import java.util.Date;
33  import java.util.List;
34  
35  import javax.portlet.PortletURL;
36  
37  import javax.servlet.http.HttpServletRequest;
38  
39  /**
40   * <a href="DirectoryOpenSearchImpl.java.html"><b><i>View Source</i></b></a>
41   *
42   * @author Brian Wing Shun Chan
43   */
44  public class DirectoryOpenSearchImpl extends BaseOpenSearchImpl {
45  
46      public static final String SEARCH_PATH = "/c/directory/open_search";
47  
48      public String search(
49              HttpServletRequest request, long groupId, long userId,
50              String keywords, int startPage, int itemsPerPage, String format)
51          throws SearchException {
52  
53          try {
54              return _search(request, keywords, startPage, itemsPerPage, format);
55          }
56          catch (Exception e) {
57              throw new SearchException(e);
58          }
59      }
60  
61      private String _search(
62              HttpServletRequest request, String keywords, int startPage,
63              int itemsPerPage, String format)
64          throws Exception {
65  
66          ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
67              WebKeys.THEME_DISPLAY);
68  
69          int start = (startPage * itemsPerPage) - itemsPerPage;
70          int end = startPage * itemsPerPage;
71  
72          List<User> results = UserLocalServiceUtil.search(
73              themeDisplay.getCompanyId(), keywords, Boolean.TRUE, null, start,
74              end, new UserLastNameComparator(true));
75  
76          String[] queryTerms = StringUtil.split(keywords, StringPool.SPACE);
77  
78          int total = UserLocalServiceUtil.searchCount(
79              themeDisplay.getCompanyId(), keywords, Boolean.TRUE, null);
80  
81          Object[] values = addSearchResults(
82              queryTerms, keywords, startPage, itemsPerPage, total, start,
83              "Liferay Directory Search: " + keywords, SEARCH_PATH, format,
84              themeDisplay);
85  
86          Document doc = (Document)values[0];
87          Element root = (Element)values[1];
88  
89          for (User user : results) {
90              String portletId = PortletKeys.DIRECTORY;
91  
92              //String portletTitle = PortalUtil.getPortletTitle(
93              //  portletId, themeDisplay.getUser());
94  
95              PortletURL portletURL = getPortletURL(
96                  request, portletId, themeDisplay.getScopeGroupId());
97  
98              portletURL.setParameter("struts_action", "/directory/view_user");
99              portletURL.setParameter(
100                 "p_u_i_d", String.valueOf(user.getUserId()));
101 
102             String title = user.getFullName();
103             String url = portletURL.toString();
104             Date modifedDate = user.getModifiedDate();
105             String content =
106                 user.getFullName() + " &lt;" + user.getEmailAddress() + "&gt;";
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 = LogFactoryUtil.getLog(
121         DirectoryOpenSearchImpl.class);
122 
123 }