001// --------------------------------------------------------------------------------
002// Copyright 2002-2024 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.model.control.contact.common.exception.CannotDeleteContactMechanismInUseException;
020import com.echothree.model.control.contact.common.exception.UnknownContactMechanismNameException;
021import com.echothree.model.control.contact.server.control.ContactControl;
022import com.echothree.model.control.payment.server.control.PartyPaymentMethodControl;
023import com.echothree.model.data.contact.server.entity.ContactMechanism;
024import com.echothree.model.data.party.common.pk.PartyPK;
025import com.echothree.util.common.message.ExecutionErrors;
026import com.echothree.util.server.control.BaseLogic;
027import com.echothree.util.server.message.ExecutionErrorAccumulator;
028import com.echothree.util.server.persistence.EntityPermission;
029import com.echothree.util.server.persistence.Session;
030
031public class ContactMechanismLogic
032    extends BaseLogic {
033    
034    private ContactMechanismLogic() {
035        super();
036    }
037    
038    private static class ContactMechanismLogicHolder {
039        static ContactMechanismLogic instance = new ContactMechanismLogic();
040    }
041    
042    public static ContactMechanismLogic getInstance() {
043        return ContactMechanismLogicHolder.instance;
044    }
045    
046    public ContactMechanism getContactMechanismByName(final ExecutionErrorAccumulator eea, final String contactMechanismName,
047            final EntityPermission entityPermission) {
048        var contactControl = Session.getModelController(ContactControl.class);
049        var contactMechanism = contactControl.getContactMechanismByName(contactMechanismName, entityPermission);
050
051        if(contactMechanism == null) {
052            handleExecutionError(UnknownContactMechanismNameException.class, eea, ExecutionErrors.UnknownContactMechanismName.name(), contactMechanismName);
053        }
054
055        return contactMechanism;
056    }
057
058    public ContactMechanism getContactMechanismByName(final ExecutionErrorAccumulator eea, final String contactMechanismName) {
059        return getContactMechanismByName(eea, contactMechanismName, EntityPermission.READ_ONLY);
060    }
061
062    public ContactMechanism getContactMechanismByNameForUpdate(final ExecutionErrorAccumulator eea, final String contactMechanismName) {
063        return getContactMechanismByName(eea, contactMechanismName, EntityPermission.READ_WRITE);
064    }
065
066    public void deleteContactMechanism(final ExecutionErrorAccumulator eea, final ContactMechanism contactMechanism,
067            final PartyPK deletedBy) {
068        var contactControl = Session.getModelController(ContactControl.class);
069        var partyPaymentMethodControl = Session.getModelController(PartyPaymentMethodControl.class);
070        boolean cannotDeleteContactMechanismInUse = false;
071
072        // Check if the ContactMechanism is in-use by any PartyPaymentMethodCreditCard.
073        var partyContactMechanisms = contactControl.getPartyContactMechanismsByContactMechanism(contactMechanism);
074        for(var partyContactMechanism : partyContactMechanisms) {
075            if(partyPaymentMethodControl.countPartyPaymentMethodCreditCardsByIssuerPartyContactMechanism(partyContactMechanism) != 0
076                    || partyPaymentMethodControl.countPartyPaymentMethodCreditCardsByBillingPartyContactMechanism(partyContactMechanism) != 0) {
077                cannotDeleteContactMechanismInUse = true;
078                break;
079            }
080        }
081
082        if(cannotDeleteContactMechanismInUse) {
083            handleExecutionError(CannotDeleteContactMechanismInUseException.class, eea, ExecutionErrors.CannotDeleteContactMechanismInUse.name(),
084                    contactMechanism.getLastDetail().getContactMechanismName());
085        }
086
087        if(!eea.hasExecutionErrors()) {
088            contactControl.deleteContactMechanism(contactMechanism, deletedBy);
089        }
090    }
091
092    public void deleteContactMechanism(final ExecutionErrorAccumulator eea, final String contactMechanismName,
093            final PartyPK deletedBy) {
094        var contactMechanism = getContactMechanismByNameForUpdate(eea, contactMechanismName);
095
096        if(!eea.hasExecutionErrors()) {
097            deleteContactMechanism(eea, contactMechanism, deletedBy);
098        }
099    }
100
101}