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.party.server.logic;
018
019import com.echothree.model.control.core.common.exception.InvalidParameterCountException;
020import com.echothree.model.control.core.server.control.CoreControl;
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.core.server.entity.EntityInstance;
034import com.echothree.model.data.employee.server.entity.PartyEmployee;
035import com.echothree.model.data.party.common.pk.PartyPK;
036import com.echothree.model.data.party.server.entity.Party;
037import com.echothree.model.data.workflow.server.entity.Workflow;
038import com.echothree.model.data.workflow.server.entity.WorkflowDestination;
039import com.echothree.model.data.workflow.server.entity.WorkflowEntityStatus;
040import com.echothree.util.common.message.ExecutionErrors;
041import com.echothree.util.server.control.BaseLogic;
042import com.echothree.util.server.message.ExecutionErrorAccumulator;
043import com.echothree.util.server.persistence.Session;
044import java.util.Map;
045import java.util.Set;
046
047public class EmployeeLogic
048        extends BaseLogic {
049    
050    private EmployeeLogic() {
051        super();
052    }
053    
054    private static class EmployeeLogicHolder {
055        static EmployeeLogic instance = new EmployeeLogic();
056    }
057    
058    public static EmployeeLogic getInstance() {
059        return EmployeeLogicHolder.instance;
060    }
061
062    public PartyEmployee getPartyEmployeeByName(final ExecutionErrorAccumulator eea, final String employeeName, final String partyName) {
063        var parameterCount = (employeeName == null ? 0 : 1) + (partyName == null ? 0 : 1);
064        PartyEmployee partyEmployee = null;
065
066        if(parameterCount == 1) {
067            var employeeControl = Session.getModelController(EmployeeControl.class);
068
069            if(employeeName != null) {
070                partyEmployee = employeeControl.getPartyEmployeeByName(employeeName);
071
072                if(partyEmployee == null) {
073                    handleExecutionError(UnknownEmployeeNameException.class, eea, ExecutionErrors.UnknownEmployeeName.name(), employeeName);
074                }
075            } else {
076                var partyControl = Session.getModelController(PartyControl.class);
077                Party party = partyControl.getPartyByName(partyName);
078
079                if(party != null) {
080                    PartyLogic.getInstance().checkPartyType(eea, party, PartyTypes.EMPLOYEE.name());
081
082                    partyEmployee = employeeControl.getPartyEmployee(party);
083                } else {
084                    handleExecutionError(UnknownPartyNameException.class, eea, ExecutionErrors.UnknownPartyName.name(), partyName);
085                }
086            }
087        } else {
088            handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name());
089        }
090
091        return partyEmployee;
092    }
093
094    public void setEmployeeStatus(final Session session, ExecutionErrorAccumulator eea, Party party, String employeeStatusChoice, PartyPK modifiedBy) {
095        var coreControl = Session.getModelController(CoreControl.class);
096        var workflowControl = Session.getModelController(WorkflowControl.class);
097        var workflow = WorkflowLogic.getInstance().getWorkflowByName(eea, EmployeeStatusConstants.Workflow_EMPLOYEE_STATUS);
098        EntityInstance entityInstance = coreControl.getEntityInstanceByBasePK(party.getPrimaryKey());
099        WorkflowEntityStatus workflowEntityStatus = workflowControl.getWorkflowEntityStatusByEntityInstanceForUpdate(workflow, entityInstance);
100        WorkflowDestination workflowDestination = employeeStatusChoice == null ? null : workflowControl.getWorkflowDestinationByName(workflowEntityStatus.getWorkflowStep(), employeeStatusChoice);
101
102        if(workflowDestination != null || employeeStatusChoice == null) {
103            var workflowDestinationLogic = WorkflowDestinationLogic.getInstance();
104            String currentWorkflowStepName = workflowEntityStatus.getWorkflowStep().getLastDetail().getWorkflowStepName();
105            Map<String, Set<String>> map = workflowDestinationLogic.getWorkflowDestinationsAsMap(workflowDestination);
106            Long triggerTime = null;
107
108            if(currentWorkflowStepName.equals(EmployeeStatusConstants.WorkflowStep_ACTIVE)) {
109                if(workflowDestinationLogic.workflowDestinationMapContainsStep(map, EmployeeStatusConstants.Workflow_EMPLOYEE_STATUS, EmployeeStatusConstants.WorkflowStep_INACTIVE)) {
110                    UserKeyLogic.getInstance().clearUserKeysByParty(party);
111                    UserSessionLogic.getInstance().deleteUserSessionsByParty(party);
112                }
113            } else if(currentWorkflowStepName.equals(EmployeeStatusConstants.WorkflowStep_INACTIVE)) {
114                if(workflowDestinationLogic.workflowDestinationMapContainsStep(map, EmployeeStatusConstants.Workflow_EMPLOYEE_STATUS, EmployeeStatusConstants.WorkflowStep_ACTIVE)) {
115                    // Nothing at this time.
116                }
117            }
118
119            if(eea == null || !eea.hasExecutionErrors()) {
120                workflowControl.transitionEntityInWorkflow(eea, workflowEntityStatus, workflowDestination, triggerTime, modifiedBy);
121            }
122        } else {
123            handleExecutionError(UnknownEmployeeStatusChoiceException.class, eea, ExecutionErrors.UnknownEmployeeStatusChoice.name(), employeeStatusChoice);
124        }
125    }
126
127}