001// --------------------------------------------------------------------------------
002// Copyright 2002-2024 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.CoreControl;
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.ContactMechanism;
029import com.echothree.model.data.contact.server.entity.ContactMechanismPurpose;
030import com.echothree.model.data.contact.server.entity.ContactMechanismType;
031import com.echothree.model.data.contact.server.entity.PartyContactMechanism;
032import com.echothree.model.data.core.server.entity.EntityInstance;
033import com.echothree.model.data.party.server.entity.Party;
034import com.echothree.util.common.persistence.BasePK;
035import com.echothree.util.server.control.BaseLogic;
036import com.echothree.util.server.persistence.Session;
037
038public class ContactEmailAddressLogic
039    extends BaseLogic {
040    
041    private ContactEmailAddressLogic() {
042        super();
043    }
044    
045    private static class ContactEmailAddressLogicHolder {
046        static ContactEmailAddressLogic instance = new ContactEmailAddressLogic();
047    }
048    
049    public static ContactEmailAddressLogic getInstance() {
050        return ContactEmailAddressLogicHolder.instance;
051    }
052    
053    public PartyContactMechanism createContactEmailAddress(Party party, String emailAddress, Boolean allowSolicitation,
054            String description, String contactMechanismPurposeName, BasePK createdBy) {
055        var contactControl = Session.getModelController(ContactControl.class);
056        var coreControl = Session.getModelController(CoreControl.class);
057        var workflowControl = Session.getModelController(WorkflowControl.class);
058        String contactMechanismName = SequenceGeneratorLogic.getInstance().getNextSequenceValue(null, SequenceTypes.CONTACT_MECHANISM.name());
059        ContactMechanismType contactMechanismType = contactControl.getContactMechanismTypeByName(ContactMechanismTypes.EMAIL_ADDRESS.name());
060
061        ContactMechanism contactMechanism = contactControl.createContactMechanism(contactMechanismName, contactMechanismType,
062                allowSolicitation, createdBy);
063
064        contactControl.createContactEmailAddress(contactMechanism, emailAddress, createdBy);
065
066        PartyContactMechanism partyContactMechanism = contactControl.createPartyContactMechanism(party, contactMechanism, description, Boolean.FALSE, 1, createdBy);
067
068        EntityInstance entityInstance = coreControl.getEntityInstanceByBasePK(contactMechanism.getPrimaryKey());
069        workflowControl.addEntityToWorkflowUsingNames(null, EmailAddressStatusConstants.Workflow_EMAIL_ADDRESS_STATUS,
070                EmailAddressStatusConstants.WorkflowEntrance_NEW_EMAIL_ADDRESS, entityInstance, null, null, createdBy);
071        workflowControl.addEntityToWorkflowUsingNames(null, EmailAddressVerificationConstants.Workflow_EMAIL_ADDRESS_VERIFICATION,
072                EmailAddressVerificationConstants.WorkflowEntrance_NEW_EMAIL_ADDRESS, entityInstance, null, null, createdBy);
073
074        if(contactMechanismPurposeName != null) {
075            ContactMechanismPurpose contactMechanismPurpose = contactControl.getContactMechanismPurposeByName(ContactMechanismPurposes.PRIMARY_EMAIL.name());
076
077            contactControl.createPartyContactMechanismPurpose(partyContactMechanism, contactMechanismPurpose, Boolean.FALSE, 1, createdBy);
078        }
079        
080        return partyContactMechanism;
081    }
082    
083}