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.party.server.logic;
018
019import com.echothree.model.control.core.common.exception.InvalidParameterCountException;
020import com.echothree.model.control.core.server.control.EntityInstanceControl;
021import com.echothree.model.control.employee.common.workflow.EmployeeStatusConstants;
022import com.echothree.model.control.employee.server.control.EmployeeControl;
023import com.echothree.model.control.party.common.PartyTypes;
024import com.echothree.model.control.party.common.exception.UnknownEmployeeNameException;
025import com.echothree.model.control.party.common.exception.UnknownEmployeeStatusChoiceException;
026import com.echothree.model.control.party.common.exception.UnknownPartyNameException;
027import com.echothree.model.control.party.server.control.PartyControl;
028import com.echothree.model.control.user.server.logic.UserKeyLogic;
029import com.echothree.model.control.user.server.logic.UserSessionLogic;
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.employee.server.entity.PartyEmployee;
034import com.echothree.model.data.party.common.pk.PartyPK;
035import com.echothree.model.data.party.server.entity.Party;
036import com.echothree.util.common.message.ExecutionErrors;
037import com.echothree.util.server.control.BaseLogic;
038import com.echothree.util.server.message.ExecutionErrorAccumulator;
039import com.echothree.util.server.persistence.Session;
040import javax.enterprise.context.ApplicationScoped;
041import javax.enterprise.inject.spi.CDI;
042import javax.inject.Inject;
043
044@ApplicationScoped
045public class EmployeeLogic
046        extends BaseLogic {
047
048    @Inject
049    EmployeeControl employeeControl;
050
051    @Inject
052    EntityInstanceControl entityInstanceControl;
053
054    @Inject
055    PartyControl partyControl;
056
057    @Inject
058    WorkflowControl workflowControl;
059
060    @Inject
061    PartyLogic partyLogic;
062
063    @Inject
064    UserKeyLogic userKeyLogic;
065
066    @Inject
067    UserSessionLogic userSessionLogic;
068
069    @Inject
070    WorkflowDestinationLogic workflowDestinationLogic;
071
072    @Inject
073    WorkflowLogic workflowLogic;
074
075    protected EmployeeLogic() {
076        super();
077    }
078
079    public static EmployeeLogic getInstance() {
080        return CDI.current().select(EmployeeLogic.class).get();
081    }
082
083    public PartyEmployee getPartyEmployeeByName(final ExecutionErrorAccumulator eea, final String employeeName, final String partyName) {
084        var parameterCount = (employeeName == null ? 0 : 1) + (partyName == null ? 0 : 1);
085        PartyEmployee partyEmployee = null;
086
087        if(parameterCount == 1) {
088            if(employeeName != null) {
089                partyEmployee = employeeControl.getPartyEmployeeByName(employeeName);
090
091                if(partyEmployee == null) {
092                    handleExecutionError(UnknownEmployeeNameException.class, eea, ExecutionErrors.UnknownEmployeeName.name(), employeeName);
093                }
094            } else {
095                var party = partyControl.getPartyByName(partyName);
096
097                if(party != null) {
098                    partyLogic.checkPartyType(eea, party, PartyTypes.EMPLOYEE.name());
099
100                    partyEmployee = employeeControl.getPartyEmployee(party);
101                } else {
102                    handleExecutionError(UnknownPartyNameException.class, eea, ExecutionErrors.UnknownPartyName.name(), partyName);
103                }
104            }
105        } else {
106            handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name());
107        }
108
109        return partyEmployee;
110    }
111
112    public void setEmployeeStatus(final Session session, ExecutionErrorAccumulator eea, Party party, String employeeStatusChoice, PartyPK modifiedBy) {
113        var workflow = workflowLogic.getWorkflowByName(eea, EmployeeStatusConstants.Workflow_EMPLOYEE_STATUS);
114        var entityInstance = entityInstanceControl.getEntityInstanceByBasePK(party.getPrimaryKey());
115        var workflowEntityStatus = workflowControl.getWorkflowEntityStatusByEntityInstanceForUpdate(workflow, entityInstance);
116        var workflowDestination = employeeStatusChoice == null ? null : workflowControl.getWorkflowDestinationByName(workflowEntityStatus.getWorkflowStep(), employeeStatusChoice);
117
118        if(workflowDestination != null || employeeStatusChoice == null) {
119            var currentWorkflowStepName = workflowEntityStatus.getWorkflowStep().getLastDetail().getWorkflowStepName();
120            var map = workflowDestinationLogic.getWorkflowDestinationsAsMap(workflowDestination);
121            Long triggerTime = null;
122
123            if(currentWorkflowStepName.equals(EmployeeStatusConstants.WorkflowStep_ACTIVE)) {
124                if(workflowDestinationLogic.workflowDestinationMapContainsStep(map, EmployeeStatusConstants.Workflow_EMPLOYEE_STATUS, EmployeeStatusConstants.WorkflowStep_INACTIVE)) {
125                    userKeyLogic.clearUserKeysByParty(party);
126                    userSessionLogic.deleteUserSessionsByParty(party);
127                }
128            } else if(currentWorkflowStepName.equals(EmployeeStatusConstants.WorkflowStep_INACTIVE)) {
129                if(workflowDestinationLogic.workflowDestinationMapContainsStep(map, EmployeeStatusConstants.Workflow_EMPLOYEE_STATUS, EmployeeStatusConstants.WorkflowStep_ACTIVE)) {
130                    // Nothing at this time.
131                }
132            }
133
134            if(eea == null || !eea.hasExecutionErrors()) {
135                workflowControl.transitionEntityInWorkflow(eea, workflowEntityStatus, workflowDestination, triggerTime, modifiedBy);
136            }
137        } else {
138            handleExecutionError(UnknownEmployeeStatusChoiceException.class, eea, ExecutionErrors.UnknownEmployeeStatusChoice.name(), employeeStatusChoice);
139        }
140    }
141
142}