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