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.transfer;
018
019import javax.inject.Inject;
020import com.echothree.model.control.inventory.common.InventoryOptions;
021import com.echothree.model.control.inventory.common.transfer.LotAliasTransfer;
022import com.echothree.model.control.inventory.common.transfer.LotTimeTransfer;
023import com.echothree.model.control.inventory.common.transfer.LotTransfer;
024import com.echothree.model.control.inventory.server.control.LotAliasControl;
025import com.echothree.model.control.inventory.server.control.LotTimeControl;
026import com.echothree.model.control.item.server.control.ItemControl;
027import com.echothree.model.data.inventory.server.entity.Lot;
028import com.echothree.model.data.user.server.entity.UserVisit;
029import com.echothree.util.common.transfer.MapWrapper;
030import javax.enterprise.context.RequestScoped;
031
032@RequestScoped
033public class LotTransferCache
034        extends BaseInventoryTransferCache<Lot, LotTransfer> {
035
036    @Inject
037    ItemControl itemControl;
038
039    @Inject
040    LotAliasControl lotAliasControl;
041
042    @Inject
043    LotTimeControl lotTimeControl;
044
045    boolean includeLotAliases;
046    boolean includeLotTimes;
047
048    /** Creates a new instance of LotTransferCache */
049    protected LotTransferCache() {
050        super();
051
052        var options = session.getOptions();
053        if(options != null) {
054            setIncludeUuid(options.contains(InventoryOptions.LotIncludeUuid));
055            includeLotAliases = options.contains(InventoryOptions.LotIncludeLotAliases);
056            includeLotTimes = options.contains(InventoryOptions.LotIncludeLotTimes);
057        }
058
059        setIncludeEntityInstance(true);
060    }
061    
062    @Override
063    public LotTransfer getTransfer(UserVisit userVisit, Lot lot) {
064        var lotTransfer = get(lot);
065        
066        if(lotTransfer == null) {
067            var lotDetail = lot.getLastDetail();
068
069            var item = itemControl.getItemTransfer(userVisit, lotDetail.getItem());
070            var lotIdentifier = lotDetail.getLotIdentifier();
071
072            lotTransfer = new LotTransfer(item, lotIdentifier);
073            put(userVisit, lot, lotTransfer);
074
075            if(includeLotAliases) {
076                var lotAliasTransfers = lotAliasControl.getLotAliasTransfersByLot(userVisit, lot);
077                var lotAliases = new MapWrapper<LotAliasTransfer>(lotAliasTransfers.size());
078
079                lotAliasTransfers.forEach((lotAliasTransfer) -> {
080                    lotAliases.put(lotAliasTransfer.getLotAliasType().getLotAliasTypeName(), lotAliasTransfer);
081                });
082
083                lotTransfer.setLotAliases(lotAliases);
084            }
085
086            if(includeLotTimes) {
087                var lotTimeTransfers = lotTimeControl.getLotTimeTransfersByLot(userVisit, lot);
088                var lotTimes = new MapWrapper<LotTimeTransfer>(lotTimeTransfers.size());
089
090                lotTimeTransfers.forEach((lotTimeTransfer) -> {
091                    lotTimes.put(lotTimeTransfer.getLotTimeType().getLotTimeTypeName(), lotTimeTransfer);
092                });
093
094                lotTransfer.setLotTimes(lotTimes);
095            }
096        }
097        
098        return lotTransfer;
099    }
100    
101}