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