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.letter.server.transfer;
018
019import javax.inject.Inject;
020import com.echothree.model.control.contact.server.control.ContactControl;
021import com.echothree.model.control.letter.common.transfer.LetterSourceTransfer;
022import com.echothree.model.control.letter.server.control.LetterControl;
023import com.echothree.model.control.party.server.control.PartyControl;
024import com.echothree.model.data.letter.server.entity.LetterSource;
025import com.echothree.model.data.user.server.entity.UserVisit;
026import javax.enterprise.context.RequestScoped;
027
028@RequestScoped
029public class LetterSourceTransferCache
030        extends BaseLetterTransferCache<LetterSource, LetterSourceTransfer> {
031
032    @Inject
033    ContactControl contactControl;
034
035    @Inject
036    LetterControl letterControl;
037
038    @Inject
039    PartyControl partyControl;
040    
041    /** Creates a new instance of LetterSourceTransferCache */
042    protected LetterSourceTransferCache() {
043        super();
044        
045        setIncludeEntityInstance(true);
046    }
047    
048    public LetterSourceTransfer getLetterSourceTransfer(UserVisit userVisit, LetterSource letterSource) {
049        var letterSourceTransfer = get(letterSource);
050        
051        if(letterSourceTransfer == null) {
052            var letterSourceDetail = letterSource.getLastDetail();
053            var letterSourceName = letterSourceDetail.getLetterSourceName();
054            var companyTransfer = partyControl.getCompanyTransfer(userVisit, letterSourceDetail.getCompanyParty());
055            var emailAddressPartyContactMechanismTransfer = contactControl.getPartyContactMechanismTransfer(userVisit, letterSourceDetail.getEmailAddressPartyContactMechanism());
056            var postalAddressPartyContactMechanismTransfer = contactControl.getPartyContactMechanismTransfer(userVisit, letterSourceDetail.getPostalAddressPartyContactMechanism());
057            var letterSourcePartyContactMechanismTransfer = contactControl.getPartyContactMechanismTransfer(userVisit, letterSourceDetail.getLetterSourcePartyContactMechanism());
058            var isDefault = letterSourceDetail.getIsDefault();
059            var sortOrder = letterSourceDetail.getSortOrder();
060            var description = letterControl.getBestLetterSourceDescription(letterSource, getLanguage(userVisit));
061            
062            letterSourceTransfer = new LetterSourceTransfer(letterSourceName, companyTransfer,
063                    emailAddressPartyContactMechanismTransfer, postalAddressPartyContactMechanismTransfer,
064                    letterSourcePartyContactMechanismTransfer, isDefault, sortOrder, description);
065            put(userVisit, letterSource, letterSourceTransfer);
066        }
067        
068        return letterSourceTransfer;
069    }
070    
071}