001/*
002 *  Copyright 2020 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.core.migration.action.data.impl;
017
018import org.apache.avalon.framework.configuration.Configuration;
019import org.apache.avalon.framework.configuration.ConfigurationException;
020import org.apache.commons.lang3.StringUtils;
021
022import org.ametys.core.migration.version.Version;
023
024/**
025 * Data for an Script upgrade
026 */
027public class ScriptActionData extends AbstractActionData
028{
029    private String _file;
030    
031    private String _plugin;
032    
033    private String _script;
034    
035    /**
036     * Create the Upgrade based on the upgrade xml line
037     * Must contains id and type, plus a script or the file where to get the script
038     * May contain restartAfter, component and/or file
039     * @param id id of the action
040     * @param version version concerned by this upgrade
041     * @param comment The comment about this action
042     * @param from if this actions is the equivalent of multiple actions, this is the version id just before the 1st action impacted by this action
043     * @param type type of action
044     * @param pluginName name of the plugin
045     * @param configuration the extension upgrade line to add
046     * @param restartRequired true if a restart is required after the action
047
048     * @throws ConfigurationException something is missing
049     */
050    public ScriptActionData(String id, Version version, String comment, String from, String type, String pluginName, Configuration configuration, boolean restartRequired) throws ConfigurationException
051    {
052        super(id, version, comment, from, type, pluginName, configuration, restartRequired);
053        _file = configuration.getAttribute("file", null);
054        _plugin = configuration.getAttribute("plugin", null);
055        _script = configuration.getValue(null);
056        if (StringUtils.isAllBlank(_script, _file))
057        {
058            throw new ConfigurationException("The configuration must contain at least a script, or a file to fetch the script.", configuration);
059        }
060    }
061
062    /**
063     * Get the path for the script to run
064     * @return The path for the script to run
065     */
066    public String getFile()
067    {
068        return _file;
069    }
070
071    /**
072     * Get the path for the script to run
073     * @return The path for the script to run
074     */
075    public String getScript()
076    {
077        return _script;
078    }
079    
080    /**
081     * Get the plugin containing the sql script (either the default plugin of the extension, or the one passed in the config)
082     * @return The plugin containing the sql script (either the default plugin of the extension, or the one passed in the config)
083     */
084    public String getPlugin()
085    {
086        return _plugin != null ? _plugin : _pluginName;
087    }
088    
089    @Override
090    public String toString()
091    {
092        String result = super.toString();
093
094        if (StringUtils.isNotBlank(_plugin))
095        {
096            result += " plugin : '" + _plugin + "'";
097        }
098        
099        if (StringUtils.isNotBlank(_file))
100        {
101            result += " file : '" + _file + "'";
102        }
103        
104        return result;
105    }
106}