001// -------------------------------------------------------------------------------- 002// Copyright 2002-2026 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.shipping.server.command; 018 019import com.echothree.control.user.shipping.common.form.CreateShippingMethodForm; 020import com.echothree.model.control.party.common.PartyTypes; 021import com.echothree.model.control.security.common.SecurityRoleGroups; 022import com.echothree.model.control.security.common.SecurityRoles; 023import com.echothree.model.control.selector.common.SelectorKinds; 024import com.echothree.model.control.selector.common.SelectorTypes; 025import com.echothree.model.control.selector.server.control.SelectorControl; 026import com.echothree.model.control.shipping.server.control.ShippingControl; 027import com.echothree.model.data.selector.server.entity.Selector; 028import com.echothree.model.data.user.common.pk.UserVisitPK; 029import com.echothree.util.common.command.BaseResult; 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.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 java.util.List; 038import javax.enterprise.context.Dependent; 039import javax.inject.Inject; 040 041@Dependent 042public class CreateShippingMethodCommand 043 extends BaseSimpleCommand<CreateShippingMethodForm> { 044 045 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 046 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 047 048 static { 049 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of( 050 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 051 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 052 new SecurityRoleDefinition(SecurityRoleGroups.ShippingMethod.name(), SecurityRoles.Create.name()) 053 )) 054 )); 055 056 FORM_FIELD_DEFINITIONS = List.of( 057 new FieldDefinition("ShippingMethodName", FieldType.ENTITY_NAME, true, null, null), 058 new FieldDefinition("GeoCodeSelectorName", FieldType.ENTITY_NAME, false, null, null), 059 new FieldDefinition("ItemSelectorName", FieldType.ENTITY_NAME, false, null, null), 060 new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null), 061 new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L) 062 ); 063 } 064 065 @Inject 066 SelectorControl selectorControl; 067 068 @Inject 069 ShippingControl shippingControl; 070 071 072 /** Creates a new instance of CreateShippingMethodCommand */ 073 public CreateShippingMethodCommand() { 074 super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false); 075 } 076 077 @Override 078 protected BaseResult execute() { 079 var shippingMethodName = form.getShippingMethodName(); 080 var shippingMethod = shippingControl.getShippingMethodByName(shippingMethodName); 081 082 if(shippingMethod == null) { 083 var geoCodeSelectorName = form.getGeoCodeSelectorName(); 084 Selector geoCodeSelector = null; 085 086 if(geoCodeSelectorName != null) { 087 var selectorKind = selectorControl.getSelectorKindByName(SelectorKinds.POSTAL_ADDRESS.name()); 088 089 if(selectorKind != null) { 090 var selectorType = selectorControl.getSelectorTypeByName(selectorKind, 091 SelectorTypes.CARRIER.name()); 092 093 if(selectorType != null) { 094 geoCodeSelector = selectorControl.getSelectorByName(selectorType, geoCodeSelectorName); 095 } else { 096 addExecutionError(ExecutionErrors.UnknownSelectorTypeName.name(), SelectorTypes.SHIPPING_METHOD.name()); 097 } 098 } else { 099 addExecutionError(ExecutionErrors.UnknownSelectorKindName.name(), SelectorKinds.POSTAL_ADDRESS.name()); 100 } 101 } 102 103 if(geoCodeSelectorName == null || geoCodeSelector != null) { 104 var itemSelectorName = form.getItemSelectorName(); 105 Selector itemSelector = null; 106 107 if(itemSelectorName != null) { 108 var selectorKind = selectorControl.getSelectorKindByName(SelectorKinds.ITEM.name()); 109 110 if(selectorKind != null) { 111 var selectorType = selectorControl.getSelectorTypeByName(selectorKind, 112 SelectorTypes.CARRIER.name()); 113 114 if(selectorType != null) { 115 itemSelector = selectorControl.getSelectorByName(selectorType, itemSelectorName); 116 } else { 117 addExecutionError(ExecutionErrors.UnknownSelectorTypeName.name(), SelectorTypes.SHIPPING_METHOD.name()); 118 } 119 } else { 120 addExecutionError(ExecutionErrors.UnknownSelectorKindName.name(), SelectorKinds.ITEM.name()); 121 } 122 } 123 124 if(itemSelectorName == null || itemSelector != null) { 125 var createdBy = getPartyPK(); 126 var sortOrder = Integer.valueOf(form.getSortOrder()); 127 var description = form.getDescription(); 128 129 shippingMethod = shippingControl.createShippingMethod(shippingMethodName, geoCodeSelector, itemSelector, sortOrder, createdBy); 130 131 if(description != null) { 132 shippingControl.createShippingMethodDescription(shippingMethod, getPreferredLanguage(), description, 133 createdBy); 134 } 135 } else { 136 addExecutionError(ExecutionErrors.UnknownItemSelectorName.name(), itemSelectorName); 137 } 138 } else { 139 addExecutionError(ExecutionErrors.UnknownGeoCodeSelectorName.name(), geoCodeSelectorName); 140 } 141 } else { 142 addExecutionError(ExecutionErrors.DuplicateShippingMethodName.name(), shippingMethodName); 143 } 144 145 return null; 146 } 147 148}