001    /**
002     * Copyright (c) 2000-present 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.util;
016    
017    import com.liferay.portal.kernel.dao.search.SearchContainer;
018    
019    import java.util.HashMap;
020    import java.util.Map;
021    
022    import javax.servlet.http.HttpServletRequest;
023    
024    /**
025     * @author Shinn Lok
026     */
027    public class SearchContainerReference {
028    
029            public SearchContainerReference(
030                    HttpServletRequest request, String namespace) {
031    
032                    _namespace = namespace;
033    
034                    request.setAttribute(WebKeys.SEARCH_CONTAINER_REFERENCE, this);
035            }
036    
037            public String getId(HttpServletRequest request) {
038                    return getId(request, SearchContainer.DEFAULT_VAR);
039            }
040    
041            public String getId(HttpServletRequest request, String var) {
042                    if (_searchContainers == null) {
043                            return StringPool.BLANK;
044                    }
045    
046                    SearchContainer<?> searchContainer = _searchContainers.get(var);
047    
048                    if (searchContainer == null) {
049                            return StringPool.BLANK;
050                    }
051    
052                    return searchContainer.getId(request, _namespace);
053            }
054    
055            public void register(SearchContainer<?> searchContainer) {
056                    register(SearchContainer.DEFAULT_VAR, searchContainer);
057            }
058    
059            public void register(String var, SearchContainer<?> searchContainer) {
060                    if (_searchContainers == null) {
061                            _searchContainers = new HashMap<>();
062                    }
063    
064                    _searchContainers.put(var, searchContainer);
065            }
066    
067            private final String _namespace;
068            private Map<String, SearchContainer<?>> _searchContainers;
069    
070    }