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