com.power4j.fist.boot.mybaits.migrate.ImportPipelineBuilder Maven / Gradle / Ivy
/*
* Copyright 2024. ChenJun ([email protected] & https://github.com/John-Chan)
*
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.gnu.org/licenses/lgpl.html
*
* 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 com.power4j.fist.boot.mybaits.migrate;
import com.power4j.fist.boot.mybaits.crud.repository.Repository;
import com.power4j.fist.data.migrate.ImportDataHandler;
import java.io.Serializable;
import java.util.function.Function;
/**
* @author CJ ([email protected])
* @since 1.0
*/
public class ImportPipelineBuilder
{
private final Repository repository;
private final Function mapper;
private UniqueResolveEnum uniqueResolve;
private BatchUniqueResolver batchUniqueResolver;
private UniqueResolver uniqueResolver;
private boolean cleanAll;
private long maxImportCount;
public static ImportPipelineBuilder
of(Repository repository,
Function mapper) {
return new ImportPipelineBuilder<>(repository, mapper);
}
public ImportPipelineBuilder(Repository repository, Function mapper) {
this.repository = repository;
this.mapper = mapper;
}
public ImportPipelineBuilder
conflictResolve(UniqueResolveEnum conflictResolve) {
this.uniqueResolve = conflictResolve;
return this;
}
public ImportPipelineBuilder
batchUniqueResolver(BatchUniqueResolver resolver) {
this.uniqueResolver = null;
this.batchUniqueResolver = resolver;
return this;
}
public ImportPipelineBuilder uniqueResolver(UniqueResolver resolver) {
this.batchUniqueResolver = null;
this.uniqueResolver = resolver;
return this;
}
public ImportPipelineBuilder cleanAll(boolean cleanAll) {
this.cleanAll = cleanAll;
return this;
}
public ImportPipelineBuilder
maxImportCount(long maxImportCount) {
this.maxImportCount = maxImportCount;
return this;
}
public ImportDataHandler
build() {
if (!cleanAll && uniqueResolve == null) {
throw new IllegalStateException("must set unique resolve strategy when cleanAll is false");
}
if (uniqueResolver == null && batchUniqueResolver == null) {
throw new IllegalStateException("must set uniqueResolver or batchUniqueResolver");
}
if (batchUniqueResolver != null) {
return new BatchImportPipeline<>(uniqueResolve, batchUniqueResolver, repository, mapper, cleanAll,
maxImportCount);
}
return new ImportPipeline<>(uniqueResolve, uniqueResolver, repository, mapper, cleanAll, maxImportCount);
}
}