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.DuplicateInventoryLocationBucketException; 020import com.echothree.model.control.inventory.common.exception.PartyBucketInUseException; 021import com.echothree.model.control.inventory.server.control.BucketControl; 022import com.echothree.model.data.inventory.server.entity.InventoryBucketType; 023import com.echothree.model.data.inventory.server.entity.InventoryLocation; 024import com.echothree.model.data.inventory.server.entity.InventoryLocationBucket; 025import com.echothree.model.data.inventory.server.value.InventoryLocationBucketValue; 026import com.echothree.util.common.message.ExecutionErrors; 027import com.echothree.util.common.persistence.BasePK; 028import com.echothree.util.server.control.BaseLogic; 029import com.echothree.util.server.message.ExecutionErrorAccumulator; 030import java.util.List; 031import javax.enterprise.context.ApplicationScoped; 032import javax.inject.Inject; 033 034@ApplicationScoped 035public class InventoryLocationBucketLogic 036 extends BaseLogic { 037 038 @Inject 039 BucketControl bucketControl; 040 041 protected InventoryLocationBucketLogic() { 042 super(); 043 } 044 045 public InventoryLocationBucket createInventoryLocationBucket(final ExecutionErrorAccumulator eea, 046 final InventoryLocation inventoryLocation, final InventoryBucketType inventoryBucketType, 047 final Long quantity, final BasePK createdBy) { 048 var inventoryLocationBucket = getInventoryLocationBucket(inventoryLocation, inventoryBucketType); 049 050 if(inventoryLocationBucket == null) { 051 inventoryLocationBucket = bucketControl.createInventoryLocationBucket(inventoryLocation, 052 inventoryBucketType, quantity, createdBy); 053 } else { 054 handleExecutionError(DuplicateInventoryLocationBucketException.class, eea, 055 ExecutionErrors.DuplicateInventoryLocationBucket.name()); 056 } 057 058 return inventoryLocationBucket; 059 } 060 061 public InventoryLocationBucket getInventoryLocationBucket(final InventoryLocation inventoryLocation, 062 final InventoryBucketType inventoryBucketType) { 063 return bucketControl.getInventoryLocationBucket(inventoryLocation, inventoryBucketType); 064 } 065 066 public void updateInventoryLocationBucketFromValue(final InventoryLocationBucketValue inventoryLocationBucketValue, 067 final BasePK updatedBy) { 068 bucketControl.updateInventoryLocationBucketFromValue(inventoryLocationBucketValue, updatedBy); 069 } 070 071 private void removeInventoryLocationBuckets(final ExecutionErrorAccumulator eea, 072 final List<InventoryLocationBucket> inventoryLocationBuckets, final BasePK removedBy) { 073 var partyBucketInUse = inventoryLocationBuckets.stream() 074 .anyMatch(inventoryLocationBucket -> inventoryLocationBucket.getQuantity() != 0); 075 076 if(partyBucketInUse) { 077 handleExecutionError(PartyBucketInUseException.class, eea, ExecutionErrors.PartyBucketInUse.name()); 078 } 079 080 if(eea == null || !eea.hasExecutionErrors()) { 081 bucketControl.removeInventoryLocationBuckets(inventoryLocationBuckets, removedBy); 082 } 083 } 084 085 public void removeInventoryLocationBucket(final ExecutionErrorAccumulator eea, 086 final InventoryLocationBucket inventoryLocationBucket, final BasePK removedBy) { 087 var inventoryLocationBucketForUpdate = bucketControl.getInventoryLocationBucketForUpdate( 088 inventoryLocationBucket.getInventoryLocation(), inventoryLocationBucket.getInventoryBucketType()); 089 090 removeInventoryLocationBuckets(eea, List.of(inventoryLocationBucketForUpdate), removedBy); 091 } 092 093 public void removeInventoryLocationBucketsByInventoryLocation(final ExecutionErrorAccumulator eea, 094 final InventoryLocation inventoryLocation, final BasePK removedBy) { 095 removeInventoryLocationBuckets(eea, 096 bucketControl.getInventoryLocationBucketsByInventoryLocationForUpdate(inventoryLocation), removedBy); 097 } 098 099 public void removeInventoryLocationBucketsByInventoryBucketType(final ExecutionErrorAccumulator eea, 100 final InventoryBucketType inventoryBucketType, final BasePK removedBy) { 101 removeInventoryLocationBuckets(eea, 102 bucketControl.getInventoryLocationBucketsByInventoryBucketTypeForUpdate(inventoryBucketType), removedBy); 103 } 104 105}