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.server.control.LotTimeControl;
020import com.echothree.model.data.inventory.server.entity.Lot;
021import com.echothree.util.common.message.ExecutionErrors;
022import com.echothree.util.common.persistence.BasePK;
023import com.echothree.util.server.message.ExecutionErrorAccumulator;
024import javax.enterprise.context.ApplicationScoped;
025import javax.enterprise.inject.spi.CDI;
026import javax.inject.Inject;
027
028@ApplicationScoped
029public class LotTimeLogic {
030
031    @Inject
032    LotTimeControl lotTimeControl;
033
034    protected LotTimeLogic() {
035        super();
036    }
037
038    public static LotTimeLogic getInstance() {
039        return CDI.current().select(LotTimeLogic.class).get();
040    }
041
042    public void createOrUpdateLotTimeIfNotNull(final ExecutionErrorAccumulator ema, final Lot lot, final String lotTimeTypeName, final Long time,
043            final BasePK partyPK) {
044        if(time != null) {
045            createOrUpdateLotTime(ema, lot, lotTimeTypeName, time, partyPK);
046        }
047    }
048
049    public void createOrUpdateLotTime(final ExecutionErrorAccumulator ema, final Lot lot, final String lotTimeTypeName, final Long time,
050            final BasePK partyPK) {
051        var lotTimeType = lotTimeControl.getLotTimeTypeByName(lotTimeTypeName);
052
053        if(lotTimeType == null) {
054            if(ema != null) {
055                ema.addExecutionError(ExecutionErrors.UnknownLotTimeTypeName.name(), lotTimeTypeName);
056            }
057        } else {
058            var lotTimeValue = lotTimeControl.getLotTimeValueForUpdate(lot, lotTimeType);
059
060            if(lotTimeValue == null) {
061                lotTimeControl.createLotTime(lot, lotTimeType, time, partyPK);
062            } else {
063                lotTimeValue.setTime(time);
064                lotTimeControl.updateLotTimeFromValue(lotTimeValue, partyPK);
065            }
066        }
067    }
068
069    public Long getLotTime(final ExecutionErrorAccumulator ema, final Lot lot, final String lotTimeTypeName) {
070        var lotDetail = lot.getLastDetail();
071        var lotTimeType = lotTimeControl.getLotTimeTypeByName(lotTimeTypeName);
072        Long result = null;
073
074        if(lotTimeType == null) {
075            if(ema != null) {
076                ema.addExecutionError(ExecutionErrors.UnknownLotTimeTypeName.name(), lotTimeTypeName);
077            }
078        } else {
079            var lotTime = lotTimeControl.getLotTime(lot, lotTimeType);
080
081            if(lotTime == null) {
082                if(ema != null) {
083                    ema.addExecutionError(ExecutionErrors.UnknownLotTime.name(), lotDetail.getLotIdentifier(), lotTimeTypeName);
084                }
085            } else {
086                result = lotTime.getTime();
087            }
088        }
089
090        return result;
091    }
092
093    public void deleteLotTime(final ExecutionErrorAccumulator ema, final Lot lot, final String lotTimeTypeName, final BasePK deletedBy) {
094        var lotDetail = lot.getLastDetail();
095        var lotTimeType = lotTimeControl.getLotTimeTypeByName(lotTimeTypeName);
096
097        if(lotTimeType == null) {
098            if(ema != null) {
099                ema.addExecutionError(ExecutionErrors.UnknownLotTimeTypeName.name(), lotTimeTypeName);
100            }
101        } else {
102            var lotTime = lotTimeControl.getLotTimeForUpdate(lot, lotTimeType);
103
104            if(lotTime == null) {
105                if(ema != null) {
106                    ema.addExecutionError(ExecutionErrors.UnknownLotTime.name(), lotDetail.getLotIdentifier(), lotTimeTypeName);
107                }
108            } else {
109                lotTimeControl.deleteLotTime(lotTime, deletedBy);
110            }
111        }
112    }
113
114}