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