001// -------------------------------------------------------------------------------- 002// Copyright 2002-2024 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.CoreControl; 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.user.server.logic.UserKeyLogic; 034import com.echothree.model.control.user.server.logic.UserSessionLogic; 035import com.echothree.model.control.workflow.server.control.WorkflowControl; 036import com.echothree.model.control.workflow.server.logic.WorkflowDestinationLogic; 037import com.echothree.model.control.workflow.server.logic.WorkflowLogic; 038import com.echothree.model.data.core.server.entity.EntityInstance; 039import com.echothree.model.data.customer.server.entity.Customer; 040import com.echothree.model.data.party.common.pk.PartyPK; 041import com.echothree.model.data.party.server.entity.Party; 042import com.echothree.model.data.workflow.server.entity.WorkflowDestination; 043import com.echothree.model.data.workflow.server.entity.WorkflowEntityStatus; 044import com.echothree.util.common.message.ExecutionErrors; 045import com.echothree.util.server.control.BaseLogic; 046import com.echothree.util.server.message.ExecutionErrorAccumulator; 047import com.echothree.util.server.persistence.Session; 048import java.util.Map; 049import java.util.Set; 050 051public class CustomerLogic 052 extends BaseLogic { 053 054 private CustomerLogic() { 055 super(); 056 } 057 058 private static class CustomerLogicHolder { 059 static CustomerLogic instance = new CustomerLogic(); 060 } 061 062 public static CustomerLogic getInstance() { 063 return CustomerLogicHolder.instance; 064 } 065 066 public Customer getCustomerByName(final ExecutionErrorAccumulator eea, final String customerName, final String partyName, 067 final UniversalEntitySpec universalEntitySpec) { 068 var parameterCount = (customerName == null ? 0 : 1) + (partyName == null ? 0 : 1) + 069 EntityInstanceLogic.getInstance().countPossibleEntitySpecs(universalEntitySpec); 070 Customer customer = null; 071 072 if(parameterCount == 1) { 073 var customerControl = Session.getModelController(CustomerControl.class); 074 var partyControl = Session.getModelController(PartyControl.class); 075 076 if(customerName != null) { 077 customer = customerControl.getCustomerByName(customerName); 078 079 if(customer == null) { 080 handleExecutionError(UnknownCustomerNameException.class, eea, ExecutionErrors.UnknownCustomerName.name(), customerName); 081 } 082 } else if(partyName != null) { 083 var party = partyControl.getPartyByName(partyName); 084 085 if(party != null) { 086 PartyLogic.getInstance().checkPartyType(eea, party, PartyTypes.CUSTOMER.name()); 087 088 customer = customerControl.getCustomer(party); 089 } else { 090 handleExecutionError(UnknownPartyNameException.class, eea, ExecutionErrors.UnknownPartyName.name(), partyName); 091 } 092 } else if(universalEntitySpec != null) { 093 var entityInstance = EntityInstanceLogic.getInstance().getEntityInstance(eea, universalEntitySpec, 094 ComponentVendors.ECHO_THREE.name(), EntityTypes.Party.name()); 095 096 if(!eea.hasExecutionErrors()) { 097 var party = partyControl.getPartyByEntityInstance(entityInstance); 098 099 PartyLogic.getInstance().checkPartyType(eea, party, PartyTypes.CUSTOMER.name()); 100 101 customer = customerControl.getCustomer(party); 102 } 103 } 104 } else { 105 handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name()); 106 } 107 108 return customer; 109 } 110 111 public void setCustomerStatus(final Session session, ExecutionErrorAccumulator eea, Party party, String customerStatusChoice, PartyPK modifiedBy) { 112 var coreControl = Session.getModelController(CoreControl.class); 113 var workflowControl = Session.getModelController(WorkflowControl.class); 114 var workflow = WorkflowLogic.getInstance().getWorkflowByName(eea, CustomerStatusConstants.Workflow_CUSTOMER_STATUS); 115 EntityInstance entityInstance = coreControl.getEntityInstanceByBasePK(party.getPrimaryKey()); 116 WorkflowEntityStatus workflowEntityStatus = workflowControl.getWorkflowEntityStatusByEntityInstanceForUpdate(workflow, entityInstance); 117 WorkflowDestination workflowDestination = customerStatusChoice == null ? null : workflowControl.getWorkflowDestinationByName(workflowEntityStatus.getWorkflowStep(), customerStatusChoice); 118 119 if(workflowDestination != null || customerStatusChoice == null) { 120 var workflowDestinationLogic = WorkflowDestinationLogic.getInstance(); 121 String currentWorkflowStepName = workflowEntityStatus.getWorkflowStep().getLastDetail().getWorkflowStepName(); 122 Map<String, Set<String>> map = workflowDestinationLogic.getWorkflowDestinationsAsMap(workflowDestination); 123 Long triggerTime = null; 124 125 if(currentWorkflowStepName.equals(CustomerStatusConstants.WorkflowStep_ACTIVE)) { 126 if(workflowDestinationLogic.workflowDestinationMapContainsStep(map, CustomerStatusConstants.Workflow_CUSTOMER_STATUS, CustomerStatusConstants.WorkflowStep_INACTIVE)) { 127 UserKeyLogic.getInstance().clearUserKeysByParty(party); 128 UserSessionLogic.getInstance().deleteUserSessionsByParty(party); 129 } 130 } else if(currentWorkflowStepName.equals(CustomerStatusConstants.WorkflowStep_INACTIVE)) { 131 if(workflowDestinationLogic.workflowDestinationMapContainsStep(map, CustomerStatusConstants.Workflow_CUSTOMER_STATUS, CustomerStatusConstants.WorkflowStep_ACTIVE)) { 132 // Nothing at this time. 133 } 134 } 135 136 if(eea == null || !eea.hasExecutionErrors()) { 137 workflowControl.transitionEntityInWorkflow(eea, workflowEntityStatus, workflowDestination, triggerTime, modifiedBy); 138 } 139 } else { 140 handleExecutionError(UnknownCustomerStatusChoiceException.class, eea, ExecutionErrors.UnknownCustomerStatusChoice.name(), customerStatusChoice); 141 } 142 } 143 144}