001/*
002 * (C) Copyright 2011 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 *     Anahide Tchertchian
016 */
017package org.nuxeo.theme.compat.negotiators;
018
019import java.util.List;
020
021import javax.faces.context.FacesContext;
022
023import org.apache.commons.logging.Log;
024import org.apache.commons.logging.LogFactory;
025import org.jboss.seam.Component;
026import org.nuxeo.ecm.core.api.DocumentModel;
027import org.nuxeo.runtime.api.Framework;
028import org.nuxeo.theme.localconfiguration.LocalThemeConfig;
029import org.nuxeo.theme.localconfiguration.LocalThemeHelper;
030import org.nuxeo.theme.negotiation.Negotiator;
031import org.nuxeo.theme.negotiation.Scheme;
032import org.nuxeo.theme.styling.service.ThemeStylingService;
033
034/**
035 * Returns the flavor associated to the current space (workspace, section, ...) as a String. Return null otherwise.
036 *
037 * @since 5.5
038 */
039public class LocalThemeFlavor implements Scheme {
040
041    private static final Log log = LogFactory.getLog(LocalThemeFlavor.class);
042
043    public String getOutcome(Object context) {
044        Boolean useOldThemeConf = Boolean.valueOf(Framework.getProperty(LocalThemeConfig.OLD_THEME_CONFIGURATION_PROPERTY));
045        if (Boolean.TRUE.equals(useOldThemeConf)) {
046            return null;
047        }
048
049        DocumentModel currentSuperSpace = (DocumentModel) Component.getInstance("currentSuperSpace");
050        if (currentSuperSpace == null) {
051            return null;
052        }
053
054        // Get the placeful local theme configuration for the current
055        // workspace.
056        LocalThemeConfig localThemeConfig = LocalThemeHelper.getLocalThemeConfig(currentSuperSpace);
057        if (localThemeConfig == null) {
058            return null;
059        }
060
061        // extract the flavor
062        String flavor = localThemeConfig.getFlavor();
063        if (flavor == null) {
064            return null;
065        }
066
067        // Check that the theme page accepts this flavor
068        FacesContext faces = (FacesContext) context;
069        String theme = (String) faces.getExternalContext().getRequestMap().get(
070                Negotiator.NEGOTIATION_RESULT_PREFIX + Negotiator.NEGOTIATION_OBJECT.theme.name());
071        if (theme != null) {
072            ThemeStylingService service = Framework.getService(ThemeStylingService.class);
073            if (service == null) {
074                log.error("Could not find the ThemeStylingService");
075                return null;
076            }
077            List<String> flavors = service.getFlavorNames(theme);
078            if (flavors != null && flavors.contains(flavor)) {
079                return flavor;
080            } else {
081                if (log.isDebugEnabled()) {
082                    log.debug(String.format("Local configuration: current theme page "
083                            + "'%s' does not accept the flavor '%s'", theme, flavor));
084                }
085            }
086        }
087        return null;
088    }
089}