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