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.CreateContactTelephoneForm; 020import com.echothree.control.user.contact.common.result.ContactResultFactory; 021import com.echothree.model.control.contact.common.ContactMechanismTypes; 022import com.echothree.model.control.contact.common.workflow.TelephoneStatusConstants; 023import com.echothree.model.control.contact.server.control.ContactControl; 024import com.echothree.model.control.core.server.control.EntityInstanceControl; 025import com.echothree.model.control.geo.server.control.GeoControl; 026import com.echothree.model.control.party.common.PartyTypes; 027import com.echothree.model.control.party.server.control.PartyControl; 028import com.echothree.model.control.security.common.SecurityRoleGroups; 029import com.echothree.model.control.security.common.SecurityRoles; 030import com.echothree.model.control.sequence.common.SequenceTypes; 031import com.echothree.model.control.sequence.server.logic.SequenceGeneratorLogic; 032import com.echothree.model.control.workflow.server.control.WorkflowControl; 033import com.echothree.model.data.user.common.pk.UserVisitPK; 034import com.echothree.util.common.command.BaseResult; 035import com.echothree.util.common.command.SecurityResult; 036import com.echothree.util.common.message.ExecutionErrors; 037import com.echothree.util.common.persistence.BasePK; 038import com.echothree.util.common.string.StringUtils; 039import com.echothree.util.common.validation.FieldDefinition; 040import com.echothree.util.common.validation.FieldType; 041import com.echothree.util.server.control.BaseSimpleCommand; 042import com.echothree.util.server.control.CommandSecurityDefinition; 043import com.echothree.util.server.control.PartyTypeDefinition; 044import com.echothree.util.server.control.SecurityRoleDefinition; 045import java.util.List; 046import java.util.Locale; 047import java.util.regex.Pattern; 048import javax.enterprise.context.Dependent; 049import javax.inject.Inject; 050 051@Dependent 052public class CreateContactTelephoneCommand 053 extends BaseSimpleCommand<CreateContactTelephoneForm> { 054 055 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 056 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 057 058 static { 059 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of( 060 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 061 new PartyTypeDefinition(PartyTypes.CUSTOMER.name(), null), 062 new PartyTypeDefinition(PartyTypes.VENDOR.name(), null), 063 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 064 new SecurityRoleDefinition(SecurityRoleGroups.ContactMechanism.name(), SecurityRoles.Create.name()) 065 )) 066 )); 067 068 FORM_FIELD_DEFINITIONS = List.of( 069 new FieldDefinition("PartyName", FieldType.ENTITY_NAME, false, null, null), 070 new FieldDefinition("CountryName", FieldType.ENTITY_NAME, true, null, null), 071 new FieldDefinition("AreaCode", FieldType.STRING, false, 1L, 5L), 072 new FieldDefinition("TelephoneNumber", FieldType.STRING, true, 1L, 25L), 073 new FieldDefinition("TelephoneExtension", FieldType.NUMBERS, false, 1L, 10L), 074 new FieldDefinition("AllowSolicitation", FieldType.BOOLEAN, true, null, null), 075 new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L) 076 ); 077 } 078 079 @Inject 080 ContactControl contactControl; 081 082 @Inject 083 EntityInstanceControl entityInstanceControl; 084 085 @Inject 086 GeoControl geoControl; 087 088 @Inject 089 PartyControl partyControl; 090 091 @Inject 092 WorkflowControl workflowControl; 093 094 @Inject 095 SequenceGeneratorLogic sequenceGeneratorLogic; 096 097 098 /** Creates a new instance of CreateContactTelephoneCommand */ 099 public CreateContactTelephoneCommand() { 100 super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false); 101 } 102 103 @Override 104 protected SecurityResult security() { 105 var securityResult = super.security(); 106 107 return securityResult != null ? securityResult : selfOnly(form); 108 } 109 110 @Override 111 protected BaseResult execute() { 112 var result = ContactResultFactory.getCreateContactTelephoneResult(); 113 var partyName = form.getPartyName(); 114 var party = partyName == null ? getParty() : partyControl.getPartyByName(partyName); 115 116 if(party != null) { 117 var countryName = form.getCountryName(); 118 var countryAlias = StringUtils.getInstance().cleanStringToName(countryName).toUpperCase(Locale.getDefault()); 119 var countryGeoCode = geoControl.getCountryByAlias(countryAlias); 120 121 if(countryGeoCode != null) { 122 var geoCodeCountry = geoControl.getGeoCodeCountry(countryGeoCode); 123 var areaCode = form.getAreaCode(); 124 125 if(!geoCodeCountry.getAreaCodeRequired() || areaCode != null) { 126 var areaCodePattern = geoCodeCountry.getAreaCodePattern(); 127 var pattern = areaCodePattern == null? null: Pattern.compile(areaCodePattern); 128 129 if(pattern == null || pattern.matcher(areaCode).matches()) { 130 var telephoneNumberPattern = geoCodeCountry.getTelephoneNumberPattern(); 131 var telephoneNumber = form.getTelephoneNumber(); 132 133 pattern = telephoneNumberPattern == null? null: Pattern.compile(telephoneNumberPattern); 134 135 if(pattern == null || pattern.matcher(telephoneNumber).matches()) { 136 BasePK createdBy = getPartyPK(); 137 var telephoneExtension = form.getTelephoneExtension(); 138 var allowSolicitation = Boolean.valueOf(form.getAllowSolicitation()); 139 var description = form.getDescription(); 140 var contactMechanismName = sequenceGeneratorLogic.getNextSequenceValue(null, SequenceTypes.CONTACT_MECHANISM.name()); 141 142 var contactMechanismType = contactControl.getContactMechanismTypeByName(ContactMechanismTypes.TELECOM_ADDRESS.name()); 143 var contactMechanism = contactControl.createContactMechanism(contactMechanismName, 144 contactMechanismType, allowSolicitation, createdBy); 145 146 contactControl.createContactTelephone(contactMechanism, countryGeoCode, areaCode, telephoneNumber, 147 telephoneExtension, createdBy); 148 149 contactControl.createPartyContactMechanism(party, contactMechanism, description, false, 1, createdBy); 150 151 var entityInstance = entityInstanceControl.getEntityInstanceByBasePK(contactMechanism.getPrimaryKey()); 152 workflowControl.addEntityToWorkflowUsingNames(null, TelephoneStatusConstants.Workflow_TELEPHONE_STATUS, 153 TelephoneStatusConstants.WorkflowEntrance_NEW_TELEPHONE, entityInstance, null, null, createdBy); 154 155 result.setContactMechanismName(contactMechanism.getLastDetail().getContactMechanismName()); 156 result.setEntityRef(contactMechanism.getPrimaryKey().getEntityRef()); 157 } else { 158 addExecutionError(ExecutionErrors.InvalidTelephoneNumber.name(), telephoneNumber); 159 } 160 } else { 161 addExecutionError(ExecutionErrors.InvalidAreaCode.name(), areaCode); 162 } 163 } else { 164 addExecutionError(ExecutionErrors.MissingAreaCode.name()); 165 } 166 } else { 167 addExecutionError(ExecutionErrors.UnknownCountryName.name(), countryName); 168 } 169 } else { 170 addExecutionError(ExecutionErrors.UnknownPartyName.name(), partyName); 171 } 172 173 return result; 174 } 175 176}