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    protected PartyContactListLogic() {
040        super();
041    }
042
043    @Inject
044    ContactListControl contactListControl;
045
046    @Inject
047    ContactListLogic contactListLogic;
048
049    @Inject
050    PartyLogic partyLogic;
051
052    public PartyContactList getPartyContactListByUniversalSpec(final ExecutionErrorAccumulator eea,
053            final PartyContactListUniversalSpec universalSpec, final EntityPermission entityPermission) {
054        PartyContactList partyContactList = null;
055        var partyName = universalSpec.getPartyName();
056        var contactListName = universalSpec.getContactListName();
057        var parameterCount = (partyName == null && contactListName == null ? 0 : 1) + EntityInstanceLogic.getInstance().countPossibleEntitySpecs(universalSpec);
058
059        switch(parameterCount) {
060            case 1 -> {
061                if(partyName == null && contactListName == null) {
062                    var entityInstance = EntityInstanceLogic.getInstance().getEntityInstance(eea, universalSpec,
063                            ComponentVendors.ECHO_THREE.name(), EntityTypes.PartyContactList.name());
064
065                    if(eea == null || !eea.hasExecutionErrors()) {
066                        partyContactList = contactListControl.getPartyContactListByEntityInstance(entityInstance, entityPermission);
067                    }
068                } else {
069                    if(partyName != null && contactListName != null) {
070                        var party = partyLogic.getPartyByName(eea, partyName);
071
072                        if(!eea.hasExecutionErrors()) {
073                            var contactList = contactListLogic.getContactListByName(eea, contactListName);
074
075                            if(!eea.hasExecutionErrors()) {
076                                partyContactList = contactListControl.getPartyContactList(party, contactList, entityPermission);
077
078                                if(partyContactList == null) {
079                                    handleExecutionError(UnknownPartyContactListException.class, eea, ExecutionErrors.UnknownPartyContactList.name(), partyName, contactListName);
080                                }
081                            }
082                        }
083                    } else {
084                        handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name());
085                    }
086                }
087            }
088            default ->
089                    handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name());
090        }
091
092        return partyContactList;
093    }
094
095    public PartyContactList getPartyContactListByUniversalSpec(final ExecutionErrorAccumulator eea,
096            final PartyContactListUniversalSpec universalSpec) {
097        return getPartyContactListByUniversalSpec(eea, universalSpec, EntityPermission.READ_ONLY);
098    }
099
100    public PartyContactList getPartyContactListByUniversalSpecForUpdate(final ExecutionErrorAccumulator eea,
101            final PartyContactListUniversalSpec universalSpec) {
102        return getPartyContactListByUniversalSpec(eea, universalSpec, EntityPermission.READ_WRITE);
103    }
104
105}