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.cms.content;
017
018import java.util.HashMap;
019import java.util.Map;
020
021import org.apache.avalon.framework.parameters.Parameters;
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024import org.apache.cocoon.acting.ServiceableAction;
025import org.apache.cocoon.environment.ObjectModelHelper;
026import org.apache.cocoon.environment.Redirector;
027import org.apache.cocoon.environment.Request;
028import org.apache.cocoon.environment.SourceResolver;
029import org.apache.commons.lang.StringUtils;
030import org.apache.commons.lang3.ArrayUtils;
031
032import org.ametys.cms.contenttype.ContentTypeExtensionPoint;
033import org.ametys.cms.contenttype.ContentTypesHelper;
034import org.ametys.cms.repository.Content;
035import org.ametys.plugins.repository.AmetysObject;
036import org.ametys.plugins.repository.AmetysObjectResolver;
037import org.ametys.plugins.repository.version.VersionAwareAmetysObject;
038
039/**
040 * This action export the plugin name and content type for the current content.
041 */
042public class GetContentAction extends ServiceableAction
043{
044    /** The key for plugin's name */
045    public static final String RESULT_PLUGINNAME = "plugin";
046    /** The key for content type */
047    public static final String RESULT_CONTENTTYPE = "contentType";
048    /** The key for content version */
049    public static final String RESULT_CONTENTVERSION = "contentVersion";
050    /** The key for rendering  language */
051    public static final String RESULT_RENDERINGLANGUAGE = "renderingLanguage";
052    
053    /** The content type extension point */
054    protected ContentTypeExtensionPoint _contentTypeExtensionPoint;
055    /** The ametys object resolver */
056    protected AmetysObjectResolver _resolver;
057    /** Helper for content types */
058    protected ContentTypesHelper _contentTypeshelper;
059    
060    @Override
061    public void service(ServiceManager serviceManager) throws ServiceException
062    {
063        super.service(serviceManager);
064        _contentTypeExtensionPoint = (ContentTypeExtensionPoint) serviceManager.lookup(ContentTypeExtensionPoint.ROLE);
065        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
066        _contentTypeshelper = (ContentTypesHelper) manager.lookup(ContentTypesHelper.ROLE);
067    }
068
069    public Map<String, String> act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception
070    {
071        Map<String, String> result = new HashMap<>();
072        Request request = ObjectModelHelper.getRequest(objectModel);
073        
074        AmetysObject ametysObject;
075        String id = parameters.getParameter("id", null);
076        String path = parameters.getParameter("path", null);
077        
078        if (id != null)
079        {
080            ametysObject = _resolver.resolveById(id);
081        }
082        else if (path != null)
083        {
084            path = path.replaceAll("%3A", ":");
085            ametysObject = _resolver.resolveByPath(path);
086        }
087        else
088        {
089            Content content = (Content) request.getAttribute(Content.class.getName());
090            ametysObject = content != null ? content : (AmetysObject) request.getAttribute(AmetysObject.class.getName());
091        }
092        
093        if (ametysObject instanceof Content)
094        {
095            String versionLabel = parameters.getParameter("versionLabel", null);
096            String revision = parameters.getParameter("contentVersion", null);
097            
098            if (StringUtils.isNotEmpty(versionLabel) && ametysObject instanceof VersionAwareAmetysObject versionAO)
099            {
100                String[] allLabels = versionAO.getAllLabels();
101                if (ArrayUtils.contains(allLabels, versionLabel))
102                {
103                    versionAO.switchToLabel(versionLabel);
104                }
105            }
106            else if (StringUtils.isNotEmpty(revision) && ametysObject instanceof VersionAwareAmetysObject versionAO)
107            {
108                versionAO.switchToRevision(revision);
109            }
110
111            Content content = (Content) ametysObject;
112            request.setAttribute(Content.class.getName(), content);
113            if (request.getAttribute(RESULT_RENDERINGLANGUAGE) == null)
114            {
115                request.setAttribute(RESULT_RENDERINGLANGUAGE, content.getLanguage());
116                result.put(RESULT_RENDERINGLANGUAGE, content.getLanguage());
117            }
118            else
119            {
120                result.put(RESULT_RENDERINGLANGUAGE, (String) request.getAttribute(RESULT_RENDERINGLANGUAGE));
121            }
122            
123            String contentTypeId = _contentTypeshelper.getContentTypeIdForRendering(content);
124            
125            request.setAttribute(RESULT_CONTENTTYPE, contentTypeId);
126            
127            result.put(RESULT_CONTENTTYPE, contentTypeId);
128            result.put(RESULT_PLUGINNAME, _contentTypeshelper.getContentTypePluginForRendering(content));
129            result.put(RESULT_CONTENTVERSION, revision);
130        }
131        else
132        {
133            request.setAttribute(AmetysObject.class.getName(), ametysObject);
134        }
135        
136        return result;
137    }
138}
139