All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.qi4j.migration.assembly.MigrationRules Maven / Gradle / Ivy

/*
 * Copyright (c) 2009, Rickard Öberg. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package org.qi4j.migration.assembly;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.qi4j.api.util.ListMap;

/**
 * Set of migration rules.
 */
public class MigrationRules
{
    // to-version -> List of from-versions
    ListMap versionChanges = new ListMap();

    // key=fromversion->toversion value=list of rules for that transition
    ListMap rules = new ListMap();

    public void addRule( T migrationRule )
    {
        versionChanges.add( migrationRule.toVersion(), migrationRule.fromVersion() );
        rules.add( migrationRule.fromVersion() + "->" + migrationRule.toVersion(), migrationRule );
    }

    public Iterable rulesBetweenVersions( String fromVersion, String toVersion )
    {
        try
        {
            String ruleToVersion = findHighestToVersion( toVersion );

            return migrationRules( fromVersion, ruleToVersion );
        }
        catch( IllegalArgumentException e )
        {
            return null;
        }
    }

    private List migrationRules( String fromVersion, String toVersion )
    {
        List list = versionChanges.get( toVersion );

        if( list == null )
        {
            return null; // No possible rules for this transition
        }

        for( String possibleFromVersion : list )
        {
            if( fromVersion.equals( possibleFromVersion ) )
            {
                // We found the end of the version transitions - return rules, but filter on entity type
                return new ArrayList( getRulesForTransition( fromVersion, toVersion ) );
            }
            else
            {
                List migrationRules = migrationRules( fromVersion, possibleFromVersion );
                if( migrationRules == null )
                {
                    continue; // Wrong transition - try another one
                }

                // Add entity-filtered rules from this part of the version transition
                migrationRules.addAll( getRulesForTransition( possibleFromVersion, toVersion ) );
                return migrationRules;
            }
        }
        return null;
    }

    /**
     * Find highest version below the given to-version for which there are rules registered.
     *
     * @param toVersion The ending toVersion in the search.
     *
     * @return The highest version below the given version for which there are rules registered.
     */
    private String findHighestToVersion( String toVersion )
    {
        if( versionChanges.get( toVersion ) == null )
        {
            List toVersions = new ArrayList( versionChanges.keySet() );
            Collections.sort( toVersions, Collections.reverseOrder() );
            for( String version : toVersions )
            {
                if( version.compareTo( toVersion ) <= 0 )
                {
                    // Found version to change to
                    return version;
                }
            }
            throw new IllegalArgumentException( "No version found in rules that matches the given to-version:" + toVersion );
        }
        else
        {
            return toVersion;
        }
    }

    private List getRulesForTransition( String fromVersion, String toVersion )
    {
        return rules.get( fromVersion + "->" + toVersion );
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy