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.contactlist.server.logic; 018 019import com.echothree.control.user.contactlist.common.spec.ContactListUniversalSpec; 020import com.echothree.model.control.contactlist.common.exception.UnknownContactListContactMechanismPurposeException; 021import com.echothree.model.control.contactlist.common.exception.UnknownContactListNameException; 022import com.echothree.model.control.contactlist.common.exception.UnknownDefaultContactListException; 023import com.echothree.model.control.contactlist.common.workflow.PartyContactListStatusConstants; 024import com.echothree.model.control.contactlist.server.control.ContactListControl; 025import com.echothree.model.control.core.common.ComponentVendors; 026import com.echothree.model.control.core.common.EntityTypes; 027import com.echothree.model.control.core.common.exception.InvalidParameterCountException; 028import com.echothree.model.control.core.server.control.EntityInstanceControl; 029import com.echothree.model.control.core.server.logic.EntityInstanceLogic; 030import com.echothree.model.control.customer.server.control.CustomerControl; 031import com.echothree.model.control.party.common.PartyTypes; 032import com.echothree.model.control.party.server.logic.PartyLogic; 033import com.echothree.model.control.workflow.server.control.WorkflowControl; 034import com.echothree.model.data.contact.server.entity.ContactMechanismPurpose; 035import com.echothree.model.data.contactlist.server.entity.ContactList; 036import com.echothree.model.data.contactlist.server.entity.ContactListContactMechanismPurpose; 037import com.echothree.model.data.contactlist.server.entity.CustomerTypeContactList; 038import com.echothree.model.data.contactlist.server.entity.CustomerTypeContactListGroup; 039import com.echothree.model.data.contactlist.server.entity.PartyContactList; 040import com.echothree.model.data.contactlist.server.entity.PartyTypeContactList; 041import com.echothree.model.data.contactlist.server.entity.PartyTypeContactListGroup; 042import com.echothree.model.data.party.server.entity.Party; 043import com.echothree.util.common.message.ExecutionErrors; 044import com.echothree.util.common.persistence.BasePK; 045import com.echothree.util.server.control.BaseLogic; 046import com.echothree.util.server.message.ExecutionErrorAccumulator; 047import com.echothree.util.server.persistence.EntityPermission; 048import java.util.HashSet; 049import java.util.Set; 050import javax.enterprise.context.ApplicationScoped; 051import javax.enterprise.inject.spi.CDI; 052import javax.inject.Inject; 053 054@ApplicationScoped 055public class ContactListLogic 056 extends BaseLogic { 057 058 @Inject 059 ContactListControl contactListControl; 060 061 @Inject 062 CustomerControl customerControl; 063 064 @Inject 065 EntityInstanceControl entityInstanceControl; 066 067 @Inject 068 WorkflowControl workflowControl; 069 070 @Inject 071 ContactListChainLogic contactListChainLogic; 072 073 @Inject 074 EntityInstanceLogic entityInstanceLogic; 075 076 @Inject 077 PartyLogic partyLogic; 078 079 protected ContactListLogic() { 080 super(); 081 } 082 083 public static ContactListLogic getInstance() { 084 return CDI.current().select(ContactListLogic.class).get(); 085 } 086 087 public ContactList getContactListByName(final ExecutionErrorAccumulator eea, final String contactListName, 088 final EntityPermission entityPermission) { 089 var contactList = contactListControl.getContactListByName(contactListName, entityPermission); 090 091 if(contactList == null) { 092 handleExecutionError(UnknownContactListNameException.class, eea, ExecutionErrors.UnknownContactListName.name(), contactListName); 093 } 094 095 return contactList; 096 } 097 098 public ContactList getContactListByName(final ExecutionErrorAccumulator eea, final String contactListName) { 099 return getContactListByName(eea, contactListName, EntityPermission.READ_ONLY); 100 } 101 102 public ContactList getContactListByNameForUpdate(final ExecutionErrorAccumulator eea, final String contactListName) { 103 return getContactListByName(eea, contactListName, EntityPermission.READ_WRITE); 104 } 105 106 public ContactList getContactListByUniversalSpec(final ExecutionErrorAccumulator eea, 107 final ContactListUniversalSpec universalSpec, boolean allowDefault, final EntityPermission entityPermission) { 108 ContactList contactList = null; 109 var contactListName = universalSpec.getContactListName(); 110 var parameterCount = (contactListName == null ? 0 : 1) + entityInstanceLogic.countPossibleEntitySpecs(universalSpec); 111 112 switch(parameterCount) { 113 case 0 -> { 114 if(allowDefault) { 115 contactList = contactListControl.getDefaultContactList(entityPermission); 116 117 if(contactList == null) { 118 handleExecutionError(UnknownDefaultContactListException.class, eea, ExecutionErrors.UnknownDefaultContactList.name()); 119 } 120 } else { 121 handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name()); 122 } 123 } 124 case 1 -> { 125 if(contactListName == null) { 126 var entityInstance = entityInstanceLogic.getEntityInstance(eea, universalSpec, 127 ComponentVendors.ECHO_THREE.name(), EntityTypes.ContactList.name()); 128 129 if(eea == null || !eea.hasExecutionErrors()) { 130 contactList = contactListControl.getContactListByEntityInstance(entityInstance, entityPermission); 131 } 132 } else { 133 contactList = getContactListByName(eea, contactListName, entityPermission); 134 } 135 } 136 default -> 137 handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name()); 138 } 139 140 return contactList; 141 } 142 143 public ContactList getContactListByUniversalSpec(final ExecutionErrorAccumulator eea, 144 final ContactListUniversalSpec universalSpec, boolean allowDefault) { 145 return getContactListByUniversalSpec(eea, universalSpec, allowDefault, EntityPermission.READ_ONLY); 146 } 147 148 public ContactList getContactListByUniversalSpecForUpdate(final ExecutionErrorAccumulator eea, 149 final ContactListUniversalSpec universalSpec, boolean allowDefault) { 150 return getContactListByUniversalSpec(eea, universalSpec, allowDefault, EntityPermission.READ_WRITE); 151 } 152 153 public ContactListContactMechanismPurpose getContactListContactMechanismPurpose(final ExecutionErrorAccumulator eea, final ContactList contactList, 154 final ContactMechanismPurpose contactMechanismPurpose) { 155 var contactListContactMechanismPurpose = contactListControl.getContactListContactMechanismPurpose(contactList, contactMechanismPurpose); 156 157 if(contactListContactMechanismPurpose == null) { 158 handleExecutionError(UnknownContactListContactMechanismPurposeException.class, eea, ExecutionErrors.UnknownContactListContactMechanismPurpose.name(), 159 contactList.getLastDetail().getContactListName(), contactMechanismPurpose.getContactMechanismPurposeName()); 160 } 161 162 return contactListContactMechanismPurpose; 163 } 164 165 public boolean isPartyOnContactList(final Party party, final ContactList contactList) { 166 return contactListControl.getPartyContactList(party, contactList) != null; 167 } 168 169 public void addContactListToParty(final ExecutionErrorAccumulator eea, final Party party, final ContactList contactList, 170 final ContactListContactMechanismPurpose preferredContactListContactMechanismPurpose, final BasePK createdBy) { 171 var partyContactList = contactListControl.createPartyContactList(party, contactList, preferredContactListContactMechanismPurpose, createdBy); 172 var entityInstance = entityInstanceControl.getEntityInstanceByBasePK(partyContactList.getPrimaryKey()); 173 var workflowEntrance = contactList.getLastDetail().getDefaultPartyContactListStatus(); 174 var workflowEntranceName = workflowEntrance.getLastDetail().getWorkflowEntranceName(); 175 176 workflowControl.addEntityToWorkflow(workflowEntrance, entityInstance, null, null, createdBy); 177 178 if(workflowEntranceName.equals(PartyContactListStatusConstants.WorkflowEntrance_NEW_AWAITING_VERIFICATION)) { 179 contactListChainLogic.createContactListConfirmationChainInstance(eea, party, partyContactList, createdBy); 180 } else if(workflowEntranceName.equals(PartyContactListStatusConstants.WorkflowEntrance_NEW_ACTIVE)) { 181 contactListChainLogic.createContactListSubscribeChainInstance(eea, party, partyContactList, createdBy); 182 } 183 } 184 185 public void removeContactListFromParty(final ExecutionErrorAccumulator eea, final PartyContactList partyContactList, final BasePK deletedBy) { 186 var entityInstance = entityInstanceControl.getEntityInstanceByBasePK(partyContactList.getPrimaryKey()); 187 var workflowEntityStatus = workflowControl.getWorkflowEntityStatusByEntityInstanceForUpdateUsingNames(PartyContactListStatusConstants.Workflow_PARTY_CONTACT_LIST_STATUS, entityInstance); 188 var workflowStepName = workflowEntityStatus.getWorkflowStep().getLastDetail().getWorkflowStepName(); 189 190 if(workflowStepName.equals(PartyContactListStatusConstants.WorkflowStep_ACTIVE)) { 191 contactListChainLogic.createContactListUnsubscribeChainInstance(eea, partyContactList.getLastDetail().getParty(), partyContactList, deletedBy); 192 } 193 194 contactListControl.deletePartyContactList(partyContactList, deletedBy); 195 workflowControl.deleteWorkflowEntityStatus(workflowEntityStatus, deletedBy); 196 } 197 198 public void setupInitialContactLists(final ExecutionErrorAccumulator eea, final Party party, final BasePK createdBy) { 199 var partyType = party.getLastDetail().getPartyType(); 200 Set<ContactList> contactLists = new HashSet<>(); 201 202 contactListControl.getPartyTypeContactListsByPartyType(partyType) 203 .stream() 204 .filter(PartyTypeContactList::getAddWhenCreated) 205 .map(PartyTypeContactList::getContactList) 206 .forEach(contactLists::add); 207 208 contactListControl.getPartyTypeContactListGroupsByPartyType(partyType) 209 .stream() 210 .filter(PartyTypeContactListGroup::getAddWhenCreated) 211 .forEach((partyTypeContactListGroup) -> 212 contactLists.addAll(contactListControl.getContactListsByContactListGroup(partyTypeContactListGroup.getContactListGroup())) 213 ); 214 215 if(partyLogic.isPartyType(party, PartyTypes.CUSTOMER.name())) { 216 var customerType = customerControl.getCustomer(party).getCustomerType(); 217 218 contactListControl.getCustomerTypeContactListsByCustomerType(customerType) 219 .stream() 220 .filter(CustomerTypeContactList::getAddWhenCreated) 221 .map(CustomerTypeContactList::getContactList) 222 .forEach(contactLists::add); 223 224 contactListControl.getCustomerTypeContactListGroupsByCustomerType(customerType) 225 .stream() 226 .filter(CustomerTypeContactListGroup::getAddWhenCreated) 227 .forEach((customerTypeContactListGroup) -> 228 contactLists.addAll(contactListControl.getContactListsByContactListGroup(customerTypeContactListGroup.getContactListGroup())) 229 ); 230 } 231 232 if(!hasExecutionErrors(eea)) { 233 contactLists.forEach((contactList) -> { 234 addContactListToParty(eea, party, contactList, null, createdBy); 235 }); 236 } 237 } 238 239}