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.handler;
017
018import java.time.Instant;
019
020import org.apache.avalon.framework.configuration.Configurable;
021import org.apache.avalon.framework.configuration.Configuration;
022import org.apache.avalon.framework.configuration.ConfigurationException;
023import org.apache.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025import org.apache.avalon.framework.service.Serviceable;
026import org.apache.commons.lang3.StringUtils;
027
028import org.ametys.core.migration.MigrationException;
029import org.ametys.core.migration.storage.VersionStorage;
030import org.ametys.core.migration.storage.VersionStorageExtensionPoint;
031import org.ametys.core.migration.version.Version;
032import org.ametys.runtime.plugin.component.AbstractLogEnabled;
033import org.ametys.runtime.plugin.component.PluginAware;
034
035/**
036 * Abstract class for {@link VersionHandler}.
037 */
038public abstract class AbstractVersionHandler extends AbstractLogEnabled implements VersionHandler, Serviceable, Configurable, PluginAware
039{
040    /** Version storage extension point */
041    protected VersionStorageExtensionPoint _versionStorageEP;
042    
043    /** Version storage object */
044    protected VersionStorage _versionStorage;
045
046    /** Version storage id */
047    protected String _versionStorageId;
048
049    /** Version handler id */
050    protected String _id;
051    
052    @Override
053    public void setPluginInfo(String pluginName, String featureName, String id)
054    {
055        _id = id;
056    }
057    
058    @Override
059    public void service(ServiceManager manager) throws ServiceException
060    {
061        _versionStorageEP = (VersionStorageExtensionPoint) manager.lookup(VersionStorageExtensionPoint.ROLE);
062    }
063
064    @Override
065    public void configure(Configuration configuration) throws ConfigurationException
066    {
067        _versionStorageId = configuration.getChild("version-storage").getValue(null);
068        if (StringUtils.isBlank(_versionStorageId))
069        {
070            throw new ConfigurationException("<version-storage> is mandatory and should not be blank", configuration);
071        }
072    }
073    
074    /**
075     * Lazy load of the version storage
076     * @return Lazy loaded {@link VersionStorage}
077     */
078    protected VersionStorage getVersionStorage()
079    {
080        if (_versionStorage == null)
081        {
082            _versionStorage = _versionStorageEP.getExtension(_versionStorageId);
083        }
084        return _versionStorage;
085    }
086    
087    @Override
088    public void addVersion(Version newVersion) throws MigrationException
089    {
090        getLogger().debug("Add version for: {}", newVersion.toString());
091        newVersion.setExecutionInstant(Instant.now());
092        getVersionStorage().addVersion(newVersion);
093        getLogger().debug("End add version for: {}", newVersion.toString());
094    }
095}