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.CreatePartyAliasForm; 020import com.echothree.control.user.party.server.command.util.PartyAliasUtil; 021import com.echothree.model.control.party.common.PartyTypes; 022import com.echothree.model.control.party.server.control.PartyControl; 023import com.echothree.model.control.security.common.SecurityRoles; 024import com.echothree.util.common.command.BaseResult; 025import com.echothree.util.common.message.ExecutionErrors; 026import com.echothree.util.common.validation.FieldDefinition; 027import com.echothree.util.common.validation.FieldType; 028import com.echothree.util.server.control.BaseSimpleCommand; 029import com.echothree.util.server.control.CommandSecurityDefinition; 030import com.echothree.util.server.control.PartyTypeDefinition; 031import com.echothree.util.server.control.SecurityRoleDefinition; 032import com.echothree.util.server.persistence.Session; 033import java.util.Arrays; 034import java.util.Collections; 035import java.util.List; 036import java.util.regex.Pattern; 037import javax.enterprise.context.RequestScoped; 038 039@RequestScoped 040public class CreatePartyAliasCommand 041 extends BaseSimpleCommand<CreatePartyAliasForm> { 042 043 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 044 045 static { 046 FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 047 new FieldDefinition("PartyName", FieldType.ENTITY_NAME, true, null, null), 048 new FieldDefinition("PartyAliasTypeName", FieldType.ENTITY_NAME, true, null, null), 049 new FieldDefinition("Alias", FieldType.ENTITY_NAME, true, null, null) 050 )); 051 } 052 053 /** Creates a new instance of CreatePartyAliasCommand */ 054 public CreatePartyAliasCommand() { 055 super(null, FORM_FIELD_DEFINITIONS, false); 056 } 057 058 @Override 059 protected CommandSecurityDefinition getCommandSecurityDefinition() { 060 return new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList( 061 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 062 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList( 063 new SecurityRoleDefinition(PartyAliasUtil.getInstance().getSecurityRoleGroupNameByPartySpec(form), SecurityRoles.Create.name()) 064 ))) 065 ))); 066 } 067 068 @Override 069 protected BaseResult execute() { 070 var partyControl = Session.getModelController(PartyControl.class); 071 var partyName = form.getPartyName(); 072 var party = partyControl.getPartyByName(partyName); 073 074 if(party != null) { 075 var partyType = party.getLastDetail().getPartyType(); 076 var partyAliasTypeName = form.getPartyAliasTypeName(); 077 var partyAliasType = partyControl.getPartyAliasTypeByName(partyType, partyAliasTypeName); 078 079 if(partyAliasType != null) { 080 var partyAliasTypeDetail = partyAliasType.getLastDetail(); 081 var validationPattern = partyAliasTypeDetail.getValidationPattern(); 082 var alias = form.getAlias(); 083 084 if(validationPattern != null) { 085 var pattern = Pattern.compile(validationPattern); 086 var m = pattern.matcher(alias); 087 088 if(!m.matches()) { 089 addExecutionError(ExecutionErrors.InvalidAlias.name(), alias); 090 } 091 } 092 093 if(!hasExecutionErrors()) { 094 if(partyControl.getPartyAlias(party, partyAliasType) == null && partyControl.getPartyAliasByAlias(partyAliasType, alias) == null) { 095 partyControl.createPartyAlias(party, partyAliasType, alias, getPartyPK()); 096 } else { 097 addExecutionError(ExecutionErrors.DuplicatePartyAlias.name(), partyName, partyAliasTypeName, alias); 098 } 099 } 100 } else { 101 addExecutionError(ExecutionErrors.UnknownPartyAliasTypeName.name(), partyType.getPartyTypeName(), partyAliasTypeName); 102 } 103 } else { 104 addExecutionError(ExecutionErrors.UnknownPartyName.name(), partyName); 105 } 106 107 return null; 108 } 109 110}