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.control.user.contact.server.command;
018
019import com.echothree.control.user.contact.common.form.CreateContactEmailAddressForm;
020import com.echothree.control.user.contact.common.result.ContactResultFactory;
021import com.echothree.model.control.contact.server.logic.ContactEmailAddressLogic;
022import com.echothree.model.control.party.common.PartyTypes;
023import com.echothree.model.control.party.server.control.PartyControl;
024import com.echothree.model.control.security.common.SecurityRoleGroups;
025import com.echothree.model.control.security.common.SecurityRoles;
026import com.echothree.model.data.user.common.pk.UserVisitPK;
027import com.echothree.util.common.command.SecurityResult;
028import com.echothree.util.common.message.ExecutionErrors;
029import com.echothree.util.common.validation.FieldDefinition;
030import com.echothree.util.common.validation.FieldType;
031import com.echothree.util.common.command.BaseResult;
032import com.echothree.util.common.persistence.BasePK;
033import com.echothree.util.server.control.BaseSimpleCommand;
034import com.echothree.util.server.control.CommandSecurityDefinition;
035import com.echothree.util.server.control.PartyTypeDefinition;
036import com.echothree.util.server.control.SecurityRoleDefinition;
037import com.echothree.util.server.persistence.Session;
038import java.util.Arrays;
039import java.util.Collections;
040import java.util.List;
041import javax.enterprise.context.RequestScoped;
042
043@RequestScoped
044public class CreateContactEmailAddressCommand
045        extends BaseSimpleCommand<CreateContactEmailAddressForm> {
046    
047    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
048    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
049
050    static {
051        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList(
052                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
053                new PartyTypeDefinition(PartyTypes.CUSTOMER.name(), null),
054                new PartyTypeDefinition(PartyTypes.VENDOR.name(), null),
055                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList(
056                        new SecurityRoleDefinition(SecurityRoleGroups.ContactMechanism.name(), SecurityRoles.Create.name())
057                        )))
058                )));
059
060        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
061                new FieldDefinition("PartyName", FieldType.ENTITY_NAME, false, null, null),
062                new FieldDefinition("EmailAddress", FieldType.EMAIL_ADDRESS, true, null, null),
063                new FieldDefinition("AllowSolicitation", FieldType.BOOLEAN, true, null, null),
064                new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L)
065                ));
066    }
067    
068    /** Creates a new instance of CreateContactEmailAddressCommand */
069    public CreateContactEmailAddressCommand() {
070        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false);
071    }
072
073    @Override
074    protected SecurityResult security() {
075        var securityResult = super.security();
076
077        return securityResult != null ? securityResult : selfOnly(form);
078    }
079
080    @Override
081    protected BaseResult execute() {
082        var result = ContactResultFactory.getCreateContactEmailAddressResult();
083        var partyControl = Session.getModelController(PartyControl.class);
084        var partyName = form.getPartyName();
085        var party = partyName == null ? getParty() : partyControl.getPartyByName(partyName);
086        
087        if(party != null) {
088            BasePK createdBy = getPartyPK();
089            var emailAddress = form.getEmailAddress();
090            var allowSolicitation = Boolean.valueOf(form.getAllowSolicitation());
091            var description = form.getDescription();
092
093            var partyContactMechanism = ContactEmailAddressLogic.getInstance().createContactEmailAddress(party,
094                    emailAddress, allowSolicitation, description, null, createdBy);
095            var contactMechanism = partyContactMechanism.getLastDetail().getContactMechanism();
096            
097            result.setContactMechanismName(contactMechanism.getLastDetail().getContactMechanismName());
098            result.setEntityRef(contactMechanism.getPrimaryKey().getEntityRef());
099        } else {
100            addExecutionError(ExecutionErrors.UnknownPartyName.name(), partyName);
101        }
102        
103        return result;
104    }
105
106}