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.contact.server.logic;
018
019import com.echothree.model.control.contact.common.ContactMechanismPurposes;
020import com.echothree.model.control.contact.common.ContactMechanismTypes;
021import com.echothree.model.control.contact.common.workflow.EmailAddressStatusConstants;
022import com.echothree.model.control.contact.common.workflow.EmailAddressVerificationConstants;
023import com.echothree.model.control.contact.server.control.ContactControl;
024import com.echothree.model.control.core.server.control.EntityInstanceControl;
025import com.echothree.model.control.sequence.common.SequenceTypes;
026import com.echothree.model.control.sequence.server.logic.SequenceGeneratorLogic;
027import com.echothree.model.control.workflow.server.control.WorkflowControl;
028import com.echothree.model.data.contact.server.entity.PartyContactMechanism;
029import com.echothree.model.data.party.server.entity.Party;
030import com.echothree.util.common.persistence.BasePK;
031import com.echothree.util.server.control.BaseLogic;
032import javax.enterprise.context.ApplicationScoped;
033import javax.enterprise.inject.spi.CDI;
034import javax.inject.Inject;
035
036@ApplicationScoped
037public class ContactEmailAddressLogic
038    extends BaseLogic {
039
040    @Inject
041    ContactControl contactControl;
042
043    @Inject
044    EntityInstanceControl entityInstanceControl;
045
046    @Inject
047    WorkflowControl workflowControl;
048
049    @Inject
050    SequenceGeneratorLogic sequenceGeneratorLogic;
051
052    protected ContactEmailAddressLogic() {
053        super();
054    }
055
056    public static ContactEmailAddressLogic getInstance() {
057        return CDI.current().select(ContactEmailAddressLogic.class).get();
058    }
059    
060    public PartyContactMechanism createContactEmailAddress(Party party, String emailAddress, Boolean allowSolicitation,
061            String description, String contactMechanismPurposeName, BasePK createdBy) {
062        var contactMechanismName = sequenceGeneratorLogic.getNextSequenceValue(null, SequenceTypes.CONTACT_MECHANISM.name());
063        var contactMechanismType = contactControl.getContactMechanismTypeByName(ContactMechanismTypes.EMAIL_ADDRESS.name());
064
065        var contactMechanism = contactControl.createContactMechanism(contactMechanismName, contactMechanismType,
066                allowSolicitation, createdBy);
067
068        contactControl.createContactEmailAddress(contactMechanism, emailAddress, createdBy);
069
070        var partyContactMechanism = contactControl.createPartyContactMechanism(party, contactMechanism, description, false, 1, createdBy);
071
072        var entityInstance = entityInstanceControl.getEntityInstanceByBasePK(contactMechanism.getPrimaryKey());
073        workflowControl.addEntityToWorkflowUsingNames(null, EmailAddressStatusConstants.Workflow_EMAIL_ADDRESS_STATUS,
074                EmailAddressStatusConstants.WorkflowEntrance_NEW_EMAIL_ADDRESS, entityInstance, null, null, createdBy);
075        workflowControl.addEntityToWorkflowUsingNames(null, EmailAddressVerificationConstants.Workflow_EMAIL_ADDRESS_VERIFICATION,
076                EmailAddressVerificationConstants.WorkflowEntrance_NEW_EMAIL_ADDRESS, entityInstance, null, null, createdBy);
077
078        if(contactMechanismPurposeName != null) {
079            var contactMechanismPurpose = contactControl.getContactMechanismPurposeByName(ContactMechanismPurposes.PRIMARY_EMAIL.name());
080
081            contactControl.createPartyContactMechanismPurpose(partyContactMechanism, contactMechanismPurpose, false, 1, createdBy);
082        }
083        
084        return partyContactMechanism;
085    }
086    
087}