001// -------------------------------------------------------------------------------- 002// Copyright 2002-2025 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; 030import javax.enterprise.context.ApplicationScoped; 031import javax.enterprise.inject.spi.CDI; 032 033@ApplicationScoped 034public class ContactMechanismLogic 035 extends BaseLogic { 036 037 protected ContactMechanismLogic() { 038 super(); 039 } 040 041 public static ContactMechanismLogic getInstance() { 042 return CDI.current().select(ContactMechanismLogic.class).get(); 043 } 044 045 public ContactMechanism getContactMechanismByName(final ExecutionErrorAccumulator eea, final String contactMechanismName, 046 final EntityPermission entityPermission) { 047 var contactControl = Session.getModelController(ContactControl.class); 048 var contactMechanism = contactControl.getContactMechanismByName(contactMechanismName, entityPermission); 049 050 if(contactMechanism == null) { 051 handleExecutionError(UnknownContactMechanismNameException.class, eea, ExecutionErrors.UnknownContactMechanismName.name(), contactMechanismName); 052 } 053 054 return contactMechanism; 055 } 056 057 public ContactMechanism getContactMechanismByName(final ExecutionErrorAccumulator eea, final String contactMechanismName) { 058 return getContactMechanismByName(eea, contactMechanismName, EntityPermission.READ_ONLY); 059 } 060 061 public ContactMechanism getContactMechanismByNameForUpdate(final ExecutionErrorAccumulator eea, final String contactMechanismName) { 062 return getContactMechanismByName(eea, contactMechanismName, EntityPermission.READ_WRITE); 063 } 064 065 public void deleteContactMechanism(final ExecutionErrorAccumulator eea, final ContactMechanism contactMechanism, 066 final PartyPK deletedBy) { 067 var contactControl = Session.getModelController(ContactControl.class); 068 var partyPaymentMethodControl = Session.getModelController(PartyPaymentMethodControl.class); 069 var cannotDeleteContactMechanismInUse = false; 070 071 // Check if the ContactMechanism is in-use by any PartyPaymentMethodCreditCard. 072 var partyContactMechanisms = contactControl.getPartyContactMechanismsByContactMechanism(contactMechanism); 073 for(var partyContactMechanism : partyContactMechanisms) { 074 if(partyPaymentMethodControl.countPartyPaymentMethodCreditCardsByIssuerPartyContactMechanism(partyContactMechanism) != 0 075 || partyPaymentMethodControl.countPartyPaymentMethodCreditCardsByBillingPartyContactMechanism(partyContactMechanism) != 0) { 076 cannotDeleteContactMechanismInUse = true; 077 break; 078 } 079 } 080 081 if(cannotDeleteContactMechanismInUse) { 082 handleExecutionError(CannotDeleteContactMechanismInUseException.class, eea, ExecutionErrors.CannotDeleteContactMechanismInUse.name(), 083 contactMechanism.getLastDetail().getContactMechanismName()); 084 } 085 086 if(!eea.hasExecutionErrors()) { 087 contactControl.deleteContactMechanism(contactMechanism, deletedBy); 088 } 089 } 090 091 public void deleteContactMechanism(final ExecutionErrorAccumulator eea, final String contactMechanismName, 092 final PartyPK deletedBy) { 093 var contactMechanism = getContactMechanismByNameForUpdate(eea, contactMechanismName); 094 095 if(!eea.hasExecutionErrors()) { 096 deleteContactMechanism(eea, contactMechanism, deletedBy); 097 } 098 } 099 100}