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