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.geo.server.command; 018 019import com.echothree.control.user.geo.common.form.CreateGeoCodeAliasForm; 020import com.echothree.model.control.geo.server.control.GeoControl; 021import com.echothree.model.control.party.common.PartyTypes; 022import com.echothree.model.control.security.common.SecurityRoleGroups; 023import com.echothree.model.control.security.common.SecurityRoles; 024import com.echothree.model.data.geo.server.entity.GeoCode; 025import com.echothree.model.data.geo.server.entity.GeoCodeAlias; 026import com.echothree.model.data.geo.server.entity.GeoCodeAliasType; 027import com.echothree.model.data.geo.server.entity.GeoCodeScope; 028import com.echothree.model.data.geo.server.entity.GeoCodeType; 029import com.echothree.model.data.user.common.pk.UserVisitPK; 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.common.command.BaseResult; 034import com.echothree.util.server.control.BaseSimpleCommand; 035import com.echothree.util.server.control.CommandSecurityDefinition; 036import com.echothree.util.server.control.PartyTypeDefinition; 037import com.echothree.util.server.control.SecurityRoleDefinition; 038import com.echothree.util.server.persistence.Session; 039import java.util.Arrays; 040import java.util.Collections; 041import java.util.List; 042import java.util.regex.Matcher; 043import java.util.regex.Pattern; 044 045public class CreateGeoCodeAliasCommand 046 extends BaseSimpleCommand<CreateGeoCodeAliasForm> { 047 048 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 049 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 050 051 static { 052 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList( 053 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 054 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList( 055 new SecurityRoleDefinition(SecurityRoleGroups.GeoCodeAlias.name(), SecurityRoles.Create.name()) 056 ))) 057 ))); 058 059 FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 060 new FieldDefinition("GeoCodeName", FieldType.ENTITY_NAME, true, null, null), 061 new FieldDefinition("GeoCodeAliasTypeName", FieldType.ENTITY_NAME, true, null, null), 062 new FieldDefinition("Alias", FieldType.ENTITY_NAME, true, null, null) 063 )); 064 } 065 066 /** Creates a new instance of CreateGeoCodeAliasCommand */ 067 public CreateGeoCodeAliasCommand(UserVisitPK userVisitPK, CreateGeoCodeAliasForm form) { 068 super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false); 069 } 070 071 @Override 072 protected BaseResult execute() { 073 var geoControl = Session.getModelController(GeoControl.class); 074 String geoCodeName = form.getGeoCodeName(); 075 GeoCode geoCode = geoControl.getGeoCodeByName(geoCodeName); 076 077 if(geoCode != null) { 078 GeoCodeType geoCodeType = geoCode.getLastDetail().getGeoCodeType(); 079 String geoCodeAliasTypeName = form.getGeoCodeAliasTypeName(); 080 GeoCodeAliasType geoCodeAliasType = geoControl.getGeoCodeAliasTypeByNameForUpdate(geoCodeType, geoCodeAliasTypeName); 081 082 if(geoCodeAliasType != null) { 083 GeoCodeScope geoCodeScope = geoCode.getLastDetail().getGeoCodeScope(); 084 GeoCodeAlias geoCodeAlias = geoControl.getGeoCodeAliasForUpdate(geoCode, geoCodeAliasType); 085 String alias = form.getAlias(); 086 087 if(geoCodeAlias == null) { 088 geoCodeAlias = geoControl.getGeoCodeAliasByAliasWithinScope(geoCodeScope, geoCodeAliasType, alias); 089 090 if(geoCodeAlias == null) { 091 String validationPattern = geoCodeAliasType.getLastDetail().getValidationPattern(); 092 093 if(validationPattern != null) { 094 Pattern pattern = Pattern.compile(validationPattern); 095 Matcher m = pattern.matcher(alias); 096 097 if(!m.matches()) { 098 addExecutionError(ExecutionErrors.InvalidAlias.name(), alias); 099 } 100 } 101 102 if(!hasExecutionErrors()) { 103 geoControl.createGeoCodeAlias(geoCode, geoCodeAliasType, alias, getPartyPK()); 104 } 105 } else { 106 addExecutionError(ExecutionErrors.DuplicateGeoCodeAlias.name(), geoCodeName, geoCodeScope.getLastDetail().getGeoCodeScopeName(), geoCodeAliasTypeName, alias); 107 } 108 } else { 109 addExecutionError(ExecutionErrors.DuplicateGeoCodeAlias.name(), geoCodeName, geoCodeScope.getLastDetail().getGeoCodeScopeName(), geoCodeAliasTypeName, alias); 110 } 111 } else { 112 addExecutionError(ExecutionErrors.UnknownGeoCodeAliasTypeName.name(), geoCodeType.getLastDetail().getGeoCodeTypeName(), geoCodeAliasTypeName); 113 } 114 } else { 115 addExecutionError(ExecutionErrors.UnknownGeoCodeName.name(), geoCodeName); 116 } 117 118 return null; 119 } 120 121}