001// -------------------------------------------------------------------------------- 002// Copyright 2002-2025 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; 045 046@ApplicationScoped 047public class CustomerLogic 048 extends BaseLogic { 049 050 protected CustomerLogic() { 051 super(); 052 } 053 054 public static CustomerLogic getInstance() { 055 return CDI.current().select(CustomerLogic.class).get(); 056 } 057 058 public Customer getCustomerByName(final ExecutionErrorAccumulator eea, final String customerName, final String partyName, 059 final UniversalEntitySpec universalEntitySpec) { 060 var parameterCount = (customerName == null ? 0 : 1) + (partyName == null ? 0 : 1) + 061 EntityInstanceLogic.getInstance().countPossibleEntitySpecs(universalEntitySpec); 062 Customer customer = null; 063 064 if(parameterCount == 1) { 065 var customerControl = Session.getModelController(CustomerControl.class); 066 var partyControl = Session.getModelController(PartyControl.class); 067 068 if(customerName != null) { 069 customer = customerControl.getCustomerByName(customerName); 070 071 if(customer == null) { 072 handleExecutionError(UnknownCustomerNameException.class, eea, ExecutionErrors.UnknownCustomerName.name(), customerName); 073 } 074 } else if(partyName != null) { 075 var party = partyControl.getPartyByName(partyName); 076 077 if(party != null) { 078 PartyLogic.getInstance().checkPartyType(eea, party, PartyTypes.CUSTOMER.name()); 079 080 customer = customerControl.getCustomer(party); 081 } else { 082 handleExecutionError(UnknownPartyNameException.class, eea, ExecutionErrors.UnknownPartyName.name(), partyName); 083 } 084 } else if(universalEntitySpec != null) { 085 var entityInstance = EntityInstanceLogic.getInstance().getEntityInstance(eea, universalEntitySpec, 086 ComponentVendors.ECHO_THREE.name(), EntityTypes.Party.name()); 087 088 if(!eea.hasExecutionErrors()) { 089 var party = partyControl.getPartyByEntityInstance(entityInstance); 090 091 PartyLogic.getInstance().checkPartyType(eea, party, PartyTypes.CUSTOMER.name()); 092 093 customer = customerControl.getCustomer(party); 094 } 095 } 096 } else { 097 handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name()); 098 } 099 100 return customer; 101 } 102 103 public void setCustomerStatus(final Session session, final ExecutionErrorAccumulator eea, final Party party, final String customerStatusChoice, final PartyPK modifiedBy) { 104 var entityInstanceControl = Session.getModelController(EntityInstanceControl.class); 105 var workflowControl = Session.getModelController(WorkflowControl.class); 106 var workflow = WorkflowLogic.getInstance().getWorkflowByName(eea, CustomerStatusConstants.Workflow_CUSTOMER_STATUS); 107 var entityInstance = entityInstanceControl.getEntityInstanceByBasePK(party.getPrimaryKey()); 108 var workflowEntityStatus = workflowControl.getWorkflowEntityStatusByEntityInstanceForUpdate(workflow, entityInstance); 109 var workflowDestination = customerStatusChoice == null ? null : workflowControl.getWorkflowDestinationByName(workflowEntityStatus.getWorkflowStep(), customerStatusChoice); 110 111 if(workflowDestination != null || customerStatusChoice == null) { 112 var workflowDestinationLogic = WorkflowDestinationLogic.getInstance(); 113 var currentWorkflowStepName = workflowEntityStatus.getWorkflowStep().getLastDetail().getWorkflowStepName(); 114 var map = workflowDestinationLogic.getWorkflowDestinationsAsMap(workflowDestination); 115 Long triggerTime = null; 116 117 if(currentWorkflowStepName.equals(CustomerStatusConstants.WorkflowStep_VISITOR)) { 118 if(workflowDestinationLogic.workflowDestinationMapContainsStep(map, CustomerStatusConstants.Workflow_CUSTOMER_STATUS, CustomerStatusConstants.WorkflowStep_SHOPPER)) { 119 // Nothing at this time. 120 } 121 } else if(currentWorkflowStepName.equals(CustomerStatusConstants.WorkflowStep_SHOPPER)) { 122 // Nothing at this time. 123 } 124 125 if(eea == null || !eea.hasExecutionErrors()) { 126 workflowControl.transitionEntityInWorkflow(eea, workflowEntityStatus, workflowDestination, triggerTime, modifiedBy); 127 } 128 } else { 129 handleExecutionError(UnknownCustomerStatusChoiceException.class, eea, ExecutionErrors.UnknownCustomerStatusChoice.name(), customerStatusChoice); 130 } 131 } 132 133}