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

io.vertx.up.uca.jooq.AbstractAction Maven / Gradle / Ivy

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

import io.github.jklingsporn.vertx.jooq.classic.VertxDAO;
import io.vertx.core.json.JsonObject;
import io.vertx.tp.plugin.jooq.JooqDsl;
import io.vertx.tp.plugin.jooq.condition.JooqCond;
import io.vertx.up.log.Annal;
import io.vertx.up.log.Debugger;
import io.vertx.up.util.Ut;
import org.jooq.*;
import org.jooq.impl.DSL;

import java.util.*;

/**
 * @author Lang
 * Two mode:
 *
 * 1) Dim1: Sync / Async
 * 2) Dim2: Pojo / Bind-Pojo / Non-Pojo
 * 3) Dim3: T, List, JsonObject, JsonArray
 *
 * This class is for basic operation abstraction such as:
 *
 * INSERT, UPDATE, DELETE, SELECT etc.
 *
 * The scope is default ( Package Only )
 */
@SuppressWarnings("all")
abstract class AbstractAction {
    protected transient final JooqDsl dsl;
    protected transient final JqAnalyzer analyzer;

    protected AbstractAction(final JqAnalyzer analyzer) {
        this.analyzer = analyzer;
        this.dsl = analyzer.dsl();
    }

    protected VertxDAO dao() {
        return this.dsl.dao();
    }

    protected DSLContext context() {
        return this.dsl.context();
    }

    protected void logging(final String pattern, final Object... args) {
        final Annal logger = Annal.get(getClass());
        if (Debugger.onJooqCondition()) {
            logger.info(pattern, args);
        }
    }

    // -------------------------------- Input Method
    /*
     * Parameter processing here
     * Here are two situations:
     * 1): No collection -> List -> [Element]
     * 2): List type -> Direct for list -> [Element, ...]
     */
    protected Collection parameters(final Object value) {
        if (value instanceof Collection) {
            return (Collection) value;
        } else {
            /*
             * List as the first collection type selected
             */
            return Arrays.asList(value);
        }
    }

    protected Condition condition(final JsonObject criteria) {
        return Ut.isNil(criteria) ? null : JooqCond.transform(criteria, this.analyzer::column);
    }

    protected Condition conditionAnd(final JsonObject criteria) {
        return JooqCond.transform(criteria, Operator.AND, this.analyzer::column);
    }

    // ---------------------------------- Sync Operation
    protected  Record newRecord(T pojo) {
        Objects.requireNonNull(pojo);
        final Record record = this.context().newRecord(this.analyzer.table(), pojo);
        int size = record.size();
        for (int i = 0; i < size; i++)
            if (record.get(i) == null) {
                @SuppressWarnings("unchecked")
                Field field = (Field) record.field(i);
                if (!field.getDataType().nullable() && !field.getDataType().identity())
                    record.set(field, DSL.defaultValue());
            }
        return record;
    }

    protected  UpdateConditionStep editRecord(T pojo) {
        Objects.requireNonNull(pojo);
        Record record = this.context().newRecord(this.analyzer.table(), pojo);
        Condition where = DSL.trueCondition();
        UniqueKey pk = this.analyzer.table().getPrimaryKey();
        for (TableField tableField : pk.getFields()) {
            //exclude primary keys from update
            record.changed(tableField, false);
            where = where.and(((TableField) tableField).eq(record.get(tableField)));
        }
        Map valuesToUpdate =
            Arrays.stream(record.fields())
                .collect(HashMap::new, (m, f) -> m.put(f.getName(), f.getValue(record)), HashMap::putAll);
        return this.context().update(this.analyzer.table()).set(valuesToUpdate).where(where);
    }
    // ---------------------------------- Output Method
}