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