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.inject.Inject; 042 043@ApplicationScoped 044public class CampaignContentLogic 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 CampaignContentLogic() { 066 super(); 067 } 068 069 public CampaignContent getCampaignContentByName(final ExecutionErrorAccumulator eea, final String campaignContentName, 070 final EntityPermission entityPermission) { 071 var campaignContent = campaignControl.getCampaignContentByName(campaignContentName, entityPermission); 072 073 if(campaignContent == null) { 074 handleExecutionError(UnknownCampaignContentNameException.class, eea, ExecutionErrors.UnknownCampaignContentName.name(), campaignContentName); 075 } 076 077 return campaignContent; 078 } 079 080 public CampaignContent getCampaignContentByName(final ExecutionErrorAccumulator eea, final String campaignContentName) { 081 return getCampaignContentByName(eea, campaignContentName, EntityPermission.READ_ONLY); 082 } 083 084 public CampaignContent getCampaignContentByNameForUpdate(final ExecutionErrorAccumulator eea, final String campaignContentName) { 085 return getCampaignContentByName(eea, campaignContentName, EntityPermission.READ_WRITE); 086 } 087 088 public CampaignContent getCampaignContentByValue(final ExecutionErrorAccumulator eea, final String campaignContentValue, 089 final EntityPermission entityPermission) { 090 var campaignContent = campaignControl.getCampaignContentByValue(campaignContentValue, entityPermission); 091 092 if(campaignContent == null) { 093 handleExecutionError(UnknownCampaignContentValueException.class, eea, ExecutionErrors.UnknownCampaignContentValue.name(), campaignContentValue); 094 } 095 096 return campaignContent; 097 } 098 099 public CampaignContent getCampaignContentByValue(final ExecutionErrorAccumulator eea, final String campaignContentValue) { 100 return getCampaignContentByValue(eea, campaignContentValue, EntityPermission.READ_ONLY); 101 } 102 103 public CampaignContent getCampaignContentByValueForUpdate(final ExecutionErrorAccumulator eea, final String campaignContentValue) { 104 return getCampaignContentByValue(eea, campaignContentValue, EntityPermission.READ_WRITE); 105 } 106 107 public CampaignContent getCampaignContentByUniversalSpec(final ExecutionErrorAccumulator eea, 108 final CampaignContentUniversalSpec universalSpec, final EntityPermission entityPermission) { 109 CampaignContent campaignContent = null; 110 var campaignContentName = universalSpec.getCampaignContentName(); 111 var parameterCount = (campaignContentName == null ? 0 : 1) + entityInstanceLogic.countPossibleEntitySpecs(universalSpec); 112 113 switch(parameterCount) { 114 case 1 -> { 115 if(campaignContentName == null) { 116 var entityInstance = entityInstanceLogic.getEntityInstance(eea, universalSpec, 117 ComponentVendors.ECHO_THREE.name(), EntityTypes.CampaignContent.name()); 118 119 if(eea == null || !eea.hasExecutionErrors()) { 120 campaignContent = campaignControl.getCampaignContentByEntityInstance(entityInstance, entityPermission); 121 } 122 } else { 123 campaignContent = getCampaignContentByName(eea, campaignContentName, entityPermission); 124 } 125 } 126 default -> 127 handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name()); 128 } 129 130 return campaignContent; 131 } 132 133 public CampaignContent getCampaignContentByUniversalSpec(final ExecutionErrorAccumulator eea, 134 final CampaignContentUniversalSpec universalSpec) { 135 return getCampaignContentByUniversalSpec(eea, universalSpec, EntityPermission.READ_ONLY); 136 } 137 138 public CampaignContent getCampaignContentByUniversalSpecForUpdate(final ExecutionErrorAccumulator eea, 139 final CampaignContentUniversalSpec universalSpec) { 140 return getCampaignContentByUniversalSpec(eea, universalSpec, EntityPermission.READ_WRITE); 141 } 142 143 public void setCampaignContentStatus(final Session session, ExecutionErrorAccumulator eea, CampaignContent campaignContent, String campaignContentStatusChoice, PartyPK modifiedBy) { 144 var workflow = workflowLogic.getWorkflowByName(eea, CampaignContentStatusConstants.Workflow_CAMPAIGN_CONTENT_STATUS); 145 var entityInstance = entityInstanceControl.getEntityInstanceByBasePK(campaignContent.getPrimaryKey()); 146 var workflowEntityStatus = workflowControl.getWorkflowEntityStatusByEntityInstanceForUpdate(workflow, entityInstance); 147 var workflowDestination = campaignContentStatusChoice == null ? null : workflowControl.getWorkflowDestinationByName(workflowEntityStatus.getWorkflowStep(), campaignContentStatusChoice); 148 149 if(workflowDestination != null || campaignContentStatusChoice == null) { 150 var currentWorkflowStepName = workflowEntityStatus.getWorkflowStep().getLastDetail().getWorkflowStepName(); 151 var map = workflowDestinationLogic.getWorkflowDestinationsAsMap(workflowDestination); 152 Long triggerTime = null; 153 154 if(currentWorkflowStepName.equals(CampaignContentStatusConstants.WorkflowStep_ACTIVE)) { 155 if(workflowDestinationLogic.workflowDestinationMapContainsStep(map, CampaignContentStatusConstants.Workflow_CAMPAIGN_CONTENT_STATUS, CampaignContentStatusConstants.WorkflowStep_INACTIVE)) { 156 // Nothing at this time. 157 } 158 } else if(currentWorkflowStepName.equals(CampaignContentStatusConstants.WorkflowStep_INACTIVE)) { 159 if(workflowDestinationLogic.workflowDestinationMapContainsStep(map, CampaignContentStatusConstants.Workflow_CAMPAIGN_CONTENT_STATUS, CampaignContentStatusConstants.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(UnknownCampaignContentStatusChoiceException.class, eea, ExecutionErrors.UnknownCampaignContentStatusChoice.name(), campaignContentStatusChoice); 169 } 170 } 171 172}