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.CampaignContentUniversalSpec;
020import com.echothree.model.control.campaign.common.exception.UnknownCampaignContentNameException;
021import com.echothree.model.control.campaign.common.exception.UnknownCampaignContentStatusChoiceException;
022import com.echothree.model.control.campaign.common.exception.UnknownCampaignContentValueException;
023import com.echothree.model.control.campaign.common.workflow.CampaignContentStatusConstants;
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.CampaignContent;
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 CampaignContentLogic
046        extends BaseLogic {
047
048    @Inject
049    CampaignControl campaignControl;
050
051    @Inject
052    EntityInstanceControl entityInstanceControl;
053
054    @Inject
055    WorkflowControl workflowControl;
056
057    @Inject
058    EntityInstanceLogic entityInstanceLogic;
059
060    @Inject
061    WorkflowDestinationLogic workflowDestinationLogic;
062
063    @Inject
064    WorkflowLogic workflowLogic;
065
066    protected CampaignContentLogic() {
067        super();
068    }
069
070    public static CampaignContentLogic getInstance() {
071        return CDI.current().select(CampaignContentLogic.class).get();
072    }
073
074    public CampaignContent getCampaignContentByName(final ExecutionErrorAccumulator eea, final String campaignContentName,
075            final EntityPermission entityPermission) {
076        var campaignContent = campaignControl.getCampaignContentByName(campaignContentName, entityPermission);
077
078        if(campaignContent == null) {
079            handleExecutionError(UnknownCampaignContentNameException.class, eea, ExecutionErrors.UnknownCampaignContentName.name(), campaignContentName);
080        }
081
082        return campaignContent;
083    }
084
085    public CampaignContent getCampaignContentByName(final ExecutionErrorAccumulator eea, final String campaignContentName) {
086        return getCampaignContentByName(eea, campaignContentName, EntityPermission.READ_ONLY);
087    }
088
089    public CampaignContent getCampaignContentByNameForUpdate(final ExecutionErrorAccumulator eea, final String campaignContentName) {
090        return getCampaignContentByName(eea, campaignContentName, EntityPermission.READ_WRITE);
091    }
092
093    public CampaignContent getCampaignContentByValue(final ExecutionErrorAccumulator eea, final String campaignContentValue,
094            final EntityPermission entityPermission) {
095        var campaignContent = campaignControl.getCampaignContentByValue(campaignContentValue, entityPermission);
096
097        if(campaignContent == null) {
098            handleExecutionError(UnknownCampaignContentValueException.class, eea, ExecutionErrors.UnknownCampaignContentValue.name(), campaignContentValue);
099        }
100
101        return campaignContent;
102    }
103
104    public CampaignContent getCampaignContentByValue(final ExecutionErrorAccumulator eea, final String campaignContentValue) {
105        return getCampaignContentByValue(eea, campaignContentValue, EntityPermission.READ_ONLY);
106    }
107
108    public CampaignContent getCampaignContentByValueForUpdate(final ExecutionErrorAccumulator eea, final String campaignContentValue) {
109        return getCampaignContentByValue(eea, campaignContentValue, EntityPermission.READ_WRITE);
110    }
111
112    public CampaignContent getCampaignContentByUniversalSpec(final ExecutionErrorAccumulator eea,
113            final CampaignContentUniversalSpec universalSpec, final EntityPermission entityPermission) {
114        CampaignContent campaignContent = null;
115        var campaignContentName = universalSpec.getCampaignContentName();
116        var parameterCount = (campaignContentName == null ? 0 : 1) + entityInstanceLogic.countPossibleEntitySpecs(universalSpec);
117
118        switch(parameterCount) {
119            case 1 -> {
120                if(campaignContentName == null) {
121                    var entityInstance = entityInstanceLogic.getEntityInstance(eea, universalSpec,
122                            ComponentVendors.ECHO_THREE.name(), EntityTypes.CampaignContent.name());
123
124                    if(eea == null || !eea.hasExecutionErrors()) {
125                        campaignContent = campaignControl.getCampaignContentByEntityInstance(entityInstance, entityPermission);
126                    }
127                } else {
128                    campaignContent = getCampaignContentByName(eea, campaignContentName, entityPermission);
129                }
130            }
131            default ->
132                    handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name());
133        }
134
135        return campaignContent;
136    }
137
138    public CampaignContent getCampaignContentByUniversalSpec(final ExecutionErrorAccumulator eea,
139            final CampaignContentUniversalSpec universalSpec) {
140        return getCampaignContentByUniversalSpec(eea, universalSpec, EntityPermission.READ_ONLY);
141    }
142
143    public CampaignContent getCampaignContentByUniversalSpecForUpdate(final ExecutionErrorAccumulator eea,
144            final CampaignContentUniversalSpec universalSpec) {
145        return getCampaignContentByUniversalSpec(eea, universalSpec, EntityPermission.READ_WRITE);
146    }
147
148    public void setCampaignContentStatus(final Session session, ExecutionErrorAccumulator eea, CampaignContent campaignContent, String campaignContentStatusChoice, PartyPK modifiedBy) {
149        var workflow = workflowLogic.getWorkflowByName(eea, CampaignContentStatusConstants.Workflow_CAMPAIGN_CONTENT_STATUS);
150        var entityInstance = entityInstanceControl.getEntityInstanceByBasePK(campaignContent.getPrimaryKey());
151        var workflowEntityStatus = workflowControl.getWorkflowEntityStatusByEntityInstanceForUpdate(workflow, entityInstance);
152        var workflowDestination = campaignContentStatusChoice == null ? null : workflowControl.getWorkflowDestinationByName(workflowEntityStatus.getWorkflowStep(), campaignContentStatusChoice);
153
154        if(workflowDestination != null || campaignContentStatusChoice == null) {
155            var currentWorkflowStepName = workflowEntityStatus.getWorkflowStep().getLastDetail().getWorkflowStepName();
156            var map = workflowDestinationLogic.getWorkflowDestinationsAsMap(workflowDestination);
157            Long triggerTime = null;
158
159            if(currentWorkflowStepName.equals(CampaignContentStatusConstants.WorkflowStep_ACTIVE)) {
160                if(workflowDestinationLogic.workflowDestinationMapContainsStep(map, CampaignContentStatusConstants.Workflow_CAMPAIGN_CONTENT_STATUS, CampaignContentStatusConstants.WorkflowStep_INACTIVE)) {
161                    // Nothing at this time.
162                }
163            } else if(currentWorkflowStepName.equals(CampaignContentStatusConstants.WorkflowStep_INACTIVE)) {
164                if(workflowDestinationLogic.workflowDestinationMapContainsStep(map, CampaignContentStatusConstants.Workflow_CAMPAIGN_CONTENT_STATUS, CampaignContentStatusConstants.WorkflowStep_ACTIVE)) {
165                    // Nothing at this time.
166                }
167            }
168
169            if(eea == null || !eea.hasExecutionErrors()) {
170                workflowControl.transitionEntityInWorkflow(eea, workflowEntityStatus, workflowDestination, triggerTime, modifiedBy);
171            }
172        } else {
173            handleExecutionError(UnknownCampaignContentStatusChoiceException.class, eea, ExecutionErrors.UnknownCampaignContentStatusChoice.name(), campaignContentStatusChoice);
174        }
175    }
176    
177}