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.contactlist.server.logic; 018 019import com.echothree.model.control.contactlist.common.exception.UnknownContactListContactMechanismPurposeException; 020import com.echothree.model.control.contactlist.common.exception.UnknownContactListNameException; 021import com.echothree.model.control.contactlist.common.workflow.PartyContactListStatusConstants; 022import com.echothree.model.control.contactlist.server.control.ContactListControl; 023import com.echothree.model.control.core.server.control.EntityInstanceControl; 024import com.echothree.model.control.customer.server.control.CustomerControl; 025import com.echothree.model.control.party.common.PartyTypes; 026import com.echothree.model.control.party.server.logic.PartyLogic; 027import com.echothree.model.control.workflow.server.control.WorkflowControl; 028import com.echothree.model.data.contact.server.entity.ContactMechanismPurpose; 029import com.echothree.model.data.contactlist.server.entity.ContactList; 030import com.echothree.model.data.contactlist.server.entity.ContactListContactMechanismPurpose; 031import com.echothree.model.data.contactlist.server.entity.PartyContactList; 032import com.echothree.model.data.party.server.entity.Party; 033import com.echothree.util.common.message.ExecutionErrors; 034import com.echothree.util.common.persistence.BasePK; 035import com.echothree.util.server.control.BaseLogic; 036import com.echothree.util.server.message.ExecutionErrorAccumulator; 037import com.echothree.util.server.persistence.Session; 038import java.util.HashSet; 039import java.util.Set; 040import javax.enterprise.context.ApplicationScoped; 041import javax.enterprise.inject.spi.CDI; 042 043@ApplicationScoped 044public class ContactListLogic 045 extends BaseLogic { 046 047 protected ContactListLogic() { 048 super(); 049 } 050 051 public static ContactListLogic getInstance() { 052 return CDI.current().select(ContactListLogic.class).get(); 053 } 054 055 public ContactList getContactListByName(final ExecutionErrorAccumulator eea, final String contactListName) { 056 var contactListControl = Session.getModelController(ContactListControl.class); 057 var contactList = contactListControl.getContactListByName(contactListName); 058 059 if(contactList == null) { 060 handleExecutionError(UnknownContactListNameException.class, eea, ExecutionErrors.UnknownContactListName.name(), contactListName); 061 } 062 063 return contactList; 064 } 065 066 public ContactListContactMechanismPurpose getContactListContactMechanismPurpose(final ExecutionErrorAccumulator eea, final ContactList contactList, 067 final ContactMechanismPurpose contactMechanismPurpose) { 068 var contactListControl = Session.getModelController(ContactListControl.class); 069 var contactListContactMechanismPurpose = contactListControl.getContactListContactMechanismPurpose(contactList, contactMechanismPurpose); 070 071 if(contactListContactMechanismPurpose == null) { 072 handleExecutionError(UnknownContactListContactMechanismPurposeException.class, eea, ExecutionErrors.UnknownContactListContactMechanismPurpose.name(), 073 contactList.getLastDetail().getContactListName(), contactMechanismPurpose.getContactMechanismPurposeName()); 074 } 075 076 return contactListContactMechanismPurpose; 077 } 078 079 public boolean isPartyOnContactList(final Party party, final ContactList contactList) { 080 var contactListControl = Session.getModelController(ContactListControl.class); 081 082 return contactListControl.getPartyContactList(party, contactList) != null; 083 } 084 085 public void addContactListToParty(final ExecutionErrorAccumulator eea, final Party party, final ContactList contactList, 086 final ContactListContactMechanismPurpose preferredContactListContactMechanismPurpose, final BasePK createdBy) { 087 var contactListControl = Session.getModelController(ContactListControl.class); 088 var entityInstanceControl = Session.getModelController(EntityInstanceControl.class); 089 var workflowControl = Session.getModelController(WorkflowControl.class); 090 var partyContactList = contactListControl.createPartyContactList(party, contactList, preferredContactListContactMechanismPurpose, createdBy); 091 var entityInstance = entityInstanceControl.getEntityInstanceByBasePK(partyContactList.getPrimaryKey()); 092 var workflowEntrance = contactList.getLastDetail().getDefaultPartyContactListStatus(); 093 var workflowEntranceName = workflowEntrance.getLastDetail().getWorkflowEntranceName(); 094 095 workflowControl.addEntityToWorkflow(workflowEntrance, entityInstance, null, null, createdBy); 096 097 if(workflowEntranceName.equals(PartyContactListStatusConstants.WorkflowEntrance_NEW_AWAITING_VERIFICATION)) { 098 ContactListChainLogic.getInstance().createContactListConfirmationChainInstance(eea, party, partyContactList, createdBy); 099 } else if(workflowEntranceName.equals(PartyContactListStatusConstants.WorkflowEntrance_NEW_ACTIVE)) { 100 ContactListChainLogic.getInstance().createContactListSubscribeChainInstance(eea, party, partyContactList, createdBy); 101 } 102 } 103 104 public void removeContactListFromParty(final ExecutionErrorAccumulator eea, final PartyContactList partyContactList, final BasePK deletedBy) { 105 var contactListControl = Session.getModelController(ContactListControl.class); 106 var entityInstanceControl = Session.getModelController(EntityInstanceControl.class); 107 var workflowControl = Session.getModelController(WorkflowControl.class); 108 var entityInstance = entityInstanceControl.getEntityInstanceByBasePK(partyContactList.getPrimaryKey()); 109 var workflowEntityStatus = workflowControl.getWorkflowEntityStatusByEntityInstanceForUpdateUsingNames(PartyContactListStatusConstants.Workflow_PARTY_CONTACT_LIST_STATUS, entityInstance); 110 var workflowStepName = workflowEntityStatus.getWorkflowStep().getLastDetail().getWorkflowStepName(); 111 112 if(workflowStepName.equals(PartyContactListStatusConstants.WorkflowStep_ACTIVE)) { 113 ContactListChainLogic.getInstance().createContactListUnsubscribeChainInstance(eea, partyContactList.getLastDetail().getParty(), partyContactList, deletedBy); 114 } 115 116 contactListControl.deletePartyContactList(partyContactList, deletedBy); 117 workflowControl.deleteWorkflowEntityStatus(workflowEntityStatus, deletedBy); 118 } 119 120 public void setupInitialContactLists(final ExecutionErrorAccumulator eea, final Party party, final BasePK createdBy) { 121 var contactListControl = Session.getModelController(ContactListControl.class); 122 var partyType = party.getLastDetail().getPartyType(); 123 Set<ContactList> contactLists = new HashSet<>(); 124 125 contactListControl.getPartyTypeContactListsByPartyType(partyType).stream().filter((partyTypeContactList) -> partyTypeContactList.getAddWhenCreated()).map((partyTypeContactList) -> partyTypeContactList.getContactList()).forEach((contactList) -> { 126 contactLists.add(contactList); 127 }); 128 contactListControl.getPartyTypeContactListGroupsByPartyType(partyType).stream().filter((partyTypeContactListGroup) -> partyTypeContactListGroup.getAddWhenCreated()).forEach((partyTypeContactListGroup) -> { 129 contactLists.addAll(contactListControl.getContactListsByContactListGroup(partyTypeContactListGroup.getContactListGroup())); 130 }); 131 132 if(PartyLogic.getInstance().isPartyType(party, PartyTypes.CUSTOMER.name())) { 133 var customerControl = Session.getModelController(CustomerControl.class); 134 var customerType = customerControl.getCustomer(party).getCustomerType(); 135 136 contactListControl.getCustomerTypeContactListsByCustomerType(customerType).stream().filter((customerTypeContactList) -> customerTypeContactList.getAddWhenCreated()).map((customerTypeContactList) -> customerTypeContactList.getContactList()).forEach((contactList) -> { 137 contactLists.add(contactList); 138 }); 139 contactListControl.getCustomerTypeContactListGroupsByCustomerType(customerType).stream().filter((customerTypeContactListGroup) -> customerTypeContactListGroup.getAddWhenCreated()).forEach((customerTypeContactListGroup) -> { 140 contactLists.addAll(contactListControl.getContactListsByContactListGroup(customerTypeContactListGroup.getContactListGroup())); 141 }); 142 } 143 144 if(!hasExecutionErrors(eea)) { 145 contactLists.forEach((contactList) -> { 146 addContactListToParty(eea, party, contactList, null, createdBy); 147 }); 148 } 149 } 150 151}