001/*
002 * (C) Copyright 2009 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 *     stan
018 */
019
020package org.nuxeo.ecm.webapp.webcontainer;
021
022import java.util.ArrayList;
023import java.util.List;
024
025import javax.faces.context.FacesContext;
026import javax.servlet.http.HttpServletRequest;
027
028import org.jboss.seam.ScopeType;
029import org.jboss.seam.annotations.In;
030import org.jboss.seam.annotations.Name;
031import org.jboss.seam.annotations.Scope;
032import org.nuxeo.theme.styling.service.ThemeStylingService;
033import org.nuxeo.theme.styling.service.descriptors.FlavorDescriptor;
034import org.nuxeo.theme.styling.service.descriptors.IconDescriptor;
035import org.nuxeo.theme.styling.service.descriptors.LogoDescriptor;
036
037@Name("themeActions")
038@Scope(ScopeType.PAGE)
039public class ThemeActionsBean implements ThemeActions {
040
041    private static final long serialVersionUID = 1L;
042
043    protected String defaultPage;
044
045    protected String currentPage;
046
047    protected String currentFlavor;
048
049    protected LogoDescriptor currentLogo;
050
051    protected List<IconDescriptor> currentFavicons;
052
053    @In(create = true, required = false)
054    protected transient ThemeStylingService themeStylingService;
055
056    @Override
057    public String getDefaultTheme() {
058        if (defaultPage == null) {
059            defaultPage = themeStylingService.negotiate("jsfDefaultPage", FacesContext.getCurrentInstance());
060        }
061        return defaultPage;
062    }
063
064    @Override
065    public LogoDescriptor getLogo() {
066        if (currentLogo == null) {
067            String flavor = getCurrentFlavor();
068            currentLogo = getLogo(flavor);
069        }
070        return currentLogo;
071    }
072
073    /**
074     * Returns favicons for current flavor.
075     *
076     * @since 7.4
077     */
078    public List<IconDescriptor> getFavicons() {
079        if (currentFavicons == null) {
080            String flavor = getCurrentFlavor();
081            FlavorDescriptor f = themeStylingService.getFlavor(flavor);
082            currentFavicons = new ArrayList<IconDescriptor>();
083            if (f != null) {
084                List<IconDescriptor> icons = f.getFavicons();
085                currentFavicons.addAll(icons);
086            }
087        }
088        return currentFavicons;
089    }
090
091    public String getCurrentFlavor() {
092        if (currentFlavor == null) {
093            // put current page name to request for flavor negotiation
094            FacesContext faces = FacesContext.getCurrentInstance();
095            HttpServletRequest request = (HttpServletRequest) faces.getExternalContext().getRequest();
096            request.setAttribute("jsfPage", getCurrentPage());
097            currentFlavor = themeStylingService.negotiate("jsfFlavor", FacesContext.getCurrentInstance());
098        }
099        return currentFlavor;
100    }
101
102    @Override
103    public String getCurrentFlavor(String pageName) {
104        if (currentPage == null || !currentPage.equals(pageName)) {
105            setCurrentPage(pageName);
106            // reset current flavor to resolve it again
107            currentFlavor = null;
108        }
109        return getCurrentFlavor();
110    }
111
112    public String getCurrentPage() {
113        if (currentPage == null) {
114            currentPage = themeStylingService.negotiate("jsfPage", FacesContext.getCurrentInstance());
115            if (currentPage == null) {
116                // BBB
117                currentPage = getDefaultTheme();
118            }
119        }
120        return currentPage;
121    }
122
123    public void setCurrentPage(String pageName) {
124        currentPage = pageName;
125    }
126
127    @Override
128    public LogoDescriptor getLogo(String flavorName) {
129        if (flavorName == null) {
130            return null;
131        }
132        return themeStylingService.getLogo(flavorName);
133    }
134
135    @Override
136    public FlavorDescriptor getFlavor(String flavorName) {
137        if (flavorName == null) {
138            return null;
139        }
140        return themeStylingService.getFlavor(flavorName);
141    }
142
143}