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