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.communication.server.transfer;
018
019import javax.inject.Inject;
020import com.echothree.model.control.communication.common.CommunicationConstants;
021import com.echothree.model.control.communication.common.CommunicationOptions;
022import com.echothree.model.control.communication.common.transfer.CommunicationEmailSourceTransfer;
023import com.echothree.model.control.communication.common.transfer.CommunicationSourceTransfer;
024import com.echothree.model.control.communication.server.control.CommunicationControl;
025import com.echothree.model.data.communication.server.entity.CommunicationSource;
026import com.echothree.model.data.user.server.entity.UserVisit;
027import javax.enterprise.context.RequestScoped;
028
029@RequestScoped
030public class CommunicationSourceTransferCache
031        extends BaseCommunicationTransferCache<CommunicationSource, CommunicationSourceTransfer> {
032
033    @Inject
034    CommunicationControl communicationControl;
035
036    boolean includeRelated;
037    
038    /** Creates a new instance of CommunicationSourceTransferCache */
039    protected CommunicationSourceTransferCache() {
040        super();
041        
042        var options = session.getOptions();
043        if(options != null) {
044            includeRelated = options.contains(CommunicationOptions.CommunicationSourceIncludeRelated);
045        }
046        
047        setIncludeEntityInstance(true);
048    }
049    
050    public CommunicationSourceTransfer getCommunicationSourceTransfer(UserVisit userVisit, CommunicationSource communicationSource) {
051        var communicationSourceTransfer = get(communicationSource);
052        
053        if(communicationSourceTransfer == null) {
054            var communicationSourceDetail = communicationSource.getLastDetail();
055            var communicationSourceName = communicationSourceDetail.getCommunicationSourceName();
056            var communicationSourceTypeTransfer = communicationControl.getCommunicationSourceTypeTransfer(userVisit,
057                    communicationSourceDetail.getCommunicationSourceType());
058            var sortOrder = communicationSourceDetail.getSortOrder();
059            var description = communicationControl.getBestCommunicationSourceDescription(communicationSource, getLanguage(userVisit));
060            CommunicationEmailSourceTransfer communicationEmailSourceTransfer = null;
061            
062            if(includeRelated) {
063                var communicationSourceTypeName = communicationSourceTypeTransfer.getCommunicationSourceTypeName();
064                
065                if(communicationSourceTypeName.equals(CommunicationConstants.CommunicationSourceType_EMAIL)) {
066                    communicationEmailSourceTransfer = communicationControl.getCommunicationEmailSourceTransfer(userVisit,
067                            communicationControl.getCommunicationEmailSource(communicationSource));
068                }
069            }
070            
071            communicationSourceTransfer = new CommunicationSourceTransfer(communicationSourceName, communicationSourceTypeTransfer, 
072                    sortOrder, description, communicationEmailSourceTransfer);
073            put(userVisit, communicationSource, communicationSourceTransfer);
074        }
075        
076        return communicationSourceTransfer;
077    }
078    
079}