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.web.repository.ModifiableSiteAwareAmetysObject;
028import org.ametys.web.repository.site.Site;
029import org.ametys.web.repository.site.SiteManager;
030
031import com.opensymphony.workflow.WorkflowException;
032
033/**
034 * OSWorkflow function to create a content by copy of another
035 * 
036 * The required transient variables:
037 * - CreateContentByCopyFunction.BASE_CONTENT_KEY - Content The content that will be used for duplication.
038 * - or CreateContentByCopyFunction.BASE_CONTENT_ID - String The id of content The content that will be used for duplication.
039 * 
040 * - CreateContentByCopyFunction.COPY_MAP_KEY - Map<String, Object> The map of properties to copy. Can be null if CreateContentByCopyFunction.COPY_METADATASET_NAME is used
041 * - CreateContentByCopyFunction.SITE_KEY - The content's site name
042 */
043public class CreateContentByCopyFunction extends org.ametys.cms.workflow.copy.CreateContentByCopyFunction
044{
045    /** Constant for storing the site to use into the transient variables map. */
046    public static final String SITE_KEY = Site.class.getName();
047    
048    /** The sites manager */
049    protected SiteManager _siteManager;
050    
051    @Override
052    public void service(ServiceManager manager) throws ServiceException
053    {
054        super.service(manager);
055        _siteManager = (SiteManager) manager.lookup(SiteManager.ROLE);
056    }
057    
058    @Override
059    protected String _getObjectType(Map transientVars)
060    {
061        return RepositoryConstants.NAMESPACE_PREFIX + ":defaultWebContent";
062    }
063    
064    @Override
065    protected ModifiableTraversableAmetysObject _getContentRoot(Map transientVars) throws WorkflowException
066    {
067        String siteName = _getNonNullVar(transientVars, SITE_KEY, "Missing sitename");
068
069        Site site = null;
070        try
071        {
072            site = _siteManager.getSite(siteName);
073        }
074        catch (UnknownAmetysObjectException e)
075        {
076            throw new WorkflowException("Site does not exist", e);
077        }
078        
079        // FIXME use constant or site getter
080        return site.getChild(RepositoryConstants.NAMESPACE_PREFIX_INTERNAL + ":contents");
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        // FIXME API what if not modifiable
091        ((ModifiableSiteAwareAmetysObject) content).setSiteName(siteName);
092    }
093}