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