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.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 java.util.List;
038import javax.enterprise.context.Dependent;
039import javax.inject.Inject;
040
041@Dependent
042public class CreateContactEmailAddressCommand
043        extends BaseSimpleCommand<CreateContactEmailAddressForm> {
044    
045    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
046    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
047
048    static {
049        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
050                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
051                new PartyTypeDefinition(PartyTypes.CUSTOMER.name(), null),
052                new PartyTypeDefinition(PartyTypes.VENDOR.name(), null),
053                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
054                        new SecurityRoleDefinition(SecurityRoleGroups.ContactMechanism.name(), SecurityRoles.Create.name())
055                ))
056        ));
057
058        FORM_FIELD_DEFINITIONS = List.of(
059                new FieldDefinition("PartyName", FieldType.ENTITY_NAME, false, null, null),
060                new FieldDefinition("EmailAddress", FieldType.EMAIL_ADDRESS, true, null, null),
061                new FieldDefinition("AllowSolicitation", FieldType.BOOLEAN, true, null, null),
062                new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L)
063        );
064    }
065
066    @Inject
067    PartyControl partyControl;
068
069    @Inject
070    ContactEmailAddressLogic contactEmailAddressLogic;
071
072    
073    /** Creates a new instance of CreateContactEmailAddressCommand */
074    public CreateContactEmailAddressCommand() {
075        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false);
076    }
077
078    @Override
079    protected SecurityResult security() {
080        var securityResult = super.security();
081
082        return securityResult != null ? securityResult : selfOnly(form);
083    }
084
085    @Override
086    protected BaseResult execute() {
087        var result = ContactResultFactory.getCreateContactEmailAddressResult();
088        var partyName = form.getPartyName();
089        var party = partyName == null ? getParty() : partyControl.getPartyByName(partyName);
090        
091        if(party != null) {
092            BasePK createdBy = getPartyPK();
093            var emailAddress = form.getEmailAddress();
094            var allowSolicitation = Boolean.valueOf(form.getAllowSolicitation());
095            var description = form.getDescription();
096
097            var partyContactMechanism = contactEmailAddressLogic.createContactEmailAddress(party,
098                    emailAddress, allowSolicitation, description, null, createdBy);
099            var contactMechanism = partyContactMechanism.getLastDetail().getContactMechanism();
100            
101            result.setContactMechanismName(contactMechanism.getLastDetail().getContactMechanismName());
102            result.setEntityRef(contactMechanism.getPrimaryKey().getEntityRef());
103        } else {
104            addExecutionError(ExecutionErrors.UnknownPartyName.name(), partyName);
105        }
106        
107        return result;
108    }
109
110}