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