001/*
002 *  Copyright 2024 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.properties.section.content;
017
018import java.util.HashMap;
019import java.util.LinkedHashMap;
020import java.util.Locale;
021import java.util.Map;
022import java.util.Optional;
023
024import org.apache.avalon.framework.configuration.Configuration;
025import org.apache.avalon.framework.configuration.ConfigurationException;
026import org.apache.avalon.framework.context.Context;
027import org.apache.avalon.framework.context.ContextException;
028import org.apache.avalon.framework.context.Contextualizable;
029import org.apache.avalon.framework.service.ServiceException;
030import org.apache.avalon.framework.service.ServiceManager;
031import org.apache.avalon.framework.service.Serviceable;
032import org.apache.cocoon.components.ContextHelper;
033import org.apache.cocoon.i18n.I18nUtils;
034import org.apache.commons.lang3.LocaleUtils;
035import org.apache.commons.lang3.StringUtils;
036
037import org.ametys.cms.contenttype.ContentTypesHelper;
038import org.ametys.cms.properties.section.AbstractDefaultPropertySection;
039import org.ametys.cms.repository.Content;
040import org.ametys.plugins.repository.AmetysObject;
041import org.ametys.runtime.model.DefinitionContext;
042import org.ametys.runtime.model.View;
043import org.ametys.runtime.model.type.DataContext;
044
045/**
046 * Section to display details view of the content.
047 */
048public class ContentViewSection extends AbstractDefaultPropertySection implements Serviceable, Contextualizable
049{
050    private Context _context;
051    private ContentTypesHelper _contentTypesHelper;
052    private String _viewName;
053    
054    public void contextualize(Context context) throws ContextException
055    {
056        _context = context;
057    }
058    
059    public void service(ServiceManager smanager) throws ServiceException
060    {
061        _contentTypesHelper = (ContentTypesHelper) smanager.lookup(ContentTypesHelper.ROLE);
062    }
063    
064    @Override
065    public void configure(Configuration configuration) throws ConfigurationException
066    {
067        super.configure(configuration);
068        _viewName = configuration.getChild("view").getValue("details");
069    }
070    
071    public boolean supports(AmetysObject ametysObject)
072    {
073        return ametysObject instanceof Content;
074    }
075    
076    @Override
077    public Map<String, Object> getParameters(AmetysObject ametysObject)
078    {
079        Map<String, Object> resultMap = new HashMap<>(super.getParameters(ametysObject));
080        // FIXME CMS-12210 renderingLanguage should not be used anymore
081        resultMap.put("renderingLanguage", _getLocale((Content) ametysObject).getLanguage());
082        return resultMap;
083    }
084    
085    @Override
086    protected Map<String, Object> buildData(AmetysObject ametysObject)
087    {
088        Map<String, Object> resultMap = new LinkedHashMap<>();
089        
090        Content content = (Content) ametysObject;
091
092        View view = _contentTypesHelper.getView(_viewName, content);
093        if (view != null)
094        {
095            // Model
096            resultMap.put("view", view.toJSON(DefinitionContext.newInstance()));
097
098            // Values
099            Locale locale = _getLocale(content);
100            resultMap.put("values", content.dataToJSON(view, DataContext.newInstance().withEmptyValues(false).withLocale(locale)));
101        }
102        
103        return resultMap;
104    }
105    
106    @Override
107    protected String _getDefaultXSLT()
108    {
109        return "view:cms://stylesheets/properties/content/view.xsl";
110    }
111    
112    private Locale _getLocale(Content content)
113    {
114        return Optional.ofNullable(content.getLanguage())
115            .or(() -> Optional.ofNullable(ContextHelper.getRequest(_context).getParameter("lang")))
116            .filter(StringUtils::isNotEmpty)
117            .map(LocaleUtils::toLocale)
118            .orElseGet(() -> I18nUtils.findLocale(ContextHelper.getObjectModel(_context), "locale", null, Locale.getDefault(), true));
119    }
120}