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

com.kenshoo.pl.data.AbstractRecordCommand Maven / Gradle / Ivy

Go to download

A Java persistence layer based on JOOQ for high performance and business flow support.

There is a newer version: 0.1.121-jooq-3.16.3
Show newest version
package com.kenshoo.pl.data;

import com.kenshoo.jooq.DataTable;
import gnu.trove.map.hash.THashMap;
import org.jooq.Field;
import org.jooq.Record;
import org.jooq.TableField;
import org.jooq.UniqueKey;

import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

public abstract class AbstractRecordCommand {

    private final DataTable table;
    private final Map, Object> values = new THashMap<>();

    protected AbstractRecordCommand(DataTable table) {
        this.table = table;
    }

    public  void set(Field field, T value) {
        values.put(field, value);
    }

    public  T get(Field field) {
        return (T) values.get(field);
    }

    public Stream> getFields() {
        return values.keySet().stream();
    }

    public Stream getValues(Stream> fields) {
        return fields.map(values::get);
    }

    public DataTable getTable() {
        return table;
    }

    public boolean isEmpty() {
        return values.isEmpty();
    }

    protected static TableField getPrimaryKeyField(DataTable table, Class dataType) {
        UniqueKey primaryKey = table.getPrimaryKey();
        if (primaryKey == null) {
            throw new IllegalArgumentException("Table " + table + " doesn't have a primary key");
        }
        List> primaryKeyFields = primaryKey.getFields();
        TableField primaryKeyField = primaryKeyFields.get(0);
        if (primaryKeyFields.size() > 1 || primaryKeyField.getType() != dataType) {
            throw new IllegalArgumentException("Primary key of table " + table.getName() + " is not of type " + dataType.getSimpleName());
        }
        return primaryKeyField;
    }
}