BasicProductVO.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.api;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import org.ameba.http.AbstractBase;
import org.openwms.core.units.api.Measurable;
import org.openwms.core.units.api.Weight;
import org.openwms.wms.location.api.LocationVO;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * A BasicProductVO.
 *
 * @author Heiko Scherrer
 */
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public class BasicProductVO extends AbstractBase<BasicProductVO> {

    /** The persistent unique key. */
    @JsonProperty("pKey")
    @NotEmpty(message = "{owms.wms.inv.product.pKey}", groups = ValidationGroups.UpdateProduct.class)
    private String pKey;
    /** The product id is part of the unique business key. */
    @JsonProperty("sku")
    @NotEmpty(message = "{owms.wms.inv.product.sku}", groups = ValidationGroups.CreateProduct.class)
    private String sku;
    /** An identifying label of the Product. */
    @JsonProperty("label")
    private String label;
    /** The name of the {@code Account} the Product belongs to. */
    @JsonProperty("accountId")
    private String accountId;
    /** Products may be defined with different base units. */
    @JsonProperty("baseUnit")
    @NotNull(message = "{owms.wms.inv.product.baseUnit}", groups = ValidationGroups.CreateProduct.class)
    private Measurable baseUnit;
    /** Textual descriptive text. */
    @JsonProperty("description")
    private String description;
    /** The Product definition can be set to be unavailable for further operations. */
    @JsonProperty("availabilityState")
    private AvailabilityState availabilityState;
    /** Products may be classified, ie. hazardous. */
    @JsonProperty("classification")
    private String classification;
    /** Products may be grouped. */
    @JsonProperty("group")
    private String group;
    /** Where the Product has to be placed in stock. */
    @JsonProperty("stockZone")
    private String stockZone;
    /** A {@code Product} can be packed and stored in different box sizes. */
    @JsonManagedReference
    @JsonProperty("units")
    public List<UomRelationVO> units;
    /** The defined dimension of the {@code Product} in in's {@code baseUnit}. */
    @JsonProperty("dimension")
    private DimensionVO dimension;
    /** The defined weight of the {@code Product}. */
    @JsonProperty("netWeight")
    private Weight netWeight;
    /** What is typically the preferable {@code Location} where the {@code Product} shall be stored. */
    @JsonProperty("preferableStorageLocation")
    private LocationVO preferableStorageLocation;
    /** A list of rules that define what kind of {@code Product} can be stacked on top of this one. */
    @JsonManagedReference
    @JsonProperty("stackingRules")
    private List<StackingRuleVO> stackingRules;
    /** Arbitrary detail information on this product, might by populated with ERP information. */
    @JsonProperty("details")
    private Map<String, String> details;

    public String skuAndLabel() {
        return sku + (label == null ? "/--" : "/"+label);
    }

    @Override
    public String toString() {
        return sku;
    }

    @JsonCreator
    public BasicProductVO() {
    }

    public BasicProductVO(String sku) {
        this.sku = sku;
    }

    private BasicProductVO(Builder builder) {
        setpKey(builder.pKey);
        setSku(builder.sku);
        setLabel(builder.label);
        setAccountId(builder.accountId);
        setBaseUnit(builder.baseUnit);
        setDescription(builder.description);
        setAvailabilityState(builder.availabilityState);
        setClassification(builder.classification);
        setGroup(builder.group);
        setStockZone(builder.stockZone);
        setUnits(builder.units);
        setDimension(builder.dimension);
        setPreferableStorageLocation(builder.preferableStorageLocation);
        setStackingRules(builder.stackingRules);
        setDetails(builder.details);
    }

    public static Builder newBuilder() {
        return new Builder();
    }

    public String getpKey() {
        return pKey;
    }

    public void setpKey(String pKey) {
        this.pKey = pKey;
    }

    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 Measurable getBaseUnit() {
        return baseUnit;
    }

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

    public String getDescription() {
        return description;
    }

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

    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 String getStockZone() {
        return stockZone;
    }

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

    public List<UomRelationVO> getUnits() {
        return units;
    }

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

    public DimensionVO getDimension() {
        return dimension;
    }

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

    public Weight getNetWeight() {
        return netWeight;
    }

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

    public LocationVO getPreferableStorageLocation() {
        return preferableStorageLocation;
    }

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

    public List<StackingRuleVO> getStackingRules() {
        return stackingRules;
    }

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

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

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

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

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

    public static final class Builder {
        private String pKey;
        private String sku;
        private String label;
        private String accountId;
        private Measurable baseUnit;
        private String description;
        private AvailabilityState availabilityState;
        private String classification;
        private String group;
        private String stockZone;
        private List<UomRelationVO> units;
        private DimensionVO dimension;
        private LocationVO preferableStorageLocation;
        private List<StackingRuleVO> stackingRules;
        private Map<String, String> details;

        private Builder() {
        }

        public Builder pKey(String val) {
            pKey = val;
            return this;
        }

        public Builder sku(String val) {
            sku = val;
            return this;
        }

        public Builder label(String val) {
            label = val;
            return this;
        }

        public Builder accountId(String val) {
            accountId = val;
            return this;
        }

        public Builder baseUnit(Measurable val) {
            baseUnit = val;
            return this;
        }

        public Builder description(String val) {
            description = val;
            return this;
        }

        public Builder availabilityState(AvailabilityState val) {
            availabilityState = val;
            return this;
        }

        public Builder classification(String val) {
            classification = val;
            return this;
        }

        public Builder group(String val) {
            group = val;
            return this;
        }

        public Builder stockZone(String val) {
            stockZone = val;
            return this;
        }

        public Builder addUnit(UomRelationVO val) {
            if (units == null) {
                units = new ArrayList<>();
            }
            units.add(val);
            return this;
        }

        public Builder dimension(DimensionVO val) {
            dimension = val;
            return this;
        }

        public Builder preferableStorageLocation(LocationVO val) {
            preferableStorageLocation = val;
            return this;
        }

        public Builder stackingRules(List<StackingRuleVO> val) {
            stackingRules = val;
            return this;
        }

        public Builder details(Map<String, String> val) {
            details = val;
            return this;
        }

        public BasicProductVO build() {
            return new BasicProductVO(this);
        }
    }
}