001/*
002 *  Copyright 2010 Anyware Services
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 */
016package org.ametys.web.source;
017
018import java.util.ArrayList;
019import java.util.List;
020
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023import org.apache.cocoon.components.ContextHelper;
024import org.apache.cocoon.environment.Request;
025import org.apache.commons.lang3.StringUtils;
026
027import org.ametys.cms.source.ContentView;
028import org.ametys.cms.source.DefaultContentView;
029import org.ametys.web.contenttype.SkinContentViewHelper;
030import org.ametys.web.contenttype.SkinContentViewHelper.SkinContentView;
031
032/**
033 * This implementation of a {@link ContentView} looks first in the directory skin://stylesheets/content/[cType]/[cType]-[view].[extension] then skin://stylesheets/content/[cType]/[cType].[extension] then defaults to {@link DefaultContentView}.
034 */
035public class WebContentView extends DefaultContentView
036{
037    private SkinContentViewHelper _skinContentViewHelper;
038
039    @Override
040    public void service(ServiceManager smanager) throws ServiceException
041    {
042        super.service(smanager);
043        _skinContentViewHelper = (SkinContentViewHelper) smanager.lookup(SkinContentViewHelper.ROLE);
044    }
045    
046    @Override
047    protected List<String> _getDefaultSourceURIs(String pluginName, String formatSuffix, String extension)
048    {
049        List<String> result = new ArrayList<>();
050        if (!"web".equals(pluginName))
051        {
052            result.add("plugin:web://stylesheets/default-content" + formatSuffix + "." + extension);
053        }
054        
055        result.addAll(super._getDefaultSourceURIs(pluginName, formatSuffix, extension));
056        
057        return result;
058    }
059    
060    @Override
061    protected List<String> _getSourceURIsForDynamicContentType(String contentTypeAlias, String view, String formatSuffix, String pluginName, String extension)
062    {
063        ArrayList<String> result = new ArrayList<>();
064        
065        result.add("skin://stylesheets/content/" + contentTypeAlias + "/" + contentTypeAlias + formatSuffix + "-" + view + "." + extension);
066        result.add("skin://stylesheets/content/" + contentTypeAlias + "/" + contentTypeAlias + formatSuffix + "." + extension);
067        result.addAll(super._getSourceURIsForDynamicContentType(contentTypeAlias, view, formatSuffix, pluginName, extension));
068
069        return result;
070    }
071    
072    @Override
073    protected List<String> _getSourceURIsForContentTypeWithPlugin(String pluginName, String contentTypeAlias, String contentTypeId, String view, String formatSuffix, String extension)
074    {
075        List<String> result = new ArrayList<>();
076        
077        Request request = ContextHelper.getRequest(_context);
078        String renderingViewName = request.getParameter(SkinContentViewHelper.REQUEST_PARAM_RENDERING_VIEW_NAME);
079        
080        if (StringUtils.isNotEmpty(renderingViewName))
081        {
082            String format = formatSuffix.length() == 0 ? "html" : StringUtils.substringAfter(formatSuffix, "2");
083            
084            // check if this rendering view exists
085            SkinContentView contentViewFromSkin = _skinContentViewHelper.getContentViewFromSkin(renderingViewName, contentTypeId, format);
086            if (contentViewFromSkin != null)
087            {
088                result.add("skin://stylesheets/content/" + contentTypeAlias + "/" + contentTypeAlias + formatSuffix + "-" + contentViewFromSkin.name() + "." + extension);
089                result.add("skin://stylesheets/content/" + contentTypeAlias + "/" + contentTypeAlias + formatSuffix + "-" + contentViewFromSkin.modelViewName() + "." + extension);
090            }
091            else
092            {
093                // skin view does not exist, back to model view
094                result.add("skin://stylesheets/content/" + contentTypeAlias + "/" + contentTypeAlias + formatSuffix + "-" + view + "." + extension);
095            }
096        }
097        else
098        {
099            result.add("skin://stylesheets/content/" + contentTypeAlias + "/" + contentTypeAlias + formatSuffix + "-" + view + "." + extension);
100        }
101        
102        result.add("skin://stylesheets/content/" + contentTypeAlias + "/" + contentTypeAlias + formatSuffix + "." + extension);
103        result.addAll(super._getSourceURIsForContentTypeWithPlugin(pluginName, contentTypeAlias, contentTypeId, view, formatSuffix, extension));
104
105        return result;
106    }
107}