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