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