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

io.vertx.up.unity.JooqWriter Maven / Gradle / Ivy

There is a newer version: 0.9.0
Show newest version
package io.vertx.up.unity;

import io.github.jklingsporn.vertx.jooq.future.VertxDAO;
import io.vertx.core.Future;
import io.vertx.core.json.JsonObject;
import org.jooq.Condition;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;
import java.util.function.Function;

/**
 * Jooq Splitted Writter
 * Create
 * Update
 * Delete
 */
@SuppressWarnings("all")
class JooqWriter {

    private transient final VertxDAO vertxDAO;
    private transient JooqReader reader;
    private transient JooqAnalyzer analyzer;

    private JooqWriter(final VertxDAO vertxDAO) {
        this.vertxDAO = vertxDAO;
    }

    static JooqWriter create(final VertxDAO vertxDAO) {
        return new JooqWriter(vertxDAO);
    }

    JooqWriter on(JooqAnalyzer analyzer) {
        this.analyzer = analyzer;
        return this;
    }

    JooqWriter on(JooqReader reader) {
        this.reader = reader;
        return this;
    }

    // ============ INSERT Operation =============

    /* Async insert operation with key returned: INSERT ( AUTO INCREAMENT ) */
     Future insertReturningPrimaryAsync(final T entity,
                                              final Consumer consumer) {
        final CompletableFuture future = this.vertxDAO.insertReturningPrimaryAsync(entity);
        return Async.toFuture(future).compose(id -> {
            if (null != consumer) consumer.accept(id);
            return Future.succeededFuture(entity);
        });
    }

    /* Async insert operation: INSERT */
     Future insertAsync(final T entity) {
        final CompletableFuture future = this.vertxDAO.insertAsync(entity);
        return Async.toFuture(future).compose(nil -> Future.succeededFuture(entity));
    }

     Future> insertAsync(final List entities) {
        final CompletableFuture future = this.vertxDAO.insertAsync(entities);
        return Async.toFuture(future).compose(nil -> Future.succeededFuture(entities));
    }

    /* Sync insert operation: INSERT */
     T insert(final T entity) {
        this.vertxDAO.insert(entity);
        return entity;
    }

     List insert(final List entities) {
        this.vertxDAO.insert(entities);
        return entities;
    }

    // ============ UPDATE Operation =============

    /* Async insert operation: UPDATE */
     Future updateAsync(final T entity) {
        final CompletableFuture future = this.vertxDAO.updateAsync(entity);
        return Async.toFuture(future).compose(nil -> Future.succeededFuture(entity));
    }

     Future> updateAsync(final List entities) {
        final CompletableFuture future = this.vertxDAO.updateAsync(entities);
        return Async.toFuture(future).compose(nil -> Future.succeededFuture(entities));
    }

    /* Sync insert operation: UPDATE */
     T update(final T entity) {
        this.vertxDAO.update(entity);
        return entity;
    }

     List update(final List entities) {
        this.vertxDAO.update(entities);
        return entities;
    }

    // ============ DELETE Operation =============

    /* Async delete operation: DELETE */
     Future deleteAsync(final T entity) {
        final CompletableFuture future = this.vertxDAO.deleteAsync(Arrays.asList(entity));
        return Async.toFuture(future).compose(nil -> Future.succeededFuture(entity));
    }

     Future deleteByIdAsync(final ID id) {
        final CompletableFuture future = this.vertxDAO.deleteByIdAsync(id);
        return Async.toFuture(future).compose(nil -> Future.succeededFuture(Boolean.TRUE));
    }

     Future deleteByIdAsync(final Collection ids) {
        final CompletableFuture future = this.vertxDAO.deleteByIdAsync(ids);
        return Async.toFuture(future).compose(nil -> Future.succeededFuture(Boolean.TRUE));
    }

     Future deleteAsync(final JsonObject filters, final String pojo) {
        final Condition condition = JooqCond.transform(filters, null, this.analyzer::getColumn);
        final CompletableFuture deleted = this.vertxDAO.deleteExecAsync(condition);
        return Async.toFuture(deleted).compose(nil -> Future.succeededFuture(Boolean.TRUE));
    }

    /* Sync delete operation: DELETE */
     T delete(final T entity) {
        this.vertxDAO.delete(entity);
        return entity;
    }

     Boolean deleteById(final ID id) {
        this.vertxDAO.deleteById(id);
        return Boolean.TRUE;
    }

     Boolean deleteById(final Collection ids) {
        this.vertxDAO.deleteById(ids);
        return Boolean.TRUE;
    }

     Boolean delete(final JsonObject filters, final String pojo) {
        final Condition condition = JooqCond.transform(filters, null, this.analyzer::getColumn);
        final List result = this.reader.fetch(condition);
        result.stream().map(item -> this.delete(item));
        return Boolean.TRUE;
    }

    // ============ UPDATE Operation (Save) =============
     Future saveAsync(final Object id, final Function copyFun) {
        return this.reader.findByIdAsync(id).compose(old -> this.updateAsync(copyFun.apply(old)));
    }

     T save(final Object id, final Function copyFun) {
        final T old = this.reader.findById(id);
        return copyFun.apply(old);
    }
    /*
     * Old Code
    private  List extractIds(final JsonArray array) {
        return array.stream()
                .map(item -> (JsonObject) item)
                .map(item -> item.getValue("key"))
                .filter(Objects::nonNull)
                .map(item -> (ID) item)
                .collect(Collectors.toList());
    } */
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy