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