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.party.server.command;
018
019import com.echothree.control.user.party.common.form.GetPartyRelationshipsForm;
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.party.server.logic.PartyLogic;
024import com.echothree.model.control.party.server.logic.PartyRelationshipLogic;
025import com.echothree.model.control.security.common.SecurityRoleGroups;
026import com.echothree.model.control.security.common.SecurityRoles;
027import com.echothree.model.data.party.server.entity.Party;
028import com.echothree.model.data.party.server.entity.PartyRelationship;
029import com.echothree.model.data.party.server.entity.PartyRelationshipType;
030import com.echothree.model.data.party.server.entity.RoleType;
031import com.echothree.model.data.party.server.factory.PartyRelationshipFactory;
032import com.echothree.util.common.command.BaseResult;
033import com.echothree.util.common.message.ExecutionErrors;
034import com.echothree.util.common.validation.FieldDefinition;
035import com.echothree.util.common.validation.FieldType;
036import com.echothree.util.server.control.BasePaginatedMultipleEntitiesCommand;
037import com.echothree.util.server.control.CommandSecurityDefinition;
038import com.echothree.util.server.control.PartyTypeDefinition;
039import com.echothree.util.server.control.SecurityRoleDefinition;
040import java.util.Collection;
041import java.util.List;
042import javax.enterprise.context.Dependent;
043import javax.inject.Inject;
044
045@Dependent
046public class GetPartyRelationshipsCommand
047        extends BasePaginatedMultipleEntitiesCommand<PartyRelationship, GetPartyRelationshipsForm> {
048
049    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
050    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
051    
052    static {
053        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
054                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
055                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
056                        new SecurityRoleDefinition(SecurityRoleGroups.Party.name(), SecurityRoles.PartyRelationship.name())
057                ))
058        ));
059
060        FORM_FIELD_DEFINITIONS = List.of(
061                new FieldDefinition("PartyRelationshipTypeName", FieldType.ENTITY_NAME, true, null, null),
062                new FieldDefinition("FromPartyName", FieldType.ENTITY_NAME, false, null, null),
063                new FieldDefinition("FromRoleTypeName", FieldType.ENTITY_NAME, false, null, null),
064                new FieldDefinition("ToPartyName", FieldType.ENTITY_NAME, false, null, null),
065                new FieldDefinition("ToRoleTypeName", FieldType.ENTITY_NAME, false, null, null)
066        );
067    }
068
069    @Inject
070    PartyControl partyControl;
071
072    @Inject
073    PartyLogic partyLogic;
074
075    @Inject
076    PartyRelationshipLogic partyRelationshipLogic;
077
078
079    /** Creates a new instance of GetPartyRelationshipsCommand */
080    public GetPartyRelationshipsCommand() {
081        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true);
082    }
083
084    private PartyRelationshipType partyRelationshipType;
085    private Party fromParty;
086    private RoleType fromRoleType;
087    private Party toParty;
088    private RoleType toRoleType;
089
090    @Override
091    protected void handleForm() {
092        var fromPartyName = form.getFromPartyName();
093        var fromRoleTypeName = form.getFromRoleTypeName();
094        var toPartyName = form.getToPartyName();
095        var toRoleTypeName = form.getToRoleTypeName();
096        var fromParameterCount = (fromPartyName == null ? 0 : 1) + (fromRoleTypeName == null ? 0 : 1);
097        var toParameterCount = (toPartyName == null ? 0 : 1) + (toRoleTypeName == null ? 0 : 1);
098
099        if((fromParameterCount == 2 && toParameterCount == 0) || (fromParameterCount == 0 && toParameterCount == 2)) {
100            var partyRelationshipTypeName = form.getPartyRelationshipTypeName();
101
102            partyRelationshipType = partyRelationshipLogic.getPartyRelationshipTypeByName(this, partyRelationshipTypeName);
103
104            if(!hasExecutionErrors()) {
105                if(fromParameterCount == 2) {
106                    fromParty = partyLogic.getPartyByName(this, fromPartyName);
107
108                    if(!hasExecutionErrors()) {
109                        fromRoleType = partyRelationshipLogic.getRoleTypeByName(this, fromRoleTypeName);
110                    }
111                }
112
113                if(toParameterCount == 2) {
114                    toParty = partyLogic.getPartyByName(this, toPartyName);
115
116                    if(!hasExecutionErrors()) {
117                        toRoleType = partyRelationshipLogic.getRoleTypeByName(this, toRoleTypeName);
118                    }
119                }
120            }
121        } else {
122            addExecutionError(ExecutionErrors.InvalidParameterCombination.name());
123        }
124    }
125
126    @Override
127    protected Long getTotalEntities() {
128        Long total = null;
129
130        if(!hasExecutionErrors()) {
131            if(fromParty != null) {
132                total = partyControl.countPartyRelationshipsByFromRelationship(partyRelationshipType, fromParty, fromRoleType);
133            } else {
134                total = partyControl.countPartyRelationshipsByToRelationship(partyRelationshipType, toParty, toRoleType);
135            }
136        }
137
138        return total;
139    }
140
141    @Override
142    protected Collection<PartyRelationship> getEntities() {
143        Collection<PartyRelationship> partyRelationships = null;
144
145        if(!hasExecutionErrors()) {
146            if(fromParty != null) {
147                partyRelationships = partyControl.getPartyRelationshipsByFromRelationship(partyRelationshipType, fromParty, fromRoleType);
148            } else {
149                partyRelationships = partyControl.getPartyRelationshipsByToRelationship(partyRelationshipType, toParty, toRoleType);
150            }
151        }
152
153        return partyRelationships;
154    }
155
156    @Override
157    protected BaseResult getResult(Collection<PartyRelationship> entities) {
158        var result = PartyResultFactory.getGetPartyRelationshipsResult();
159
160        if(entities != null) {
161            var userVisit = getUserVisit();
162
163            if(fromParty != null) {
164                result.setFromParty(partyControl.getPartyTransfer(userVisit, fromParty));
165            }
166
167            if(toParty != null) {
168                result.setToParty(partyControl.getPartyTransfer(userVisit, toParty));
169            }
170
171            if(session.hasLimit(PartyRelationshipFactory.class)) {
172                result.setPartyRelationshipCount(getTotalEntities());
173            }
174
175            result.setPartyRelationships(partyControl.getPartyRelationshipTransfers(userVisit, entities));
176        }
177
178        return result;
179    }
180
181}