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.contactlist.server.logic;
018
019import com.echothree.control.user.contactlist.common.spec.PartyContactListUniversalSpec;
020import com.echothree.model.control.contactlist.common.exception.UnknownPartyContactListException;
021import com.echothree.model.control.contactlist.server.control.ContactListControl;
022import com.echothree.model.control.core.common.ComponentVendors;
023import com.echothree.model.control.core.common.EntityTypes;
024import com.echothree.model.control.core.common.exception.InvalidParameterCountException;
025import com.echothree.model.control.core.server.logic.EntityInstanceLogic;
026import com.echothree.model.control.party.server.logic.PartyLogic;
027import com.echothree.model.data.contactlist.server.entity.PartyContactList;
028import com.echothree.util.common.message.ExecutionErrors;
029import com.echothree.util.server.control.BaseLogic;
030import com.echothree.util.server.message.ExecutionErrorAccumulator;
031import com.echothree.util.server.persistence.EntityPermission;
032import javax.enterprise.context.ApplicationScoped;
033import javax.inject.Inject;
034
035@ApplicationScoped
036public class PartyContactListLogic
037        extends BaseLogic {
038
039    @Inject
040    ContactListControl contactListControl;
041
042    @Inject
043    ContactListLogic contactListLogic;
044
045    @Inject
046    EntityInstanceLogic entityInstanceLogic;
047
048    @Inject
049    PartyLogic partyLogic;
050
051    protected PartyContactListLogic() {
052        super();
053    }
054
055    public PartyContactList getPartyContactListByUniversalSpec(final ExecutionErrorAccumulator eea,
056            final PartyContactListUniversalSpec universalSpec, final EntityPermission entityPermission) {
057        PartyContactList partyContactList = null;
058        var partyName = universalSpec.getPartyName();
059        var contactListName = universalSpec.getContactListName();
060        var parameterCount = (partyName == null && contactListName == null ? 0 : 1) + entityInstanceLogic.countPossibleEntitySpecs(universalSpec);
061
062        switch(parameterCount) {
063            case 1 -> {
064                if(partyName == null && contactListName == null) {
065                    var entityInstance = entityInstanceLogic.getEntityInstance(eea, universalSpec,
066                            ComponentVendors.ECHO_THREE.name(), EntityTypes.PartyContactList.name());
067
068                    if(eea == null || !eea.hasExecutionErrors()) {
069                        partyContactList = contactListControl.getPartyContactListByEntityInstance(entityInstance, entityPermission);
070                    }
071                } else {
072                    if(partyName != null && contactListName != null) {
073                        var party = partyLogic.getPartyByName(eea, partyName);
074
075                        if(!eea.hasExecutionErrors()) {
076                            var contactList = contactListLogic.getContactListByName(eea, contactListName);
077
078                            if(!eea.hasExecutionErrors()) {
079                                partyContactList = contactListControl.getPartyContactList(party, contactList, entityPermission);
080
081                                if(partyContactList == null) {
082                                    handleExecutionError(UnknownPartyContactListException.class, eea, ExecutionErrors.UnknownPartyContactList.name(), partyName, contactListName);
083                                }
084                            }
085                        }
086                    } else {
087                        handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name());
088                    }
089                }
090            }
091            default ->
092                    handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name());
093        }
094
095        return partyContactList;
096    }
097
098    public PartyContactList getPartyContactListByUniversalSpec(final ExecutionErrorAccumulator eea,
099            final PartyContactListUniversalSpec universalSpec) {
100        return getPartyContactListByUniversalSpec(eea, universalSpec, EntityPermission.READ_ONLY);
101    }
102
103    public PartyContactList getPartyContactListByUniversalSpecForUpdate(final ExecutionErrorAccumulator eea,
104            final PartyContactListUniversalSpec universalSpec) {
105        return getPartyContactListByUniversalSpec(eea, universalSpec, EntityPermission.READ_WRITE);
106    }
107
108}