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

sirius.biz.tenants.TenantAware Maven / Gradle / Ivy

There is a newer version: 9.6
Show newest version
/*
 * Made with all the love in the world
 * by scireum in Remshalden, Germany
 *
 * Copyright by scireum GmbH
 * http://www.scireum.de - [email protected]
 */

package sirius.biz.tenants;

import sirius.biz.model.BizEntity;
import sirius.db.mixing.Column;
import sirius.db.mixing.EntityRef;
import sirius.kernel.health.Exceptions;

import java.util.Objects;
import java.util.function.Supplier;

/**
 * Base class which marks subclasses as aware of their tenant they belong to.
 */
public abstract class TenantAware extends BizEntity {

    /**
     * Contains the tenant the entity belongs to.
     */
    public static final Column TENANT = Column.named("tenant");
    private final EntityRef tenant = EntityRef.on(Tenant.class, EntityRef.OnDelete.CASCADE);

    public EntityRef getTenant() {
        return tenant;
    }

    /**
     * Asserts that the given object has the same tenant as this object.
     *
     * @param fieldLabel the field in which the referenced object would be stored - used to generate an appropriate
     *                   error message
     * @param other      the object to check
     */
    public void assertSameTenant(Supplier fieldLabel, TenantAware other) {
        if (other != null && (!Objects.equals(other.getTenant().getId(), getTenant().getId()))) {
            throw Exceptions.createHandled()
                            .withNLSKey("TenantAware.invalidTenant")
                            .set("field", fieldLabel.get())
                            .handle();
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy