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

org.nuiton.topia.templates.sql.plan.TopiaEntitySqlDeletePlanBuilder Maven / Gradle / Ivy

There is a newer version: 10.0.1
Show newest version
package org.nuiton.topia.templates.sql.plan;

/*-
 * #%L
 * Toolkit :: Templates
 * %%
 * Copyright (C) 2017 - 2024 Ultreia.io
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program.  If not, see
 * .
 * #L%
 */

import org.nuiton.topia.service.sql.model.TopiaEntitySqlDescriptor;
import org.nuiton.topia.service.sql.model.TopiaEntitySqlDescriptors;
import org.nuiton.topia.service.sql.model.TopiaEntitySqlSelector;
import org.nuiton.topia.service.sql.model.TopiaEntitySqlTable;
import org.nuiton.topia.service.sql.plan.delete.TopiaEntitySqlDeletePlan;
import org.nuiton.topia.service.sql.plan.delete.TopiaEntitySqlDeletePlanTask;

import java.util.LinkedHashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.function.Consumer;
import java.util.stream.Collectors;

/**
 * Created on 28/02/2022.
 *
 * @author Tony Chemit - [email protected]
 * @since 1.67
 */
public abstract class TopiaEntitySqlDeletePlanBuilder {

    protected final Set tasks;
    protected final Set associationTasks;
    protected final TopiaEntitySqlDescriptors descriptors;
    protected final List entityNames;

    public TopiaEntitySqlDeletePlanBuilder(TopiaEntitySqlDescriptors descriptors) {
        this.descriptors = Objects.requireNonNull(descriptors);
        this.tasks = new LinkedHashSet<>();
        this.associationTasks = new LinkedHashSet<>();
        this.entityNames = descriptors.stream().map(d -> d.getTable().getEntityName()).collect(Collectors.toList());
    }

    protected abstract void startConsumeTable(TopiaEntitySqlDescriptor descriptor);

    public final TopiaEntitySqlDeletePlan build() {
        onTables(this::startConsumeTable);
        Set result = new LinkedHashSet<>(associationTasks);
        result.addAll(tasks);
        return new TopiaEntitySqlDeletePlan(result);
    }

    public List getEntityNames() {
        return entityNames;
    }

    public void onTables(Consumer consumer) {
        descriptors.forEach(consumer);
    }

    protected void addSimpleTask(String gav, String deleteSql) {
        tasks.add(new TopiaEntitySqlDeletePlanTask(gav, deleteSql));
    }

    protected void addAssociationTask(String gav, String deleteSql) {
        associationTasks.add(new TopiaEntitySqlDeletePlanTask(gav, deleteSql));
    }

    protected void addTableDeleteSql(TopiaEntitySqlTable table, TopiaEntitySqlSelector selector, boolean noPath) {
        addSimpleTask(table.getSchemaAndTableName(), SqlHelper.newDeleteStatementSql(table, noPath, selector));
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy