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