ProductStackingRule.java

/*
 * Copyright 2005-2025 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.openwms.wms.inventory;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.ForeignKey;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import jakarta.persistence.UniqueConstraint;
import org.ameba.integration.jpa.ApplicationEntity;
import org.springframework.util.Assert;

import java.io.Serializable;
import java.util.Objects;

/**
 * A ProductStackingRule is a rule that defines what kind of {@link Product} can be stacked on other {@link Product}s. Additionally
 * a maximum number of units of the stacked {@link Product}s must be defined.
 *
 * @author Heiko Scherrer
 * @GlossaryTerm
 * @see Product
 */
@Entity
@Table(name = "WMS_INV_PRODUCT_STACKING_RULE",
        uniqueConstraints = @UniqueConstraint(name = "UC_PRODSTRULE", columnNames = {"C_BASE_PRODUCT", "C_ALLOWED_PRODUCT"})
)
public class ProductStackingRule extends ApplicationEntity implements Serializable {

    /** To separate fields in toString method. */
    static final String SEPARATOR = "::";

    /** Parent {@link Product} (not-null). */
    @ManyToOne
    @JoinColumn(name = "C_BASE_PRODUCT", nullable = false, foreignKey = @ForeignKey(name = "FK_PSR_BP_PRODUCT"))
    private Product baseProduct;

    /** Number of units the {@code allowedProduct} can be stacked on the owning {@code baseProduct} (not-null). */
    @Column(name = "C_NO_PRODUCTS", nullable = false)
    private int noProducts;

    /** The allowed {@link Product} that may be placed on the owning {@link Product} (not-null). */
    @ManyToOne
    @JoinColumn(name = "C_ALLOWED_PRODUCT", nullable = false, foreignKey = @ForeignKey(name = "FK_PSR_AP_PRODUCT"))
    private Product allowedProduct;

    /*~ ----------------------------- constructors ------------------- */

    /**
     * Dear JPA...
     */
    protected ProductStackingRule() {
    }

    /**
     * Create a new {@code ProductStackingRule}. Define the number of {@code allowedProduct}s of the {@code allowedProduct} type can be
     * stacked on this {@code baseProduct}.
     *
     * @param noProducts The number of {@code allowedProduct} stacks
     * @param baseProduct The {@link Product} that is able to carry all {@code allowedProduct} must not be {@literal null}
     * @param allowedProduct The {@link Product} that can be stacked onto the {@code baseProduct} must not be {@literal null}
     */
    ProductStackingRule(int noProducts, Product baseProduct, Product allowedProduct) {
        Assert.notNull(baseProduct, "baseProduct must not ne null");
        Assert.notNull(allowedProduct, "allowedProduct must not ne null");
        this.noProducts = noProducts;
        this.baseProduct = baseProduct;
        this.allowedProduct = allowedProduct;
    }

    /*~ ----------------------------- methods ------------------- */

    /**
     * Get the baseProduct.
     *
     * @return The baseProduct.
     */
    public Product getBaseProduct() {
        return baseProduct;
    }

    /**
     * Returns the number of {@link Product}s that may be placed on the owning {@link Product}.
     *
     * @return The number of allowed Product
     */
    public int getNoProducts() {
        return this.noProducts;
    }

    /**
     * Returns the allowed {@link Product} that may be placed on the owning {@link Product}.
     *
     * @return The allowed Product
     */
    public Product getAllowedProduct() {
        return this.allowedProduct;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        if (!super.equals(o)) return false;
        ProductStackingRule that = (ProductStackingRule) o;
        return noProducts == that.noProducts && Objects.equals(baseProduct, that.baseProduct) && Objects.equals(allowedProduct, that.allowedProduct);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public int hashCode() {
        return Objects.hash(super.hashCode(), baseProduct, noProducts, allowedProduct);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String toString() {
        return noProducts + SEPARATOR + baseProduct + SEPARATOR + allowedProduct;
    }
}