001// --------------------------------------------------------------------------------
002// Copyright 2002-2026 Echo Three, LLC
003//
004// Licensed under the Apache License, Version 2.0 (the "License");
005// you may not use this file except in compliance with the License.
006// You may obtain a copy of the License at
007//
008//     http://www.apache.org/licenses/LICENSE-2.0
009//
010// Unless required by applicable law or agreed to in writing, software
011// distributed under the License is distributed on an "AS IS" BASIS,
012// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013// See the License for the specific language governing permissions and
014// limitations under the License.
015// --------------------------------------------------------------------------------
016
017package com.echothree.model.control.inventory.server.logic;
018
019import com.echothree.model.control.inventory.common.exception.DuplicateInventoryLocationException;
020import com.echothree.model.control.inventory.common.exception.UnknownInventoryLocationException;
021import com.echothree.model.control.inventory.server.control.InventoryLocationControl;
022import com.echothree.model.control.party.common.PartyTypes;
023import com.echothree.model.control.party.server.logic.PartyLogic;
024import com.echothree.model.control.uom.server.logic.UnitOfMeasureTypeLogic;
025import com.echothree.model.data.inventory.server.entity.InventoryCondition;
026import com.echothree.model.data.inventory.server.entity.InventoryLocation;
027import com.echothree.model.data.item.server.entity.Item;
028import com.echothree.model.data.party.server.entity.Party;
029import com.echothree.model.data.uom.server.entity.UnitOfMeasureType;
030import com.echothree.model.data.warehouse.server.entity.Location;
031import com.echothree.util.common.message.ExecutionErrors;
032import com.echothree.util.common.persistence.BasePK;
033import com.echothree.util.server.control.BaseLogic;
034import com.echothree.util.server.message.ExecutionErrorAccumulator;
035import com.echothree.util.server.persistence.EntityPermission;
036import javax.enterprise.context.ApplicationScoped;
037import javax.enterprise.inject.spi.CDI;
038import javax.inject.Inject;
039
040@ApplicationScoped
041public class InventoryLocationLogic
042        extends BaseLogic {
043
044    @Inject
045    InventoryLocationControl inventoryLocationControl;
046
047    @Inject
048    PartyLogic partyLogic;
049
050    @Inject
051    UnitOfMeasureTypeLogic unitOfMeasureTypeLogic;
052
053    protected InventoryLocationLogic() {
054        super();
055    }
056
057    public Party getOwnerParty(final ExecutionErrorAccumulator eea, final String ownerPartyName) {
058        return partyLogic.getPartyByName(eea, ownerPartyName, PartyTypes.COMPANY.name(), PartyTypes.VENDOR.name());
059    }
060
061    public UnitOfMeasureType getUnitOfMeasureType(final ExecutionErrorAccumulator eea, final Item item,
062            final String unitOfMeasureTypeName) {
063        return unitOfMeasureTypeLogic.getUnitOfMeasureTypeByName(eea,
064                item.getLastDetail().getUnitOfMeasureKind(), unitOfMeasureTypeName);
065    }
066
067    public InventoryLocation createInventoryLocation(final ExecutionErrorAccumulator eea, final Location location,
068            final Party ownerParty, final Item item, final UnitOfMeasureType unitOfMeasureType,
069            final InventoryCondition inventoryCondition, final BasePK createdBy) {
070        var inventoryLocation = inventoryLocationControl.getInventoryLocation(location, item);
071
072        if(inventoryLocation == null) {
073            inventoryLocation = inventoryLocationControl.createInventoryLocation(location, ownerParty, item,
074                    unitOfMeasureType, inventoryCondition, createdBy);
075        } else {
076            handleExecutionError(DuplicateInventoryLocationException.class, eea,
077                    ExecutionErrors.DuplicateInventoryLocation.name(), location.getLastDetail().getLocationName(),
078                    item.getLastDetail().getItemName());
079        }
080
081        return inventoryLocation;
082    }
083
084    public InventoryLocation getInventoryLocation(final ExecutionErrorAccumulator eea, final Location location,
085            final Item item, final EntityPermission entityPermission) {
086        var inventoryLocation = entityPermission == EntityPermission.READ_WRITE
087                ? inventoryLocationControl.getInventoryLocationForUpdate(location, item)
088                : inventoryLocationControl.getInventoryLocation(location, item);
089
090        if(inventoryLocation == null) {
091            handleExecutionError(UnknownInventoryLocationException.class, eea,
092                    ExecutionErrors.UnknownInventoryLocation.name(), location.getLastDetail().getLocationName(),
093                    item.getLastDetail().getItemName());
094        }
095
096        return inventoryLocation;
097    }
098
099    public InventoryLocation getInventoryLocation(final ExecutionErrorAccumulator eea, final Location location,
100            final Item item) {
101        return getInventoryLocation(eea, location, item, EntityPermission.READ_ONLY);
102    }
103
104    public InventoryLocation getInventoryLocationForUpdate(final ExecutionErrorAccumulator eea, final Location location,
105            final Item item) {
106        return getInventoryLocation(eea, location, item, EntityPermission.READ_WRITE);
107    }
108
109    public void deleteInventoryLocation(final ExecutionErrorAccumulator eea,
110            final InventoryLocation inventoryLocation, final BasePK deletedBy) {
111        inventoryLocationControl.deleteInventoryLocation(inventoryLocation, deletedBy);
112    }
113
114}