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.UnknownCampaignContentNameException;
020import com.echothree.model.control.campaign.common.exception.UnknownCampaignContentStatusChoiceException;
021import com.echothree.model.control.campaign.common.exception.UnknownCampaignContentValueException;
022import com.echothree.model.control.campaign.common.workflow.CampaignContentStatusConstants;
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.CampaignContent;
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 CampaignContentLogic
039        extends BaseLogic {
040
041    protected CampaignContentLogic() {
042        super();
043    }
044
045    public static CampaignContentLogic getInstance() {
046        return CDI.current().select(CampaignContentLogic.class).get();
047    }
048    
049    public CampaignContent getCampaignContentByName(final ExecutionErrorAccumulator eea, final String campaignContentName) {
050        var campaignControl = Session.getModelController(CampaignControl.class);
051        var campaignContent = campaignControl.getCampaignContentByName(campaignContentName);
052
053        if(campaignContent == null) {
054            handleExecutionError(UnknownCampaignContentNameException.class, eea, ExecutionErrors.UnknownCampaignContentName.name(), campaignContentName);
055        }
056
057        return campaignContent;
058    }
059    
060    public CampaignContent getCampaignContentByValue(final ExecutionErrorAccumulator eea, final String campaignContentValue) {
061        var campaignControl = Session.getModelController(CampaignControl.class);
062        var campaignContent = campaignControl.getCampaignContentByValue(campaignContentValue);
063
064        if(campaignContent == null) {
065            handleExecutionError(UnknownCampaignContentValueException.class, eea, ExecutionErrors.UnknownCampaignContentValue.name(), campaignContentValue);
066        }
067
068        return campaignContent;
069    }
070    
071    public void setCampaignContentStatus(final Session session, ExecutionErrorAccumulator eea, CampaignContent campaignContent, String campaignContentStatusChoice, 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, CampaignContentStatusConstants.Workflow_CAMPAIGN_CONTENT_STATUS);
076        var entityInstance = entityInstanceControl.getEntityInstanceByBasePK(campaignContent.getPrimaryKey());
077        var workflowEntityStatus = workflowControl.getWorkflowEntityStatusByEntityInstanceForUpdate(workflow, entityInstance);
078        var workflowDestination = campaignContentStatusChoice == null ? null : workflowControl.getWorkflowDestinationByName(workflowEntityStatus.getWorkflowStep(), campaignContentStatusChoice);
079
080        if(workflowDestination != null || campaignContentStatusChoice == 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(CampaignContentStatusConstants.WorkflowStep_ACTIVE)) {
087                if(workflowDestinationLogic.workflowDestinationMapContainsStep(map, CampaignContentStatusConstants.Workflow_CAMPAIGN_CONTENT_STATUS, CampaignContentStatusConstants.WorkflowStep_INACTIVE)) {
088                    // Nothing at this time.
089                }
090            } else if(currentWorkflowStepName.equals(CampaignContentStatusConstants.WorkflowStep_INACTIVE)) {
091                if(workflowDestinationLogic.workflowDestinationMapContainsStep(map, CampaignContentStatusConstants.Workflow_CAMPAIGN_CONTENT_STATUS, CampaignContentStatusConstants.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(UnknownCampaignContentStatusChoiceException.class, eea, ExecutionErrors.UnknownCampaignContentStatusChoice.name(), campaignContentStatusChoice);
101        }
102    }
103    
104}