001/*
002 * (C) Copyright 2009 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     stan
016 */
017
018package org.nuxeo.ecm.webapp.webcontainer;
019
020import java.util.ArrayList;
021import java.util.List;
022
023import javax.faces.context.FacesContext;
024import javax.servlet.http.HttpServletRequest;
025
026import org.jboss.seam.ScopeType;
027import org.jboss.seam.annotations.In;
028import org.jboss.seam.annotations.Name;
029import org.jboss.seam.annotations.Scope;
030import org.nuxeo.theme.styling.service.ThemeStylingService;
031import org.nuxeo.theme.styling.service.descriptors.FlavorDescriptor;
032import org.nuxeo.theme.styling.service.descriptors.IconDescriptor;
033import org.nuxeo.theme.styling.service.descriptors.LogoDescriptor;
034
035@Name("themeActions")
036@Scope(ScopeType.PAGE)
037public class ThemeActionsBean implements ThemeActions {
038
039    private static final long serialVersionUID = 1L;
040
041    protected String defaultPage;
042
043    protected String currentPage;
044
045    protected String currentFlavor;
046
047    protected LogoDescriptor currentLogo;
048
049    protected List<IconDescriptor> currentFavicons;
050
051    @In(create = true, required = false)
052    protected transient ThemeStylingService themeStylingService;
053
054    @Override
055    public String getDefaultTheme() {
056        if (defaultPage == null) {
057            defaultPage = themeStylingService.negotiate("jsfDefaultPage", FacesContext.getCurrentInstance());
058        }
059        return defaultPage;
060    }
061
062    @Override
063    public LogoDescriptor getLogo() {
064        if (currentLogo == null) {
065            String flavor = getCurrentFlavor();
066            currentLogo = getLogo(flavor);
067        }
068        return currentLogo;
069    }
070
071    /**
072     * Returns favicons for current flavor.
073     *
074     * @since 7.4
075     */
076    public List<IconDescriptor> getFavicons() {
077        if (currentFavicons == null) {
078            String flavor = getCurrentFlavor();
079            FlavorDescriptor f = themeStylingService.getFlavor(flavor);
080            currentFavicons = new ArrayList<IconDescriptor>();
081            if (f != null) {
082                List<IconDescriptor> icons = f.getFavicons();
083                currentFavicons.addAll(icons);
084            }
085        }
086        return currentFavicons;
087    }
088
089    public String getCurrentFlavor() {
090        if (currentFlavor == null) {
091            // put current page name to request for flavor negotiation
092            FacesContext faces = FacesContext.getCurrentInstance();
093            HttpServletRequest request = (HttpServletRequest) faces.getExternalContext().getRequest();
094            request.setAttribute("jsfPage", getCurrentPage());
095            currentFlavor = themeStylingService.negotiate("jsfFlavor", FacesContext.getCurrentInstance());
096        }
097        return currentFlavor;
098    }
099
100    @Override
101    public String getCurrentFlavor(String pageName) {
102        if (currentPage == null || !currentPage.equals(pageName)) {
103            setCurrentPage(pageName);
104            // reset current flavor to resolve it again
105            currentFlavor = null;
106        }
107        return getCurrentFlavor();
108    }
109
110    public String getCurrentPage() {
111        if (currentPage == null) {
112            currentPage = themeStylingService.negotiate("jsfPage", FacesContext.getCurrentInstance());
113            if (currentPage == null) {
114                // BBB
115                currentPage = getDefaultTheme();
116            }
117        }
118        return currentPage;
119    }
120
121    public void setCurrentPage(String pageName) {
122        currentPage = pageName;
123    }
124
125    @Override
126    public LogoDescriptor getLogo(String flavorName) {
127        if (flavorName == null) {
128            return null;
129        }
130        return themeStylingService.getLogo(flavorName);
131    }
132
133    @Override
134    public FlavorDescriptor getFlavor(String flavorName) {
135        if (flavorName == null) {
136            return null;
137        }
138        return themeStylingService.getFlavor(flavorName);
139    }
140
141}