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.term.server.transfer;
018
019import javax.inject.Inject;
020import com.echothree.model.control.accounting.server.control.AccountingControl;
021import com.echothree.model.control.party.server.control.PartyControl;
022import com.echothree.model.control.term.common.transfer.PartyCreditLimitTransfer;
023import com.echothree.model.data.term.server.entity.PartyCreditLimit;
024import com.echothree.model.data.user.server.entity.UserVisit;
025import com.echothree.util.server.string.AmountUtils;
026import javax.enterprise.context.RequestScoped;
027
028@RequestScoped
029public class PartyCreditLimitTransferCache
030        extends BaseTermTransferCache<PartyCreditLimit, PartyCreditLimitTransfer> {
031
032    @Inject
033    AccountingControl accountingControl;
034
035    @Inject
036    PartyControl partyControl;
037
038    /** Creates a new instance of PartyCreditLimitTransferCache */
039    protected PartyCreditLimitTransferCache() {
040        super();
041    }
042    
043    public PartyCreditLimitTransfer getPartyCreditLimitTransfer(UserVisit userVisit, PartyCreditLimit partyCreditLimit) {
044        var partyCreditLimitTransfer = get(partyCreditLimit);
045        
046        if(partyCreditLimitTransfer == null) {
047            var partyTransfer = partyControl.getPartyTransfer(userVisit, partyCreditLimit.getParty());
048            var currency = partyCreditLimit.getCurrency();
049            var currencyTransfer = accountingControl.getCurrencyTransfer(userVisit, currency);
050            var creditLimit = AmountUtils.getInstance().formatAmount(currency, partyCreditLimit.getCreditLimit());
051            var potentialCreditLimit = AmountUtils.getInstance().formatAmount(currency, partyCreditLimit.getPotentialCreditLimit());
052            
053            partyCreditLimitTransfer = new PartyCreditLimitTransfer(partyTransfer, currencyTransfer, creditLimit,
054                    potentialCreditLimit);
055            put(userVisit, partyCreditLimit, partyCreditLimitTransfer);
056        }
057        
058        return partyCreditLimitTransfer;
059    }
060    
061}