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.inject.Inject;
038
039@ApplicationScoped
040public class InventoryLocationLogic
041        extends BaseLogic {
042
043    @Inject
044    InventoryLocationControl inventoryLocationControl;
045
046    @Inject
047    PartyLogic partyLogic;
048
049    @Inject
050    UnitOfMeasureTypeLogic unitOfMeasureTypeLogic;
051
052    protected InventoryLocationLogic() {
053        super();
054    }
055
056    public Party getOwnerParty(final ExecutionErrorAccumulator eea, final String ownerPartyName) {
057        return partyLogic.getPartyByName(eea, ownerPartyName, PartyTypes.COMPANY.name(), PartyTypes.VENDOR.name());
058    }
059
060    public UnitOfMeasureType getUnitOfMeasureType(final ExecutionErrorAccumulator eea, final Item item,
061            final String unitOfMeasureTypeName) {
062        return unitOfMeasureTypeLogic.getUnitOfMeasureTypeByName(eea,
063                item.getLastDetail().getUnitOfMeasureKind(), unitOfMeasureTypeName);
064    }
065
066    public InventoryLocation createInventoryLocation(final ExecutionErrorAccumulator eea, final Location location,
067            final Party ownerParty, final Item item, final UnitOfMeasureType unitOfMeasureType,
068            final InventoryCondition inventoryCondition, final BasePK createdBy) {
069        var inventoryLocation = inventoryLocationControl.getInventoryLocation(location, item);
070
071        if(inventoryLocation == null) {
072            inventoryLocation = inventoryLocationControl.createInventoryLocation(location, ownerParty, item,
073                    unitOfMeasureType, inventoryCondition, createdBy);
074        } else {
075            handleExecutionError(DuplicateInventoryLocationException.class, eea,
076                    ExecutionErrors.DuplicateInventoryLocation.name(), location.getLastDetail().getLocationName(),
077                    item.getLastDetail().getItemName());
078        }
079
080        return inventoryLocation;
081    }
082
083    public InventoryLocation getInventoryLocation(final ExecutionErrorAccumulator eea, final Location location,
084            final Item item, final EntityPermission entityPermission) {
085        var inventoryLocation = entityPermission == EntityPermission.READ_WRITE
086                ? inventoryLocationControl.getInventoryLocationForUpdate(location, item)
087                : inventoryLocationControl.getInventoryLocation(location, item);
088
089        if(inventoryLocation == null) {
090            handleExecutionError(UnknownInventoryLocationException.class, eea,
091                    ExecutionErrors.UnknownInventoryLocation.name(), location.getLastDetail().getLocationName(),
092                    item.getLastDetail().getItemName());
093        }
094
095        return inventoryLocation;
096    }
097
098    public InventoryLocation getInventoryLocation(final ExecutionErrorAccumulator eea, final Location location,
099            final Item item) {
100        return getInventoryLocation(eea, location, item, EntityPermission.READ_ONLY);
101    }
102
103    public InventoryLocation getInventoryLocationForUpdate(final ExecutionErrorAccumulator eea, final Location location,
104            final Item item) {
105        return getInventoryLocation(eea, location, item, EntityPermission.READ_WRITE);
106    }
107
108    public void deleteInventoryLocation(final ExecutionErrorAccumulator eea,
109            final InventoryLocation inventoryLocation, final BasePK deletedBy) {
110        inventoryLocationControl.deleteInventoryLocation(inventoryLocation, deletedBy);
111    }
112
113}