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.form.DeletePartyContactMechanismRelationshipForm;
020import com.echothree.model.control.contact.server.control.ContactControl;
021import com.echothree.model.control.party.server.control.PartyControl;
022import com.echothree.model.data.contact.server.entity.ContactMechanism;
023import com.echothree.model.data.contact.server.entity.PartyContactMechanism;
024import com.echothree.model.data.contact.server.entity.PartyContactMechanismRelationship;
025import com.echothree.model.data.party.server.entity.Party;
026import com.echothree.model.data.user.common.pk.UserVisitPK;
027import com.echothree.util.common.message.ExecutionErrors;
028import com.echothree.util.common.validation.FieldDefinition;
029import com.echothree.util.common.validation.FieldType;
030import com.echothree.util.common.command.BaseResult;
031import com.echothree.util.server.control.BaseSimpleCommand;
032import com.echothree.util.server.persistence.Session;
033import java.util.Arrays;
034import java.util.Collections;
035import java.util.List;
036
037public class DeletePartyContactMechanismRelationshipCommand
038        extends BaseSimpleCommand<DeletePartyContactMechanismRelationshipForm> {
039    
040    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
041    
042    static {
043        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
044                new FieldDefinition("PartyName", FieldType.ENTITY_NAME, true, null, null),
045                new FieldDefinition("FromContactMechanismName", FieldType.ENTITY_NAME, true, null, null),
046                new FieldDefinition("ToContactMechanismName", FieldType.ENTITY_NAME, true, null, null)
047                ));
048    }
049    
050    /** Creates a new instance of DeletePartyContactMechanismRelationshipCommand */
051    public DeletePartyContactMechanismRelationshipCommand(UserVisitPK userVisitPK, DeletePartyContactMechanismRelationshipForm form) {
052        super(userVisitPK, form, null, FORM_FIELD_DEFINITIONS, false);
053    }
054    
055    @Override
056    protected BaseResult execute() {
057        var partyControl = Session.getModelController(PartyControl.class);
058        String partyName = form.getPartyName();
059        Party party = partyControl.getPartyByName(partyName);
060        
061        if(party != null) {
062            var contactControl = Session.getModelController(ContactControl.class);
063            String fromContactMechanismName = form.getFromContactMechanismName();
064            ContactMechanism fromContactMechanism = contactControl.getContactMechanismByName(fromContactMechanismName);
065
066            if(fromContactMechanism != null) {
067                PartyContactMechanism fromPartyContactMechanism = contactControl.getPartyContactMechanism(party, fromContactMechanism);
068                
069                if(fromPartyContactMechanism != null) {
070                    String toContactMechanismName = form.getToContactMechanismName();
071                    ContactMechanism toContactMechanism = contactControl.getContactMechanismByName(toContactMechanismName);
072
073                    if(toContactMechanism != null) {
074                        PartyContactMechanism toPartyContactMechanism = contactControl.getPartyContactMechanism(party, toContactMechanism);
075                        
076                        if(toPartyContactMechanism != null) {
077                            PartyContactMechanismRelationship partyContactMechanismRelationship = contactControl.getPartyContactMechanismRelationshipForUpdate(fromPartyContactMechanism,
078                                    toPartyContactMechanism);
079
080                            if(partyContactMechanismRelationship != null) {
081                                contactControl.deletePartyContactMechanismRelationship(partyContactMechanismRelationship, getPartyPK());
082                            } else {
083                                addExecutionError(ExecutionErrors.UnknownPartyContactMechanismRelationship.name(), partyName, fromContactMechanismName, toContactMechanismName);
084                            }
085                        } else {
086                            addExecutionError(ExecutionErrors.UnknownToPartyContactMechanism.name(), partyName, toContactMechanismName);
087                        }
088                    } else {
089                        addExecutionError(ExecutionErrors.UnknownToContactMechanismName.name(), toContactMechanismName);
090                    }
091                } else {
092                    addExecutionError(ExecutionErrors.UnknownFromPartyContactMechanism.name(), partyName, fromContactMechanismName);
093                }
094            } else {
095                addExecutionError(ExecutionErrors.UnknownFromContactMechanismName.name(), fromContactMechanismName);
096            }
097        } else {
098            addExecutionError(ExecutionErrors.UnknownPartyName.name(), partyName);
099        }
100        
101        return null;
102    }
103    
104}