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.order.server.command; 018 019import com.echothree.control.user.order.common.form.CreateOrderAliasForm; 020import com.echothree.control.user.order.server.command.util.OrderAliasUtil; 021import com.echothree.model.control.order.server.control.OrderAliasControl; 022import com.echothree.model.control.order.server.control.OrderControl; 023import com.echothree.model.control.order.server.control.OrderTypeControl; 024import com.echothree.model.control.party.common.PartyTypes; 025import com.echothree.model.control.security.common.SecurityRoles; 026import com.echothree.model.data.order.server.entity.Order; 027import com.echothree.model.data.order.server.entity.OrderAliasType; 028import com.echothree.model.data.order.server.entity.OrderAliasTypeDetail; 029import com.echothree.model.data.order.server.entity.OrderType; 030import com.echothree.model.data.user.common.pk.UserVisitPK; 031import com.echothree.util.common.message.ExecutionErrors; 032import com.echothree.util.common.validation.FieldDefinition; 033import com.echothree.util.common.validation.FieldType; 034import com.echothree.util.common.command.BaseResult; 035import com.echothree.util.server.control.BaseSimpleCommand; 036import com.echothree.util.server.control.CommandSecurityDefinition; 037import com.echothree.util.server.control.PartyTypeDefinition; 038import com.echothree.util.server.control.SecurityRoleDefinition; 039import com.echothree.util.server.persistence.Session; 040import java.util.Arrays; 041import java.util.Collections; 042import java.util.List; 043import java.util.regex.Matcher; 044import java.util.regex.Pattern; 045 046public class CreateOrderAliasCommand 047 extends BaseSimpleCommand<CreateOrderAliasForm> { 048 049 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 050 051 static { 052 FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 053 new FieldDefinition("OrderTypeName", FieldType.ENTITY_NAME, true, null, null), 054 new FieldDefinition("OrderName", FieldType.ENTITY_NAME, true, null, null), 055 new FieldDefinition("OrderAliasTypeName", FieldType.ENTITY_NAME, true, null, null), 056 new FieldDefinition("Alias", FieldType.ENTITY_NAME, true, null, null) 057 )); 058 } 059 060 /** Creates a new instance of CreateOrderAliasCommand */ 061 public CreateOrderAliasCommand(UserVisitPK userVisitPK, CreateOrderAliasForm form) { 062 super(userVisitPK, form, new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList( 063 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 064 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList( 065 new SecurityRoleDefinition(OrderAliasUtil.getInstance().getSecurityRoleGroupNameByOrderTypeSpec(form), SecurityRoles.Create.name()) 066 ))) 067 ))), FORM_FIELD_DEFINITIONS, false); 068 } 069 070 @Override 071 protected BaseResult execute() { 072 var orderTypeControl = Session.getModelController(OrderTypeControl.class); 073 var orderTypeName = form.getOrderTypeName(); 074 var orderType = orderTypeControl.getOrderTypeByName(orderTypeName); 075 076 if(orderType != null) { 077 var orderControl = Session.getModelController(OrderControl.class); 078 String orderName = form.getOrderName(); 079 Order order = orderControl.getOrderByName(orderType, orderName); 080 081 if(order != null) { 082 var orderAliasControl = Session.getModelController(OrderAliasControl.class); 083 String orderAliasTypeName = form.getOrderAliasTypeName(); 084 OrderAliasType orderAliasType = orderAliasControl.getOrderAliasTypeByName(orderType, orderAliasTypeName); 085 086 if(orderAliasType != null) { 087 OrderAliasTypeDetail orderAliasTypeDetail = orderAliasType.getLastDetail(); 088 String validationPattern = orderAliasTypeDetail.getValidationPattern(); 089 String alias = form.getAlias(); 090 091 if(validationPattern != null) { 092 Pattern pattern = Pattern.compile(validationPattern); 093 Matcher m = pattern.matcher(alias); 094 095 if(!m.matches()) { 096 addExecutionError(ExecutionErrors.InvalidAlias.name(), alias); 097 } 098 } 099 100 if(!hasExecutionErrors()) { 101 if(orderAliasControl.getOrderAlias(order, orderAliasType) == null && orderAliasControl.getOrderAliasByAlias(orderAliasType, alias) == null) { 102 orderAliasControl.createOrderAlias(order, orderAliasType, alias, getPartyPK()); 103 } else { 104 addExecutionError(ExecutionErrors.DuplicateOrderAlias.name(), orderTypeName, orderName, orderAliasTypeName, alias); 105 } 106 } 107 } else { 108 addExecutionError(ExecutionErrors.UnknownOrderAliasTypeName.name(), orderTypeName, orderAliasTypeName); 109 } 110 } else { 111 addExecutionError(ExecutionErrors.UnknownOrderName.name(), orderTypeName, orderName); 112 } 113 } else { 114 addExecutionError(ExecutionErrors.UnknownOrderTypeName.name(), orderTypeName); 115 } 116 117 return null; 118 } 119 120}