001/*
002 *  Copyright 2025 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.cms.content;
017
018import java.util.Map;
019import java.util.Optional;
020
021import org.apache.avalon.framework.component.Component;
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024import org.apache.avalon.framework.service.Serviceable;
025import org.apache.commons.lang3.StringUtils;
026
027import org.ametys.cms.contenttype.ContentType;
028import org.ametys.cms.contenttype.ContentTypesHelper;
029import org.ametys.cms.contenttype.ContentTypesParserHelper.ViewConfigurations;
030import org.ametys.runtime.model.View;
031import org.ametys.runtime.model.ViewParserContext;
032
033/**
034 * Default implementation of a {@link ContentViewProvider}
035 */
036public class DefaultContentViewProvider implements ContentViewProvider, Component, Serviceable
037{
038    /** The content types helper */
039    protected ContentTypesHelper _contentTypesHelper;
040    
041    public void service(ServiceManager manager) throws ServiceException
042    {
043        _contentTypesHelper = (ContentTypesHelper) manager.lookup(ContentTypesHelper.ROLE);
044    }
045    
046    public Map<String, ViewConfigurations> getViewConfigurations(ContentType contentType, ViewParserContext context)
047    {
048        return contentType.getViewConfigurations();
049    }
050    
051    public Optional<ViewConfigurations> getViewConfigurations(ContentType contentType, String viewName, ViewParserContext context)
052    {
053        Map<String, ViewConfigurations> viewConfigurations = getViewConfigurations(contentType, context);
054        return Optional.ofNullable(viewConfigurations.get(viewName));
055    }
056    
057    public View getView(String viewName, String[] contentTypeIds, String[] mixinIds)
058    {
059        return _contentTypesHelper.getView(viewName, contentTypeIds, mixinIds);
060    }
061
062    public View getViewWithFallback(String viewName, String fallbackViewName, String[] contentTypeIds, String[] mixinIds)
063    {
064        String usedFallbackViewName = Optional.ofNullable(fallbackViewName)
065                                              .filter(StringUtils::isNotBlank)
066                                              .orElse(ContentType.MAIN_VIEW_NAME);
067
068        // Use the fallbackViewName if no viewName is provided
069        String usedViewName = Optional.ofNullable(viewName)
070                                      .filter(StringUtils::isNotBlank)
071                                      .orElse(usedFallbackViewName);
072
073        View view = getView(usedViewName, contentTypeIds, mixinIds);
074
075        if (view == null && !usedViewName.equals(usedFallbackViewName))
076        {
077            view = getView(usedFallbackViewName, contentTypeIds, mixinIds);
078        }
079
080        return view;
081    }
082}