UomRelation.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.ForeignKey;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.MapKeyColumn;
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.openwms.core.units.UnitConstants;
import org.openwms.core.units.api.Measurable;
import org.openwms.core.units.persistence.UnitUserType;

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

import static jakarta.persistence.FetchType.EAGER;

/**
 * A UomRelation.
 *
 * @author Heiko Scherrer
 */
@Audited
@AuditOverride(forClass = ApplicationEntity.class)
@AuditOverride(forClass = BaseEntity.class)
@Entity
@Table(name = "WMS_INV_PRODUCT_UNIT", uniqueConstraints = @UniqueConstraint(
        name = "UC_INV_PUNIT_PPK_LABEL",
        columnNames = {"C_PRODUCT_PK", "C_LABEL"}
))
public class UomRelation extends ApplicationEntity implements Serializable {

    /** The Product this relation belongs to. */
    @NotNull
    @ManyToOne(fetch = EAGER) //Define FetchType.LAZY for @ManyToOne association
    @JoinColumn(name = "C_PRODUCT_PK", referencedColumnName = "C_PK", foreignKey = @ForeignKey(name = "FK_PUNIT_PRODUCT"), nullable = false)
    private Product product;

    /** An identifying label of the UomRelation. */
    @NotBlank
    @Column(name = "C_LABEL", nullable = false)
    private String label;

    /** An internal code of UomRelation */
    @Column(name = "C_CODE")
    private String code;

    /** The actual unit definition of this relation to the Product. */
    @NotNull
    @CompositeType(UnitUserType.class)
    @AttributeOverride(name = "magnitude", column = @Column(name = "C_QTY", length = UnitConstants.QUANTITY_LENGTH, nullable = false))
    @AttributeOverride(name = "unitType", column = @Column(name = "C_QTY_TYPE", nullable = false))
    private Measurable<?,?,?> unit;

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

    /** The defined dimension of the {@code UomRelation}. */
    @Embedded
    private Dimension dimension;

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

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

    public UomRelation(String label, @NotNull Measurable unit) {
        this.label = label;
        this.unit = unit;
    }

    public UomRelation(Measurable unit) {
        this.unit = unit;
    }

    public Product getProduct() {
        return product;
    }

    public void setProduct(Product product) {
        this.product = product;
    }

    public String getLabel() {
        return label;
    }

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

    public String getCode() {
        return code;
    }

    public void setCode(String internalCode) {
        this.code = internalCode;
    }

    public Measurable getUnit() {
        return unit;
    }

    public void setUnit(Measurable unit) {
        this.unit = unit;
    }

    public String getDescription() {
        return description;
    }

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

    public Dimension getDimension() {
        return dimension;
    }

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

    public Map<String, String> getDetails() {
        return details == null ? new HashMap<>(0) : details;
    }

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

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

    @Override
    public String toString() {
        return unit.toString();
    }

    @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;
        UomRelation that = (UomRelation) o;
        return Objects.equals(product, that.product) &&
                Objects.equals(unit, that.unit);
    }

    @Override
    public int hashCode() {
        return Objects.hash(super.hashCode(), product, unit);
    }
}