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