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 /** Creates a new instance of GetPartyRelationshipsCommand */ 070 public GetPartyRelationshipsCommand() { 071 super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true); 072 } 073 074 @Inject 075 PartyControl partyControl; 076 077 @Inject 078 PartyLogic partyLogic; 079 080 @Inject 081 PartyRelationshipLogic partyRelationshipLogic; 082 083 private PartyRelationshipType partyRelationshipType; 084 private Party fromParty; 085 private RoleType fromRoleType; 086 private Party toParty; 087 private RoleType toRoleType; 088 089 @Override 090 protected void handleForm() { 091 var fromPartyName = form.getFromPartyName(); 092 var fromRoleTypeName = form.getFromRoleTypeName(); 093 var toPartyName = form.getToPartyName(); 094 var toRoleTypeName = form.getToRoleTypeName(); 095 var fromParameterCount = (fromPartyName == null ? 0 : 1) + (fromRoleTypeName == null ? 0 : 1); 096 var toParameterCount = (toPartyName == null ? 0 : 1) + (toRoleTypeName == null ? 0 : 1); 097 098 if((fromParameterCount == 2 && toParameterCount == 0) || (fromParameterCount == 0 && toParameterCount == 2)) { 099 var partyRelationshipTypeName = form.getPartyRelationshipTypeName(); 100 101 partyRelationshipType = partyRelationshipLogic.getPartyRelationshipTypeByName(this, partyRelationshipTypeName); 102 103 if(!hasExecutionErrors()) { 104 if(fromParameterCount == 2) { 105 fromParty = partyLogic.getPartyByName(this, fromPartyName); 106 107 if(!hasExecutionErrors()) { 108 fromRoleType = partyRelationshipLogic.getRoleTypeByName(this, fromRoleTypeName); 109 } 110 } 111 112 if(toParameterCount == 2) { 113 toParty = partyLogic.getPartyByName(this, toPartyName); 114 115 if(!hasExecutionErrors()) { 116 toRoleType = partyRelationshipLogic.getRoleTypeByName(this, toRoleTypeName); 117 } 118 } 119 } 120 } else { 121 addExecutionError(ExecutionErrors.InvalidParameterCombination.name()); 122 } 123 } 124 125 @Override 126 protected Long getTotalEntities() { 127 Long total = null; 128 129 if(!hasExecutionErrors()) { 130 if(fromParty != null) { 131 total = partyControl.countPartyRelationshipsByFromRelationship(partyRelationshipType, fromParty, fromRoleType); 132 } else { 133 total = partyControl.countPartyRelationshipsByToRelationship(partyRelationshipType, toParty, toRoleType); 134 } 135 } 136 137 return total; 138 } 139 140 @Override 141 protected Collection<PartyRelationship> getEntities() { 142 Collection<PartyRelationship> partyRelationships = null; 143 144 if(!hasExecutionErrors()) { 145 if(fromParty != null) { 146 partyRelationships = partyControl.getPartyRelationshipsByFromRelationship(partyRelationshipType, fromParty, fromRoleType); 147 } else { 148 partyRelationships = partyControl.getPartyRelationshipsByToRelationship(partyRelationshipType, toParty, toRoleType); 149 } 150 } 151 152 return partyRelationships; 153 } 154 155 @Override 156 protected BaseResult getResult(Collection<PartyRelationship> entities) { 157 var result = PartyResultFactory.getGetPartyRelationshipsResult(); 158 159 if(entities != null) { 160 var userVisit = getUserVisit(); 161 162 if(fromParty != null) { 163 result.setFromParty(partyControl.getPartyTransfer(userVisit, fromParty)); 164 } 165 166 if(toParty != null) { 167 result.setToParty(partyControl.getPartyTransfer(userVisit, toParty)); 168 } 169 170 if(session.hasLimit(PartyRelationshipFactory.class)) { 171 result.setPartyRelationshipCount(getTotalEntities()); 172 } 173 174 result.setPartyRelationships(partyControl.getPartyRelationshipTransfers(userVisit, entities)); 175 } 176 177 return result; 178 } 179 180}