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.logic;
018
019import com.echothree.control.user.campaign.common.spec.CampaignSourceUniversalSpec;
020import com.echothree.model.control.campaign.common.exception.UnknownCampaignSourceNameException;
021import com.echothree.model.control.campaign.common.exception.UnknownCampaignSourceStatusChoiceException;
022import com.echothree.model.control.campaign.common.exception.UnknownCampaignSourceValueException;
023import com.echothree.model.control.campaign.common.workflow.CampaignSourceStatusConstants;
024import com.echothree.model.control.campaign.server.control.CampaignControl;
025import com.echothree.model.control.core.common.ComponentVendors;
026import com.echothree.model.control.core.common.EntityTypes;
027import com.echothree.model.control.core.common.exception.InvalidParameterCountException;
028import com.echothree.model.control.core.server.control.EntityInstanceControl;
029import com.echothree.model.control.core.server.logic.EntityInstanceLogic;
030import com.echothree.model.control.workflow.server.control.WorkflowControl;
031import com.echothree.model.control.workflow.server.logic.WorkflowDestinationLogic;
032import com.echothree.model.control.workflow.server.logic.WorkflowLogic;
033import com.echothree.model.data.campaign.server.entity.CampaignSource;
034import com.echothree.model.data.party.common.pk.PartyPK;
035import com.echothree.util.common.message.ExecutionErrors;
036import com.echothree.util.server.control.BaseLogic;
037import com.echothree.util.server.message.ExecutionErrorAccumulator;
038import com.echothree.util.server.persistence.EntityPermission;
039import com.echothree.util.server.persistence.Session;
040import javax.enterprise.context.ApplicationScoped;
041import javax.enterprise.inject.spi.CDI;
042import javax.inject.Inject;
043
044@ApplicationScoped
045public class CampaignSourceLogic
046        extends BaseLogic {
047
048    protected CampaignSourceLogic() {
049        super();
050    }
051
052    public static CampaignSourceLogic getInstance() {
053        return CDI.current().select(CampaignSourceLogic.class).get();
054    }
055
056    @Inject
057    CampaignControl campaignControl;
058
059    public CampaignSource getCampaignSourceByName(final ExecutionErrorAccumulator eea, final String campaignSourceName,
060            final EntityPermission entityPermission) {
061        var campaignSource = campaignControl.getCampaignSourceByName(campaignSourceName, entityPermission);
062
063        if(campaignSource == null) {
064            handleExecutionError(UnknownCampaignSourceNameException.class, eea, ExecutionErrors.UnknownCampaignSourceName.name(), campaignSourceName);
065        }
066
067        return campaignSource;
068    }
069
070    public CampaignSource getCampaignSourceByName(final ExecutionErrorAccumulator eea, final String campaignSourceName) {
071        return getCampaignSourceByName(eea, campaignSourceName, EntityPermission.READ_ONLY);
072    }
073
074    public CampaignSource getCampaignSourceByNameForUpdate(final ExecutionErrorAccumulator eea, final String campaignSourceName) {
075        return getCampaignSourceByName(eea, campaignSourceName, EntityPermission.READ_WRITE);
076    }
077
078    public CampaignSource getCampaignSourceByValue(final ExecutionErrorAccumulator eea, final String campaignSourceValue,
079            final EntityPermission entityPermission) {
080        var campaignSource = campaignControl.getCampaignSourceByValue(campaignSourceValue, entityPermission);
081
082        if(campaignSource == null) {
083            handleExecutionError(UnknownCampaignSourceValueException.class, eea, ExecutionErrors.UnknownCampaignSourceValue.name(), campaignSourceValue);
084        }
085
086        return campaignSource;
087    }
088
089    public CampaignSource getCampaignSourceByValue(final ExecutionErrorAccumulator eea, final String campaignSourceValue) {
090        return getCampaignSourceByValue(eea, campaignSourceValue, EntityPermission.READ_ONLY);
091    }
092
093    public CampaignSource getCampaignSourceByValueForUpdate(final ExecutionErrorAccumulator eea, final String campaignSourceValue) {
094        return getCampaignSourceByValue(eea, campaignSourceValue, EntityPermission.READ_WRITE);
095    }
096
097    public CampaignSource getCampaignSourceByUniversalSpec(final ExecutionErrorAccumulator eea,
098            final CampaignSourceUniversalSpec universalSpec, final EntityPermission entityPermission) {
099        CampaignSource campaignSource = null;
100        var campaignSourceName = universalSpec.getCampaignSourceName();
101        var parameterCount = (campaignSourceName == null ? 0 : 1) + EntityInstanceLogic.getInstance().countPossibleEntitySpecs(universalSpec);
102
103        switch(parameterCount) {
104            case 1 -> {
105                if(campaignSourceName == null) {
106                    var entityInstance = EntityInstanceLogic.getInstance().getEntityInstance(eea, universalSpec,
107                            ComponentVendors.ECHO_THREE.name(), EntityTypes.CampaignSource.name());
108
109                    if(eea == null || !eea.hasExecutionErrors()) {
110                        campaignSource = campaignControl.getCampaignSourceByEntityInstance(entityInstance, entityPermission);
111                    }
112                } else {
113                    campaignSource = getCampaignSourceByName(eea, campaignSourceName, entityPermission);
114                }
115            }
116            default ->
117                    handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name());
118        }
119
120        return campaignSource;
121    }
122
123    public CampaignSource getCampaignSourceByUniversalSpec(final ExecutionErrorAccumulator eea,
124            final CampaignSourceUniversalSpec universalSpec) {
125        return getCampaignSourceByUniversalSpec(eea, universalSpec, EntityPermission.READ_ONLY);
126    }
127
128    public CampaignSource getCampaignSourceByUniversalSpecForUpdate(final ExecutionErrorAccumulator eea,
129            final CampaignSourceUniversalSpec universalSpec) {
130        return getCampaignSourceByUniversalSpec(eea, universalSpec, EntityPermission.READ_WRITE);
131    }
132    
133    public void setCampaignSourceStatus(final Session session, ExecutionErrorAccumulator eea, CampaignSource campaignSource, String campaignSourceStatusChoice, PartyPK modifiedBy) {
134        var entityInstanceControl = Session.getModelController(EntityInstanceControl.class);
135        var workflowControl = Session.getModelController(WorkflowControl.class);
136        var workflowLogic = WorkflowLogic.getInstance();
137        var workflow = workflowLogic.getWorkflowByName(eea, CampaignSourceStatusConstants.Workflow_CAMPAIGN_SOURCE_STATUS);
138        var entityInstance = entityInstanceControl.getEntityInstanceByBasePK(campaignSource.getPrimaryKey());
139        var workflowEntityStatus = workflowControl.getWorkflowEntityStatusByEntityInstanceForUpdate(workflow, entityInstance);
140        var workflowDestination = campaignSourceStatusChoice == null ? null : workflowControl.getWorkflowDestinationByName(workflowEntityStatus.getWorkflowStep(), campaignSourceStatusChoice);
141
142        if(workflowDestination != null || campaignSourceStatusChoice == null) {
143            var workflowDestinationLogic = WorkflowDestinationLogic.getInstance();
144            var currentWorkflowStepName = workflowEntityStatus.getWorkflowStep().getLastDetail().getWorkflowStepName();
145            var map = workflowDestinationLogic.getWorkflowDestinationsAsMap(workflowDestination);
146            Long triggerTime = null;
147
148            if(currentWorkflowStepName.equals(CampaignSourceStatusConstants.WorkflowStep_ACTIVE)) {
149                if(workflowDestinationLogic.workflowDestinationMapContainsStep(map, CampaignSourceStatusConstants.Workflow_CAMPAIGN_SOURCE_STATUS, CampaignSourceStatusConstants.WorkflowStep_INACTIVE)) {
150                    // Nothing at this time.
151                }
152            } else if(currentWorkflowStepName.equals(CampaignSourceStatusConstants.WorkflowStep_INACTIVE)) {
153                if(workflowDestinationLogic.workflowDestinationMapContainsStep(map, CampaignSourceStatusConstants.Workflow_CAMPAIGN_SOURCE_STATUS, CampaignSourceStatusConstants.WorkflowStep_ACTIVE)) {
154                    // Nothing at this time.
155                }
156            }
157
158            if(eea == null || !eea.hasExecutionErrors()) {
159                workflowControl.transitionEntityInWorkflow(eea, workflowEntityStatus, workflowDestination, triggerTime, modifiedBy);
160            }
161        } else {
162            handleExecutionError(UnknownCampaignSourceStatusChoiceException.class, eea, ExecutionErrors.UnknownCampaignSourceStatusChoice.name(), campaignSourceStatusChoice);
163        }
164    }
165    
166}