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