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.DuplicatePartyBucketException; 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.InventoryCondition; 024import com.echothree.model.data.inventory.server.entity.PartyBucket; 025import com.echothree.model.data.inventory.server.value.PartyBucketValue; 026import com.echothree.model.data.item.server.entity.Item; 027import com.echothree.model.data.party.server.entity.Party; 028import com.echothree.model.data.uom.server.entity.UnitOfMeasureType; 029import com.echothree.util.common.message.ExecutionErrors; 030import com.echothree.util.common.persistence.BasePK; 031import com.echothree.util.server.control.BaseLogic; 032import com.echothree.util.server.message.ExecutionErrorAccumulator; 033import java.util.List; 034import javax.enterprise.context.ApplicationScoped; 035import javax.inject.Inject; 036 037@ApplicationScoped 038public class PartyBucketLogic 039 extends BaseLogic { 040 041 @Inject 042 BucketControl bucketControl; 043 044 protected PartyBucketLogic() { 045 super(); 046 } 047 048 public PartyBucket createPartyBucket(final ExecutionErrorAccumulator eea, final Party party, final Item item, 049 final UnitOfMeasureType unitOfMeasureType, final InventoryCondition inventoryCondition, 050 final InventoryBucketType inventoryBucketType, final Long quantity, final BasePK createdBy) { 051 var partyBucket = getPartyBucket(party, item, unitOfMeasureType, inventoryCondition, inventoryBucketType); 052 053 if(partyBucket == null) { 054 partyBucket = bucketControl.createPartyBucket(party, item, unitOfMeasureType, inventoryCondition, 055 inventoryBucketType, quantity, createdBy); 056 } else { 057 handleExecutionError(DuplicatePartyBucketException.class, eea, 058 ExecutionErrors.DuplicatePartyBucket.name()); 059 } 060 061 return partyBucket; 062 } 063 064 public PartyBucket getPartyBucket(final Party party, final Item item, 065 final UnitOfMeasureType unitOfMeasureType, final InventoryCondition inventoryCondition, 066 final InventoryBucketType inventoryBucketType) { 067 return bucketControl.getPartyBucket(party, item, unitOfMeasureType, inventoryCondition, inventoryBucketType); 068 } 069 070 public void updatePartyBucketFromValue(final PartyBucketValue partyBucketValue, final BasePK updatedBy) { 071 bucketControl.updatePartyBucketFromValue(partyBucketValue, updatedBy); 072 } 073 074 private void removePartyBuckets(final ExecutionErrorAccumulator eea, final List<PartyBucket> partyBuckets, 075 final BasePK removedBy) { 076 var partyBucketInUse = partyBuckets.stream().anyMatch(partyBucket -> partyBucket.getQuantity() != 0); 077 078 if(partyBucketInUse) { 079 handleExecutionError(PartyBucketInUseException.class, eea, ExecutionErrors.PartyBucketInUse.name()); 080 } 081 082 if(eea == null || !eea.hasExecutionErrors()) { 083 bucketControl.removePartyBuckets(partyBuckets, removedBy); 084 } 085 } 086 087 public void removePartyBucket(final ExecutionErrorAccumulator eea, final PartyBucket partyBucket, 088 final BasePK removedBy) { 089 var partyBucketForUpdate = bucketControl.getPartyBucketForUpdate(partyBucket.getParty(), partyBucket.getItem(), 090 partyBucket.getUnitOfMeasureType(), partyBucket.getInventoryCondition(), partyBucket.getInventoryBucketType()); 091 092 removePartyBuckets(eea, List.of(partyBucketForUpdate), removedBy); 093 } 094 095 public void removePartyBucketsByParty(final ExecutionErrorAccumulator eea, final Party party, 096 final BasePK removedBy) { 097 removePartyBuckets(eea, bucketControl.getPartyBucketsByPartyForUpdate(party), removedBy); 098 } 099 100 public void removePartyBucketsByItem(final ExecutionErrorAccumulator eea, final Item item, 101 final BasePK removedBy) { 102 removePartyBuckets(eea, bucketControl.getPartyBucketsByItemForUpdate(item), removedBy); 103 } 104 105 public void removePartyBucketsByUnitOfMeasureType(final ExecutionErrorAccumulator eea, 106 final UnitOfMeasureType unitOfMeasureType, final BasePK removedBy) { 107 removePartyBuckets(eea, bucketControl.getPartyBucketsByUnitOfMeasureTypeForUpdate(unitOfMeasureType), removedBy); 108 } 109 110 public void removePartyBucketsByInventoryCondition(final ExecutionErrorAccumulator eea, 111 final InventoryCondition inventoryCondition, final BasePK removedBy) { 112 removePartyBuckets(eea, bucketControl.getPartyBucketsByInventoryConditionForUpdate(inventoryCondition), removedBy); 113 } 114 115 public void removePartyBucketsByInventoryBucketType(final ExecutionErrorAccumulator eea, 116 final InventoryBucketType inventoryBucketType, final BasePK removedBy) { 117 removePartyBuckets(eea, bucketControl.getPartyBucketsByInventoryBucketTypeForUpdate(inventoryBucketType), removedBy); 118 } 119 120}