1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.kernel.search;
16  
17  import com.liferay.portal.kernel.util.ParamUtil;
18  import com.liferay.portal.kernel.util.StringUtil;
19  import com.liferay.portal.kernel.util.WebKeys;
20  import com.liferay.portal.theme.ThemeDisplay;
21  
22  import java.io.Serializable;
23  
24  import java.util.Enumeration;
25  import java.util.HashMap;
26  import java.util.Map;
27  
28  import javax.servlet.http.HttpServletRequest;
29  
30  /**
31   * <a href="SearchContextFactory.java.html"><b><i>View Source</i></b></a>
32   *
33   * @author Brian Wing Shun Chan
34   */
35  public class SearchContextFactory {
36  
37      public static SearchContext getInstance(HttpServletRequest request) {
38          SearchContext searchContext = new SearchContext();
39  
40          // Theme display
41  
42          ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
43              WebKeys.THEME_DISPLAY);
44  
45          searchContext.setCompanyId(themeDisplay.getCompanyId());
46          searchContext.setGroupIds(new long[] {themeDisplay.getScopeGroupId()});
47          searchContext.setUserId(themeDisplay.getUserId());
48  
49          // Attributes
50  
51          Map<String, Serializable> attributes =
52              new HashMap<String, Serializable>();
53  
54          Enumeration<String> enu = request.getParameterNames();
55  
56          while (enu.hasMoreElements()) {
57              String param = enu.nextElement();
58  
59              String[] values = request.getParameterValues(param);
60  
61              if ((values != null) && (values.length > 0)) {
62                  if (values.length == 1) {
63                      attributes.put(param, values[0]);
64                  }
65                  else {
66                      attributes.put(param, values);
67                  }
68              }
69          }
70  
71          searchContext.setAttributes(attributes);
72  
73          // Asset
74  
75          long[] assetCategoryIds = StringUtil.split(
76              ParamUtil.getString(request, "assetCategoryIds"), 0L);
77  
78          String[] assetTagNames = StringUtil.split(
79              ParamUtil.getString(request, "assetTagNames"));
80  
81          searchContext.setAssetCategoryIds(assetCategoryIds);
82          searchContext.setAssetTagNames(assetTagNames);
83  
84          return searchContext;
85      }
86  
87  }