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.employee.server.logic;
018
019import com.echothree.control.user.employee.common.spec.TerminationReasonDescriptionSpec;
020import com.echothree.model.control.employee.common.exception.UnknownTerminationReasonDescriptionException;
021import com.echothree.model.control.employee.common.exception.UnknownTerminationReasonNameException;
022import com.echothree.model.control.employee.server.control.EmployeeControl;
023import com.echothree.model.control.party.server.logic.LanguageLogic;
024import com.echothree.model.data.employee.server.entity.TerminationReason;
025import com.echothree.model.data.employee.server.entity.TerminationReasonDescription;
026import com.echothree.util.common.message.ExecutionErrors;
027import com.echothree.util.server.control.BaseLogic;
028import com.echothree.util.server.message.ExecutionErrorAccumulator;
029import com.echothree.util.server.persistence.EntityPermission;
030import javax.enterprise.context.ApplicationScoped;
031import javax.inject.Inject;
032
033@ApplicationScoped
034public class TerminationReasonLogic
035        extends BaseLogic {
036
037    @Inject
038    EmployeeControl employeeControl;
039
040    @Inject
041    LanguageLogic languageLogic;
042
043    protected TerminationReasonLogic() {
044        super();
045    }
046    
047    public TerminationReason getTerminationReasonByName(final ExecutionErrorAccumulator eea, final String terminationReasonName,
048            final EntityPermission entityPermission) {
049        var terminationReason = employeeControl.getTerminationReasonByName(terminationReasonName, entityPermission);
050
051        if(terminationReason == null) {
052            handleExecutionError(UnknownTerminationReasonNameException.class, eea, ExecutionErrors.UnknownTerminationReasonName.name(), terminationReasonName);
053        }
054
055        return terminationReason;
056    }
057    
058    public TerminationReason getTerminationReasonByName(final ExecutionErrorAccumulator eea, final String terminationReasonName) {
059        return getTerminationReasonByName(eea, terminationReasonName, EntityPermission.READ_ONLY);
060    }
061
062    public TerminationReason getTerminationReasonByNameForUpdate(final ExecutionErrorAccumulator eea, final String terminationReasonName) {
063        return getTerminationReasonByName(eea, terminationReasonName, EntityPermission.READ_WRITE);
064    }
065
066    public TerminationReasonDescription getTerminationReasonDescriptionBySpec(final ExecutionErrorAccumulator eea,
067            final TerminationReasonDescriptionSpec spec, final EntityPermission entityPermission) {
068        TerminationReasonDescription terminationReasonDescription = null;
069        var terminationReasonName = spec.getTerminationReasonName();
070        var terminationReason = getTerminationReasonByName(eea, terminationReasonName);
071
072        if(!hasExecutionErrors(eea)) {
073            var languageIsoName = spec.getLanguageIsoName();
074            var language = languageLogic.getLanguageByName(eea, languageIsoName);
075
076            if(!hasExecutionErrors(eea)) {
077                terminationReasonDescription = employeeControl.getTerminationReasonDescription(terminationReason, language,
078                        entityPermission);
079
080                if(terminationReasonDescription == null) {
081                    handleExecutionError(UnknownTerminationReasonDescriptionException.class, eea, ExecutionErrors.UnknownTerminationReasonDescription.name(), terminationReasonName, languageIsoName);
082                }
083            }
084        }
085
086        return terminationReasonDescription;
087    }
088
089}