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