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

There is a newer version: 2.5.0
Show newest version
/*
 * Copyright (C) 2024 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 static de.fraunhofer.iosb.ilt.frostserver.util.Constants.NOT_IMPLEMENTED_MULTI_VALUE_PK;

import de.fraunhofer.iosb.ilt.frostserver.model.EntityType;
import de.fraunhofer.iosb.ilt.frostserver.model.core.Entity;
import de.fraunhofer.iosb.ilt.frostserver.model.core.EntitySet;
import de.fraunhofer.iosb.ilt.frostserver.model.core.PkValue;
import de.fraunhofer.iosb.ilt.frostserver.persistence.pgjooq.JooqPersistenceManager;
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 de.fraunhofer.iosb.ilt.frostserver.util.exception.IncompleteEntityException;
import de.fraunhofer.iosb.ilt.frostserver.util.exception.NoSuchEntityException;
import org.apache.commons.lang3.NotImplementedException;
import org.jooq.Condition;
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 navigation property this relation represents.
     */
    private final NavigationPropertyMain navProp;

    /**
     * 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;

    /**
     * If the relation is symmetrical, on insert both [A,B] and [B,A] tuples are
     * inserted, and on delete both are deleted.
     */
    private boolean symmetrical;

    public RelationManyToMany(NavigationPropertyMain navProp, S source, L linkTable, T target) {
        this(navProp, source, linkTable, target, false);
    }

    public RelationManyToMany(NavigationPropertyMain navProp, S source, L linkTable, T target, boolean symmetrical) {
        if (source == null) {
            // Source is only used for finding the generics...
            LOGGER.error("NULL source");
        }
        this.navProp = navProp;
        this.linkTable = linkTable;
        this.target = target;
        this.targetType = navProp.getEntityType();
        this.name = navProp.getName();
        this.symmetrical = symmetrical;
        if (source.getPkFields().size() != 1 || target.getPkFields().size() != 1) {
            throw new NotImplementedException(NOT_IMPLEMENTED_MULTI_VALUE_PK);
        }
    }

    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 NavigationPropertyMain getNavProp() {
        return navProp;
    }

    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.asSecure(queryState.getNextAlias(), queryState.getPersistenceManager());
        Field sourceField = sourceFieldAcc.getField(source);
        Field sourceLinkField = sourceLinkFieldAcc.getField(linkTableAliased);
        Field targetLinkField = targetLinkFieldAcc.getField(linkTableAliased);
        Field targetField = targetFieldAcc.getField(targetAliased);
        queryState.setSqlFrom(queryState.getSqlFrom().leftJoin(linkTableAliased).on(sourceLinkField.eq(sourceField)));
        queryState.setSqlFrom(queryState.getSqlFrom().leftJoin(targetAliased).on(targetField.eq(targetLinkField)));
        queryState.setDistinctRequired(true);
        return QueryBuilder.createJoinedRef(sourceRef, navProp, targetAliased);
    }

    @Override
    public void semiJoinTo(S joinSource, StaMainTable joinTarget, QueryState queryState) {
        if (joinTarget.getEntityType() != target.getEntityType()) {
            throw new IllegalArgumentException("SemiJoin target entity type incorrect. Expected " + target.getEntityType() + ", got " + joinTarget.getEntityType());
        }
        L linkTableAliased = (L) linkTable.as(queryState.getNextAlias());
        Field sourceField = sourceFieldAcc.getField(joinSource);
        Field sourceLinkField = sourceLinkFieldAcc.getField(linkTableAliased);
        Field targetLinkField = targetLinkFieldAcc.getField(linkTableAliased);
        Field targetField = targetFieldAcc.getField((T) joinTarget);
        queryState.setSqlFrom(queryState.getSqlFrom().leftJoin(linkTableAliased).on(sourceLinkField.eq(sourceField)));
        queryState.setSqlWhere(queryState.getSqlWhere().and(targetField.eq(targetLinkField)));
    }

    @Override
    public void link(JooqPersistenceManager pm, Entity source, EntitySet targets, NavigationPropertyMain navProp) throws NoSuchEntityException, IncompleteEntityException {
        final PkValue primaryKeyValues = source.getPrimaryKeyValues();

        final Object sourceId = primaryKeyValues.get(0);
        int count = pm.getDslContext().deleteFrom(linkTable)
                .where(sourceLinkFieldAcc.getField(linkTable).eq(sourceId))
                .execute();
        LOGGER.debug("Removed {} relations from {}", count, linkTable.getName());
        for (Entity targetEntity : targets) {
            link(pm, sourceId, targetEntity.getPrimaryKeyValues().get(0));
        }

    }

    @Override
    public void link(JooqPersistenceManager pm, Entity source, Entity target, NavigationPropertyMain navProp) throws NoSuchEntityException, IncompleteEntityException {
        link(pm, source.getPrimaryKeyValues().get(0), target.getPrimaryKeyValues().get(0));
    }

    protected void link(JooqPersistenceManager pm, Object sourceId, Object targetId) {
        pm.getDslContext().insertInto(linkTable)
                .set(sourceLinkFieldAcc.getField(linkTable), sourceId)
                .set(targetLinkFieldAcc.getField(linkTable), targetId)
                .onConflictDoNothing()
                .execute();
        if (symmetrical && !sourceId.equals(targetId)) {
            pm.getDslContext().insertInto(linkTable)
                    .set(sourceLinkFieldAcc.getField(linkTable), targetId)
                    .set(targetLinkFieldAcc.getField(linkTable), sourceId)
                    .onConflictDoNothing()
                    .execute();
        }
    }

    @Override
    public void unLink(JooqPersistenceManager pm, Entity source, Entity target, NavigationPropertyMain navProp) {
        final Object sourceId = source.getPrimaryKeyValues().get(0);
        final Object targetId = target.getPrimaryKeyValues().get(0);
        final Condition sourceCondition = sourceLinkFieldAcc.getField(linkTable).eq(sourceId);
        final Condition targetCondition = targetLinkFieldAcc.getField(linkTable).eq(targetId);
        pm.getDslContext().deleteFrom(linkTable)
                .where(sourceCondition.and(targetCondition))
                .limit(1)
                .execute();
        if (symmetrical) {
            final Condition sourceConditionInv = sourceLinkFieldAcc.getField(linkTable).eq(targetId);
            final Condition targetConditionInv = targetLinkFieldAcc.getField(linkTable).eq(sourceId);
            pm.getDslContext().deleteFrom(linkTable)
                    .where(sourceConditionInv.and(targetConditionInv))
                    .limit(1)
                    .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;
    }

}