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.ContactMechanismUniversalSpec;
020import com.echothree.model.control.contact.common.exception.CannotDeleteContactMechanismInUseException;
021import com.echothree.model.control.contact.common.exception.UnknownContactMechanismNameException;
022import com.echothree.model.control.contact.server.control.ContactControl;
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.control.payment.server.control.PartyPaymentMethodControl;
028import com.echothree.model.data.contact.server.entity.ContactMechanism;
029import com.echothree.model.data.party.common.pk.PartyPK;
030import com.echothree.util.common.message.ExecutionErrors;
031import com.echothree.util.server.control.BaseLogic;
032import com.echothree.util.server.message.ExecutionErrorAccumulator;
033import com.echothree.util.server.persistence.EntityPermission;
034import javax.enterprise.context.ApplicationScoped;
035import javax.inject.Inject;
036
037@ApplicationScoped
038public class ContactMechanismLogic
039    extends BaseLogic {
040
041    @Inject
042    ContactControl contactControl;
043
044    @Inject
045    PartyPaymentMethodControl partyPaymentMethodControl;
046
047    @Inject
048    EntityInstanceLogic entityInstanceLogic;
049
050    protected ContactMechanismLogic() {
051        super();
052    }
053
054    public ContactMechanism getContactMechanismByName(final ExecutionErrorAccumulator eea, final String contactMechanismName,
055            final EntityPermission entityPermission) {
056        var contactMechanism = contactControl.getContactMechanismByName(contactMechanismName, entityPermission);
057
058        if(contactMechanism == null) {
059            handleExecutionError(UnknownContactMechanismNameException.class, eea, ExecutionErrors.UnknownContactMechanismName.name(), contactMechanismName);
060        }
061
062        return contactMechanism;
063    }
064
065    public ContactMechanism getContactMechanismByName(final ExecutionErrorAccumulator eea, final String contactMechanismName) {
066        return getContactMechanismByName(eea, contactMechanismName, EntityPermission.READ_ONLY);
067    }
068
069    public ContactMechanism getContactMechanismByNameForUpdate(final ExecutionErrorAccumulator eea, final String contactMechanismName) {
070        return getContactMechanismByName(eea, contactMechanismName, EntityPermission.READ_WRITE);
071    }
072
073    public ContactMechanism getContactMechanismByUniversalSpec(final ExecutionErrorAccumulator eea,
074            final ContactMechanismUniversalSpec universalSpec, final EntityPermission entityPermission) {
075        ContactMechanism contactMechanism = null;
076        var contactMechanismName = universalSpec.getContactMechanismName();
077        var parameterCount = (contactMechanismName == null ? 0 : 1) + entityInstanceLogic.countPossibleEntitySpecs(universalSpec);
078
079        switch(parameterCount) {
080            case 1 -> {
081                if(contactMechanismName == null) {
082                    var entityInstance = entityInstanceLogic.getEntityInstance(eea, universalSpec,
083                            ComponentVendors.ECHO_THREE.name(), EntityTypes.ContactMechanism.name());
084
085                    if(eea == null || !eea.hasExecutionErrors()) {
086                        contactMechanism = contactControl.getContactMechanismByEntityInstance(entityInstance, entityPermission);
087                    }
088                } else {
089                    contactMechanism = getContactMechanismByName(eea, contactMechanismName, entityPermission);
090                }
091            }
092            default ->
093                    handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name());
094        }
095
096        return contactMechanism;
097    }
098
099    public ContactMechanism getContactMechanismByUniversalSpec(final ExecutionErrorAccumulator eea,
100            final ContactMechanismUniversalSpec universalSpec) {
101        return getContactMechanismByUniversalSpec(eea, universalSpec, EntityPermission.READ_ONLY);
102    }
103
104    public ContactMechanism getContactMechanismByUniversalSpecForUpdate(final ExecutionErrorAccumulator eea,
105            final ContactMechanismUniversalSpec universalSpec) {
106        return getContactMechanismByUniversalSpec(eea, universalSpec, EntityPermission.READ_WRITE);
107    }
108
109    public void deleteContactMechanism(final ExecutionErrorAccumulator eea, final ContactMechanism contactMechanism,
110            final PartyPK deletedBy) {
111        var cannotDeleteContactMechanismInUse = false;
112
113        // Check if the ContactMechanism is in-use by any PartyPaymentMethodCreditCard.
114        var partyContactMechanisms = contactControl.getPartyContactMechanismsByContactMechanism(contactMechanism);
115        for(var partyContactMechanism : partyContactMechanisms) {
116            if(partyPaymentMethodControl.countPartyPaymentMethodCreditCardsByIssuerPartyContactMechanism(partyContactMechanism) != 0
117                    || partyPaymentMethodControl.countPartyPaymentMethodCreditCardsByBillingPartyContactMechanism(partyContactMechanism) != 0) {
118                cannotDeleteContactMechanismInUse = true;
119                break;
120            }
121        }
122
123        if(cannotDeleteContactMechanismInUse) {
124            handleExecutionError(CannotDeleteContactMechanismInUseException.class, eea, ExecutionErrors.CannotDeleteContactMechanismInUse.name(),
125                    contactMechanism.getLastDetail().getContactMechanismName());
126        }
127
128        if(eea == null || !eea.hasExecutionErrors()) {
129            contactControl.deleteContactMechanism(contactMechanism, deletedBy);
130        }
131    }
132
133    public void deleteContactMechanism(final ExecutionErrorAccumulator eea, final String contactMechanismName,
134            final PartyPK deletedBy) {
135        var contactMechanism = getContactMechanismByNameForUpdate(eea, contactMechanismName);
136
137        if(eea == null || !eea.hasExecutionErrors()) {
138            deleteContactMechanism(eea, contactMechanism, deletedBy);
139        }
140    }
141
142}