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.UnknownCampaignNameException;
020import com.echothree.model.control.campaign.common.exception.UnknownCampaignStatusChoiceException;
021import com.echothree.model.control.campaign.common.exception.UnknownCampaignValueException;
022import com.echothree.model.control.campaign.common.workflow.CampaignStatusConstants;
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.Campaign;
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 CampaignLogic
039        extends BaseLogic {
040
041    protected CampaignLogic() {
042        super();
043    }
044
045    public static CampaignLogic getInstance() {
046        return CDI.current().select(CampaignLogic.class).get();
047    }
048    
049    public Campaign getCampaignByName(final ExecutionErrorAccumulator eea, final String campaignName) {
050        var campaignControl = Session.getModelController(CampaignControl.class);
051        var campaign = campaignControl.getCampaignByName(campaignName);
052
053        if(campaign == null) {
054            handleExecutionError(UnknownCampaignNameException.class, eea, ExecutionErrors.UnknownCampaignName.name(), campaignName);
055        }
056
057        return campaign;
058    }
059    
060    public Campaign getCampaignByValue(final ExecutionErrorAccumulator eea, final String campaignValue) {
061        var campaignControl = Session.getModelController(CampaignControl.class);
062        var campaign = campaignControl.getCampaignByValue(campaignValue);
063
064        if(campaign == null) {
065            handleExecutionError(UnknownCampaignValueException.class, eea, ExecutionErrors.UnknownCampaignValue.name(), campaignValue);
066        }
067
068        return campaign;
069    }
070    
071    public void setCampaignStatus(final Session session, ExecutionErrorAccumulator eea, Campaign campaign, String campaignStatusChoice, 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, CampaignStatusConstants.Workflow_CAMPAIGN_STATUS);
076        var entityInstance = entityInstanceControl.getEntityInstanceByBasePK(campaign.getPrimaryKey());
077        var workflowEntityStatus = workflowControl.getWorkflowEntityStatusByEntityInstanceForUpdate(workflow, entityInstance);
078        var workflowDestination = campaignStatusChoice == null ? null : workflowControl.getWorkflowDestinationByName(workflowEntityStatus.getWorkflowStep(), campaignStatusChoice);
079
080        if(workflowDestination != null || campaignStatusChoice == 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(CampaignStatusConstants.WorkflowStep_ACTIVE)) {
087                if(workflowDestinationLogic.workflowDestinationMapContainsStep(map, CampaignStatusConstants.Workflow_CAMPAIGN_STATUS, CampaignStatusConstants.WorkflowStep_INACTIVE)) {
088                    // Nothing at this time.
089                }
090            } else if(currentWorkflowStepName.equals(CampaignStatusConstants.WorkflowStep_INACTIVE)) {
091                if(workflowDestinationLogic.workflowDestinationMapContainsStep(map, CampaignStatusConstants.Workflow_CAMPAIGN_STATUS, CampaignStatusConstants.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(UnknownCampaignStatusChoiceException.class, eea, ExecutionErrors.UnknownCampaignStatusChoice.name(), campaignStatusChoice);
101        }
102    }
103    
104}