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