1   /**
2    * Copyright (c) 2000-2008 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.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  /**
47   * <a href="DirectoryOpenSearchImpl.java.html"><b><i>View Source</i></b></a>
48   *
49   * @author Brian Wing Shun Chan
50   *
51   */
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, String keywords, int startPage,
58              int itemsPerPage)
59          throws SearchException {
60  
61          try {
62              return _search(request, keywords, startPage, itemsPerPage);
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)
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 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, start,
89              "Liferay Directory Search: " + keywords, SEARCH_PATH, themeDisplay);
90  
91          org.dom4j.Document doc = (org.dom4j.Document)values[0];
92          Element root = (Element)values[1];
93  
94          for (User user : results) {
95              String portletId = PortletKeys.DIRECTORY;
96  
97              //String portletTitle = PortalUtil.getPortletTitle(
98              //  portletId, themeDisplay.getUser());
99  
100             PortletURL portletURL = getPortletURL(request, portletId);
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(root, title, url, modifedDate, content, score);
110         }
111 
112         if (_log.isDebugEnabled()) {
113             _log.debug("Return\n" + doc.asXML());
114         }
115 
116         return doc.asXML();
117     }
118 
119     private static Log _log = LogFactory.getLog(DirectoryOpenSearchImpl.class);
120 
121 }