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