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.edit.ContactEditFactory; 020import com.echothree.control.user.contact.common.edit.ContactTelephoneEdit; 021import com.echothree.control.user.contact.common.form.EditContactTelephoneForm; 022import com.echothree.control.user.contact.common.result.ContactResultFactory; 023import com.echothree.control.user.contact.common.result.EditContactTelephoneResult; 024import com.echothree.control.user.contact.common.spec.PartyContactMechanismSpec; 025import com.echothree.model.control.contact.common.ContactMechanismTypes; 026import com.echothree.model.control.contact.server.control.ContactControl; 027import com.echothree.model.control.geo.server.control.GeoControl; 028import com.echothree.model.control.party.common.PartyTypes; 029import com.echothree.model.control.party.server.control.PartyControl; 030import com.echothree.model.control.security.common.SecurityRoleGroups; 031import com.echothree.model.control.security.common.SecurityRoles; 032import com.echothree.model.data.contact.server.entity.ContactMechanism; 033import com.echothree.model.data.contact.server.entity.PartyContactMechanism; 034import com.echothree.model.data.geo.server.entity.GeoCode; 035import com.echothree.model.data.user.common.pk.UserVisitPK; 036import com.echothree.util.common.command.SecurityResult; 037import com.echothree.util.common.message.ExecutionErrors; 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.common.command.EditMode; 042import com.echothree.util.server.control.BaseAbstractEditCommand; 043import com.echothree.util.server.control.CommandSecurityDefinition; 044import com.echothree.util.server.control.PartyTypeDefinition; 045import com.echothree.util.server.control.SecurityRoleDefinition; 046import java.util.List; 047import java.util.Locale; 048import java.util.regex.Pattern; 049import javax.enterprise.context.Dependent; 050import javax.inject.Inject; 051 052@Dependent 053public class EditContactTelephoneCommand 054 extends BaseAbstractEditCommand<PartyContactMechanismSpec, ContactTelephoneEdit, EditContactTelephoneResult, PartyContactMechanism, ContactMechanism> { 055 056 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 057 private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS; 058 private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS; 059 060 static { 061 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of( 062 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 063 new PartyTypeDefinition(PartyTypes.CUSTOMER.name(), null), 064 new PartyTypeDefinition(PartyTypes.VENDOR.name(), null), 065 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 066 new SecurityRoleDefinition(SecurityRoleGroups.ContactMechanism.name(), SecurityRoles.Edit.name()) 067 )) 068 )); 069 070 SPEC_FIELD_DEFINITIONS = List.of( 071 new FieldDefinition("PartyName", FieldType.ENTITY_NAME, false, null, null), 072 new FieldDefinition("ContactMechanismName", FieldType.ENTITY_NAME, true, null, null) 073 ); 074 075 EDIT_FIELD_DEFINITIONS = List.of( 076 new FieldDefinition("AllowSolicitation", FieldType.BOOLEAN, true, null, null), 077 new FieldDefinition("CountryName", FieldType.ENTITY_NAME, false, null, null), 078 new FieldDefinition("AreaCode", FieldType.STRING, false, 1L, 5L), 079 new FieldDefinition("TelephoneNumber", FieldType.STRING, true, 1L, 25L), 080 new FieldDefinition("TelephoneExtension", FieldType.NUMBERS, false, 1L, 10L), 081 new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L) 082 ); 083 } 084 085 @Inject 086 ContactControl contactControl; 087 088 @Inject 089 GeoControl geoControl; 090 091 @Inject 092 PartyControl partyControl; 093 094 095 /** Creates a new instance of EditContactTelephoneCommand */ 096 public EditContactTelephoneCommand() { 097 super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 098 } 099 100 @Override 101 protected SecurityResult security() { 102 var securityResult = super.security(); 103 104 return securityResult != null ? securityResult : selfOnly(spec); 105 } 106 107 @Override 108 public EditContactTelephoneResult getResult() { 109 return ContactResultFactory.getEditContactTelephoneResult(); 110 } 111 112 @Override 113 public ContactTelephoneEdit getEdit() { 114 return ContactEditFactory.getContactTelephoneEdit(); 115 } 116 117 @Override 118 public PartyContactMechanism getEntity(EditContactTelephoneResult result) { 119 PartyContactMechanism partyContactMechanism = null; 120 var partyName = spec.getPartyName(); 121 var party = partyName == null ? getParty() : partyControl.getPartyByName(partyName); 122 123 if(party != null) { 124 var contactMechanismName = spec.getContactMechanismName(); 125 var contactMechanism = contactControl.getContactMechanismByName(contactMechanismName); 126 127 if(contactMechanism != null) { 128 if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) { 129 partyContactMechanism = contactControl.getPartyContactMechanism(party, contactMechanism); 130 } else { // EditMode.UPDATE 131 partyContactMechanism = contactControl.getPartyContactMechanismForUpdate(party, contactMechanism); 132 } 133 134 if(partyContactMechanism != null) { 135 var lastContactMechanismDetail = contactMechanism.getLastDetail(); 136 var contactMechanismTypeName = lastContactMechanismDetail.getContactMechanismType().getContactMechanismTypeName(); 137 138 result.setContactMechanism(contactControl.getContactMechanismTransfer(getUserVisit(), contactMechanism)); 139 140 if(!ContactMechanismTypes.TELECOM_ADDRESS.name().equals(contactMechanismTypeName)) { 141 addExecutionError(ExecutionErrors.InvalidContactMechanismType.name(), contactMechanismTypeName); 142 } 143 } else { 144 addExecutionError(ExecutionErrors.UnknownPartyContactMechanism.name(), partyName, contactMechanismName); 145 } 146 } else { 147 addExecutionError(ExecutionErrors.UnknownContactMechanismName.name(), contactMechanismName); 148 } 149 } else { 150 addExecutionError(ExecutionErrors.UnknownPartyName.name(), partyName); 151 } 152 153 return partyContactMechanism; 154 } 155 156 @Override 157 public ContactMechanism getLockEntity(PartyContactMechanism partyContactMechanism) { 158 return partyContactMechanism.getLastDetail().getContactMechanism(); 159 } 160 161 @Override 162 public void fillInResult(EditContactTelephoneResult result, PartyContactMechanism partyContactMechanism) { 163 result.setContactMechanism(contactControl.getContactMechanismTransfer(getUserVisit(), 164 partyContactMechanism.getLastDetail().getContactMechanism())); 165 } 166 167 @Override 168 public void doLock(ContactTelephoneEdit edit, PartyContactMechanism partyContactMechanism) { 169 var contactMechanism = partyContactMechanism.getLastDetail().getContactMechanism(); 170 var contactMechanismDetail = contactMechanism.getLastDetail(); 171 var contactTelephone = contactControl.getContactTelephone(contactMechanism); 172 var partyContactMechanismDetail = partyContactMechanism.getLastDetail(); 173 174 edit.setAllowSolicitation(contactMechanismDetail.getAllowSolicitation().toString()); 175 edit.setCountryName(geoControl.getAliasForCountry(contactTelephone.getCountryGeoCode())); 176 edit.setAreaCode(contactTelephone.getAreaCode()); 177 edit.setTelephoneNumber(contactTelephone.getTelephoneNumber()); 178 edit.setTelephoneExtension(contactTelephone.getTelephoneExtension()); 179 edit.setDescription(partyContactMechanismDetail.getDescription()); 180 } 181 182 GeoCode countryGeoCode; 183 184 @Override 185 public void canUpdate(PartyContactMechanism partyContactMechanism) { 186 var countryName = edit.getCountryName(); 187 var countryAlias = StringUtils.getInstance().cleanStringToName(countryName).toUpperCase(Locale.getDefault()); 188 189 countryGeoCode = geoControl.getCountryByAlias(countryAlias); 190 191 if(countryGeoCode != null) { 192 var geoCodeCountry = geoControl.getGeoCodeCountry(countryGeoCode); 193 var areaCode = edit.getAreaCode(); 194 195 if(!geoCodeCountry.getAreaCodeRequired() || areaCode != null) { 196 var areaCodePattern = geoCodeCountry.getAreaCodePattern(); 197 var pattern = areaCodePattern == null? null: Pattern.compile(areaCodePattern); 198 199 if(pattern == null || pattern.matcher(areaCode).matches()) { 200 var telephoneNumberPattern = geoCodeCountry.getTelephoneNumberPattern(); 201 var telephoneNumber = edit.getTelephoneNumber(); 202 203 pattern = telephoneNumberPattern == null? null: Pattern.compile(telephoneNumberPattern); 204 205 if(!(pattern == null || pattern.matcher(telephoneNumber).matches())) { 206 addExecutionError(ExecutionErrors.InvalidTelephoneNumber.name(), telephoneNumber); 207 } 208 } else { 209 addExecutionError(ExecutionErrors.InvalidAreaCode.name(), areaCode); 210 } 211 } else { 212 addExecutionError(ExecutionErrors.MissingAreaCode.name()); 213 } 214 } else { 215 addExecutionError(ExecutionErrors.UnknownCountryName.name(), countryName); 216 } 217 } 218 219 @Override 220 public void doUpdate(PartyContactMechanism partyContactMechanism) { 221 var updatedBy = getPartyPK(); 222 var contactMechanism = partyContactMechanism.getLastDetail().getContactMechanism(); 223 var contactMechanismDetailValue = contactControl.getContactMechanismDetailValue(contactMechanism.getLastDetail()); 224 var contactTelephoneValue = contactControl.getContactTelephoneValueForUpdate(contactMechanism); 225 var partyContactMechanismDetailValue = contactControl.getPartyContactMechanismDetailValueForUpdate(partyContactMechanism); 226 227 contactMechanismDetailValue.setAllowSolicitation(Boolean.valueOf(edit.getAllowSolicitation())); 228 contactTelephoneValue.setCountryGeoCodePK(countryGeoCode.getPrimaryKey()); 229 contactTelephoneValue.setAreaCode(edit.getAreaCode()); 230 contactTelephoneValue.setTelephoneNumber(edit.getTelephoneNumber()); 231 contactTelephoneValue.setTelephoneExtension(edit.getTelephoneExtension()); 232 partyContactMechanismDetailValue.setDescription(edit.getDescription()); 233 234 contactControl.updateContactMechanismFromValue(contactMechanismDetailValue, updatedBy); 235 contactControl.updateContactTelephoneFromValue(contactTelephoneValue, updatedBy); 236 contactControl.updatePartyContactMechanismFromValue(partyContactMechanismDetailValue, updatedBy); 237 } 238 239} 240