001/*
002 *  Copyright 2016 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.workflow;
017
018import java.util.Map;
019
020import org.apache.avalon.framework.service.ServiceException;
021import org.apache.avalon.framework.service.ServiceManager;
022
023import org.ametys.cms.repository.ModifiableContent;
024import org.ametys.plugins.repository.ModifiableTraversableAmetysObject;
025import org.ametys.plugins.repository.RepositoryConstants;
026import org.ametys.plugins.repository.UnknownAmetysObjectException;
027import org.ametys.runtime.i18n.I18nizableText;
028import org.ametys.web.repository.ModifiableSiteAwareAmetysObject;
029import org.ametys.web.repository.site.Site;
030import org.ametys.web.repository.site.SiteManager;
031
032import com.opensymphony.workflow.WorkflowException;
033
034/**
035 * OSWorkflow function to create a content by copy of another
036 * 
037 * The required transient variables:
038 * - CreateContentByCopyFunction.BASE_CONTENT_KEY - Content The content that will be used for duplication.
039 * - or CreateContentByCopyFunction.BASE_CONTENT_ID - String The id of content The content that will be used for duplication.
040 * 
041 * - CreateContentByCopyFunction.COPY_MAP_KEY - Map<String, Object> The map of properties to copy. Can be null if CreateContentByCopyFunction.COPY_VIEW_NAME is used
042 * - CreateContentByCopyFunction.SITE_KEY - The content's site name
043 */
044public class CreateContentByCopyFunction extends org.ametys.cms.workflow.copy.CreateContentByCopyFunction
045{
046    /** Constant for storing the site to use into the transient variables map. */
047    public static final String SITE_KEY = Site.class.getName();
048    
049    /** The sites manager */
050    protected SiteManager _siteManager;
051    
052    @Override
053    public void service(ServiceManager manager) throws ServiceException
054    {
055        super.service(manager);
056        _siteManager = (SiteManager) manager.lookup(SiteManager.ROLE);
057    }
058    
059    @Override
060    protected String _getObjectType(Map transientVars, Map args)
061    {
062        return RepositoryConstants.NAMESPACE_PREFIX + ":defaultWebContent";
063    }
064    
065    @Override
066    protected ModifiableTraversableAmetysObject _getContentRoot(Map transientVars) throws WorkflowException
067    {
068        String siteName = _getNonNullVar(transientVars, SITE_KEY, "Missing sitename");
069
070        Site site = null;
071        try
072        {
073            site = _siteManager.getSite(siteName);
074        }
075        catch (UnknownAmetysObjectException e)
076        {
077            throw new WorkflowException("Site does not exist", e);
078        }
079        
080        return site.getRootContents();
081    }
082    
083    @Override
084    protected void _populateContent(Map transientVars, ModifiableContent content) throws WorkflowException
085    {
086        super._populateContent(transientVars, content);
087        
088        String siteName = _getNonNullVar(transientVars, SITE_KEY, "Missing sitename");
089        
090        ((ModifiableSiteAwareAmetysObject) content).setSiteName(siteName);
091    }
092    
093    @Override
094    public I18nizableText getLabel()
095    {
096        return new I18nizableText("plugin.web", "PLUGINS_WEB_CREATE_CONTENT_BY_COPY_FUNCTION_LABEL");
097    }
098}