1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.directory.util;
21  
22  import com.liferay.portal.kernel.log.Log;
23  import com.liferay.portal.kernel.log.LogFactoryUtil;
24  import com.liferay.portal.kernel.search.SearchException;
25  import com.liferay.portal.kernel.xml.Document;
26  import com.liferay.portal.kernel.xml.Element;
27  import com.liferay.portal.model.User;
28  import com.liferay.portal.search.BaseOpenSearchImpl;
29  import com.liferay.portal.service.UserLocalServiceUtil;
30  import com.liferay.portal.theme.ThemeDisplay;
31  import com.liferay.portal.util.PortletKeys;
32  import com.liferay.portal.util.WebKeys;
33  import com.liferay.portal.util.comparator.UserLastNameComparator;
34  
35  import java.util.Date;
36  import java.util.List;
37  
38  import javax.portlet.PortletURL;
39  
40  import javax.servlet.http.HttpServletRequest;
41  
42  /**
43   * <a href="DirectoryOpenSearchImpl.java.html"><b><i>View Source</i></b></a>
44   *
45   * @author Brian Wing Shun Chan
46   *
47   */
48  public class DirectoryOpenSearchImpl extends BaseOpenSearchImpl {
49  
50      public static final String SEARCH_PATH = "/c/directory/open_search";
51  
52      public String search(
53              HttpServletRequest request, long groupId, long userId,
54              String keywords, int startPage, int itemsPerPage, String format)
55          throws SearchException {
56  
57          try {
58              return _search(request, keywords, startPage, itemsPerPage, format);
59          }
60          catch (Exception e) {
61              throw new SearchException(e);
62          }
63      }
64  
65      private String _search(
66              HttpServletRequest request, String keywords, int startPage,
67              int itemsPerPage, String format)
68          throws Exception {
69  
70          ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
71              WebKeys.THEME_DISPLAY);
72  
73          int start = (startPage * itemsPerPage) - itemsPerPage;
74          int end = startPage * itemsPerPage;
75  
76          List<User> results = UserLocalServiceUtil.search(
77              themeDisplay.getCompanyId(), keywords, Boolean.TRUE, null, start,
78              end, new UserLastNameComparator(true));
79  
80          int total = UserLocalServiceUtil.searchCount(
81              themeDisplay.getCompanyId(), keywords, Boolean.TRUE, null);
82  
83          Object[] values = addSearchResults(
84              keywords, startPage, itemsPerPage, total, start,
85              "Liferay Directory Search: " + keywords, SEARCH_PATH, format,
86              themeDisplay);
87  
88          Document doc = (Document)values[0];
89          Element root = (Element)values[1];
90  
91          for (User user : results) {
92              String portletId = PortletKeys.DIRECTORY;
93  
94              //String portletTitle = PortalUtil.getPortletTitle(
95              //  portletId, themeDisplay.getUser());
96  
97              PortletURL portletURL = getPortletURL(request, portletId);
98  
99              String title = user.getFullName();
100             String url = portletURL.toString();
101             Date modifedDate = user.getModifiedDate();
102             String content =
103                 user.getFullName() + " &lt;" + user.getEmailAddress() + "&gt;";
104             double score = 1.0;
105 
106             addSearchResult(
107                 root, title, url, modifedDate, content, score, format);
108         }
109 
110         if (_log.isDebugEnabled()) {
111             _log.debug("Return\n" + doc.asXML());
112         }
113 
114         return doc.asXML();
115     }
116 
117     private static Log _log =
118         LogFactoryUtil.getLog(DirectoryOpenSearchImpl.class);
119 
120 }