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.CannotDeletePersonalTitleInUseException; 021import com.echothree.model.control.party.common.exception.UnknownPersonalTitleIdException; 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.PersonalTitle; 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.enterprise.inject.spi.CDI; 032import javax.inject.Inject; 033 034@ApplicationScoped 035public class PersonalTitleLogic 036 extends BaseLogic { 037 038 @Inject 039 ContactControl contactControl; 040 041 @Inject 042 PartyControl partyControl; 043 044 @Inject 045 PartyPaymentMethodControl partyPaymentMethodControl; 046 047 protected PersonalTitleLogic() { 048 super(); 049 } 050 051 public static PersonalTitleLogic getInstance() { 052 return CDI.current().select(PersonalTitleLogic.class).get(); 053 } 054 055 private PersonalTitle getPersonalTitleById(final ExecutionErrorAccumulator eea, final String personalTitleId, 056 final EntityPermission entityPermission) { 057 var personalTitle = partyControl.convertPersonalTitleIdToEntity(personalTitleId, entityPermission); 058 059 if(personalTitle == null) { 060 handleExecutionError(UnknownPersonalTitleIdException.class, eea, ExecutionErrors.UnknownPersonalTitleId.name(), personalTitleId); 061 } 062 063 return personalTitle; 064 } 065 066 public PersonalTitle getPersonalTitleById(final ExecutionErrorAccumulator eea, final String personalTitleId) { 067 return getPersonalTitleById(eea, personalTitleId, EntityPermission.READ_ONLY); 068 } 069 070 public PersonalTitle getPersonalTitleByIdForUpdate(final ExecutionErrorAccumulator eea, final String personalTitleId) { 071 return getPersonalTitleById(eea, personalTitleId, EntityPermission.READ_WRITE); 072 } 073 074 public void deletePersonalTitle(final ExecutionErrorAccumulator eea, final PersonalTitle personalTitle, final PartyPK deletedBy) { 075 // Check if the PersonalTitle is in-use by any PartyPaymentMethodCreditCard, ContactPostalAddress or Person. 076 if(partyPaymentMethodControl.countPartyPaymentMethodCreditCardsByPersonalTitle(personalTitle) != 0 077 || contactControl.countContactPostalAddressesByPersonalTitle(personalTitle) != 0 078 || partyControl.countPeopleByPersonalTitle(personalTitle) != 0) { 079 handleExecutionError(CannotDeletePersonalTitleInUseException.class, eea, ExecutionErrors.CannotDeletePersonalTitleInUse.name(), 080 personalTitle.getLastDetail().getPersonalTitlePK().getEntityId().toString()); 081 } 082 083 if(eea == null || !eea.hasExecutionErrors()) { 084 partyControl.deletePersonalTitle(personalTitle, deletedBy); 085 } 086 } 087 088 public void deletePersonalTitle(final ExecutionErrorAccumulator eea, final String personalTitleId, final PartyPK deletedBy) { 089 var personalTitle = getPersonalTitleByIdForUpdate(eea, personalTitleId); 090 091 if(eea == null || !eea.hasExecutionErrors()) { 092 deletePersonalTitle(eea, personalTitle, deletedBy); 093 } 094 } 095 096}