001// --------------------------------------------------------------------------------
002// Copyright 2002-2025 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.exception.InvalidPartyTypeException;
025import com.echothree.model.control.party.common.exception.UnknownPartyNameException;
026import com.echothree.model.control.party.common.exception.UnknownPartyTypeNameException;
027import com.echothree.model.control.party.server.control.PartyControl;
028import com.echothree.model.data.core.server.entity.EntityInstance;
029import com.echothree.model.data.party.common.pk.PartyPK;
030import com.echothree.model.data.party.server.entity.Party;
031import com.echothree.model.data.party.server.entity.PartyType;
032import com.echothree.model.data.party.server.factory.PartyFactory;
033import com.echothree.util.common.message.ExecutionErrors;
034import com.echothree.util.server.control.BaseLogic;
035import com.echothree.util.server.message.ExecutionErrorAccumulator;
036import com.echothree.util.server.persistence.EntityPermission;
037import com.echothree.util.server.persistence.Session;
038import javax.enterprise.context.ApplicationScoped;
039import javax.enterprise.inject.spi.CDI;
040
041@ApplicationScoped
042public class PartyLogic
043        extends BaseLogic {
044
045    protected PartyLogic() {
046        super();
047    }
048
049    public static PartyLogic getInstance() {
050        return CDI.current().select(PartyLogic.class).get();
051    }
052    
053    /** Assume that the entityInstance passed to this function is an ECHO_THREE.Party. */
054    public Party getPartyFromEntityInstance(final EntityInstance entityInstance) {
055        var pk = new PartyPK(entityInstance.getEntityUniqueId());
056        
057        return PartyFactory.getInstance().getEntityFromPK(EntityPermission.READ_ONLY, pk);
058    }
059
060    public boolean isPartyType(final Party party, final String ... partyTypeNames) {
061        var partyTypeName = party.getLastDetail().getPartyType().getPartyTypeName();
062        var found = false;
063        var length = partyTypeNames.length;
064
065        for(var i = 0; i < length ; i++) {
066            if(partyTypeName.equals(partyTypeNames[i])) {
067                found = true;
068                break;
069            }
070        }
071
072        return found;
073    }
074
075    public String checkPartyType(final ExecutionErrorAccumulator eea, final Party party, final String ... partyTypeNames) {
076        var partyTypeName = party.getLastDetail().getPartyType().getPartyTypeName();
077        var found = false;
078        var length = partyTypeNames.length;
079
080        for(var i = 0; i < length ; i++) {
081            if(partyTypeName.equals(partyTypeNames[i])) {
082                found = true;
083                break;
084            }
085        }
086
087        if(!found) {
088            handleExecutionError(InvalidPartyTypeException.class, eea, ExecutionErrors.InvalidPartyType.name(), partyTypeName);
089        }
090
091        return partyTypeName;
092    }
093
094    public PartyType getPartyTypeByName(final ExecutionErrorAccumulator eea, final String partyTypeName) {
095        var partyControl = Session.getModelController(PartyControl.class);
096        var partyType = partyControl.getPartyTypeByName(partyTypeName);
097
098        if(partyType == null) {
099            handleExecutionError(UnknownPartyTypeNameException.class, eea, ExecutionErrors.UnknownPartyTypeName.name(), partyTypeName);
100        }
101
102        return partyType;
103    }
104    
105    public Party getPartyByName(final ExecutionErrorAccumulator eea, final String partyName,
106            final EntityPermission entityPermission) {
107        var partyControl = Session.getModelController(PartyControl.class);
108        var party = partyControl.getPartyByName(partyName, entityPermission);
109
110        if(party == null) {
111            handleExecutionError(UnknownPartyNameException.class, eea, ExecutionErrors.UnknownPartyName.name(), partyName);
112        }
113
114        return party;
115    }
116    
117    public Party getPartyByName(final ExecutionErrorAccumulator eea, final String partyName) {
118        return getPartyByName(eea, partyName, EntityPermission.READ_ONLY);
119    }
120    
121    public Party getPartyByNameForUpdate(final ExecutionErrorAccumulator eea, final String partyName) {
122        return getPartyByName(eea, partyName, EntityPermission.READ_WRITE);
123    }
124    
125    public Party getPartyByName(final ExecutionErrorAccumulator eea, final String partyName, final String ... partyTypeNames) {
126        var party = getPartyByName(eea, partyName);
127
128        if(party != null) {
129            checkPartyType(eea, party, partyTypeNames);
130        }
131
132        return party;
133    }
134
135    public Party getPartyByName(final ExecutionErrorAccumulator eea, final String partyName,
136            final UniversalEntitySpec universalEntitySpec) {
137        var parameterCount = (partyName == null ? 0 : 1) +
138                EntityInstanceLogic.getInstance().countPossibleEntitySpecs(universalEntitySpec);
139        Party party = null;
140
141        if(parameterCount == 1) {
142            var partyControl = Session.getModelController(PartyControl.class);
143
144            if(partyName != null) {
145                party = partyControl.getPartyByName(partyName);
146
147                if(party == null) {
148                    handleExecutionError(UnknownPartyNameException.class, eea, ExecutionErrors.UnknownPartyName.name(), partyName);
149                }
150            } else if(universalEntitySpec != null) {
151                var entityInstance = EntityInstanceLogic.getInstance().getEntityInstance(eea, universalEntitySpec,
152                        ComponentVendors.ECHO_THREE.name(), EntityTypes.Party.name());
153
154                if(!eea.hasExecutionErrors()) {
155                    party = partyControl.getPartyByEntityInstance(entityInstance);
156                }
157            }
158        } else {
159            handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name());
160        }
161
162        return party;
163    }
164
165}