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