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.customer.server.logic; 018 019import com.echothree.control.user.core.common.spec.UniversalEntitySpec; 020import com.echothree.model.control.core.common.ComponentVendors; 021import com.echothree.model.control.core.common.EntityTypes; 022import com.echothree.model.control.core.common.exception.InvalidParameterCountException; 023import com.echothree.model.control.core.server.control.EntityInstanceControl; 024import com.echothree.model.control.core.server.logic.EntityInstanceLogic; 025import com.echothree.model.control.customer.common.exception.UnknownCustomerNameException; 026import com.echothree.model.control.customer.common.exception.UnknownCustomerStatusChoiceException; 027import com.echothree.model.control.customer.common.workflow.CustomerStatusConstants; 028import com.echothree.model.control.customer.server.control.CustomerControl; 029import com.echothree.model.control.party.common.PartyTypes; 030import com.echothree.model.control.party.common.exception.UnknownPartyNameException; 031import com.echothree.model.control.party.server.control.PartyControl; 032import com.echothree.model.control.party.server.logic.PartyLogic; 033import com.echothree.model.control.workflow.server.control.WorkflowControl; 034import com.echothree.model.control.workflow.server.logic.WorkflowDestinationLogic; 035import com.echothree.model.control.workflow.server.logic.WorkflowLogic; 036import com.echothree.model.data.customer.server.entity.Customer; 037import com.echothree.model.data.party.common.pk.PartyPK; 038import com.echothree.model.data.party.server.entity.Party; 039import com.echothree.util.common.message.ExecutionErrors; 040import com.echothree.util.server.control.BaseLogic; 041import com.echothree.util.server.message.ExecutionErrorAccumulator; 042import com.echothree.util.server.persistence.Session; 043import javax.enterprise.context.ApplicationScoped; 044import javax.enterprise.inject.spi.CDI; 045import javax.inject.Inject; 046 047@ApplicationScoped 048public class CustomerLogic 049 extends BaseLogic { 050 051 @Inject 052 CustomerControl customerControl; 053 054 @Inject 055 EntityInstanceControl entityInstanceControl; 056 057 @Inject 058 PartyControl partyControl; 059 060 @Inject 061 WorkflowControl workflowControl; 062 063 @Inject 064 EntityInstanceLogic entityInstanceLogic; 065 066 @Inject 067 PartyLogic partyLogic; 068 069 @Inject 070 WorkflowDestinationLogic workflowDestinationLogic; 071 072 @Inject 073 WorkflowLogic workflowLogic; 074 075 protected CustomerLogic() { 076 super(); 077 } 078 079 public static CustomerLogic getInstance() { 080 return CDI.current().select(CustomerLogic.class).get(); 081 } 082 083 public Customer getCustomerByName(final ExecutionErrorAccumulator eea, final String customerName, final String partyName, 084 final UniversalEntitySpec universalEntitySpec) { 085 var parameterCount = (customerName == null ? 0 : 1) + (partyName == null ? 0 : 1) + 086 entityInstanceLogic.countPossibleEntitySpecs(universalEntitySpec); 087 Customer customer = null; 088 089 if(parameterCount == 1) { 090 if(customerName != null) { 091 customer = customerControl.getCustomerByName(customerName); 092 093 if(customer == null) { 094 handleExecutionError(UnknownCustomerNameException.class, eea, ExecutionErrors.UnknownCustomerName.name(), customerName); 095 } 096 } else if(partyName != null) { 097 var party = partyControl.getPartyByName(partyName); 098 099 if(party != null) { 100 partyLogic.checkPartyType(eea, party, PartyTypes.CUSTOMER.name()); 101 102 customer = customerControl.getCustomer(party); 103 } else { 104 handleExecutionError(UnknownPartyNameException.class, eea, ExecutionErrors.UnknownPartyName.name(), partyName); 105 } 106 } else if(universalEntitySpec != null) { 107 var entityInstance = entityInstanceLogic.getEntityInstance(eea, universalEntitySpec, 108 ComponentVendors.ECHO_THREE.name(), EntityTypes.Party.name()); 109 110 if(eea == null || !eea.hasExecutionErrors()) { 111 var party = partyControl.getPartyByEntityInstance(entityInstance); 112 113 partyLogic.checkPartyType(eea, party, PartyTypes.CUSTOMER.name()); 114 115 customer = customerControl.getCustomer(party); 116 } 117 } 118 } else { 119 handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name()); 120 } 121 122 return customer; 123 } 124 125 public void setCustomerStatus(final Session session, final ExecutionErrorAccumulator eea, final Party party, final String customerStatusChoice, final PartyPK modifiedBy) { 126 var workflow = workflowLogic.getWorkflowByName(eea, CustomerStatusConstants.Workflow_CUSTOMER_STATUS); 127 var entityInstance = entityInstanceControl.getEntityInstanceByBasePK(party.getPrimaryKey()); 128 var workflowEntityStatus = workflowControl.getWorkflowEntityStatusByEntityInstanceForUpdate(workflow, entityInstance); 129 var workflowDestination = customerStatusChoice == null ? null : workflowControl.getWorkflowDestinationByName(workflowEntityStatus.getWorkflowStep(), customerStatusChoice); 130 131 if(workflowDestination != null || customerStatusChoice == null) { 132 var currentWorkflowStepName = workflowEntityStatus.getWorkflowStep().getLastDetail().getWorkflowStepName(); 133 var map = workflowDestinationLogic.getWorkflowDestinationsAsMap(workflowDestination); 134 Long triggerTime = null; 135 136 if(currentWorkflowStepName.equals(CustomerStatusConstants.WorkflowStep_VISITOR)) { 137 if(workflowDestinationLogic.workflowDestinationMapContainsStep(map, CustomerStatusConstants.Workflow_CUSTOMER_STATUS, CustomerStatusConstants.WorkflowStep_SHOPPER)) { 138 // Nothing at this time. 139 } 140 } else if(currentWorkflowStepName.equals(CustomerStatusConstants.WorkflowStep_SHOPPER)) { 141 // Nothing at this time. 142 } 143 144 if(eea == null || !eea.hasExecutionErrors()) { 145 workflowControl.transitionEntityInWorkflow(eea, workflowEntityStatus, workflowDestination, triggerTime, modifiedBy); 146 } 147 } else { 148 handleExecutionError(UnknownCustomerStatusChoiceException.class, eea, ExecutionErrors.UnknownCustomerStatusChoice.name(), customerStatusChoice); 149 } 150 } 151 152}