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