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.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;
052
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(Collections.unmodifiableList(Arrays.asList(
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(), Collections.unmodifiableList(Arrays.asList(
066                        new SecurityRoleDefinition(SecurityRoleGroups.ContactMechanism.name(), SecurityRoles.Edit.name())
067                        )))
068                )));
069
070        SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
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 = Collections.unmodifiableList(Arrays.asList(
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    /** Creates a new instance of EditContactTelephoneCommand */
086    public EditContactTelephoneCommand(UserVisitPK userVisitPK, EditContactTelephoneForm form) {
087        super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
088    }
089    
090    @Override
091    protected SecurityResult security() {
092        var securityResult = super.security();
093
094        return securityResult != null ? securityResult : selfOnly(spec);
095    }
096
097    @Override
098    public EditContactTelephoneResult getResult() {
099        return ContactResultFactory.getEditContactTelephoneResult();
100    }
101
102    @Override
103    public ContactTelephoneEdit getEdit() {
104        return ContactEditFactory.getContactTelephoneEdit();
105    }
106
107    @Override
108    public PartyContactMechanism getEntity(EditContactTelephoneResult result) {
109        var partyControl = Session.getModelController(PartyControl.class);
110        PartyContactMechanism partyContactMechanism = null;
111        var partyName = spec.getPartyName();
112        var party = partyName == null ? getParty() : partyControl.getPartyByName(partyName);
113
114        if(party != null) {
115            var contactControl = Session.getModelController(ContactControl.class);
116            var contactMechanismName = spec.getContactMechanismName();
117            var contactMechanism = contactControl.getContactMechanismByName(contactMechanismName);
118
119            if(contactMechanism != null) {
120                if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
121                    partyContactMechanism = contactControl.getPartyContactMechanism(party, contactMechanism);
122                } else { // EditMode.UPDATE
123                    partyContactMechanism = contactControl.getPartyContactMechanismForUpdate(party, contactMechanism);
124                }
125
126                if(partyContactMechanism != null) {
127                    var lastContactMechanismDetail = contactMechanism.getLastDetail();
128                    var contactMechanismTypeName = lastContactMechanismDetail.getContactMechanismType().getContactMechanismTypeName();
129
130                    result.setContactMechanism(contactControl.getContactMechanismTransfer(getUserVisit(), contactMechanism));
131
132                    if(!ContactMechanismTypes.TELECOM_ADDRESS.name().equals(contactMechanismTypeName)) {
133                        addExecutionError(ExecutionErrors.InvalidContactMechanismType.name(), contactMechanismTypeName);
134                    }
135                } else {
136                    addExecutionError(ExecutionErrors.UnknownPartyContactMechanism.name(), partyName, contactMechanismName);
137                }
138            } else {
139                addExecutionError(ExecutionErrors.UnknownContactMechanismName.name(), contactMechanismName);
140            }
141        } else {
142            addExecutionError(ExecutionErrors.UnknownPartyName.name(), partyName);
143        }
144
145        return partyContactMechanism;
146    }
147
148    @Override
149    public ContactMechanism getLockEntity(PartyContactMechanism partyContactMechanism) {
150        return partyContactMechanism.getLastDetail().getContactMechanism();
151    }
152
153    @Override
154    public void fillInResult(EditContactTelephoneResult result, PartyContactMechanism partyContactMechanism) {
155        var contactControl = Session.getModelController(ContactControl.class);
156
157        result.setContactMechanism(contactControl.getContactMechanismTransfer(getUserVisit(),
158                partyContactMechanism.getLastDetail().getContactMechanism()));
159    }
160
161    @Override
162    public void doLock(ContactTelephoneEdit edit, PartyContactMechanism partyContactMechanism) {
163        var contactControl = Session.getModelController(ContactControl.class);
164        var geoControl = Session.getModelController(GeoControl.class);
165        var contactMechanism = partyContactMechanism.getLastDetail().getContactMechanism();
166        var contactMechanismDetail = contactMechanism.getLastDetail();
167        var contactTelephone = contactControl.getContactTelephone(contactMechanism);
168        var partyContactMechanismDetail = partyContactMechanism.getLastDetail();
169
170        edit.setAllowSolicitation(contactMechanismDetail.getAllowSolicitation().toString());
171        edit.setCountryName(geoControl.getAliasForCountry(contactTelephone.getCountryGeoCode()));
172        edit.setAreaCode(contactTelephone.getAreaCode());
173        edit.setTelephoneNumber(contactTelephone.getTelephoneNumber());
174        edit.setTelephoneExtension(contactTelephone.getTelephoneExtension());
175        edit.setDescription(partyContactMechanismDetail.getDescription());
176    }
177
178    GeoCode countryGeoCode;
179
180    @Override
181    public void canUpdate(PartyContactMechanism partyContactMechanism) {
182        var geoControl = Session.getModelController(GeoControl.class);
183        var countryName = edit.getCountryName();
184        var countryAlias = StringUtils.getInstance().cleanStringToName(countryName).toUpperCase(Locale.getDefault());
185
186        countryGeoCode = geoControl.getCountryByAlias(countryAlias);
187
188        if(countryGeoCode != null) {
189            var geoCodeCountry = geoControl.getGeoCodeCountry(countryGeoCode);
190            var areaCode = edit.getAreaCode();
191
192            if(!geoCodeCountry.getAreaCodeRequired() || areaCode != null) {
193                var areaCodePattern = geoCodeCountry.getAreaCodePattern();
194                var pattern = areaCodePattern == null? null: Pattern.compile(areaCodePattern);
195
196                if(pattern == null || pattern.matcher(areaCode).matches()) {
197                    var telephoneNumberPattern = geoCodeCountry.getTelephoneNumberPattern();
198                    var telephoneNumber = edit.getTelephoneNumber();
199
200                    pattern = telephoneNumberPattern == null? null: Pattern.compile(telephoneNumberPattern);
201
202                    if(!(pattern == null || pattern.matcher(telephoneNumber).matches())) {
203                        addExecutionError(ExecutionErrors.InvalidTelephoneNumber.name(), telephoneNumber);
204                    }
205                } else {
206                    addExecutionError(ExecutionErrors.InvalidAreaCode.name(), areaCode);
207                }
208            } else {
209                addExecutionError(ExecutionErrors.MissingAreaCode.name());
210            }
211        } else {
212            addExecutionError(ExecutionErrors.UnknownCountryName.name(), countryName);
213        }
214    }
215
216    @Override
217    public void doUpdate(PartyContactMechanism partyContactMechanism) {
218        var contactControl = Session.getModelController(ContactControl.class);
219        var updatedBy = getPartyPK();
220        var contactMechanism = partyContactMechanism.getLastDetail().getContactMechanism();
221        var contactMechanismDetailValue = contactControl.getContactMechanismDetailValue(contactMechanism.getLastDetail());
222        var contactTelephoneValue = contactControl.getContactTelephoneValueForUpdate(contactMechanism);
223        var partyContactMechanismDetailValue = contactControl.getPartyContactMechanismDetailValueForUpdate(partyContactMechanism);
224
225        contactMechanismDetailValue.setAllowSolicitation(Boolean.valueOf(edit.getAllowSolicitation()));
226        contactTelephoneValue.setCountryGeoCodePK(countryGeoCode.getPrimaryKey());
227        contactTelephoneValue.setAreaCode(edit.getAreaCode());
228        contactTelephoneValue.setTelephoneNumber(edit.getTelephoneNumber());
229        contactTelephoneValue.setTelephoneExtension(edit.getTelephoneExtension());
230        partyContactMechanismDetailValue.setDescription(edit.getDescription());
231
232        contactControl.updateContactMechanismFromValue(contactMechanismDetailValue, updatedBy);
233        contactControl.updateContactTelephoneFromValue(contactTelephoneValue, updatedBy);
234        contactControl.updatePartyContactMechanismFromValue(partyContactMechanismDetailValue, updatedBy);
235    }
236
237}
238