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.shipping.impl;

import jakarta.persistence.CollectionTable;
import jakarta.persistence.Column;
import jakarta.persistence.ElementCollection;
import jakarta.persistence.Entity;
import jakarta.persistence.ForeignKey;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.MapKeyColumn;
import jakarta.persistence.Table;
import jakarta.persistence.UniqueConstraint;
import jakarta.validation.constraints.NotBlank;
import org.ameba.annotation.Default;
import org.ameba.integration.jpa.ApplicationEntity;

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

import static jakarta.persistence.FetchType.EAGER;

/**
 * A Product is the representation of a {@code Product} like it is seen in the Shipping Service.
 *
 * @author Heiko Scherrer
 */
@Entity
@Table(name = "WMS_SHP_PRODUCT",
        uniqueConstraints = {
                @UniqueConstraint(name = "UC_SHP_PRODUCT_SKU", columnNames = {"C_SKU"}),
                @UniqueConstraint(name = "UC_SHP_PRODUCT_FOREIGN_PID", columnNames = {"C_FOREIGN_PID"})
        }
)
public class Product extends ApplicationEntity implements Comparable<Product>, Serializable {

    /** The foreign persistent key of the {@code Product}. */
    @Column(name = "C_FOREIGN_PID", nullable = false)
    @NotBlank
    private String foreignPKey;

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

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

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

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

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

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

    /**
     * Constructor with the mandatory attributes. {@link Default @Default} for the mapper.
     *
     * @param sku
     * @param foreignPKey
     */
    @Default
    public Product(String sku, String foreignPKey) {
        this.sku = sku;
        this.foreignPKey = foreignPKey;
    }

    @Override
    public void setPersistentKey(String pKey) {
        super.setPersistentKey(pKey);
    }

    public String getForeignPKey() {
        return foreignPKey;
    }

    public void setForeignPKey(String foreignPKey) {
        this.foreignPKey = foreignPKey;
    }

    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 getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Map<String, String> getDetails() {
        return details;
    }

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

    public String getStockZone() {
        return stockZone;
    }

    public void setStockZone(String stockZone) {
        this.stockZone = stockZone;
    }

    /**
     * {@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}
     *
     * All fields.
     */
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Product)) return false;
        if (!super.equals(o)) return false;
        var product = (Product) o;
        return Objects.equals(foreignPKey, product.foreignPKey) && Objects.equals(sku, product.sku) && Objects.equals(label, product.label) && Objects.equals(description, product.description) && Objects.equals(stockZone, product.stockZone) && Objects.equals(details, product.details);
    }

    /**
     * {@inheritDoc}
     *
     * All fields.
     */
    @Override
    public int hashCode() {
        return Objects.hash(super.hashCode(), foreignPKey, sku, label, description, stockZone, details);
    }
}