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.campaign.server.logic;
018
019import com.echothree.model.control.campaign.common.exception.UnknownCampaignSourceNameException;
020import com.echothree.model.control.campaign.common.exception.UnknownCampaignSourceStatusChoiceException;
021import com.echothree.model.control.campaign.common.exception.UnknownCampaignSourceValueException;
022import com.echothree.model.control.campaign.common.workflow.CampaignSourceStatusConstants;
023import com.echothree.model.control.campaign.server.control.CampaignControl;
024import com.echothree.model.control.core.server.control.EntityInstanceControl;
025import com.echothree.model.control.workflow.server.control.WorkflowControl;
026import com.echothree.model.control.workflow.server.logic.WorkflowDestinationLogic;
027import com.echothree.model.control.workflow.server.logic.WorkflowLogic;
028import com.echothree.model.data.campaign.server.entity.CampaignSource;
029import com.echothree.model.data.party.common.pk.PartyPK;
030import com.echothree.util.common.message.ExecutionErrors;
031import com.echothree.util.server.control.BaseLogic;
032import com.echothree.util.server.message.ExecutionErrorAccumulator;
033import com.echothree.util.server.persistence.Session;
034import javax.enterprise.context.ApplicationScoped;
035import javax.enterprise.inject.spi.CDI;
036
037@ApplicationScoped
038public class CampaignSourceLogic
039        extends BaseLogic {
040
041    protected CampaignSourceLogic() {
042        super();
043    }
044
045    public static CampaignSourceLogic getInstance() {
046        return CDI.current().select(CampaignSourceLogic.class).get();
047    }
048    
049    public CampaignSource getCampaignSourceByName(final ExecutionErrorAccumulator eea, final String campaignSourceName) {
050        var campaignControl = Session.getModelController(CampaignControl.class);
051        var campaignSource = campaignControl.getCampaignSourceByName(campaignSourceName);
052
053        if(campaignSource == null) {
054            handleExecutionError(UnknownCampaignSourceNameException.class, eea, ExecutionErrors.UnknownCampaignSourceName.name(), campaignSourceName);
055        }
056
057        return campaignSource;
058    }
059    
060    public CampaignSource getCampaignSourceByValue(final ExecutionErrorAccumulator eea, final String campaignSourceValue) {
061        var campaignControl = Session.getModelController(CampaignControl.class);
062        var campaignSource = campaignControl.getCampaignSourceByValue(campaignSourceValue);
063
064        if(campaignSource == null) {
065            handleExecutionError(UnknownCampaignSourceValueException.class, eea, ExecutionErrors.UnknownCampaignSourceValue.name(), campaignSourceValue);
066        }
067
068        return campaignSource;
069    }
070    
071    public void setCampaignSourceStatus(final Session session, ExecutionErrorAccumulator eea, CampaignSource campaignSource, String campaignSourceStatusChoice, PartyPK modifiedBy) {
072        var entityInstanceControl = Session.getModelController(EntityInstanceControl.class);
073        var workflowControl = Session.getModelController(WorkflowControl.class);
074        var workflowLogic = WorkflowLogic.getInstance();
075        var workflow = workflowLogic.getWorkflowByName(eea, CampaignSourceStatusConstants.Workflow_CAMPAIGN_SOURCE_STATUS);
076        var entityInstance = entityInstanceControl.getEntityInstanceByBasePK(campaignSource.getPrimaryKey());
077        var workflowEntityStatus = workflowControl.getWorkflowEntityStatusByEntityInstanceForUpdate(workflow, entityInstance);
078        var workflowDestination = campaignSourceStatusChoice == null ? null : workflowControl.getWorkflowDestinationByName(workflowEntityStatus.getWorkflowStep(), campaignSourceStatusChoice);
079
080        if(workflowDestination != null || campaignSourceStatusChoice == null) {
081            var workflowDestinationLogic = WorkflowDestinationLogic.getInstance();
082            var currentWorkflowStepName = workflowEntityStatus.getWorkflowStep().getLastDetail().getWorkflowStepName();
083            var map = workflowDestinationLogic.getWorkflowDestinationsAsMap(workflowDestination);
084            Long triggerTime = null;
085
086            if(currentWorkflowStepName.equals(CampaignSourceStatusConstants.WorkflowStep_ACTIVE)) {
087                if(workflowDestinationLogic.workflowDestinationMapContainsStep(map, CampaignSourceStatusConstants.Workflow_CAMPAIGN_SOURCE_STATUS, CampaignSourceStatusConstants.WorkflowStep_INACTIVE)) {
088                    // Nothing at this time.
089                }
090            } else if(currentWorkflowStepName.equals(CampaignSourceStatusConstants.WorkflowStep_INACTIVE)) {
091                if(workflowDestinationLogic.workflowDestinationMapContainsStep(map, CampaignSourceStatusConstants.Workflow_CAMPAIGN_SOURCE_STATUS, CampaignSourceStatusConstants.WorkflowStep_ACTIVE)) {
092                    // Nothing at this time.
093                }
094            }
095
096            if(eea == null || !eea.hasExecutionErrors()) {
097                workflowControl.transitionEntityInWorkflow(eea, workflowEntityStatus, workflowDestination, triggerTime, modifiedBy);
098            }
099        } else {
100            handleExecutionError(UnknownCampaignSourceStatusChoiceException.class, eea, ExecutionErrors.UnknownCampaignSourceStatusChoice.name(), campaignSourceStatusChoice);
101        }
102    }
103    
104}