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.control.user.inventory.common.spec.LotUniversalSpec; 020import com.echothree.model.control.core.common.ComponentVendors; 021import com.echothree.model.control.core.common.EntityTypes; 022import com.echothree.model.control.core.common.exception.InvalidParameterCountException; 023import com.echothree.model.control.core.server.logic.EntityInstanceLogic; 024import com.echothree.model.control.inventory.common.exception.DuplicateLotIdentifierException; 025import com.echothree.model.control.inventory.common.exception.UnknownLotIdentifierException; 026import com.echothree.model.control.inventory.server.control.LotControl; 027import com.echothree.model.control.item.server.logic.ItemLogic; 028import com.echothree.model.control.sequence.common.SequenceTypes; 029import com.echothree.model.control.sequence.server.logic.SequenceGeneratorLogic; 030import com.echothree.model.data.inventory.server.entity.Lot; 031import com.echothree.model.data.item.server.entity.Item; 032import com.echothree.util.common.message.ExecutionErrors; 033import com.echothree.util.common.persistence.BasePK; 034import com.echothree.util.server.control.BaseLogic; 035import com.echothree.util.server.message.ExecutionErrorAccumulator; 036import com.echothree.util.server.persistence.EntityPermission; 037import javax.enterprise.context.ApplicationScoped; 038import javax.enterprise.inject.spi.CDI; 039import javax.inject.Inject; 040 041@ApplicationScoped 042public class LotLogic 043 extends BaseLogic { 044 045 @Inject 046 LotControl lotControl; 047 048 @Inject 049 EntityInstanceLogic entityInstanceLogic; 050 051 @Inject 052 ItemLogic itemLogic; 053 054 @Inject 055 SequenceGeneratorLogic sequenceGeneratorLogic; 056 057 protected LotLogic() { 058 super(); 059 } 060 061 public static LotLogic getInstance() { 062 return CDI.current().select(LotLogic.class).get(); 063 } 064 065 public Lot createLot(final ExecutionErrorAccumulator eea, final Item item, String lotIdentifier, 066 final BasePK createdBy) { 067 Lot lot = null; 068 069 if(lotIdentifier == null) { 070 lotIdentifier = sequenceGeneratorLogic.getNextSequenceValue(eea, SequenceTypes.LOT.name()); 071 } 072 073 if(eea == null || !eea.hasExecutionErrors()) { 074 lot = lotControl.getLotByIdentifier(item, lotIdentifier); 075 if(lot == null) { 076 lot = lotControl.createLot(item, lotIdentifier, createdBy); 077 } else { 078 handleExecutionError(DuplicateLotIdentifierException.class, eea, ExecutionErrors.DuplicateLotIdentifier.name(), 079 item.getLastDetail().getItemName(), lotIdentifier); 080 } 081 } 082 083 return lot; 084 } 085 086 public Lot getLotByIdentifier(final ExecutionErrorAccumulator eea, final Item item, final String lotIdentifier, 087 final EntityPermission entityPermission) { 088 var lot = lotControl.getLotByIdentifier(item, lotIdentifier, entityPermission); 089 090 if(lot == null) { 091 handleExecutionError(UnknownLotIdentifierException.class, eea, ExecutionErrors.UnknownLotIdentifier.name(), 092 item.getLastDetail().getItemName(), lotIdentifier); 093 } 094 095 return lot; 096 } 097 098 public Lot getLotByIdentifier(final ExecutionErrorAccumulator eea, final Item item, final String lotIdentifier) { 099 return getLotByIdentifier(eea, item, lotIdentifier, EntityPermission.READ_ONLY); 100 } 101 102 public Lot getLotByIdentifierForUpdate(final ExecutionErrorAccumulator eea, final Item item, final String lotIdentifier) { 103 return getLotByIdentifier(eea, item, lotIdentifier, EntityPermission.READ_WRITE); 104 } 105 106 public Lot getLotByUniversalSpec(final ExecutionErrorAccumulator eea, final LotUniversalSpec universalSpec, 107 final EntityPermission entityPermission) { 108 Lot lot = null; 109 var itemName = universalSpec.getItemName(); 110 var lotIdentifier = universalSpec.getLotIdentifier(); 111 var parameterCount = (itemName != null && lotIdentifier != null ? 1 : 0) 112 + entityInstanceLogic.countPossibleEntitySpecs(universalSpec); 113 114 switch(parameterCount) { 115 case 1 -> { 116 if(lotIdentifier == null) { 117 var entityInstance = entityInstanceLogic.getEntityInstance(eea, universalSpec, 118 ComponentVendors.ECHO_THREE.name(), EntityTypes.Lot.name()); 119 120 if(eea == null || !eea.hasExecutionErrors()) { 121 lot = lotControl.getLotByEntityInstance(entityInstance, entityPermission); 122 } 123 } else { 124 var item = itemLogic.getItemByName(eea, itemName); 125 126 if(eea == null || !eea.hasExecutionErrors()) { 127 lot = getLotByIdentifier(eea, item, lotIdentifier, entityPermission); 128 } 129 } 130 } 131 default -> 132 handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name()); 133 } 134 135 return lot; 136 } 137 138 public Lot getLotByUniversalSpec(final ExecutionErrorAccumulator eea, 139 final LotUniversalSpec universalSpec) { 140 return getLotByUniversalSpec(eea, universalSpec, EntityPermission.READ_ONLY); 141 } 142 143 public Lot getLotByUniversalSpecForUpdate(final ExecutionErrorAccumulator eea, 144 final LotUniversalSpec universalSpec) { 145 return getLotByUniversalSpec(eea, universalSpec, EntityPermission.READ_WRITE); 146 } 147 148 public void deleteLot(final ExecutionErrorAccumulator eea, final Lot lot, 149 final BasePK deletedBy) { 150 lotControl.deleteLot(lot, deletedBy); 151 } 152 153}