001// --------------------------------------------------------------------------------
002// Copyright 2002-2024 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 com.echothree.util.server.persistence.Session;
025
026public class LotTimeLogic {
027
028    private LotTimeLogic() {
029        super();
030    }
031
032    private static class LotTimeLogicHolder {
033        static LotTimeLogic instance = new LotTimeLogic();
034    }
035
036    public static LotTimeLogic getInstance() {
037        return LotTimeLogicHolder.instance;
038    }
039
040    public void createOrUpdateLotTimeIfNotNull(final ExecutionErrorAccumulator ema, final Lot lot, final String lotTimeTypeName, final Long time,
041            final BasePK partyPK) {
042        if(time != null) {
043            createOrUpdateLotTime(ema, lot, lotTimeTypeName, time, partyPK);
044        }
045    }
046
047    public void createOrUpdateLotTime(final ExecutionErrorAccumulator ema, final Lot lot, final String lotTimeTypeName, final Long time,
048            final BasePK partyPK) {
049        var lotTimeControl = Session.getModelController(LotTimeControl.class);
050        var lotTimeType = lotTimeControl.getLotTimeTypeByName(lotTimeTypeName);
051
052        if(lotTimeType == null) {
053            if(ema != null) {
054                ema.addExecutionError(ExecutionErrors.UnknownLotTimeTypeName.name(), lotTimeTypeName);
055            }
056        } else {
057            var lotTimeValue = lotTimeControl.getLotTimeValueForUpdate(lot, lotTimeType);
058
059            if(lotTimeValue == null) {
060                lotTimeControl.createLotTime(lot, lotTimeType, time, partyPK);
061            } else {
062                lotTimeValue.setTime(time);
063                lotTimeControl.updateLotTimeFromValue(lotTimeValue, partyPK);
064            }
065        }
066    }
067
068    public Long getLotTime(final ExecutionErrorAccumulator ema, final Lot lot, final String lotTimeTypeName) {
069        var lotTimeControl = Session.getModelController(LotTimeControl.class);
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.getLotName(), 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 lotTimeControl = Session.getModelController(LotTimeControl.class);
095        var lotDetail = lot.getLastDetail();
096        var lotTimeType = lotTimeControl.getLotTimeTypeByName(lotTimeTypeName);
097
098        if(lotTimeType == null) {
099            if(ema != null) {
100                ema.addExecutionError(ExecutionErrors.UnknownLotTimeTypeName.name(), lotTimeTypeName);
101            }
102        } else {
103            var lotTime = lotTimeControl.getLotTimeForUpdate(lot, lotTimeType);
104
105            if(lotTime == null) {
106                if(ema != null) {
107                    ema.addExecutionError(ExecutionErrors.UnknownLotTime.name(), lotDetail.getLotName(), lotTimeTypeName);
108                }
109            } else {
110                lotTimeControl.deleteLotTime(lotTime, deletedBy);
111            }
112        }
113    }
114
115}