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.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 com.echothree.util.server.persistence.Session;
044import java.util.Arrays;
045import java.util.Collections;
046import java.util.List;
047import javax.enterprise.context.RequestScoped;
048
049@RequestScoped
050public class EditContactWebAddressCommand
051        extends BaseAbstractEditCommand<PartyContactMechanismSpec, ContactWebAddressEdit, EditContactWebAddressResult, PartyContactMechanism, ContactMechanism> {
052    
053    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
054    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
055    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
056    
057    static {
058        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList(
059                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
060                new PartyTypeDefinition(PartyTypes.CUSTOMER.name(), null),
061                new PartyTypeDefinition(PartyTypes.VENDOR.name(), null),
062                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList(
063                        new SecurityRoleDefinition(SecurityRoleGroups.ContactMechanism.name(), SecurityRoles.Edit.name())
064                        )))
065                )));
066
067        SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
068                new FieldDefinition("PartyName", FieldType.ENTITY_NAME, false, null, null),
069                new FieldDefinition("ContactMechanismName", FieldType.ENTITY_NAME, true, null, null)
070                ));
071        
072        EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
073                new FieldDefinition("Url", FieldType.URL, true, null, null),
074                new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L)
075                ));
076    }
077    
078    /** Creates a new instance of EditContactWebAddressCommand */
079    public EditContactWebAddressCommand() {
080        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
081    }
082    
083    @Override
084    protected SecurityResult security() {
085        var securityResult = super.security();
086
087        return securityResult != null ? securityResult : selfOnly(spec);
088    }
089    
090    @Override
091    public EditContactWebAddressResult getResult() {
092        return ContactResultFactory.getEditContactWebAddressResult();
093    }
094
095    @Override
096    public ContactWebAddressEdit getEdit() {
097        return ContactEditFactory.getContactWebAddressEdit();
098    }
099
100    @Override
101    public PartyContactMechanism getEntity(EditContactWebAddressResult result) {
102        var partyControl = Session.getModelController(PartyControl.class);
103        PartyContactMechanism partyContactMechanism = null;
104        var partyName = spec.getPartyName();
105        var party = partyName == null ? getParty() : partyControl.getPartyByName(partyName);
106
107        if(party != null) {
108            var contactControl = Session.getModelController(ContactControl.class);
109            var contactMechanismName = spec.getContactMechanismName();
110            var contactMechanism = contactControl.getContactMechanismByName(contactMechanismName);
111
112            if(contactMechanism != null) {
113                if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
114                    partyContactMechanism = contactControl.getPartyContactMechanism(party, contactMechanism);
115                } else { // EditMode.UPDATE
116                    partyContactMechanism = contactControl.getPartyContactMechanismForUpdate(party, contactMechanism);
117                }
118
119                if(partyContactMechanism != null) {
120                    var lastContactMechanismDetail = contactMechanism.getLastDetail();
121                    var contactMechanismTypeName = lastContactMechanismDetail.getContactMechanismType().getContactMechanismTypeName();
122
123                    result.setContactMechanism(contactControl.getContactMechanismTransfer(getUserVisit(), contactMechanism));
124
125                    if(!ContactMechanismTypes.WEB_ADDRESS.name().equals(contactMechanismTypeName)) {
126                        addExecutionError(ExecutionErrors.InvalidContactMechanismType.name(), contactMechanismTypeName);
127                    }
128                } else {
129                    addExecutionError(ExecutionErrors.UnknownPartyContactMechanism.name(), partyName, contactMechanismName);
130                }
131            } else {
132                addExecutionError(ExecutionErrors.UnknownContactMechanismName.name(), contactMechanismName);
133            }
134        } else {
135            addExecutionError(ExecutionErrors.UnknownPartyName.name(), partyName);
136        }
137
138        return partyContactMechanism;
139    }
140
141    @Override
142    public ContactMechanism getLockEntity(PartyContactMechanism partyContactMechanism) {
143        return partyContactMechanism.getLastDetail().getContactMechanism();
144    }
145
146    @Override
147    public void fillInResult(EditContactWebAddressResult result, PartyContactMechanism partyContactMechanism) {
148        var contactControl = Session.getModelController(ContactControl.class);
149
150        result.setContactMechanism(contactControl.getContactMechanismTransfer(getUserVisit(),
151                partyContactMechanism.getLastDetail().getContactMechanism()));
152    }
153
154    @Override
155    public void doLock(ContactWebAddressEdit edit, PartyContactMechanism partyContactMechanism) {
156        var contactControl = Session.getModelController(ContactControl.class);
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 contactControl = Session.getModelController(ContactControl.class);
168        var updatedBy = getPartyPK();
169        var contactMechanism = partyContactMechanism.getLastDetail().getContactMechanism();
170        var contactWebAddressValue = contactControl.getContactWebAddressValueForUpdate(contactMechanism);
171        var partyContactMechanismDetailValue = contactControl.getPartyContactMechanismDetailValueForUpdate(partyContactMechanism);
172
173        contactWebAddressValue.setUrl(edit.getUrl());
174        partyContactMechanismDetailValue.setDescription(edit.getDescription());
175
176        contactControl.updateContactWebAddressFromValue(contactWebAddressValue, updatedBy);
177        contactControl.updatePartyContactMechanismFromValue(partyContactMechanismDetailValue, updatedBy);
178    }
179
180}
181