001/*
002 * (C) Copyright 2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Nuxeo - initial API and implementation
018 */
019package org.nuxeo.connect.client.ui;
020
021import java.io.Serializable;
022import java.util.HashMap;
023import java.util.Map;
024
025import javax.faces.context.FacesContext;
026import javax.servlet.http.HttpServletRequest;
027import javax.servlet.http.HttpSession;
028
029import org.nuxeo.ecm.webengine.jaxrs.context.RequestContext;
030
031/**
032 * Provide contextual access to the {@link ListingFilterSetting} for each listing. Use HttpSession to store a map of
033 * {@link ListingFilterSetting} This class is used to share state between the WebEngine and the JSF parts
034 *
035 * @author Tiry (tdelprat@nuxeo.com)
036 */
037public class SharedPackageListingsSettings implements Serializable {
038
039    private static final long serialVersionUID = 1L;
040
041    protected Map<String, ListingFilterSetting> settings = new HashMap<String, ListingFilterSetting>();
042
043    public static final String SESSION_KEY = "org.nuxeo.connect.client.ui.PackageListingSettings";
044
045    public ListingFilterSetting get(String listName) {
046        if (!settings.containsKey(listName)) {
047            settings.put(listName, new ListingFilterSetting());
048        }
049        return settings.get(listName);
050    }
051
052    public static SharedPackageListingsSettings instance() {
053
054        HttpServletRequest request = null;
055        if (FacesContext.getCurrentInstance() != null) {
056            request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
057        } else if (RequestContext.getActiveContext() != null) {
058            request = RequestContext.getActiveContext().getRequest();
059        }
060
061        if (request != null) {
062            return instance(request);
063        }
064        return null;
065    }
066
067    public static SharedPackageListingsSettings instance(HttpServletRequest request) {
068        return instance(request.getSession(true));
069    }
070
071    public static SharedPackageListingsSettings instance(HttpSession session) {
072        Object val = session.getAttribute(SESSION_KEY);
073        if (val == null || !(val instanceof SharedPackageListingsSettings)) {
074            val = new SharedPackageListingsSettings();
075            session.setAttribute(SESSION_KEY, val);
076        }
077        return (SharedPackageListingsSettings) val;
078    }
079
080}