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.ContactWebAddressEdit;
021import com.echothree.control.user.contact.common.form.EditContactWebAddressForm;
022import com.echothree.control.user.contact.common.result.ContactResultFactory;
023import com.echothree.control.user.contact.common.result.EditContactWebAddressResult;
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 java.util.List;
044import javax.enterprise.context.Dependent;
045import javax.inject.Inject;
046
047@Dependent
048public class EditContactWebAddressCommand
049        extends BaseAbstractEditCommand<PartyContactMechanismSpec, ContactWebAddressEdit, EditContactWebAddressResult, 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(List.of(
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(), List.of(
061                        new SecurityRoleDefinition(SecurityRoleGroups.ContactMechanism.name(), SecurityRoles.Edit.name())
062                ))
063        ));
064
065        SPEC_FIELD_DEFINITIONS = List.of(
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 = List.of(
071                new FieldDefinition("Url", FieldType.URL, true, null, null),
072                new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L)
073        );
074    }
075
076    @Inject
077    ContactControl contactControl;
078
079    @Inject
080    PartyControl partyControl;
081
082    
083    /** Creates a new instance of EditContactWebAddressCommand */
084    public EditContactWebAddressCommand() {
085        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
086    }
087    
088    @Override
089    protected SecurityResult security() {
090        var securityResult = super.security();
091
092        return securityResult != null ? securityResult : selfOnly(spec);
093    }
094    
095    @Override
096    public EditContactWebAddressResult getResult() {
097        return ContactResultFactory.getEditContactWebAddressResult();
098    }
099
100    @Override
101    public ContactWebAddressEdit getEdit() {
102        return ContactEditFactory.getContactWebAddressEdit();
103    }
104
105    @Override
106    public PartyContactMechanism getEntity(EditContactWebAddressResult result) {
107        PartyContactMechanism partyContactMechanism = null;
108        var partyName = spec.getPartyName();
109        var party = partyName == null ? getParty() : partyControl.getPartyByName(partyName);
110
111        if(party != null) {
112            var contactMechanismName = spec.getContactMechanismName();
113            var contactMechanism = contactControl.getContactMechanismByName(contactMechanismName);
114
115            if(contactMechanism != null) {
116                if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
117                    partyContactMechanism = contactControl.getPartyContactMechanism(party, contactMechanism);
118                } else { // EditMode.UPDATE
119                    partyContactMechanism = contactControl.getPartyContactMechanismForUpdate(party, contactMechanism);
120                }
121
122                if(partyContactMechanism != null) {
123                    var lastContactMechanismDetail = contactMechanism.getLastDetail();
124                    var contactMechanismTypeName = lastContactMechanismDetail.getContactMechanismType().getContactMechanismTypeName();
125
126                    result.setContactMechanism(contactControl.getContactMechanismTransfer(getUserVisit(), contactMechanism));
127
128                    if(!ContactMechanismTypes.WEB_ADDRESS.name().equals(contactMechanismTypeName)) {
129                        addExecutionError(ExecutionErrors.InvalidContactMechanismType.name(), contactMechanismTypeName);
130                    }
131                } else {
132                    addExecutionError(ExecutionErrors.UnknownPartyContactMechanism.name(), partyName, contactMechanismName);
133                }
134            } else {
135                addExecutionError(ExecutionErrors.UnknownContactMechanismName.name(), contactMechanismName);
136            }
137        } else {
138            addExecutionError(ExecutionErrors.UnknownPartyName.name(), partyName);
139        }
140
141        return partyContactMechanism;
142    }
143
144    @Override
145    public ContactMechanism getLockEntity(PartyContactMechanism partyContactMechanism) {
146        return partyContactMechanism.getLastDetail().getContactMechanism();
147    }
148
149    @Override
150    public void fillInResult(EditContactWebAddressResult result, PartyContactMechanism partyContactMechanism) {
151        result.setContactMechanism(contactControl.getContactMechanismTransfer(getUserVisit(),
152                partyContactMechanism.getLastDetail().getContactMechanism()));
153    }
154
155    @Override
156    public void doLock(ContactWebAddressEdit edit, PartyContactMechanism partyContactMechanism) {
157        var contactMechanism = partyContactMechanism.getLastDetail().getContactMechanism();
158        var contactWebAddress = contactControl.getContactWebAddress(contactMechanism);
159        var partyContactMechanismDetail = partyContactMechanism.getLastDetail();
160
161        edit.setUrl(contactWebAddress.getUrl());
162        edit.setDescription(partyContactMechanismDetail.getDescription());
163    }
164
165    @Override
166    public void doUpdate(PartyContactMechanism partyContactMechanism) {
167        var updatedBy = getPartyPK();
168        var contactMechanism = partyContactMechanism.getLastDetail().getContactMechanism();
169        var contactWebAddressValue = contactControl.getContactWebAddressValueForUpdate(contactMechanism);
170        var partyContactMechanismDetailValue = contactControl.getPartyContactMechanismDetailValueForUpdate(partyContactMechanism);
171
172        contactWebAddressValue.setUrl(edit.getUrl());
173        partyContactMechanismDetailValue.setDescription(edit.getDescription());
174
175        contactControl.updateContactWebAddressFromValue(contactWebAddressValue, updatedBy);
176        contactControl.updatePartyContactMechanismFromValue(partyContactMechanismDetailValue, updatedBy);
177    }
178
179}
180