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 com.echothree.util.server.persistence.Session; 038import java.util.List; 039import javax.enterprise.context.Dependent; 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 /** Creates a new instance of CreateContactEmailAddressCommand */ 067 public CreateContactEmailAddressCommand() { 068 super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false); 069 } 070 071 @Override 072 protected SecurityResult security() { 073 var securityResult = super.security(); 074 075 return securityResult != null ? securityResult : selfOnly(form); 076 } 077 078 @Override 079 protected BaseResult execute() { 080 var result = ContactResultFactory.getCreateContactEmailAddressResult(); 081 var partyControl = Session.getModelController(PartyControl.class); 082 var partyName = form.getPartyName(); 083 var party = partyName == null ? getParty() : partyControl.getPartyByName(partyName); 084 085 if(party != null) { 086 BasePK createdBy = getPartyPK(); 087 var emailAddress = form.getEmailAddress(); 088 var allowSolicitation = Boolean.valueOf(form.getAllowSolicitation()); 089 var description = form.getDescription(); 090 091 var partyContactMechanism = ContactEmailAddressLogic.getInstance().createContactEmailAddress(party, 092 emailAddress, allowSolicitation, description, null, createdBy); 093 var contactMechanism = partyContactMechanism.getLastDetail().getContactMechanism(); 094 095 result.setContactMechanismName(contactMechanism.getLastDetail().getContactMechanismName()); 096 result.setEntityRef(contactMechanism.getPrimaryKey().getEntityRef()); 097 } else { 098 addExecutionError(ExecutionErrors.UnknownPartyName.name(), partyName); 099 } 100 101 return result; 102 } 103 104}