1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class DirectoryOpenSearchImpl extends BaseOpenSearchImpl {
51  
52      public static final String SEARCH_PATH = "/c/directory/open_search";
53  
54      public String search(
55              HttpServletRequest request, String keywords, int startPage,
56              int itemsPerPage)
57          throws SearchException {
58  
59          try {
60              return _search(request, keywords, startPage, itemsPerPage);
61          }
62          catch (Exception e) {
63              throw new SearchException(e);
64          }
65      }
66  
67      private String _search(
68              HttpServletRequest request, String keywords, int startPage,
69              int itemsPerPage)
70          throws Exception {
71  
72          ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
73              WebKeys.THEME_DISPLAY);
74  
75          int start = (startPage * itemsPerPage) - itemsPerPage;
76          int end = startPage * itemsPerPage;
77  
78          List<User> results = UserLocalServiceUtil.search(
79              themeDisplay.getCompanyId(), keywords, Boolean.TRUE, null, start,
80              end, new UserLastNameComparator(true));
81  
82          int total = UserLocalServiceUtil.searchCount(
83              themeDisplay.getCompanyId(), keywords, Boolean.TRUE, null);
84  
85          Object[] values = addSearchResults(
86              keywords, startPage, itemsPerPage, total, start,
87              "Liferay Directory Search: " + keywords, SEARCH_PATH, themeDisplay);
88  
89          Document doc = (Document)values[0];
90          Element root = (Element)values[1];
91  
92          for (User user : results) {
93              String portletId = PortletKeys.DIRECTORY;
94  
95              //String portletTitle = PortalUtil.getPortletTitle(
96              //  portletId, themeDisplay.getUser());
97  
98              PortletURL portletURL = getPortletURL(request, portletId);
99  
100             String title = user.getFullName();
101             String url = portletURL.toString();
102             Date modifedDate = user.getModifiedDate();
103             String content =
104                 user.getFullName() + " &lt;" + user.getEmailAddress() + "&gt;";
105             double score = 1.0;
106 
107             addSearchResult(root, title, url, modifedDate, content, score);
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 }