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