1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
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  /**
46   * <a href="DirectoryOpenSearchImpl.java.html"><b><i>View Source</i></b></a>
47   *
48   * @author Brian Wing Shun Chan
49   *
50   */
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              //String portletTitle = PortalUtil.getPortletTitle(
97              //  portletId, themeDisplay.getUser());
98  
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() + " &lt;" + user.getEmailAddress() + "&gt;";
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 }