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.UnknownDivisionNameException;
026import com.echothree.model.control.party.common.exception.UnknownPartyNameException;
027import com.echothree.model.control.party.common.exception.UseOfDivisionNameRequiresCompanyNameException;
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.PartyDivision;
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 DivisionLogic
037        extends BaseLogic {
038    
039    private DivisionLogic() {
040        super();
041    }
042    
043    private static class DivisionLogicHolder {
044        static DivisionLogic instance = new DivisionLogic();
045    }
046    
047    public static DivisionLogic getInstance() {
048        return DivisionLogicHolder.instance;
049    }
050
051    public PartyDivision getPartyDivisionByName(final ExecutionErrorAccumulator eea, final String companyName,
052            final String divisionName, final String partyName, final UniversalEntitySpec universalEntitySpec,
053            final boolean required) {
054        PartyDivision partyDivision = null;
055        Party companyParty = null;
056
057        if(companyName != null) {
058            var partyCompany = CompanyLogic.getInstance().getPartyCompanyByName(eea, companyName, null,
059                    null, true);
060
061            companyParty = hasExecutionErrors(eea) ? null : partyCompany.getParty();
062        }
063
064        if(!hasExecutionErrors(eea)) {
065            partyDivision = getPartyDivisionByName(eea, companyParty, divisionName, partyName, universalEntitySpec, required);
066        }
067
068        return partyDivision;
069    }
070
071    public PartyDivision getPartyDivisionByName(final ExecutionErrorAccumulator eea, final Party companyParty,
072            final String divisionName, final String partyName, final UniversalEntitySpec universalEntitySpec,
073            final boolean required) {
074        var parameterCount = (divisionName == null ? 0 : 1) + (partyName == null ? 0 : 1) +
075                EntityInstanceLogic.getInstance().countPossibleEntitySpecs(universalEntitySpec);
076        PartyDivision partyDivision = null;
077
078        if(companyParty != null) {
079            PartyLogic.getInstance().checkPartyType(eea, companyParty, PartyTypes.COMPANY.name());
080        }
081
082        if(!hasExecutionErrors(eea)) {
083            if(parameterCount == 1) {
084                var partyControl = Session.getModelController(PartyControl.class);
085
086                if(divisionName != null) {
087                    // Use of divisionName requires a companyParty.
088                    if(companyParty == null) {
089                        handleExecutionError(UseOfDivisionNameRequiresCompanyNameException.class, eea, ExecutionErrors.UseOfDivisionNameRequiresCompanyName.name());
090                    } else {
091                        partyDivision = partyControl.getPartyDivisionByName(companyParty, divisionName);
092
093                        if(partyDivision == null) {
094                            handleExecutionError(UnknownDivisionNameException.class, eea, ExecutionErrors.UnknownDivisionName.name(), divisionName);
095                        }
096                    }
097                } else {
098                    // Use of partyName or universalEntitySpec cannot include a companyParty.
099                    if(companyParty == null) {
100                        if(partyName != null) {
101                            Party party = partyControl.getPartyByName(partyName);
102
103                            if(party != null) {
104                                PartyLogic.getInstance().checkPartyType(eea, party, PartyTypes.DIVISION.name());
105
106                                partyDivision = partyControl.getPartyDivision(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.DIVISION.name());
118
119                                partyDivision = partyControl.getPartyDivision(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 partyDivision;
134    }
135
136}