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.contact.server.logic;
018
019import com.echothree.control.user.contact.common.spec.ContactMechanismPurposeUniversalSpec;
020import com.echothree.model.control.contact.common.exception.UnknownContactMechanismPurposeNameException;
021import com.echothree.model.control.contact.server.control.ContactControl;
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.data.contact.server.entity.ContactMechanismPurpose;
027import com.echothree.util.common.message.ExecutionErrors;
028import com.echothree.util.server.control.BaseLogic;
029import com.echothree.util.server.message.ExecutionErrorAccumulator;
030import javax.enterprise.context.ApplicationScoped;
031import javax.enterprise.inject.spi.CDI;
032import javax.inject.Inject;
033
034@ApplicationScoped
035public class ContactMechanismPurposeLogic
036    extends BaseLogic {
037
038    @Inject
039    ContactControl contactControl;
040
041    @Inject
042    EntityInstanceLogic entityInstanceLogic;
043
044    protected ContactMechanismPurposeLogic() {
045        super();
046    }
047
048    public static ContactMechanismPurposeLogic getInstance() {
049        return CDI.current().select(ContactMechanismPurposeLogic.class).get();
050    }
051
052    public ContactMechanismPurpose getContactMechanismPurposeByName(final ExecutionErrorAccumulator eea, final String contactMechanismPurposeName) {
053        var contactMechanismPurpose = contactControl.getContactMechanismPurposeByName(contactMechanismPurposeName);
054
055        if(contactMechanismPurpose == null) {
056            handleExecutionError(UnknownContactMechanismPurposeNameException.class, eea, ExecutionErrors.UnknownContactMechanismPurposeName.name(), contactMechanismPurposeName);
057        }
058
059        return contactMechanismPurpose;
060    }
061
062    public ContactMechanismPurpose getContactMechanismPurposeByUniversalSpec(final ExecutionErrorAccumulator eea,
063            final ContactMechanismPurposeUniversalSpec universalSpec) {
064        ContactMechanismPurpose contactMechanismPurpose = null;
065        var contactMechanismPurposeName = universalSpec.getContactMechanismPurposeName();
066        var parameterCount = (contactMechanismPurposeName == null ? 0 : 1) + entityInstanceLogic.countPossibleEntitySpecs(universalSpec);
067
068        switch(parameterCount) {
069            case 1 -> {
070                if(contactMechanismPurposeName == null) {
071                    var entityInstance = entityInstanceLogic.getEntityInstance(eea, universalSpec,
072                            ComponentVendors.ECHO_THREE.name(), EntityTypes.ContactMechanismPurpose.name());
073
074                    if(eea == null || !eea.hasExecutionErrors()) {
075                        contactMechanismPurpose = contactControl.getContactMechanismPurposeByEntityInstance(entityInstance);
076                    }
077                } else {
078                    contactMechanismPurpose = getContactMechanismPurposeByName(eea, contactMechanismPurposeName);
079                }
080            }
081            default ->
082                    handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name());
083        }
084
085        return contactMechanismPurpose;
086    }
087
088}