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.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 com.echothree.util.server.persistence.Session; 031 032public class PersonalTitleLogic 033 extends BaseLogic { 034 035 private PersonalTitleLogic() { 036 super(); 037 } 038 039 private static class PersonalTitleLogicHolder { 040 static PersonalTitleLogic instance = new PersonalTitleLogic(); 041 } 042 043 public static PersonalTitleLogic getInstance() { 044 return PersonalTitleLogicHolder.instance; 045 } 046 047 private PersonalTitle getPersonalTitleById(final ExecutionErrorAccumulator eea, final String personalTitleId, 048 final EntityPermission entityPermission) { 049 var partyControl = Session.getModelController(PartyControl.class); 050 var personalTitle = partyControl.convertPersonalTitleIdToEntity(personalTitleId, entityPermission); 051 052 if(personalTitle == null) { 053 handleExecutionError(UnknownPersonalTitleIdException.class, eea, ExecutionErrors.UnknownPersonalTitleId.name(), personalTitleId); 054 } 055 056 return personalTitle; 057 } 058 059 public PersonalTitle getPersonalTitleById(final ExecutionErrorAccumulator eea, final String personalTitleId) { 060 return getPersonalTitleById(eea, personalTitleId, EntityPermission.READ_ONLY); 061 } 062 063 public PersonalTitle getPersonalTitleByIdForUpdate(final ExecutionErrorAccumulator eea, final String personalTitleId) { 064 return getPersonalTitleById(eea, personalTitleId, EntityPermission.READ_WRITE); 065 } 066 067 public void deletePersonalTitle(final ExecutionErrorAccumulator eea, final PersonalTitle personalTitle, 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 PersonalTitle is in-use by any PartyPaymentMethodCreditCard, ContactPostalAddress or Person. 073 if(partyPaymentMethodControl.countPartyPaymentMethodCreditCardsByPersonalTitle(personalTitle) != 0 074 || contactControl.countContactPostalAddressesByPersonalTitle(personalTitle) != 0 075 || partyControl.countPeopleByPersonalTitle(personalTitle) != 0) { 076 handleExecutionError(CannotDeletePersonalTitleInUseException.class, eea, ExecutionErrors.CannotDeletePersonalTitleInUse.name(), 077 personalTitle.getLastDetail().getPersonalTitlePK().getEntityId().toString()); 078 } 079 080 if(!eea.hasExecutionErrors()) { 081 partyControl.deletePersonalTitle(personalTitle, deletedBy); 082 } 083 } 084 085 public void deletePersonalTitle(final ExecutionErrorAccumulator eea, final String personalTitleId, final PartyPK deletedBy) { 086 var personalTitle = getPersonalTitleByIdForUpdate(eea, personalTitleId); 087 088 if(!eea.hasExecutionErrors()) { 089 deletePersonalTitle(eea, personalTitle, deletedBy); 090 } 091 } 092 093}