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.party.server.command;
018
019import com.echothree.control.user.party.common.form.GetPartyRelationshipForm;
020import com.echothree.control.user.party.common.result.PartyResultFactory;
021import com.echothree.model.control.party.common.PartyTypes;
022import com.echothree.model.control.party.server.control.PartyControl;
023import com.echothree.model.control.security.common.SecurityRoleGroups;
024import com.echothree.model.control.security.common.SecurityRoles;
025import com.echothree.model.data.party.server.entity.PartyRelationship;
026import com.echothree.model.data.user.common.pk.UserVisitPK;
027import com.echothree.util.common.command.BaseResult;
028import com.echothree.util.common.message.ExecutionErrors;
029import com.echothree.util.common.validation.FieldDefinition;
030import com.echothree.util.common.validation.FieldType;
031import com.echothree.util.server.control.BaseSingleEntityCommand;
032import com.echothree.util.server.control.CommandSecurityDefinition;
033import com.echothree.util.server.control.PartyTypeDefinition;
034import com.echothree.util.server.control.SecurityRoleDefinition;
035import com.echothree.util.server.persistence.Session;
036import java.util.List;
037
038public class GetPartyRelationshipCommand
039        extends BaseSingleEntityCommand<PartyRelationship, GetPartyRelationshipForm> {
040
041    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
042    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
043    
044    static {
045        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
046                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
047                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
048                        new SecurityRoleDefinition(SecurityRoleGroups.Party.name(), SecurityRoles.PartyRelationship.name())
049                ))
050        ));
051
052        FORM_FIELD_DEFINITIONS = List.of(
053                new FieldDefinition("PartyRelationshipTypeName", FieldType.ENTITY_NAME, true, null, null),
054                new FieldDefinition("FromPartyName", FieldType.ENTITY_NAME, true, null, null),
055                new FieldDefinition("FromRoleTypeName", FieldType.ENTITY_NAME, true, null, null),
056                new FieldDefinition("ToPartyName", FieldType.ENTITY_NAME, true, null, null),
057                new FieldDefinition("ToRoleTypeName", FieldType.ENTITY_NAME, true, null, null)
058        );
059    }
060    
061    /** Creates a new instance of GetPartyRelationshipCommand */
062    public GetPartyRelationshipCommand(UserVisitPK userVisitPK, GetPartyRelationshipForm form) {
063        super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true);
064    }
065
066    @Override
067    protected PartyRelationship getEntity() {
068        var partyControl = Session.getModelController(PartyControl.class);
069        var partyRelationshipTypeName = form.getPartyRelationshipTypeName();
070        var partyRelationshipType = partyControl.getPartyRelationshipTypeByName(partyRelationshipTypeName);
071        PartyRelationship partyRelationship = null;
072
073        if(partyRelationshipType != null) {
074            var fromPartyName = form.getFromPartyName();
075            var fromParty = partyControl.getPartyByName(fromPartyName);
076
077            if(fromParty != null) {
078                var fromRoleTypeName = form.getFromRoleTypeName();
079                var fromRoleType = partyControl.getRoleTypeByName(fromRoleTypeName);
080
081                if(fromRoleType != null) {
082                    var toPartyName = form.getToPartyName();
083                    var toParty = partyControl.getPartyByName(toPartyName);
084
085                    if(toParty != null) {
086                        var toRoleTypeName = form.getToRoleTypeName();
087                        var toRoleType = partyControl.getRoleTypeByName(toRoleTypeName);
088
089                        if(toRoleType != null) {
090                            partyRelationship = partyControl.getPartyRelationship(partyRelationshipType, fromParty, fromRoleType, toParty, toRoleType);
091                        } else {
092                            addExecutionError(ExecutionErrors.UnknownToRoleTypeName.name(), toRoleTypeName);
093                        }
094                    } else {
095                        addExecutionError(ExecutionErrors.UnknownToPartyName.name(), toPartyName);
096                    }
097                } else {
098                    addExecutionError(ExecutionErrors.UnknownFromRoleTypeName.name(), fromRoleTypeName);
099                }
100            } else {
101                addExecutionError(ExecutionErrors.UnknownFromPartyName.name(), fromPartyName);
102            }
103        } else {
104            addExecutionError(ExecutionErrors.UnknownPartyRelationshipTypeName.name(), partyRelationshipTypeName);
105        }
106
107        return partyRelationship;
108    }
109
110    @Override
111    protected BaseResult getResult(PartyRelationship partyRelationship) {
112        var result = PartyResultFactory.getGetPartyRelationshipResult();
113
114        if(partyRelationship != null) {
115            var partyControl = Session.getModelController(PartyControl.class);
116
117            result.setPartyRelationship(partyControl.getPartyRelationshipTransfer(getUserVisit(), partyRelationship));
118        }
119
120        return result;
121    }
122
123}