Reservation.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.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.ForeignKey;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
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.Objects;

/**
 * A Reservation.
 *
 * @author Heiko Scherrer
 */
@Audited
@AuditOverride(forClass = ApplicationEntity.class)
@AuditOverride(forClass = BaseEntity.class)
@Entity
@Table(name = "WMS_INV_RESERVATION")
public class Reservation extends AbstractReservation implements Serializable {

    /** The {@code PackagingUnit} instance, the {@code Reservation} belongs to. */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "C_PACKAGING_UNIT_PK", referencedColumnName = "C_PK", nullable = false, foreignKey = @ForeignKey(name = "FK_PURESERVATION_PK"))
    private PackagingUnit packagingUnit;

    /** The reserved quantity of this PackagingUnit. */
    @CompositeType(UnitUserType.class)
    @AttributeOverride(name = "unitType", column = @Column(name = "C_QTY_TYPE_RESERVED"))
    @AttributeOverride(name = "magnitude", column = @Column(name = "C_QTY_RESERVED", length = UnitConstants.QUANTITY_LENGTH))
    private Measurable<?,?,?> quantityReserved;

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

    public Reservation(Measurable quantityReserved, String reservedBy) {
        super(reservedBy);
        this.quantityReserved = quantityReserved;
    }

    public PackagingUnit getPackagingUnit() {
        return packagingUnit;
    }

    public void setPackagingUnit(PackagingUnit packagingUnit) {
        this.packagingUnit = packagingUnit;
    }

    public Measurable getQuantityReserved() {
        return quantityReserved;
    }

    public void setQuantityReserved(Measurable quantityReserved) {
        this.quantityReserved = quantityReserved;
    }

    @Override
    public String toString() {
        return "Reservation{quantityReserved=" + quantityReserved+"}";
    }

    /**
     * {@inheritDoc}
     *
     * All fields.
     */
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Reservation)) return false;
        if (!super.equals(o)) return false;
        Reservation that = (Reservation) o;
        return Objects.equals(packagingUnit, that.packagingUnit) && Objects.equals(quantityReserved, that.quantityReserved);
    }

    /**
     * {@inheritDoc}
     *
     * All fields.
     */
    @Override
    public int hashCode() {
        return Objects.hash(super.hashCode(), packagingUnit, quantityReserved);
    }
}