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.control.user.core.common.spec.UniversalEntitySpec;
020import com.echothree.model.control.core.common.ComponentVendors;
021import com.echothree.model.control.core.common.EntityTypes;
022import com.echothree.model.control.core.common.exception.InvalidParameterCountException;
023import com.echothree.model.control.core.server.logic.EntityInstanceLogic;
024import com.echothree.model.control.party.common.PartyTypes;
025import com.echothree.model.control.party.common.exception.UnknownDepartmentNameException;
026import com.echothree.model.control.party.common.exception.UnknownPartyNameException;
027import com.echothree.model.control.party.common.exception.UseOfDepartmentNameRequiresDivisionNameException;
028import com.echothree.model.control.party.server.control.PartyControl;
029import com.echothree.model.data.party.server.entity.Party;
030import com.echothree.model.data.party.server.entity.PartyDepartment;
031import com.echothree.util.common.message.ExecutionErrors;
032import com.echothree.util.server.control.BaseLogic;
033import com.echothree.util.server.message.ExecutionErrorAccumulator;
034import javax.enterprise.context.ApplicationScoped;
035import javax.inject.Inject;
036
037@ApplicationScoped
038public class DepartmentLogic
039        extends BaseLogic {
040
041    @Inject
042    PartyControl partyControl;
043
044    @Inject
045    DivisionLogic divisionLogic;
046
047    @Inject
048    EntityInstanceLogic entityInstanceLogic;
049
050    @Inject
051    PartyLogic partyLogic;
052
053    protected DepartmentLogic() {
054        super();
055    }
056
057    public PartyDepartment getPartyDepartmentByName(final ExecutionErrorAccumulator eea, final String companyName,
058            final String divisionName, final String departmentName, final String partyName,
059            final UniversalEntitySpec universalEntitySpec, final boolean required) {
060        PartyDepartment partyDepartment = null;
061        Party divisionParty = null;
062
063        if(companyName != null || divisionName != null) {
064            var partyDivision = divisionLogic.getPartyDivisionByName(eea, companyName,
065                    divisionName, null, null, true);
066
067            divisionParty = hasExecutionErrors(eea) ? null : partyDivision.getParty();
068        }
069
070        if(!hasExecutionErrors(eea)) {
071            partyDepartment = getPartyDepartmentByName(eea, divisionParty, departmentName, partyName, universalEntitySpec,
072                    required);
073        }
074
075        return partyDepartment;
076    }
077
078    public PartyDepartment getPartyDepartmentByName(final ExecutionErrorAccumulator eea, final Party divisionParty,
079            final String departmentName, final String partyName, final UniversalEntitySpec universalEntitySpec,
080            final boolean required) {
081        var parameterCount = (departmentName == null ? 0 : 1) + (partyName == null ? 0 : 1) +
082                entityInstanceLogic.countPossibleEntitySpecs(universalEntitySpec);
083        PartyDepartment partyDepartment = null;
084
085        if(divisionParty != null) {
086            partyLogic.checkPartyType(eea, divisionParty, PartyTypes.DIVISION.name());
087        }
088
089        if(!hasExecutionErrors(eea)) {
090            if(parameterCount == 1) {
091                if(departmentName != null) {
092                    if(divisionParty == null) {
093                        handleExecutionError(UseOfDepartmentNameRequiresDivisionNameException.class, eea, ExecutionErrors.UseOfDepartmentNameRequiresDivisionName.name());
094                    } else {
095                        partyDepartment = partyControl.getPartyDepartmentByName(divisionParty, departmentName);
096
097                        if(partyDepartment == null) {
098                            handleExecutionError(UnknownDepartmentNameException.class, eea, ExecutionErrors.UnknownDepartmentName.name(), departmentName);
099                        }
100                    }
101                } else {
102                    // Use of partyName or universalEntitySpec cannot include a divisionParty.
103                    if(divisionParty == null) {
104                        if(partyName != null) {
105                            var party = partyControl.getPartyByName(partyName);
106
107                            if(party != null) {
108                                partyLogic.checkPartyType(eea, party, PartyTypes.DEPARTMENT.name());
109
110                                partyDepartment = partyControl.getPartyDepartment(party);
111                            } else {
112                                handleExecutionError(UnknownPartyNameException.class, eea, ExecutionErrors.UnknownPartyName.name(), partyName);
113                            }
114                        } else if(universalEntitySpec != null) {
115                            var entityInstance = entityInstanceLogic.getEntityInstance(eea, universalEntitySpec,
116                                    ComponentVendors.ECHO_THREE.name(), EntityTypes.Party.name());
117
118                            if(eea == null || !eea.hasExecutionErrors()) {
119                                var party = partyControl.getPartyByEntityInstance(entityInstance);
120
121                                partyLogic.checkPartyType(eea, party, PartyTypes.DEPARTMENT.name());
122
123                                partyDepartment = partyControl.getPartyDepartment(party);
124                            }
125                        }
126                    } else {
127                        handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name());
128                    }
129                }
130            } else {
131                if(required) {
132                    handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name());
133                }
134            }
135        }
136
137        return partyDepartment;
138    }
139
140}