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.transfer.CommunicationEventTransfer;
020import com.echothree.model.control.communication.server.control.CommunicationControl;
021import com.echothree.model.control.contact.server.control.ContactControl;
022import com.echothree.model.control.document.server.control.DocumentControl;
023import com.echothree.model.data.communication.server.entity.CommunicationEvent;
024import com.echothree.model.data.user.server.entity.UserVisit;
025import com.echothree.util.server.persistence.Session;
026import javax.enterprise.context.RequestScoped;
027
028@RequestScoped
029public class CommunicationEventTransferCache
030        extends BaseCommunicationTransferCache<CommunicationEvent, CommunicationEventTransfer> {
031
032    CommunicationControl communicationControl = Session.getModelController(CommunicationControl.class);
033    ContactControl contactControl = Session.getModelController(ContactControl.class);
034    DocumentControl documentControl = Session.getModelController(DocumentControl.class);
035    
036    /** Creates a new instance of CommunicationEventTransferCache */
037    protected CommunicationEventTransferCache() {
038        super();
039        
040        setIncludeEntityInstance(true);
041    }
042    
043    public CommunicationEventTransfer getCommunicationEventTransfer(UserVisit userVisit, CommunicationEvent communicationEvent) {
044        var communicationEventTransfer = get(communicationEvent);
045        
046        if(communicationEventTransfer == null) {
047            var communicationEventDetail = communicationEvent.getLastDetail();
048            var communicationEventName = communicationEventDetail.getCommunicationEventName();
049            var communicationEventTypeTransfer = communicationControl.getCommunicationEventTypeTransfer(userVisit,
050                    communicationEventDetail.getCommunicationEventType());
051            var communicationSourceTransfer = communicationControl.getCommunicationSourceTransfer(userVisit,
052                    communicationEventDetail.getCommunicationSource());
053            var communicationEventPurposeTransfer = communicationControl.getCommunicationEventPurposeTransfer(userVisit,
054                    communicationEventDetail.getCommunicationEventPurpose());
055            var originalCommunicationEvent = communicationEventDetail.getOriginalCommunicationEvent();
056            var originalCommunicationEventTransfer = originalCommunicationEvent == null? null: communicationControl.getCommunicationEventTransfer(userVisit, originalCommunicationEvent);
057            var parentCommunicationEvent = communicationEventDetail.getParentCommunicationEvent();
058            var parentCommunicationEventTransfer = parentCommunicationEvent == null? null: communicationControl.getCommunicationEventTransfer(userVisit, parentCommunicationEvent);
059            var partyContactMechanismTransfer = contactControl.getPartyContactMechanismTransfer(userVisit, communicationEventDetail.getPartyContactMechanism());
060            var documentTransfer = documentControl.getDocumentTransfer(userVisit, communicationEventDetail.getDocument());
061            
062            communicationEventTransfer = new CommunicationEventTransfer(communicationEventName, communicationEventTypeTransfer,
063                    communicationSourceTransfer, communicationEventPurposeTransfer, originalCommunicationEventTransfer,
064                    parentCommunicationEventTransfer, partyContactMechanismTransfer, documentTransfer);
065            put(userVisit, communicationEvent, communicationEventTransfer);
066            setupOwnedWorkEfforts(userVisit, communicationEvent, null, communicationEventTransfer);
067        }
068        
069        return communicationEventTransfer;
070    }
071    
072}