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.campaign.server.logic;
018
019import com.echothree.model.control.campaign.common.exception.UnknownCampaignMediumNameException;
020import com.echothree.model.control.campaign.common.exception.UnknownCampaignMediumStatusChoiceException;
021import com.echothree.model.control.campaign.common.exception.UnknownCampaignMediumValueException;
022import com.echothree.model.control.campaign.server.control.CampaignControl;
023import com.echothree.model.control.core.server.control.CoreControl;
024import com.echothree.model.control.campaign.common.workflow.CampaignMediumStatusConstants;
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.CampaignMedium;
029import com.echothree.model.data.core.server.entity.EntityInstance;
030import com.echothree.model.data.party.common.pk.PartyPK;
031import com.echothree.model.data.workflow.server.entity.Workflow;
032import com.echothree.model.data.workflow.server.entity.WorkflowDestination;
033import com.echothree.model.data.workflow.server.entity.WorkflowEntityStatus;
034import com.echothree.util.common.message.ExecutionErrors;
035import com.echothree.util.server.control.BaseLogic;
036import com.echothree.util.server.message.ExecutionErrorAccumulator;
037import com.echothree.util.server.persistence.Session;
038import java.util.Map;
039import java.util.Set;
040
041public class CampaignMediumLogic
042        extends BaseLogic {
043
044    private CampaignMediumLogic() {
045        super();
046    }
047
048    private static class CampaignMediumLogicHolder {
049        static CampaignMediumLogic instance = new CampaignMediumLogic();
050    }
051
052    public static CampaignMediumLogic getInstance() {
053        return CampaignMediumLogicHolder.instance;
054    }
055    
056    public CampaignMedium getCampaignMediumByName(final ExecutionErrorAccumulator eea, final String campaignMediumName) {
057        var campaignControl = Session.getModelController(CampaignControl.class);
058        CampaignMedium campaignMedium = campaignControl.getCampaignMediumByName(campaignMediumName);
059
060        if(campaignMedium == null) {
061            handleExecutionError(UnknownCampaignMediumNameException.class, eea, ExecutionErrors.UnknownCampaignMediumName.name(), campaignMediumName);
062        }
063
064        return campaignMedium;
065    }
066    
067    public CampaignMedium getCampaignMediumByValue(final ExecutionErrorAccumulator eea, final String campaignMediumValue) {
068        var campaignControl = Session.getModelController(CampaignControl.class);
069        CampaignMedium campaignMedium = campaignControl.getCampaignMediumByValue(campaignMediumValue);
070
071        if(campaignMedium == null) {
072            handleExecutionError(UnknownCampaignMediumValueException.class, eea, ExecutionErrors.UnknownCampaignMediumValue.name(), campaignMediumValue);
073        }
074
075        return campaignMedium;
076    }
077    
078    public void setCampaignMediumStatus(final Session session, ExecutionErrorAccumulator eea, CampaignMedium campaignMedium, String campaignMediumStatusChoice, PartyPK modifiedBy) {
079        var coreControl = Session.getModelController(CoreControl.class);
080        var workflowControl = Session.getModelController(WorkflowControl.class);
081        var workflowLogic = WorkflowLogic.getInstance();
082        Workflow workflow = workflowLogic.getWorkflowByName(eea, CampaignMediumStatusConstants.Workflow_CAMPAIGN_MEDIUM_STATUS);
083        EntityInstance entityInstance = coreControl.getEntityInstanceByBasePK(campaignMedium.getPrimaryKey());
084        WorkflowEntityStatus workflowEntityStatus = workflowControl.getWorkflowEntityStatusByEntityInstanceForUpdate(workflow, entityInstance);
085        WorkflowDestination workflowDestination = campaignMediumStatusChoice == null ? null : workflowControl.getWorkflowDestinationByName(workflowEntityStatus.getWorkflowStep(), campaignMediumStatusChoice);
086
087        if(workflowDestination != null || campaignMediumStatusChoice == null) {
088            WorkflowDestinationLogic workflowDestinationLogic = WorkflowDestinationLogic.getInstance();
089            String currentWorkflowStepName = workflowEntityStatus.getWorkflowStep().getLastDetail().getWorkflowStepName();
090            Map<String, Set<String>> map = workflowDestinationLogic.getWorkflowDestinationsAsMap(workflowDestination);
091            Long triggerTime = null;
092
093            if(currentWorkflowStepName.equals(CampaignMediumStatusConstants.WorkflowStep_ACTIVE)) {
094                if(workflowDestinationLogic.workflowDestinationMapContainsStep(map, CampaignMediumStatusConstants.Workflow_CAMPAIGN_MEDIUM_STATUS, CampaignMediumStatusConstants.WorkflowStep_INACTIVE)) {
095                    // Nothing at this time.
096                }
097            } else if(currentWorkflowStepName.equals(CampaignMediumStatusConstants.WorkflowStep_INACTIVE)) {
098                if(workflowDestinationLogic.workflowDestinationMapContainsStep(map, CampaignMediumStatusConstants.Workflow_CAMPAIGN_MEDIUM_STATUS, CampaignMediumStatusConstants.WorkflowStep_ACTIVE)) {
099                    // Nothing at this time.
100                }
101            }
102
103            if(eea == null || !eea.hasExecutionErrors()) {
104                workflowControl.transitionEntityInWorkflow(eea, workflowEntityStatus, workflowDestination, triggerTime, modifiedBy);
105            }
106        } else {
107            handleExecutionError(UnknownCampaignMediumStatusChoiceException.class, eea, ExecutionErrors.UnknownCampaignMediumStatusChoice.name(), campaignMediumStatusChoice);
108        }
109    }
110    
111}