001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.search;
016    
017    import com.liferay.portal.kernel.util.ParamUtil;
018    import com.liferay.portal.kernel.util.StringUtil;
019    import com.liferay.portal.kernel.util.WebKeys;
020    import com.liferay.portal.theme.ThemeDisplay;
021    
022    import java.io.Serializable;
023    
024    import java.util.Enumeration;
025    import java.util.HashMap;
026    import java.util.Map;
027    
028    import javax.servlet.http.HttpServletRequest;
029    
030    /**
031     * @author Brian Wing Shun Chan
032     */
033    public class SearchContextFactory {
034    
035            public static SearchContext getInstance(HttpServletRequest request) {
036                    SearchContext searchContext = new SearchContext();
037    
038                    // Theme display
039    
040                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
041                            WebKeys.THEME_DISPLAY);
042    
043                    searchContext.setCompanyId(themeDisplay.getCompanyId());
044                    searchContext.setGroupIds(new long[] {themeDisplay.getScopeGroupId()});
045                    searchContext.setUserId(themeDisplay.getUserId());
046    
047                    // Attributes
048    
049                    Map<String, Serializable> attributes =
050                            new HashMap<String, Serializable>();
051    
052                    Enumeration<String> enu = request.getParameterNames();
053    
054                    while (enu.hasMoreElements()) {
055                            String param = enu.nextElement();
056    
057                            String[] values = request.getParameterValues(param);
058    
059                            if ((values != null) && (values.length > 0)) {
060                                    if (values.length == 1) {
061                                            attributes.put(param, values[0]);
062                                    }
063                                    else {
064                                            attributes.put(param, values);
065                                    }
066                            }
067                    }
068    
069                    searchContext.setAttributes(attributes);
070    
071                    // Asset
072    
073                    long[] assetCategoryIds = StringUtil.split(
074                            ParamUtil.getString(request, "assetCategoryIds"), 0L);
075    
076                    String[] assetTagNames = StringUtil.split(
077                            ParamUtil.getString(request, "assetTagNames"));
078    
079                    searchContext.setAssetCategoryIds(assetCategoryIds);
080                    searchContext.setAssetTagNames(assetTagNames);
081    
082                    return searchContext;
083            }
084    
085    }