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.ecm.localconf;
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.styling.negotiation.AbstractNegotiator;
033import org.nuxeo.theme.styling.service.ThemeStylingService;
034
035/**
036 * Returns the flavor associated to the current space (workspace, section, ...) as a String. Return null otherwise.
037 *
038 * @since 5.5
039 */
040public class LocalThemeFlavor extends AbstractNegotiator {
041
042    private static final Log log = LogFactory.getLog(LocalThemeFlavor.class);
043
044    @Override
045    public String getResult(String target, 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 = null;
071        if (context instanceof FacesContext) {
072            faces = (FacesContext) context;
073        } else {
074            return null;
075        }
076        String theme = (String) faces.getExternalContext().getRequestMap().get(getProperty("negotiatedPageVariable"));
077        if (theme != null) {
078            ThemeStylingService service = Framework.getService(ThemeStylingService.class);
079            if (service == null) {
080                log.error("Could not find the ThemeStylingService");
081                return null;
082            }
083            List<String> flavors = service.getFlavorNames(theme);
084            if (flavors != null && flavors.contains(flavor)) {
085                return flavor;
086            } else {
087                if (log.isDebugEnabled()) {
088                    log.debug(String.format("Local configuration: current theme page "
089                            + "'%s' does not accept the flavor '%s'", theme, flavor));
090                }
091            }
092        }
093        return null;
094    }
095}