001/*
002 *  Copyright 2019 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.plugins.extraction.edition;
017
018import java.util.Map;
019
020import org.ametys.cms.workflow.CreateContentFunction;
021import org.ametys.plugins.repository.AmetysRepositoryException;
022import org.ametys.plugins.repository.ModifiableTraversableAmetysObject;
023import org.ametys.plugins.repository.RepositoryConstants;
024import org.ametys.runtime.i18n.I18nizableText;
025
026import com.opensymphony.workflow.WorkflowException;
027
028/**
029 * OSWorkflow function to create an extraction's description.
030 */
031public class CreateExtractionDescriptionFunction extends CreateContentFunction
032{
033    /** The root node name of the plugin */
034    private static final String __PLUGIN_NODE_NAME = "extraction";
035    
036    /** The root node name of the contents */
037    private static final String __CONTENTS_NODE_NAME = RepositoryConstants.NAMESPACE_PREFIX + ":contents";
038    
039    @Override
040    protected ModifiableTraversableAmetysObject _getContentRoot(Map transientVars) throws WorkflowException
041    {
042        try
043        {
044            ModifiableTraversableAmetysObject pluginsNode = _resolver.resolveByPath("/ametys:plugins");
045            
046            ModifiableTraversableAmetysObject pluginNode = _getOrCreateNode(pluginsNode, __PLUGIN_NODE_NAME, "ametys:unstructured");
047            
048            return _getOrCreateNode(pluginNode, __CONTENTS_NODE_NAME, "ametys:collection");
049        }
050        catch (AmetysRepositoryException e)
051        {
052            throw new AmetysRepositoryException("Unable to get the extraction root node", e);
053        }
054    }
055    
056    private ModifiableTraversableAmetysObject _getOrCreateNode(ModifiableTraversableAmetysObject parentNode, String nodeName, String nodeType) throws AmetysRepositoryException
057    {
058        ModifiableTraversableAmetysObject definitionsNode;
059        if (parentNode.hasChild(nodeName))
060        {
061            definitionsNode = parentNode.getChild(nodeName);
062        }
063        else
064        {
065            definitionsNode = parentNode.createChild(nodeName, nodeType);
066            parentNode.saveChanges();
067        }
068        return definitionsNode;
069    }
070    
071    @Override
072    public I18nizableText getLabel()
073    {
074        return new I18nizableText("plugin.extraction", "PLUGINS_EXTRACTION_CREATE_EXTRACTION_DESCRIPTION_FUNCTION_LABEL");
075    }
076}