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

step.migration.tasks.MigrateParametersToDynamicValues Maven / Gradle / Ivy

The newest version!
package step.migration.tasks;

import step.core.Version;
import step.core.collections.*;
import step.migration.MigrationContext;
import step.migration.MigrationTask;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Stream;

public class MigrateParametersToDynamicValues extends MigrationTask {


    private final Collection parameters;
    private final Collection parametersversions;

    public MigrateParametersToDynamicValues(CollectionFactory collectionFactory, MigrationContext migrationContext) {
        super(new Version(3,26,0), collectionFactory, migrationContext);
        parameters = collectionFactory.getCollection("parameters", Document.class);
        parametersversions = collectionFactory.getCollection("parametersversions", Document.class);
    }

    @Override
    public void runUpgradeScript() {
        logger.info("Migrating all parameters to support dynamic values...");

        AtomicInteger count = new AtomicInteger();
        try (Stream documentStream = parameters.findLazy(Filters.empty(), null, null, null, 0)) {
            documentStream.forEach(d -> {
                String value = d.getString("value");
                //value might be null for protected parameters and will stay null in that case
                if (value != null) {
                    Document dynamicValue = new Document();
                    dynamicValue.put("dynamic", false);
                    dynamicValue.put("value", value);
                    d.put("value", dynamicValue);
                    parameters.save(d);
                }
            });
        }
        logger.info("Migrated {} parameters to support dynamic values", count.get());

        logger.info("Migrating all parameters versions to support dynamic values...");

        count = new AtomicInteger();
        try (Stream documentStream = parametersversions.findLazy(Filters.empty(), null, null, null, 0)) {
            documentStream.forEach(v -> {
                DocumentObject d = v.getObject("entity");
                String value = d.getString("value");
                //value might be null for protected parameters and will stay null in that case
                if (value != null) {
                    Document dynamicValue = new Document();
                    dynamicValue.put("dynamic", false);
                    dynamicValue.put("value", value);
                    d.put("value", dynamicValue);
                    parametersversions.save(v);
                }
            });
        }
        logger.info("Migrated {} parameters versions to support dynamic values", count.get());
    }

    @Override
    public void runDowngradeScript() {

    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy