Product.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.AttributeOverride;
import jakarta.persistence.CollectionTable;
import jakarta.persistence.Column;
import jakarta.persistence.ElementCollection;
import jakarta.persistence.Embedded;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.ForeignKey;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.Lob;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.MapKeyColumn;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
import jakarta.persistence.UniqueConstraint;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import org.ameba.integration.jpa.ApplicationEntity;
import org.ameba.integration.jpa.BaseEntity;
import org.hibernate.annotations.CompositeType;
import org.hibernate.envers.AuditOverride;
import org.hibernate.envers.Audited;
import org.hibernate.envers.NotAudited;
import org.openwms.core.units.UnitConstants;
import org.openwms.core.units.api.Measurable;
import org.openwms.core.units.api.Weight;
import org.openwms.core.units.persistence.UnitUserType;
import org.openwms.wms.inventory.api.AvailabilityState;
import org.openwms.wms.location.Location;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import static jakarta.persistence.CascadeType.ALL;
import static jakarta.persistence.FetchType.EAGER;

/**
 * A Product.
 * 
 * @author Heiko Scherrer
 */
@Audited
@AuditOverride(forClass = ApplicationEntity.class)
@AuditOverride(forClass = BaseEntity.class)
@Entity
@Table(name = "WMS_INV_PRODUCT",
        uniqueConstraints = {
                @UniqueConstraint(name = "UC_INV_PRODUCT_SKU", columnNames = {"C_SKU"})
})
public class Product extends ApplicationEntity implements Comparable<Product>, Serializable {

    /** The product id is part of the unique business key. */
    @NotBlank
    @Column(name = "C_SKU", nullable = false)
    private String sku;

    /** An identifying label of the Product. */
    @Column(name = "C_LABEL")
    private String label;

    /** The name of the {@code Account} the Product belongs to. */
    @Column(name = "C_ACCOUNT")
    private String accountId;

    /** Products may be defined with different base units. */
    @NotNull
    @CompositeType(UnitUserType.class)
    @AttributeOverride(name = "magnitude", column = @Column(name = "C_BASE_UNIT_QTY", nullable = false))
    @AttributeOverride(name = "unitType", column = @Column(name = "C_BASE_UNIT_TYPE", nullable = false))
    private Measurable<?,?,?> baseUnit;

    /** Is it allowed to receive a higher quantity as expected/announced of this Product? */
    @NotNull
    @Column(name = "C_OVERBOOKING_ALLOWED", nullable = false)
    private boolean overbookingAllowed = true;

    /** A short descriptive text. */
    @Column(name = "C_DESCRIPTION")
    private String description;

    /** A longer description of the {@code Product}. */
    @Lob
    @Column(name = "C_DESCRIPTION_TEXT")
    private String descriptionText;

    /** The Product definition can be set to be unavailable for further operations. */
    @Enumerated(EnumType.STRING)
    @Column(name = "C_AV_STATE")
    private AvailabilityState availabilityState = AvailabilityState.AVAILABLE;

    /** Products may be classified, e.g. hazardous. */
    @Column(name = "C_CLASSIFICATION")
    private String classification;

    /** Products may be grouped. */
    @Column(name = "C_GROUP")
    private String group;

    /** Where the Product has to be placed in stock. */
    @Column(name = "C_STOCK_ZONE")
    private String stockZone;

    /** A {@code Product} can be packed and stored in different box sizes. */
    @NotAudited
    @OneToMany(mappedBy = "product", cascade = {ALL}, fetch = EAGER)
    private List<UomRelation> units;

    /** The defined dimension of the {@code Product} in it's {@literal baseUnit}. */
    @Embedded
    private Dimension dimension;

    /** The defined net weight of the {@code Product}. */
    @CompositeType(UnitUserType.class)
    @AttributeOverride(name = "unitType", column = @Column(name = "C_WEIGHT_TYPE"))
    @AttributeOverride(name = "magnitude", column = @Column(name = "C_WEIGHT", length = UnitConstants.WEIGHT_LENGTH))
    private Weight netWeight;

    /** What is typically the preferable {@link Location} where the {@code Product} shall be stored. */
    @ManyToOne
    @JoinColumn(name = "C_PREFERABLE_STORAGE_LOCATION", foreignKey = @ForeignKey(name = "FK_PROD_LOC_PK"))
    private Location preferableStorageLocation;

    /** A list of rules that define what kind of {@code Product} can be stacked on top of this one. */
    @NotAudited
    @OneToMany(mappedBy = "baseProduct", cascade = {ALL})
    private List<ProductStackingRule> stackingRules;

    /** Arbitrary detail information on this {@code Product}, might be populated with ERP information. */
    @NotAudited
    @ElementCollection(fetch = EAGER)
    @CollectionTable(name = "WMS_INV_PRODUCT_DETAIL",
            joinColumns = {
                    @JoinColumn(name = "C_PRODUCT_PK", referencedColumnName = "C_PK")
            },
            foreignKey = @ForeignKey(name = "FK_DETAILS_PRODUCT")
    )
    @MapKeyColumn(name = "C_KEY")
    @Column(name = "C_VALUE")
    private Map<String, String> details = new HashMap<>();

    /*~ -------------- Constructors -------------- */
    /* Dear JPA ... */
    protected Product() { }

    public Product(String sku, Measurable baseUnit) {
        this.sku = sku;
        this.baseUnit = baseUnit;
    }

    /*~ ----------------- Methods ---------------- */

    /**
     * Format and return the {@literal SKU} and {@literal label}.
     *
     * @return As String in the format {@literal sku/--} or {@literal sku/label}
     */
    public String skuAndLabel() {
        return sku + (label == null ? "/--" : "/" + label);
    }

    /**
     * Initialize and return the {@code units}.
     *
     * @return A list of defined UOMs, never {@literal null}
     */
    public List<UomRelation> getUnits() {
        if (units == null) {
            units = new ArrayList<>();
        }
        return units;
    }

    /**
     * Initialize and return the {@code stackingRules}.
     *
     * @return A list of existing stackingRules, never {@literal null}
     */
    public List<ProductStackingRule> getStackingRules() {
        if (stackingRules == null) {
            stackingRules = new ArrayList<>();
        }
        return stackingRules;
    }

    /**
     * Get the number of times this {@code Product} can be stacked on itself.
     *
     * @return The allowed number of times, 0 means not stackable
     */
    public int getOwnStackingHeight() {
        return getStackingRules().stream().filter(r -> r.getAllowedProduct().equals(this)).findFirst()
                .map(ProductStackingRule::getNoProducts).orElse(0);
    }

    /**
     * Assign stackingRules to this Product.
     */
    public boolean isStackingAllowed(Product other, int amountOfUnits) {
        return getStackingRules().stream().filter(r -> r.getAllowedProduct().equals(other))
                .findFirst()
                .filter(productStackingRule -> productStackingRule.getNoProducts() >= amountOfUnits)
                .isPresent();
    }

    /**
     * Get all the details of this {@link Product}.
     *
     * @return As Map
     */
    public Map<String, String> getDetails() {
        return details == null ? new HashMap<>(0) : details;
    }

    /**
     * Add a new detail to the {@link Product}.
     *
     * @param key The unique key of the detail
     * @param value The value as String
     * @return This instance
     */
    public Product addDetail(String key, String value) {
        if (details == null) {
            details = new HashMap<>();
        }
        details.put(key, value);
        return this;
    }

    /**
     * {@inheritDoc}
     *
     * Uses the sku for comparison
     */
    @Override
    public int compareTo(Product o) {
        return null == o ? -1 : this.sku.compareTo(o.sku);
    }

    /**
     * {@inheritDoc}
     *
     * Return the SKU;
     */
    @Override
    public String toString() {
        return sku;
    }

    /**
     * {@inheritDoc}
     *
     * Not: descriptionText, stackingRules, units, details
     */
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Product product)) return false;
        if (!super.equals(o)) return false;
        return Objects.equals(sku, product.sku) && Objects.equals(label, product.label) && Objects.equals(accountId, product.accountId) && Objects.equals(baseUnit, product.baseUnit) && Objects.equals(description, product.description) && availabilityState == product.availabilityState && Objects.equals(classification, product.classification) && Objects.equals(group, product.group) && Objects.equals(stockZone, product.stockZone) && Objects.equals(units, product.units) && Objects.equals(dimension, product.dimension) && Objects.equals(netWeight, product.netWeight) && Objects.equals(preferableStorageLocation, product.preferableStorageLocation);
    }

    /**
     * {@inheritDoc}
     *
     * Not: descriptionText, stackingRules, units, details
     */
    @Override
    public int hashCode() {
        return Objects.hash(super.hashCode(), sku, label, accountId, baseUnit, description, availabilityState, classification, group, stockZone, units, dimension, netWeight, preferableStorageLocation);
    }

    /*~ ---------------- Accessors --------------- */
    public String getSku() {
        return sku;
    }

    public void setSku(String sku) {
        this.sku = sku;
    }

    public String getLabel() {
        return label;
    }

    public void setLabel(String label) {
        this.label = label;
    }

    public String getAccountId() {
        return accountId;
    }

    public void setAccountId(String accountId) {
        this.accountId = accountId;
    }

    public AvailabilityState getAvailabilityState() {
        return availabilityState;
    }

    public void setAvailabilityState(AvailabilityState availabilityState) {
        this.availabilityState = availabilityState;
    }

    public String getClassification() {
        return classification;
    }

    public void setClassification(String classification) {
        this.classification = classification;
    }

    public String getGroup() {
        return group;
    }

    public void setGroup(String group) {
        this.group = group;
    }

    public Measurable getBaseUnit() {
        return baseUnit;
    }

    public void setBaseUnit(Measurable baseUnit) {
        this.baseUnit = baseUnit;
    }

    public boolean isOverbookingAllowed() {
        return overbookingAllowed;
    }

    public void setOverbookingAllowed(boolean overbookingAllowed) {
        this.overbookingAllowed = overbookingAllowed;
    }

    public void setUnits(List<UomRelation> units) {
        this.units = units;
    }

    public Dimension getDimension() {
        return dimension;
    }

    public void setDimension(Dimension dimension) {
        this.dimension = dimension;
    }

    public Weight getNetWeight() {
        return netWeight;
    }

    public void setNetWeight(Weight defaultWeight) {
        this.netWeight = defaultWeight;
    }

    public Location getPreferableStorageLocation() {
        return preferableStorageLocation;
    }

    public void setPreferableStorageLocation(Location preferableStorageLocation) {
        this.preferableStorageLocation = preferableStorageLocation;
    }

    void setStackingRules(List<ProductStackingRule> stackingRules) {
        this.stackingRules = stackingRules;
    }

    public void setDetails(Map<String, String> details) {
        this.details = details;
    }

    /**
     * Get the description.
     *
     * @return the description.
     */
    public String getDescription() {
        return description;
    }

    /**
     * Set the description.
     *
     * @param description The description to set.
     */
    public void setDescription(String description) {
        this.description = description;
    }

    /**
     * Get the long description text.
     *
     * @return The description text
     */
    public String getDescriptionText() {
        return descriptionText;
    }

    /**
     * Set the long description text.
     *
     * @param descriptionText The description text
     */
    public void setDescriptionText(String descriptionText) {
        this.descriptionText = descriptionText;
    }

    /**
     * Get the stockZone.
     *
     * @return the stockZone.
     */
    public String getStockZone() {
        return stockZone;
    }

    /**
     * Set the stockZone.
     *
     * @param stockZone The stockZone to set.
     */
    public void setStockZone(String stockZone) {
        this.stockZone = stockZone;
    }
}