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.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 com.echothree.util.server.persistence.Session;
033import javax.enterprise.context.ApplicationScoped;
034import javax.enterprise.inject.spi.CDI;
035
036@ApplicationScoped
037public class ContactEmailAddressLogic
038    extends BaseLogic {
039
040    protected ContactEmailAddressLogic() {
041        super();
042    }
043
044    public static ContactEmailAddressLogic getInstance() {
045        return CDI.current().select(ContactEmailAddressLogic.class).get();
046    }
047    
048    public PartyContactMechanism createContactEmailAddress(Party party, String emailAddress, Boolean allowSolicitation,
049            String description, String contactMechanismPurposeName, BasePK createdBy) {
050        var contactControl = Session.getModelController(ContactControl.class);
051        var entityInstanceControl = Session.getModelController(EntityInstanceControl.class);
052        var workflowControl = Session.getModelController(WorkflowControl.class);
053        var contactMechanismName = SequenceGeneratorLogic.getInstance().getNextSequenceValue(null, SequenceTypes.CONTACT_MECHANISM.name());
054        var contactMechanismType = contactControl.getContactMechanismTypeByName(ContactMechanismTypes.EMAIL_ADDRESS.name());
055
056        var contactMechanism = contactControl.createContactMechanism(contactMechanismName, contactMechanismType,
057                allowSolicitation, createdBy);
058
059        contactControl.createContactEmailAddress(contactMechanism, emailAddress, createdBy);
060
061        var partyContactMechanism = contactControl.createPartyContactMechanism(party, contactMechanism, description, false, 1, createdBy);
062
063        var entityInstance = entityInstanceControl.getEntityInstanceByBasePK(contactMechanism.getPrimaryKey());
064        workflowControl.addEntityToWorkflowUsingNames(null, EmailAddressStatusConstants.Workflow_EMAIL_ADDRESS_STATUS,
065                EmailAddressStatusConstants.WorkflowEntrance_NEW_EMAIL_ADDRESS, entityInstance, null, null, createdBy);
066        workflowControl.addEntityToWorkflowUsingNames(null, EmailAddressVerificationConstants.Workflow_EMAIL_ADDRESS_VERIFICATION,
067                EmailAddressVerificationConstants.WorkflowEntrance_NEW_EMAIL_ADDRESS, entityInstance, null, null, createdBy);
068
069        if(contactMechanismPurposeName != null) {
070            var contactMechanismPurpose = contactControl.getContactMechanismPurposeByName(ContactMechanismPurposes.PRIMARY_EMAIL.name());
071
072            contactControl.createPartyContactMechanismPurpose(partyContactMechanism, contactMechanismPurpose, false, 1, createdBy);
073        }
074        
075        return partyContactMechanism;
076    }
077    
078}