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