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