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.ContactEmailAddressEdit;
021import com.echothree.control.user.contact.common.form.EditContactEmailAddressForm;
022import com.echothree.control.user.contact.common.result.ContactResultFactory;
023import com.echothree.control.user.contact.common.result.EditContactEmailAddressResult;
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.party.common.PartyTypes;
028import com.echothree.model.control.party.server.control.PartyControl;
029import com.echothree.model.control.security.common.SecurityRoleGroups;
030import com.echothree.model.control.security.common.SecurityRoles;
031import com.echothree.model.data.contact.server.entity.ContactMechanism;
032import com.echothree.model.data.contact.server.entity.PartyContactMechanism;
033import com.echothree.model.data.user.common.pk.UserVisitPK;
034import com.echothree.util.common.command.SecurityResult;
035import com.echothree.util.common.message.ExecutionErrors;
036import com.echothree.util.common.validation.FieldDefinition;
037import com.echothree.util.common.validation.FieldType;
038import com.echothree.util.common.command.EditMode;
039import com.echothree.util.server.control.BaseAbstractEditCommand;
040import com.echothree.util.server.control.CommandSecurityDefinition;
041import com.echothree.util.server.control.PartyTypeDefinition;
042import com.echothree.util.server.control.SecurityRoleDefinition;
043import com.echothree.util.server.persistence.Session;
044import java.util.Arrays;
045import java.util.Collections;
046import java.util.List;
047
048public class EditContactEmailAddressCommand
049        extends BaseAbstractEditCommand<PartyContactMechanismSpec, ContactEmailAddressEdit, EditContactEmailAddressResult, PartyContactMechanism, ContactMechanism> {
050
051    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
052    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
053    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
054    
055    static {
056        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList(
057                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
058                new PartyTypeDefinition(PartyTypes.CUSTOMER.name(), null),
059                new PartyTypeDefinition(PartyTypes.VENDOR.name(), null),
060                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList(
061                        new SecurityRoleDefinition(SecurityRoleGroups.ContactMechanism.name(), SecurityRoles.Edit.name())
062                        )))
063                )));
064
065        SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
066                new FieldDefinition("PartyName", FieldType.ENTITY_NAME, false, null, null),
067                new FieldDefinition("ContactMechanismName", FieldType.ENTITY_NAME, true, null, null)
068                ));
069        
070        EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
071                new FieldDefinition("AllowSolicitation", FieldType.BOOLEAN, true, null, null),
072                new FieldDefinition("EmailAddress", FieldType.EMAIL_ADDRESS, true, null, null),
073                new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L)
074                ));
075    }
076    
077    /** Creates a new instance of EditContactEmailAddressCommand */
078    public EditContactEmailAddressCommand(UserVisitPK userVisitPK, EditContactEmailAddressForm form) {
079        super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
080    }
081    
082    @Override
083    protected SecurityResult security() {
084        var securityResult = super.security();
085
086        return securityResult != null ? securityResult : selfOnly(spec);
087    }
088
089    @Override
090    public EditContactEmailAddressResult getResult() {
091        return ContactResultFactory.getEditContactEmailAddressResult();
092    }
093
094    @Override
095    public ContactEmailAddressEdit getEdit() {
096        return ContactEditFactory.getContactEmailAddressEdit();
097    }
098
099    @Override
100    public PartyContactMechanism getEntity(EditContactEmailAddressResult result) {
101        var partyControl = Session.getModelController(PartyControl.class);
102        PartyContactMechanism partyContactMechanism = null;
103        var partyName = spec.getPartyName();
104        var party = partyName == null ? getParty() : partyControl.getPartyByName(partyName);
105
106        if(party != null) {
107            var contactControl = Session.getModelController(ContactControl.class);
108            var contactMechanismName = spec.getContactMechanismName();
109            var contactMechanism = contactControl.getContactMechanismByName(contactMechanismName);
110
111            if(contactMechanism != null) {
112                if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
113                    partyContactMechanism = contactControl.getPartyContactMechanism(party, contactMechanism);
114                } else { // EditMode.UPDATE
115                    partyContactMechanism = contactControl.getPartyContactMechanismForUpdate(party, contactMechanism);
116                }
117
118                if(partyContactMechanism != null) {
119                    var lastContactMechanismDetail = contactMechanism.getLastDetail();
120                    var contactMechanismTypeName = lastContactMechanismDetail.getContactMechanismType().getContactMechanismTypeName();
121
122                    result.setContactMechanism(contactControl.getContactMechanismTransfer(getUserVisit(), contactMechanism));
123
124                    if(!ContactMechanismTypes.EMAIL_ADDRESS.name().equals(contactMechanismTypeName)) {
125                        addExecutionError(ExecutionErrors.InvalidContactMechanismType.name(), contactMechanismTypeName);
126                    }
127                } else {
128                    addExecutionError(ExecutionErrors.UnknownPartyContactMechanism.name(), partyName, contactMechanismName);
129                }
130            } else {
131                addExecutionError(ExecutionErrors.UnknownContactMechanismName.name(), contactMechanismName);
132            }
133        } else {
134            addExecutionError(ExecutionErrors.UnknownPartyName.name(), partyName);
135        }
136
137        return partyContactMechanism;
138    }
139
140    @Override
141    public ContactMechanism getLockEntity(PartyContactMechanism partyContactMechanism) {
142        return partyContactMechanism.getLastDetail().getContactMechanism();
143    }
144
145    @Override
146    public void fillInResult(EditContactEmailAddressResult result, PartyContactMechanism partyContactMechanism) {
147        var contactControl = Session.getModelController(ContactControl.class);
148
149        result.setContactMechanism(contactControl.getContactMechanismTransfer(getUserVisit(),
150                partyContactMechanism.getLastDetail().getContactMechanism()));
151    }
152
153    @Override
154    public void doLock(ContactEmailAddressEdit edit, PartyContactMechanism partyContactMechanism) {
155        var contactControl = Session.getModelController(ContactControl.class);
156        var contactMechanism = partyContactMechanism.getLastDetail().getContactMechanism();
157        var contactMechanismDetail = contactMechanism.getLastDetail();
158        var contactEmailAddress = contactControl.getContactEmailAddress(contactMechanism);
159        var partyContactMechanismDetail = partyContactMechanism.getLastDetail();
160
161        edit.setAllowSolicitation(contactMechanismDetail.getAllowSolicitation().toString());
162        edit.setEmailAddress(contactEmailAddress.getEmailAddress());
163        edit.setDescription(partyContactMechanismDetail.getDescription());
164    }
165
166    @Override
167    public void canUpdate(PartyContactMechanism partyContactMechanism) {
168        var contactControl = Session.getModelController(ContactControl.class);
169        var userControl = getUserControl();
170        var party = partyContactMechanism.getLastDetail().getParty();
171        var contactMechanism = partyContactMechanism.getLastDetail().getContactMechanism();
172        var contactEmailAddress = contactControl.getContactEmailAddress(contactMechanism);
173        var userLogin = userControl.getUserLoginForUpdate(party);
174
175        // The only time we care about a duplicate email address is if the current email address is in use
176        // for the Party's UserLogin, and the new email address is in use by someone else as their login.
177        if(userLogin != null && userLogin.getUsername().equals(contactEmailAddress.getEmailAddress())) {
178            var emailAddress = edit.getEmailAddress();
179            var duplicateUserLogin = userControl.getUserLoginByUsername(emailAddress);
180
181            if(duplicateUserLogin != null && !duplicateUserLogin.getParty().equals(party)) {
182                addExecutionError(ExecutionErrors.DuplicateUsername.name(), emailAddress);
183            }
184        }
185    }
186
187    @Override
188    public void doUpdate(PartyContactMechanism partyContactMechanism) {
189        var contactControl = Session.getModelController(ContactControl.class);
190        var updatedBy = getPartyPK();
191        var contactMechanism = partyContactMechanism.getLastDetail().getContactMechanism();
192        var contactMechanismDetailValue = contactControl.getContactMechanismDetailValue(contactMechanism.getLastDetail());
193        var contactEmailAddressValue = contactControl.getContactEmailAddressValueForUpdate(contactMechanism);
194        var partyContactMechanismDetailValue = contactControl.getPartyContactMechanismDetailValueForUpdate(partyContactMechanism);
195
196        contactMechanismDetailValue.setAllowSolicitation(Boolean.valueOf(edit.getAllowSolicitation()));
197        contactEmailAddressValue.setEmailAddress(edit.getEmailAddress());
198        partyContactMechanismDetailValue.setDescription(edit.getDescription());
199
200        contactControl.updateContactMechanismFromValue(contactMechanismDetailValue, updatedBy);
201        contactControl.updateContactEmailAddressFromValue(contactEmailAddressValue, updatedBy);
202        contactControl.updatePartyContactMechanismFromValue(partyContactMechanismDetailValue, updatedBy);
203    }
204
205}
206