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.GetPartyAliasForm; 020import com.echothree.control.user.party.common.result.PartyResultFactory; 021import com.echothree.control.user.party.server.command.util.PartyAliasUtil; 022import com.echothree.model.control.party.common.PartyTypes; 023import com.echothree.model.control.party.server.control.PartyControl; 024import com.echothree.model.control.party.server.logic.PartyAliasTypeLogic; 025import com.echothree.model.control.party.server.logic.PartyLogic; 026import com.echothree.model.control.security.common.SecurityRoles; 027import com.echothree.model.data.party.server.entity.PartyAlias; 028import com.echothree.model.data.user.common.pk.UserVisitPK; 029import com.echothree.util.common.command.BaseResult; 030import com.echothree.util.common.message.ExecutionErrors; 031import com.echothree.util.common.validation.FieldDefinition; 032import com.echothree.util.common.validation.FieldType; 033import com.echothree.util.server.control.BaseSingleEntityCommand; 034import com.echothree.util.server.control.CommandSecurityDefinition; 035import com.echothree.util.server.control.PartyTypeDefinition; 036import com.echothree.util.server.control.SecurityRoleDefinition; 037import com.echothree.util.server.persistence.Session; 038import java.util.List; 039 040public class GetPartyAliasCommand 041 extends BaseSingleEntityCommand<PartyAlias, GetPartyAliasForm> { 042 043 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 044 045 static { 046 FORM_FIELD_DEFINITIONS = List.of( 047 new FieldDefinition("PartyName", FieldType.ENTITY_NAME, false, null, null), 048 new FieldDefinition("PartyTypeName", FieldType.ENTITY_NAME, false, null, null), 049 new FieldDefinition("PartyAliasTypeName", FieldType.ENTITY_NAME, false, null, null), 050 new FieldDefinition("Alias", FieldType.ENTITY_NAME, false, null, null) 051 ); 052 } 053 054 /** Creates a new instance of GetPartyAliasCommand */ 055 public GetPartyAliasCommand(UserVisitPK userVisitPK, GetPartyAliasForm form) { 056 super(userVisitPK, form, new CommandSecurityDefinition(List.of( 057 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 058 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 059 new SecurityRoleDefinition(PartyAliasUtil.getInstance().getSecurityRoleGroupNameBySpecs(form, form), SecurityRoles.Review.name()) 060 )) 061 )), FORM_FIELD_DEFINITIONS, false); 062 } 063 064 @Override 065 protected PartyAlias getEntity() { 066 var partyName = form.getPartyName(); 067 var partyTypeName = form.getPartyTypeName(); 068 var partyAliasTypeName = form.getPartyAliasTypeName(); 069 var alias = form.getAlias(); 070 // Must specify either PartyName + PartyAliasTypeName or PartyTypeName + PartyAliasTypeName + Alias 071 var parameterOption1 = (partyName != null) && (partyTypeName == null) && (partyAliasTypeName != null ) && (alias == null); 072 var parameterOption2 = (partyName == null) && (partyTypeName != null) && (partyAliasTypeName != null ) && (alias != null); 073 var parameterCount = (parameterOption1 ? 1 : 0) + (parameterOption2 ? 1 : 0); 074 PartyAlias partyAlias = null; 075 076 if(parameterCount == 1) { 077 var partyControl = Session.getModelController(PartyControl.class); 078 079 if(parameterOption1) { 080 var party = PartyLogic.getInstance().getPartyByName(this, partyName); 081 082 if(!hasExecutionErrors()) { 083 var partyType = party.getLastDetail().getPartyType(); 084 var partyAliasType = PartyAliasTypeLogic.getInstance().getPartyAliasTypeByName(this, partyType, partyAliasTypeName); 085 086 if(!hasExecutionErrors()) { 087 partyAlias = partyControl.getPartyAlias(party, partyAliasType); 088 089 if(partyAlias == null) { 090 addExecutionError(ExecutionErrors.UnknownPartyAlias.name(), party.getLastDetail().getPartyName(), 091 partyAliasType.getLastDetail().getPartyAliasTypeName()); 092 } 093 } 094 } 095 } else { 096 var partyAliasType = PartyAliasTypeLogic.getInstance().getPartyAliasTypeByName(this, partyTypeName, partyAliasTypeName); 097 098 if(!hasExecutionErrors()) { 099 partyAlias = partyControl.getPartyAliasByAlias(partyAliasType, alias); 100 101 if(partyAlias == null) { 102 var partyAliasTypeDetail = partyAliasType.getLastDetail(); 103 addExecutionError(ExecutionErrors.UnknownPartyAliasByAlias.name(), partyAliasTypeDetail.getPartyType().getPartyTypeName(), 104 partyAliasTypeDetail.getPartyAliasTypeName(), partyAliasType.getLastDetail().getPartyAliasTypeName(), alias); 105 } 106 } 107 } 108 } else { 109 addExecutionError(ExecutionErrors.InvalidParameterCount.name()); 110 } 111 112 return partyAlias; 113 } 114 115 @Override 116 protected BaseResult getResult(PartyAlias partyAlias) { 117 var result = PartyResultFactory.getGetPartyAliasResult(); 118 119 if(partyAlias != null) { 120 var partyControl = Session.getModelController(PartyControl.class); 121 122 result.setPartyAlias(partyControl.getPartyAliasTransfer(getUserVisit(), partyAlias)); 123 } 124 125 return result; 126 } 127 128}