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