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