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.search;
16  
17  import com.liferay.portal.kernel.search.SearchContext;
18  import com.liferay.portal.kernel.util.WebKeys;
19  import com.liferay.portal.theme.ThemeDisplay;
20  
21  import java.io.Serializable;
22  
23  import java.util.Enumeration;
24  import java.util.HashMap;
25  import java.util.Map;
26  
27  import javax.servlet.http.HttpServletRequest;
28  
29  /**
30   * <a href="SearchContextFactory.java.html"><b><i>View Source</i></b></a>
31   *
32   * @author Brian Wing Shun Chan
33   */
34  public class SearchContextFactory {
35  
36      public static SearchContext getInstance(HttpServletRequest request) {
37          SearchContext searchContext = new SearchContext();
38  
39          // Theme display
40  
41          ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
42              WebKeys.THEME_DISPLAY);
43  
44          searchContext.setCompanyId(themeDisplay.getCompanyId());
45          searchContext.setGroupId(themeDisplay.getScopeGroupId());
46          searchContext.setUserId(themeDisplay.getUserId());
47  
48          // Attributes
49  
50          Map<String, Serializable> attributes =
51              new HashMap<String, Serializable>();
52  
53          Enumeration<String> enu = request.getParameterNames();
54  
55          while (enu.hasMoreElements()) {
56              String param = enu.nextElement();
57  
58              String[] values = request.getParameterValues(param);
59  
60              if ((values != null) && (values.length > 0)) {
61                  if (values.length == 1) {
62                      attributes.put(param, values[0]);
63                  }
64                  else {
65                      attributes.put(param, values);
66                  }
67              }
68          }
69  
70          searchContext.setAttributes(attributes);
71  
72          return searchContext;
73      }
74  
75  }