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