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