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

de.fraunhofer.iosb.ilt.frostserver.persistence.pgjooq.relations.RelationManyToMany Maven / Gradle / Ivy

/*
 * Copyright (C) 2020 Fraunhofer Institut IOSB, Fraunhoferstr. 1, D 76131
 * Karlsruhe, Germany.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see .
 */
package de.fraunhofer.iosb.ilt.frostserver.persistence.pgjooq.relations;

import de.fraunhofer.iosb.ilt.frostserver.model.EntityType;
import de.fraunhofer.iosb.ilt.frostserver.persistence.pgjooq.PostgresPersistenceManager;
import de.fraunhofer.iosb.ilt.frostserver.persistence.pgjooq.QueryBuilder;
import de.fraunhofer.iosb.ilt.frostserver.persistence.pgjooq.tables.StaMainTable;
import de.fraunhofer.iosb.ilt.frostserver.persistence.pgjooq.tables.StaTable;
import de.fraunhofer.iosb.ilt.frostserver.persistence.pgjooq.utils.QueryState;
import de.fraunhofer.iosb.ilt.frostserver.persistence.pgjooq.utils.TableRef;
import de.fraunhofer.iosb.ilt.frostserver.property.NavigationPropertyMain;
import org.jooq.Field;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * A relation from a source table to a target table.
 *
 * @author hylke
 * @param  The source table.
 * @param  The link table linking source and target entities.
 * @param  The target table.
 */
public class RelationManyToMany, L extends StaTable, T extends StaMainTable> implements Relation {

    private static final Logger LOGGER = LoggerFactory.getLogger(RelationManyToMany.class.getName());

    /**
     * The target entity type of the relation.
     */
    private final EntityType targetType;
    /**
     * The name of the relation. For official relations, this is the (singular)
     * entity type name.
     */
    private final String name;

    /**
     * The field on the source side that defines the relation.
     */
    private FieldAccessor sourceFieldAcc;

    private final L linkTable;
    private FieldAccessor sourceLinkFieldAcc;
    private FieldAccessor targetLinkFieldAcc;

    /**
     * The table that is the target side of the relation.
     */
    private final T target;

    /**
     * The field on the target side that defines the relation.
     */
    private FieldAccessor targetFieldAcc;

    public RelationManyToMany(NavigationPropertyMain navProp, S source, L linkTable, T target) {
        if (source == null) {
            // Source is only used for finding the generics...
            LOGGER.error("NULL source");
        }
        this.linkTable = linkTable;
        this.target = target;
        this.targetType = navProp.getEntityType();
        this.name = navProp.getName();
    }

    public FieldAccessor getSourceFieldAcc() {
        return sourceFieldAcc;
    }

    public RelationManyToMany setSourceFieldAcc(FieldAccessor sourceFieldAcc) {
        this.sourceFieldAcc = sourceFieldAcc;
        return this;
    }

    public L getLinkTable() {
        return linkTable;
    }

    public FieldAccessor getSourceLinkFieldAcc() {
        return sourceLinkFieldAcc;
    }

    public RelationManyToMany setSourceLinkFieldAcc(FieldAccessor sourceLinkFieldAcc) {
        this.sourceLinkFieldAcc = sourceLinkFieldAcc;
        return this;
    }

    public FieldAccessor getTargetLinkFieldAcc() {
        return targetLinkFieldAcc;
    }

    public RelationManyToMany setTargetLinkFieldAcc(FieldAccessor targetLinkFieldAcc) {
        this.targetLinkFieldAcc = targetLinkFieldAcc;
        return this;
    }

    public FieldAccessor getTargetFieldAcc() {
        return targetFieldAcc;
    }

    public RelationManyToMany setTargetFieldAcc(FieldAccessor targetFieldAcc) {
        this.targetFieldAcc = targetFieldAcc;
        return this;
    }

    public T getTarget() {
        return target;
    }

    public EntityType getTargetType() {
        return targetType;
    }

    @Override
    public TableRef join(S source, QueryState queryState, TableRef sourceRef) {
        L linkTableAliased = (L) linkTable.as(queryState.getNextAlias());
        T targetAliased = (T) target.as(queryState.getNextAlias());
        Field sourceField = sourceFieldAcc.getField(source);
        Field sourceLinkField = sourceLinkFieldAcc.getField(linkTableAliased);
        Field targetLinkField = targetLinkFieldAcc.getField(linkTableAliased);
        Field targetField = targetFieldAcc.getField(targetAliased);
        queryState.setSqlFrom(queryState.getSqlFrom().innerJoin(linkTableAliased).on(sourceLinkField.eq(sourceField)));
        queryState.setSqlFrom(queryState.getSqlFrom().innerJoin(targetAliased).on(targetField.eq(targetLinkField)));
        queryState.setDistinctRequired(true);
        return QueryBuilder.createJoinedRef(sourceRef, targetType, targetAliased);
    }

    @Override
    public void link(PostgresPersistenceManager pm, Object sourceId, Object targetId) {
        pm.getDslContext().insertInto(linkTable)
                .set(sourceLinkFieldAcc.getField(linkTable), sourceId)
                .set(targetLinkFieldAcc.getField(linkTable), targetId)
                .execute();
    }

    /**
     * The name of the relation. For official relations, this is the (singular)
     * entity type name.
     *
     * @return the name
     */
    @Override
    public String getName() {
        return name;
    }

}