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.util.StringPool;
29  import com.liferay.portal.kernel.util.StringUtil;
30  import com.liferay.portal.kernel.xml.Document;
31  import com.liferay.portal.kernel.xml.Element;
32  import com.liferay.portal.model.User;
33  import com.liferay.portal.search.BaseOpenSearchImpl;
34  import com.liferay.portal.service.UserLocalServiceUtil;
35  import com.liferay.portal.theme.ThemeDisplay;
36  import com.liferay.portal.util.PortletKeys;
37  import com.liferay.portal.util.WebKeys;
38  import com.liferay.portal.util.comparator.UserLastNameComparator;
39  
40  import java.util.Date;
41  import java.util.List;
42  
43  import javax.portlet.PortletURL;
44  
45  import javax.servlet.http.HttpServletRequest;
46  
47  /**
48   * <a href="DirectoryOpenSearchImpl.java.html"><b><i>View Source</i></b></a>
49   *
50   * @author Brian Wing Shun Chan
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, long groupId, long userId,
58              String keywords, int startPage, int itemsPerPage, String format)
59          throws SearchException {
60  
61          try {
62              return _search(request, keywords, startPage, itemsPerPage, format);
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, String format)
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 UserLastNameComparator(true));
83  
84          String[] queryTerms = StringUtil.split(keywords, StringPool.SPACE);
85  
86          int total = UserLocalServiceUtil.searchCount(
87              themeDisplay.getCompanyId(), keywords, Boolean.TRUE, null);
88  
89          Object[] values = addSearchResults(
90              queryTerms, keywords, startPage, itemsPerPage, total, start,
91              "Liferay Directory Search: " + keywords, SEARCH_PATH, format,
92              themeDisplay);
93  
94          Document doc = (Document)values[0];
95          Element root = (Element)values[1];
96  
97          for (User user : results) {
98              String portletId = PortletKeys.DIRECTORY;
99  
100             //String portletTitle = PortalUtil.getPortletTitle(
101             //  portletId, themeDisplay.getUser());
102 
103             PortletURL portletURL = getPortletURL(
104                 request, portletId, themeDisplay.getScopeGroupId());
105 
106             portletURL.setParameter("struts_action", "/directory/view_user");
107             portletURL.setParameter(
108                 "p_u_i_d", String.valueOf(user.getUserId()));
109 
110             String title = user.getFullName();
111             String url = portletURL.toString();
112             Date modifedDate = user.getModifiedDate();
113             String content =
114                 user.getFullName() + " &lt;" + user.getEmailAddress() + "&gt;";
115             double score = 1.0;
116 
117             addSearchResult(
118                 root, title, url, modifedDate, content, score, format);
119         }
120 
121         if (_log.isDebugEnabled()) {
122             _log.debug("Return\n" + doc.asXML());
123         }
124 
125         return doc.asXML();
126     }
127 
128     private static Log _log =
129         LogFactoryUtil.getLog(DirectoryOpenSearchImpl.class);
130 
131 }