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.party.server.logic; 018 019import com.echothree.model.control.contact.server.control.ContactControl; 020import com.echothree.model.control.party.common.exception.CannotDeleteNameSuffixInUseException; 021import com.echothree.model.control.party.common.exception.UnknownNameSuffixIdException; 022import com.echothree.model.control.party.server.control.PartyControl; 023import com.echothree.model.control.payment.server.control.PartyPaymentMethodControl; 024import com.echothree.model.data.party.common.pk.PartyPK; 025import com.echothree.model.data.party.server.entity.NameSuffix; 026import com.echothree.util.common.message.ExecutionErrors; 027import com.echothree.util.server.control.BaseLogic; 028import com.echothree.util.server.message.ExecutionErrorAccumulator; 029import com.echothree.util.server.persistence.EntityPermission; 030import javax.enterprise.context.ApplicationScoped; 031import javax.inject.Inject; 032 033@ApplicationScoped 034public class NameSuffixLogic 035 extends BaseLogic { 036 037 @Inject 038 ContactControl contactControl; 039 040 @Inject 041 PartyControl partyControl; 042 043 @Inject 044 PartyPaymentMethodControl partyPaymentMethodControl; 045 046 protected NameSuffixLogic() { 047 super(); 048 } 049 050 private NameSuffix getNameSuffixById(final ExecutionErrorAccumulator eea, final String nameSuffixId, 051 final EntityPermission entityPermission) { 052 var nameSuffix = partyControl.convertNameSuffixIdToEntity(nameSuffixId, entityPermission); 053 054 if(nameSuffix == null) { 055 handleExecutionError(UnknownNameSuffixIdException.class, eea, ExecutionErrors.UnknownNameSuffixId.name(), nameSuffixId); 056 } 057 058 return nameSuffix; 059 } 060 061 public NameSuffix getNameSuffixById(final ExecutionErrorAccumulator eea, final String nameSuffixId) { 062 return getNameSuffixById(eea, nameSuffixId, EntityPermission.READ_ONLY); 063 } 064 065 public NameSuffix getNameSuffixByIdForUpdate(final ExecutionErrorAccumulator eea, final String nameSuffixId) { 066 return getNameSuffixById(eea, nameSuffixId, EntityPermission.READ_WRITE); 067 } 068 069 public void deleteNameSuffix(final ExecutionErrorAccumulator eea, final NameSuffix nameSuffix, final PartyPK deletedBy) { 070 // Check if the NameSuffix is in-use by any PartyPaymentMethodCreditCard, ContactPostalAddress or Person. 071 if(partyPaymentMethodControl.countPartyPaymentMethodCreditCardsByNameSuffix(nameSuffix) != 0 072 || contactControl.countContactPostalAddressesByNameSuffix(nameSuffix) != 0 073 || partyControl.countPeopleByNameSuffix(nameSuffix) != 0) { 074 handleExecutionError(CannotDeleteNameSuffixInUseException.class, eea, ExecutionErrors.CannotDeleteNameSuffixInUse.name(), 075 nameSuffix.getLastDetail().getNameSuffixPK().getEntityId().toString()); 076 } 077 078 if(eea == null || !eea.hasExecutionErrors()) { 079 partyControl.deleteNameSuffix(nameSuffix, deletedBy); 080 } 081 } 082 083 public void deleteNameSuffix(final ExecutionErrorAccumulator eea, final String nameSuffixId, final PartyPK deletedBy) { 084 var nameSuffix = getNameSuffixByIdForUpdate(eea, nameSuffixId); 085 086 if(eea == null || !eea.hasExecutionErrors()) { 087 deleteNameSuffix(eea, nameSuffix, deletedBy); 088 } 089 } 090 091}