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.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.party.common.pk.PartyPK; 028import com.echothree.model.data.selector.server.entity.Selector; 029import com.echothree.model.data.selector.server.entity.SelectorKind; 030import com.echothree.model.data.selector.server.entity.SelectorType; 031import com.echothree.model.data.shipping.server.entity.ShippingMethod; 032import com.echothree.model.data.user.common.pk.UserVisitPK; 033import com.echothree.util.common.command.BaseResult; 034import com.echothree.util.common.message.ExecutionErrors; 035import com.echothree.util.common.validation.FieldDefinition; 036import com.echothree.util.common.validation.FieldType; 037import com.echothree.util.server.control.BaseSimpleCommand; 038import com.echothree.util.server.control.CommandSecurityDefinition; 039import com.echothree.util.server.control.PartyTypeDefinition; 040import com.echothree.util.server.control.SecurityRoleDefinition; 041import com.echothree.util.server.persistence.Session; 042import java.util.Arrays; 043import java.util.Collections; 044import java.util.List; 045 046public class CreateShippingMethodCommand 047 extends BaseSimpleCommand<CreateShippingMethodForm> { 048 049 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 050 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 051 052 static { 053 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList( 054 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 055 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList( 056 new SecurityRoleDefinition(SecurityRoleGroups.ShippingMethod.name(), SecurityRoles.Create.name()) 057 ))) 058 ))); 059 060 FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 061 new FieldDefinition("ShippingMethodName", FieldType.ENTITY_NAME, true, null, null), 062 new FieldDefinition("GeoCodeSelectorName", FieldType.ENTITY_NAME, false, null, null), 063 new FieldDefinition("ItemSelectorName", FieldType.ENTITY_NAME, false, null, null), 064 new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null), 065 new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L) 066 )); 067 } 068 069 /** Creates a new instance of CreateShippingMethodCommand */ 070 public CreateShippingMethodCommand(UserVisitPK userVisitPK, CreateShippingMethodForm form) { 071 super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false); 072 } 073 074 @Override 075 protected BaseResult execute() { 076 var shippingControl = Session.getModelController(ShippingControl.class); 077 String shippingMethodName = form.getShippingMethodName(); 078 ShippingMethod shippingMethod = shippingControl.getShippingMethodByName(shippingMethodName); 079 080 if(shippingMethod == null) { 081 String geoCodeSelectorName = form.getGeoCodeSelectorName(); 082 Selector geoCodeSelector = null; 083 084 if(geoCodeSelectorName != null) { 085 var selectorControl = Session.getModelController(SelectorControl.class); 086 SelectorKind selectorKind = selectorControl.getSelectorKindByName(SelectorKinds.POSTAL_ADDRESS.name()); 087 088 if(selectorKind != null) { 089 SelectorType selectorType = selectorControl.getSelectorTypeByName(selectorKind, 090 SelectorTypes.CARRIER.name()); 091 092 if(selectorType != null) { 093 geoCodeSelector = selectorControl.getSelectorByName(selectorType, geoCodeSelectorName); 094 } else { 095 addExecutionError(ExecutionErrors.UnknownSelectorTypeName.name(), SelectorTypes.SHIPPING_METHOD.name()); 096 } 097 } else { 098 addExecutionError(ExecutionErrors.UnknownSelectorKindName.name(), SelectorKinds.POSTAL_ADDRESS.name()); 099 } 100 } 101 102 if(geoCodeSelectorName == null || geoCodeSelector != null) { 103 String itemSelectorName = form.getItemSelectorName(); 104 Selector itemSelector = null; 105 106 if(itemSelectorName != null) { 107 var selectorControl = Session.getModelController(SelectorControl.class); 108 SelectorKind selectorKind = selectorControl.getSelectorKindByName(SelectorKinds.ITEM.name()); 109 110 if(selectorKind != null) { 111 SelectorType 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 PartyPK 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}