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.campaign.server.transfer;
018
019import javax.inject.Inject;
020import com.echothree.model.control.campaign.common.CampaignOptions;
021import com.echothree.model.control.campaign.common.transfer.CampaignSourceTransfer;
022import com.echothree.model.control.campaign.common.workflow.CampaignSourceStatusConstants;
023import com.echothree.model.control.campaign.server.control.CampaignControl;
024import com.echothree.model.control.workflow.server.control.WorkflowControl;
025import com.echothree.model.data.campaign.server.entity.CampaignSource;
026import com.echothree.model.data.user.server.entity.UserVisit;
027import javax.enterprise.context.RequestScoped;
028
029@RequestScoped
030public class CampaignSourceTransferCache
031        extends BaseCampaignTransferCache<CampaignSource, CampaignSourceTransfer> {
032
033    @Inject
034    CampaignControl campaignControl;
035
036    @Inject
037    WorkflowControl workflowControl;
038    
039    /** Creates a new instance of CampaignSourceTransferCache */
040    protected CampaignSourceTransferCache() {
041        super();
042        
043        var options = session.getOptions();
044        if(options != null) {
045            setIncludeUuid(options.contains(CampaignOptions.CampaignSourceIncludeUuid));
046        }
047        
048        setIncludeEntityInstance(true);
049    }
050
051    public CampaignSourceTransfer getCampaignSourceTransfer(UserVisit userVisit, CampaignSource campaignSource) {
052        var campaignSourceTransfer = get(campaignSource);
053
054        if(campaignSourceTransfer == null) {
055            var campaignSourceDetail = campaignSource.getLastDetail();
056            var campaignSourceName = campaignSourceDetail.getCampaignSourceName();
057            var valueSha1Hash = campaignSourceDetail.getValueSha1Hash();
058            var value = campaignSourceDetail.getValue();
059            var isDefault = campaignSourceDetail.getIsDefault();
060            var sortOrder = campaignSourceDetail.getSortOrder();
061            var description = campaignControl.getBestCampaignSourceDescription(campaignSource, getLanguage(userVisit));
062
063            var entityInstance = entityInstanceControl.getEntityInstanceByBasePK(campaignSource.getPrimaryKey());
064            var campaignSourceStatusTransfer = workflowControl.getWorkflowEntityStatusTransferByEntityInstanceUsingNames(userVisit,
065                    CampaignSourceStatusConstants.Workflow_CAMPAIGN_SOURCE_STATUS, entityInstance);
066            
067            campaignSourceTransfer = new CampaignSourceTransfer(campaignSourceName, valueSha1Hash, value, isDefault, sortOrder, description,
068                    campaignSourceStatusTransfer);
069            put(userVisit, campaignSource, campaignSourceTransfer);
070        }
071
072        return campaignSourceTransfer;
073    }
074
075}