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

de.fraunhofer.iosb.ilt.frostserver.persistence.pgjooq.relations.RelationManyToManyOrdered 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 de.fraunhofer.iosb.ilt.frostserver.model.core.Entity;
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.query.OrderBy;
import org.jooq.DSLContext;
import org.jooq.Field;
import org.jooq.Record1;
import org.jooq.SelectConditionStep;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * A relation from a source table to a target table using a link table ordered
 * by a long value.
 *
 * @author hylke
 * @param  The source table.
 * @param  The link table linking source and target entities.
 * @param  The target table.
 */
public class RelationManyToManyOrdered, L extends StaTable, T extends StaMainTable> extends RelationManyToMany {

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

    /**
     * The field used for the ordering.
     */
    private FieldAccessor orderFieldAcc;
    private boolean alwaysDistinct = false;
    private boolean orderOnSource = true;

    public RelationManyToManyOrdered(NavigationPropertyMain navProp, S source, L linkTable, T target, boolean orderOnSource) {
        super(navProp, source, linkTable, target);
        this.orderOnSource = orderOnSource;
    }

    public RelationManyToManyOrdered setOrderFieldAcc(FieldAccessor orderFieldAcc) {
        this.orderFieldAcc = orderFieldAcc;
        return this;
    }

    public RelationManyToManyOrdered setAlwaysDistinct(boolean alwaysDistinct) {
        this.alwaysDistinct = alwaysDistinct;
        return this;
    }

    @Override
    public TableRef join(S source, QueryState queryState, TableRef sourceRef) {
        T targetAliased = (T) getTarget().asSecure(queryState.getNextAlias(), queryState.getPersistenceManager());
        L linkTableAliased = (L) getLinkTable().as(queryState.getNextAlias());
        Field sourceField = getSourceFieldAcc().getField(source);
        Field sourceLinkField = getSourceLinkFieldAcc().getField(linkTableAliased);
        Field targetLinkField = getTargetLinkFieldAcc().getField(linkTableAliased);
        Field targetField = getTargetFieldAcc().getField(targetAliased);
        queryState.setSqlFrom(queryState.getSqlFrom().leftJoin(linkTableAliased).on(((Field) sourceLinkField).eq(sourceField)));
        queryState.setSqlFrom(queryState.getSqlFrom().leftJoin(targetAliased).on(((Field) targetField).eq(targetLinkField)));
        if (alwaysDistinct || queryState.isFilter()) {
            queryState.setDistinctRequired(true);
        } else {
            Field orderField = orderFieldAcc.getField(linkTableAliased);
            queryState.getSqlSortFields().add(orderField, OrderBy.OrderType.ASCENDING);
        }
        return QueryBuilder.createJoinedRef(sourceRef, getNavProp(), targetAliased);
    }

    @Override
    protected void link(JooqPersistenceManager pm, Object sourceId, Object targetId) {
        final DSLContext dslContext = pm.getDslContext();
        final L linkTable = getLinkTable();
        final Field sourceLinkField = getSourceLinkFieldAcc().getField(linkTable);
        final Field targetLinkField = getTargetLinkFieldAcc().getField(linkTable);
        final Field orderField = orderFieldAcc.getField(linkTable);
        final SelectConditionStep> orderValue;
        if (orderOnSource) {
            orderValue = dslContext.selectCount()
                    .from(linkTable)
                    .where(sourceLinkField.equal(sourceId));
        } else {
            orderValue = dslContext.selectCount()
                    .from(linkTable)
                    .where(sourceLinkField.equal(targetId));
        }
        dslContext.insertInto(linkTable)
                .set(sourceLinkField, sourceId)
                .set(targetLinkField, targetId)
                .set(orderField, orderValue)
                .execute();
    }

    @Override
    public void unLink(JooqPersistenceManager pm, Entity source, Entity target, NavigationPropertyMain navProp) {
        final var linkTable = getLinkTable();
        final var sourceLinkField = getSourceLinkFieldAcc().getField(linkTable);
        final var targetLinkField = getTargetLinkFieldAcc().getField(linkTable);
        Field orderField = orderFieldAcc.getField(linkTable);

        final var sourceCondition = sourceLinkField.eq(source.getPrimaryKeyValues().get(0));
        final var targetCondition = targetLinkField.eq(target.getPrimaryKeyValues().get(0));
        final DSLContext dslContext = pm.getDslContext();
        int deletedOrderIdx = dslContext.deleteFrom(linkTable)
                .where(sourceCondition.and(targetCondition))
                .limit(1)
                .returning(orderField)
                .execute();
        int updated;
        if (orderOnSource) {
            updated = dslContext.update(linkTable)
                    .set(orderField, orderField.sub(1))
                    .where(sourceCondition)
                    .and(orderField.gt(deletedOrderIdx))
                    .execute();
        } else {
            updated = dslContext.update(linkTable)
                    .set(orderField, orderField.sub(1))
                    .where(targetCondition)
                    .and(orderField.gt(deletedOrderIdx))
                    .execute();
        }
        LOGGER.trace("Updated {} order entries", updated);
    }

}