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