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