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.CampaignUniversalSpec; 020import com.echothree.model.control.campaign.common.exception.UnknownCampaignNameException; 021import com.echothree.model.control.campaign.common.exception.UnknownCampaignStatusChoiceException; 022import com.echothree.model.control.campaign.common.exception.UnknownCampaignValueException; 023import com.echothree.model.control.campaign.common.workflow.CampaignStatusConstants; 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.Campaign; 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 CampaignLogic 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 CampaignLogic() { 066 super(); 067 } 068 069 public Campaign getCampaignByName(final ExecutionErrorAccumulator eea, final String campaignName, 070 final EntityPermission entityPermission) { 071 var campaign = campaignControl.getCampaignByName(campaignName, entityPermission); 072 073 if(campaign == null) { 074 handleExecutionError(UnknownCampaignNameException.class, eea, ExecutionErrors.UnknownCampaignName.name(), campaignName); 075 } 076 077 return campaign; 078 } 079 080 public Campaign getCampaignByName(final ExecutionErrorAccumulator eea, final String campaignName) { 081 return getCampaignByName(eea, campaignName, EntityPermission.READ_ONLY); 082 } 083 084 public Campaign getCampaignByNameForUpdate(final ExecutionErrorAccumulator eea, final String campaignName) { 085 return getCampaignByName(eea, campaignName, EntityPermission.READ_WRITE); 086 } 087 088 public Campaign getCampaignByValue(final ExecutionErrorAccumulator eea, final String campaignValue, 089 final EntityPermission entityPermission) { 090 var campaign = campaignControl.getCampaignByValue(campaignValue, entityPermission); 091 092 if(campaign == null) { 093 handleExecutionError(UnknownCampaignValueException.class, eea, ExecutionErrors.UnknownCampaignValue.name(), campaignValue); 094 } 095 096 return campaign; 097 } 098 099 public Campaign getCampaignByValue(final ExecutionErrorAccumulator eea, final String campaignValue) { 100 return getCampaignByValue(eea, campaignValue, EntityPermission.READ_ONLY); 101 } 102 103 public Campaign getCampaignByValueForUpdate(final ExecutionErrorAccumulator eea, final String campaignValue) { 104 return getCampaignByValue(eea, campaignValue, EntityPermission.READ_WRITE); 105 } 106 107 public Campaign getCampaignByUniversalSpec(final ExecutionErrorAccumulator eea, 108 final CampaignUniversalSpec universalSpec, final EntityPermission entityPermission) { 109 Campaign campaign = null; 110 var campaignName = universalSpec.getCampaignName(); 111 var parameterCount = (campaignName == null ? 0 : 1) + entityInstanceLogic.countPossibleEntitySpecs(universalSpec); 112 113 switch(parameterCount) { 114 case 1 -> { 115 if(campaignName == null) { 116 var entityInstance = entityInstanceLogic.getEntityInstance(eea, universalSpec, 117 ComponentVendors.ECHO_THREE.name(), EntityTypes.Campaign.name()); 118 119 if(eea == null || !eea.hasExecutionErrors()) { 120 campaign = campaignControl.getCampaignByEntityInstance(entityInstance, entityPermission); 121 } 122 } else { 123 campaign = getCampaignByName(eea, campaignName, entityPermission); 124 } 125 } 126 default -> 127 handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name()); 128 } 129 130 return campaign; 131 } 132 133 public Campaign getCampaignByUniversalSpec(final ExecutionErrorAccumulator eea, 134 final CampaignUniversalSpec universalSpec) { 135 return getCampaignByUniversalSpec(eea, universalSpec, EntityPermission.READ_ONLY); 136 } 137 138 public Campaign getCampaignByUniversalSpecForUpdate(final ExecutionErrorAccumulator eea, 139 final CampaignUniversalSpec universalSpec) { 140 return getCampaignByUniversalSpec(eea, universalSpec, EntityPermission.READ_WRITE); 141 } 142 143 public void setCampaignStatus(final Session session, ExecutionErrorAccumulator eea, Campaign campaign, String campaignStatusChoice, PartyPK modifiedBy) { 144 var workflow = workflowLogic.getWorkflowByName(eea, CampaignStatusConstants.Workflow_CAMPAIGN_STATUS); 145 var entityInstance = entityInstanceControl.getEntityInstanceByBasePK(campaign.getPrimaryKey()); 146 var workflowEntityStatus = workflowControl.getWorkflowEntityStatusByEntityInstanceForUpdate(workflow, entityInstance); 147 var workflowDestination = campaignStatusChoice == null ? null : workflowControl.getWorkflowDestinationByName(workflowEntityStatus.getWorkflowStep(), campaignStatusChoice); 148 149 if(workflowDestination != null || campaignStatusChoice == null) { 150 var currentWorkflowStepName = workflowEntityStatus.getWorkflowStep().getLastDetail().getWorkflowStepName(); 151 var map = workflowDestinationLogic.getWorkflowDestinationsAsMap(workflowDestination); 152 Long triggerTime = null; 153 154 if(currentWorkflowStepName.equals(CampaignStatusConstants.WorkflowStep_ACTIVE)) { 155 if(workflowDestinationLogic.workflowDestinationMapContainsStep(map, CampaignStatusConstants.Workflow_CAMPAIGN_STATUS, CampaignStatusConstants.WorkflowStep_INACTIVE)) { 156 // Nothing at this time. 157 } 158 } else if(currentWorkflowStepName.equals(CampaignStatusConstants.WorkflowStep_INACTIVE)) { 159 if(workflowDestinationLogic.workflowDestinationMapContainsStep(map, CampaignStatusConstants.Workflow_CAMPAIGN_STATUS, CampaignStatusConstants.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(UnknownCampaignStatusChoiceException.class, eea, ExecutionErrors.UnknownCampaignStatusChoice.name(), campaignStatusChoice); 169 } 170 } 171 172}