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;
029
030import org.ametys.cms.contenttype.ContentTypeExtensionPoint;
031import org.ametys.cms.contenttype.ContentTypesHelper;
032import org.ametys.cms.repository.Content;
033import org.ametys.plugins.repository.AmetysObjectResolver;
034import org.ametys.plugins.repository.version.VersionableAmetysObject;
035
036/**
037 * This action export the plugin name and content type for the current content.
038 */
039public class GetContentAction extends ServiceableAction
040{
041    /** The key for plugin's name */
042    public static final String RESULT_PLUGINNAME = "plugin";
043    /** The key for content type */
044    public static final String RESULT_CONTENTTYPE = "contentType";
045    /** The key for content version */
046    public static final String RESULT_CONTENTVERSION = "contentVersion";
047    /** The key for rendering  language */
048    public static final String RESULT_RENDERINGLANGUAGE = "renderingLanguage";
049    
050    /** The content type extension point */
051    protected ContentTypeExtensionPoint _contentTypeExtensionPoint;
052    /** The ametys object resolver */
053    protected AmetysObjectResolver _resolver;
054    /** Helper for content types */
055    protected ContentTypesHelper _contentTypeshelper;
056    
057    @Override
058    public void service(ServiceManager serviceManager) throws ServiceException
059    {
060        super.service(serviceManager);
061        _contentTypeExtensionPoint = (ContentTypeExtensionPoint) serviceManager.lookup(ContentTypeExtensionPoint.ROLE);
062        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
063        _contentTypeshelper = (ContentTypesHelper) manager.lookup(ContentTypesHelper.ROLE);
064    }
065
066    public Map<String, String> act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception
067    {
068        Map<String, String> result = new HashMap<>();
069        Request request = ObjectModelHelper.getRequest(objectModel);
070        
071        Content content;
072        String id = parameters.getParameter("id", null);
073        String path = parameters.getParameter("path", null);
074        
075        if (id != null)
076        {
077            content = _resolver.resolveById(id);
078        }
079        else
080        {
081            path = path.replaceAll("%3A", ":");
082            content = _resolver.resolveByPath(path);
083        }
084        
085        String revision = parameters.isParameter("contentVersion") ? parameters.getParameter("contentVersion") : null;
086        if (revision != null && revision.length() == 0)
087        {
088            revision = null;
089        }
090        ((VersionableAmetysObject) content).switchToRevision(revision);
091        
092        request.setAttribute(Content.class.getName(), content);
093        
094        if (request.getAttribute(RESULT_RENDERINGLANGUAGE) == null)
095        {
096            request.setAttribute(RESULT_RENDERINGLANGUAGE, content.getLanguage());
097            result.put(RESULT_RENDERINGLANGUAGE, content.getLanguage());
098        }
099        else
100        {
101            result.put(RESULT_RENDERINGLANGUAGE, (String) request.getAttribute(RESULT_RENDERINGLANGUAGE));
102        }
103
104        String contentTypeId = _contentTypeshelper.getContentTypeIdForRendering(content);
105        
106        result.put(RESULT_CONTENTTYPE, contentTypeId);
107        result.put(RESULT_PLUGINNAME, _contentTypeshelper.getContentTypePluginForRendering(content));
108        result.put(RESULT_CONTENTVERSION, revision);
109        
110        return result;
111    }
112}
113