PackagingUnitEvent.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.events;

import org.openwms.core.event.RootApplicationEvent;
import org.openwms.core.units.api.Measurable;
import org.openwms.wms.inventory.LoadUnit;
import org.openwms.wms.inventory.PackagingUnit;
import org.openwms.wms.location.Location;
import org.springframework.util.Assert;

import java.util.StringJoiner;

/**
 * A PackagingUnitEvent.
 *
 * @author Heiko Scherrer
 */
public class PackagingUnitEvent extends RootApplicationEvent {

    public boolean movedToLocation() {
        return hasFromLoadUnit() && getSource().hasActualLocation();
    }

    public boolean movedToLoadUnit() {
        return hasFromLocation() && getSource().hasLoadUnit();
    }

    public boolean movedBetweenLocations() {
        return hasFromLocation() && getSource().hasActualLocation() && !getFromLocation().getErpCode().equals(getSource().getActualLocation().getErpCode());
    }

    public boolean movedBetweenLoadUnits() {
        return hasFromLoadUnit() && getSource().hasLoadUnit() && !getFromLoadUnit().equals(getSource().getLoadUnit());
    }

    public enum TYPE {
        CREATED, UPDATED, LOCKED, UNLOCKED, MOVED, DELETED, QUANTITY_CHANGED
    }

    private TYPE type;
    private Location fromLocation;
    private LoadUnit fromLoadUnit;
    private Measurable oldQuantity;

    /**
     * Create a new PackagingUnitEvent.
     *
     * @param source The PackagingUnit, never {@literal null}
     * @param type The type of event
     */
    public PackagingUnitEvent(PackagingUnit source, TYPE type) {
        super(source);
        Assert.notNull(type, "type must not be null");
        this.type = type;
    }

    /**
     * Create a new PackagingUnitEvent.
     *
     * @param source The PackagingUnit, never {@literal null}
     * @param type The type of event
     * @param fromLoadUnit The former LoadUnit the PackagingUnit where placed in
     */
    public PackagingUnitEvent(PackagingUnit source, TYPE type, LoadUnit fromLoadUnit) {
        super(source);
        Assert.notNull(type, "type must not be null");
        Assert.notNull(fromLoadUnit, "fromLoadUnit must not be null");
        this.type = type;
        this.fromLoadUnit = fromLoadUnit;
    }

    /**
     * Create a new PackagingUnitEvent.
     *
     * @param source The PackagingUnit, never {@literal null}
     * @param type The type of event
     * @param fromLocation The former Location the PackagingUnit where placed on
     */
    public PackagingUnitEvent(PackagingUnit source, TYPE type, Location fromLocation) {
        super(source);
        Assert.notNull(type, "type must not be null");
        Assert.notNull(fromLocation, "fromLocation must not be null");
        this.type = type;
        this.fromLocation = fromLocation;
    }

    /**
     * Create a new PackagingUnitEvent.
     *
     * @param source The PackagingUnit, never {@literal null}
     * @param type The type of event
     * @param oldQuantity The former quantity of the PackagingUnit
     */
    public PackagingUnitEvent(PackagingUnit source, TYPE type, Measurable oldQuantity) {
        super(source);
        Assert.notNull(type, "type must not be null");
        Assert.notNull(oldQuantity, "oldQuantity must not be null");
        this.type = type;
        this.oldQuantity = oldQuantity;
    }

    public TYPE getType() {
        return type;
    }

    @Override
    public PackagingUnit getSource() {
        return (PackagingUnit) super.getSource();
    }

    public Location getFromLocation() {
        return fromLocation;
    }

    public boolean hasFromLocation() {
        return this.fromLocation != null;
    }

    public LoadUnit getFromLoadUnit() {
        return fromLoadUnit;
    }

    public boolean hasFromLoadUnit() {
        return this.fromLoadUnit != null;
    }

    public Measurable getOldQuantity() {
        return oldQuantity;
    }

    /**
     * {@inheritDoc}
     *
     * All fields plus super.
     */
    @Override
    public String toString() {
        return new StringJoiner(", ", PackagingUnitEvent.class.getSimpleName() + "[", "]")
                .add("type=" + type)
                .add("fromLocation=" + fromLocation)
                .add("fromLoadUnit=" + fromLoadUnit)
                .add("oldQuantity=" + oldQuantity)
                .add("source=" + source)
                .toString();
    }
}