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.ContactListGroupUniversalSpec;
020import com.echothree.model.control.contactlist.common.exception.UnknownContactListGroupNameException;
021import com.echothree.model.control.contactlist.common.exception.UnknownDefaultContactListGroupException;
022import com.echothree.model.control.contactlist.server.control.ContactListControl;
023import com.echothree.model.control.core.common.ComponentVendors;
024import com.echothree.model.control.core.common.EntityTypes;
025import com.echothree.model.control.core.common.exception.InvalidParameterCountException;
026import com.echothree.model.control.core.server.logic.EntityInstanceLogic;
027import com.echothree.model.data.contactlist.server.entity.ContactListGroup;
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 ContactListGroupLogic
037    extends BaseLogic {
038
039    @Inject
040    ContactListControl contactListControl;
041
042    @Inject
043    EntityInstanceLogic entityInstanceLogic;
044
045    protected ContactListGroupLogic() {
046        super();
047    }
048
049    public ContactListGroup getContactListGroupByName(final ExecutionErrorAccumulator eea, final String contactListGroupName,
050            final EntityPermission entityPermission) {
051        var contactListGroup = contactListControl.getContactListGroupByName(contactListGroupName, entityPermission);
052
053        if(contactListGroup == null) {
054            handleExecutionError(UnknownContactListGroupNameException.class, eea, ExecutionErrors.UnknownContactListGroupName.name(), contactListGroupName);
055        }
056
057        return contactListGroup;
058    }
059
060    public ContactListGroup getContactListGroupByName(final ExecutionErrorAccumulator eea, final String contactListGroupName) {
061        return getContactListGroupByName(eea, contactListGroupName, EntityPermission.READ_ONLY);
062    }
063
064    public ContactListGroup getContactListGroupByNameForUpdate(final ExecutionErrorAccumulator eea, final String contactListGroupName) {
065        return getContactListGroupByName(eea, contactListGroupName, EntityPermission.READ_WRITE);
066    }
067
068    public ContactListGroup getContactListGroupByUniversalSpec(final ExecutionErrorAccumulator eea,
069            final ContactListGroupUniversalSpec spec, final boolean allowDefault, final EntityPermission entityPermission) {
070        ContactListGroup contactListGroup = null;
071        var contactListGroupName = spec.getContactListGroupName();
072        var parameterCount = (contactListGroupName == null ? 0 : 1) + entityInstanceLogic.countPossibleEntitySpecs(spec);
073
074        switch(parameterCount) {
075            case 0 -> {
076                if(allowDefault) {
077                    contactListGroup = contactListControl.getDefaultContactListGroup(entityPermission);
078
079                    if(contactListGroup == null) {
080                        handleExecutionError(UnknownDefaultContactListGroupException.class, eea, ExecutionErrors.UnknownDefaultContactListGroup.name());
081                    }
082                } else {
083                    handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name());
084                }
085            }
086            case 1 -> {
087                if(contactListGroupName == null) {
088                    var entityInstance = entityInstanceLogic.getEntityInstance(eea, spec,
089                            ComponentVendors.ECHO_THREE.name(), EntityTypes.ContactListGroup.name());
090
091                    if(eea == null || !eea.hasExecutionErrors()) {
092                        contactListGroup = contactListControl.getContactListGroupByEntityInstance(entityInstance, entityPermission);
093                    }
094                } else {
095                    contactListGroup = getContactListGroupByName(eea, contactListGroupName, entityPermission);
096                }
097            }
098            default ->
099                    handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name());
100        }
101
102        return contactListGroup;
103    }
104
105    public ContactListGroup getContactListGroupByUniversalSpec(final ExecutionErrorAccumulator eea,
106            final ContactListGroupUniversalSpec spec, final boolean allowDefault) {
107        return getContactListGroupByUniversalSpec(eea, spec, allowDefault, EntityPermission.READ_ONLY);
108    }
109
110    public ContactListGroup getContactListGroupByUniversalSpecForUpdate(final ExecutionErrorAccumulator eea,
111            final ContactListGroupUniversalSpec spec, final boolean allowDefault) {
112        return getContactListGroupByUniversalSpec(eea, spec, allowDefault, EntityPermission.READ_WRITE);
113    }
114    
115}