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