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