001    /**
002     * Copyright (c) 2000-2013 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                    _request = request;
033                    _namespace = namespace;
034    
035                    request.setAttribute(WebKeys.SEARCH_CONTAINER_REFERENCE, this);
036            }
037    
038            public String getId() {
039                    return getId(SearchContainer.DEFAULT_VAR);
040            }
041    
042            public String getId(String var) {
043                    SearchContainer<?> searchContainer = _searchContainers.get(var);
044    
045                    if (searchContainer == null) {
046                            return StringPool.BLANK;
047                    }
048    
049                    return searchContainer.getId(_request, _namespace);
050            }
051    
052            public void register(SearchContainer<?> searchContainer) {
053                    register(SearchContainer.DEFAULT_VAR, searchContainer);
054            }
055    
056            public void register(String var, SearchContainer<?> searchContainer) {
057                    _searchContainers.put(var, searchContainer);
058            }
059    
060            private String _namespace;
061            private HttpServletRequest _request;
062            private Map<String, SearchContainer<?>> _searchContainers =
063                    new HashMap<String, SearchContainer<?>>();
064    
065    }