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.edit.ShippingEditFactory; 020import com.echothree.control.user.shipping.common.edit.ShippingMethodEdit; 021import com.echothree.control.user.shipping.common.form.EditShippingMethodForm; 022import com.echothree.control.user.shipping.common.result.EditShippingMethodResult; 023import com.echothree.control.user.shipping.common.result.ShippingResultFactory; 024import com.echothree.control.user.shipping.common.spec.ShippingMethodSpec; 025import com.echothree.model.control.party.common.PartyTypes; 026import com.echothree.model.control.security.common.SecurityRoleGroups; 027import com.echothree.model.control.security.common.SecurityRoles; 028import com.echothree.model.control.selector.common.SelectorKinds; 029import com.echothree.model.control.selector.common.SelectorTypes; 030import com.echothree.model.control.selector.server.control.SelectorControl; 031import com.echothree.model.control.shipping.server.control.ShippingControl; 032import com.echothree.model.data.selector.server.entity.Selector; 033import com.echothree.model.data.shipping.server.entity.ShippingMethod; 034import com.echothree.model.data.user.common.pk.UserVisitPK; 035import com.echothree.util.common.command.EditMode; 036import com.echothree.util.common.message.ExecutionErrors; 037import com.echothree.util.common.validation.FieldDefinition; 038import com.echothree.util.common.validation.FieldType; 039import com.echothree.util.server.control.BaseAbstractEditCommand; 040import com.echothree.util.server.control.CommandSecurityDefinition; 041import com.echothree.util.server.control.PartyTypeDefinition; 042import com.echothree.util.server.control.SecurityRoleDefinition; 043import java.util.List; 044import javax.enterprise.context.Dependent; 045import javax.inject.Inject; 046 047@Dependent 048public class EditShippingMethodCommand 049 extends BaseAbstractEditCommand<ShippingMethodSpec, ShippingMethodEdit, EditShippingMethodResult, ShippingMethod, ShippingMethod> { 050 051 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 052 private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS; 053 private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS; 054 055 static { 056 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of( 057 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 058 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 059 new SecurityRoleDefinition(SecurityRoleGroups.ShippingMethod.name(), SecurityRoles.Edit.name()) 060 )) 061 )); 062 063 SPEC_FIELD_DEFINITIONS = List.of( 064 new FieldDefinition("ShippingMethodName", FieldType.ENTITY_NAME, true, null, null) 065 ); 066 067 EDIT_FIELD_DEFINITIONS = List.of( 068 new FieldDefinition("ShippingMethodName", FieldType.ENTITY_NAME, true, null, null), 069 new FieldDefinition("GeoCodeSelectorName", FieldType.ENTITY_NAME, false, null, null), 070 new FieldDefinition("ItemSelectorName", FieldType.ENTITY_NAME, false, null, null), 071 new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null), 072 new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L) 073 ); 074 } 075 076 @Inject 077 SelectorControl selectorControl; 078 079 @Inject 080 ShippingControl shippingControl; 081 082 083 /** Creates a new instance of EditShippingMethodCommand */ 084 public EditShippingMethodCommand() { 085 super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 086 } 087 088 @Override 089 public EditShippingMethodResult getResult() { 090 return ShippingResultFactory.getEditShippingMethodResult(); 091 } 092 093 @Override 094 public ShippingMethodEdit getEdit() { 095 return ShippingEditFactory.getShippingMethodEdit(); 096 } 097 098 @Override 099 public ShippingMethod getEntity(EditShippingMethodResult result) { 100 ShippingMethod shippingMethod; 101 var shippingMethodName = spec.getShippingMethodName(); 102 103 if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) { 104 shippingMethod = shippingControl.getShippingMethodByName(shippingMethodName); 105 } else { // EditMode.UPDATE 106 shippingMethod = shippingControl.getShippingMethodByNameForUpdate(shippingMethodName); 107 } 108 109 if(shippingMethod != null) { 110 result.setShippingMethod(shippingControl.getShippingMethodTransfer(getUserVisit(), shippingMethod)); 111 } else { 112 addExecutionError(ExecutionErrors.UnknownShippingMethodName.name(), shippingMethodName); 113 } 114 115 return shippingMethod; 116 } 117 118 @Override 119 public ShippingMethod getLockEntity(ShippingMethod shippingMethod) { 120 return shippingMethod; 121 } 122 123 @Override 124 public void fillInResult(EditShippingMethodResult result, ShippingMethod shippingMethod) { 125 result.setShippingMethod(shippingControl.getShippingMethodTransfer(getUserVisit(), shippingMethod)); 126 } 127 128 Selector geoCodeSelector = null; 129 Selector itemSelector = null; 130 131 @Override 132 public void doLock(ShippingMethodEdit edit, ShippingMethod shippingMethod) { 133 var shippingMethodDescription = shippingControl.getShippingMethodDescription(shippingMethod, getPreferredLanguage()); 134 var shippingMethodDetail = shippingMethod.getLastDetail(); 135 136 geoCodeSelector = shippingMethodDetail.getGeoCodeSelector(); 137 shippingMethodDetail.getItemSelector(); 138 139 edit.setShippingMethodName(shippingMethodDetail.getShippingMethodName()); 140 edit.setGeoCodeSelectorName(itemSelector == null? null: geoCodeSelector.getLastDetail().getSelectorName()); 141 edit.setItemSelectorName(itemSelector == null? null: itemSelector.getLastDetail().getSelectorName()); 142 edit.setSortOrder(shippingMethodDetail.getSortOrder().toString()); 143 144 if(shippingMethodDescription != null) { 145 edit.setDescription(shippingMethodDescription.getDescription()); 146 } 147 } 148 149 @Override 150 public void canUpdate(ShippingMethod shippingMethod) { 151 var shippingMethodName = edit.getShippingMethodName(); 152 var duplicateShippingMethod = shippingControl.getShippingMethodByName(shippingMethodName); 153 154 if(duplicateShippingMethod == null || shippingMethod.equals(duplicateShippingMethod)) { 155 var geoCodeSelectorName = edit.getGeoCodeSelectorName(); 156 157 if(geoCodeSelectorName != null) { 158 var selectorKind = selectorControl.getSelectorKindByName(SelectorKinds.ITEM.name()); 159 160 if(selectorKind != null) { 161 var selectorType = selectorControl.getSelectorTypeByName(selectorKind, 162 SelectorTypes.CARRIER.name()); 163 164 if(selectorType != null) { 165 geoCodeSelector = selectorControl.getSelectorByName(selectorType, geoCodeSelectorName); 166 } else { 167 addExecutionError(ExecutionErrors.UnknownSelectorTypeName.name(), SelectorTypes.SHIPPING_METHOD.name()); 168 } 169 } else { 170 addExecutionError(ExecutionErrors.UnknownSelectorKindName.name(), SelectorKinds.ITEM.name()); 171 } 172 } 173 174 if(geoCodeSelectorName == null || geoCodeSelector != null) { 175 var itemSelectorName = edit.getItemSelectorName(); 176 177 if(itemSelectorName != null) { 178 var selectorKind = selectorControl.getSelectorKindByName(SelectorKinds.ITEM.name()); 179 180 if(selectorKind != null) { 181 var selectorType = selectorControl.getSelectorTypeByName(selectorKind, 182 SelectorTypes.CARRIER.name()); 183 184 if(selectorType != null) { 185 itemSelector = selectorControl.getSelectorByName(selectorType, itemSelectorName); 186 } else { 187 addExecutionError(ExecutionErrors.UnknownSelectorTypeName.name(), SelectorTypes.SHIPPING_METHOD.name()); 188 } 189 } else { 190 addExecutionError(ExecutionErrors.UnknownSelectorKindName.name(), SelectorKinds.ITEM.name()); 191 } 192 } 193 194 if(itemSelectorName != null && itemSelector == null) { 195 addExecutionError(ExecutionErrors.UnknownItemSelectorName.name(), itemSelectorName); 196 } 197 } else { 198 addExecutionError(ExecutionErrors.UnknownGeoCodeSelectorName.name(), geoCodeSelectorName); 199 } 200 } else { 201 addExecutionError(ExecutionErrors.DuplicateShippingMethodName.name(), shippingMethodName); 202 } 203 } 204 205 @Override 206 public void doUpdate(ShippingMethod shippingMethod) { 207 var partyPK = getPartyPK(); 208 var shippingMethodDetailValue = shippingControl.getShippingMethodDetailValueForUpdate(shippingMethod); 209 var shippingMethodDescription = shippingControl.getShippingMethodDescriptionForUpdate(shippingMethod, getPreferredLanguage()); 210 var description = edit.getDescription(); 211 212 shippingMethodDetailValue.setShippingMethodName(edit.getShippingMethodName()); 213 shippingMethodDetailValue.setGeoCodeSelectorPK(geoCodeSelector == null ? null : geoCodeSelector.getPrimaryKey()); 214 shippingMethodDetailValue.setItemSelectorPK(itemSelector == null ? null : itemSelector.getPrimaryKey()); 215 shippingMethodDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder())); 216 217 shippingControl.updateShippingMethodFromValue(shippingMethodDetailValue, partyPK); 218 219 if(shippingMethodDescription == null && description != null) { 220 shippingControl.createShippingMethodDescription(shippingMethod, getPreferredLanguage(), description, partyPK); 221 } else { 222 if(shippingMethodDescription != null && description == null) { 223 shippingControl.deleteShippingMethodDescription(shippingMethodDescription, partyPK); 224 } else { 225 if(shippingMethodDescription != null && description != null) { 226 var shippingMethodDescriptionValue = shippingControl.getShippingMethodDescriptionValue(shippingMethodDescription); 227 228 shippingMethodDescriptionValue.setDescription(description); 229 shippingControl.updateShippingMethodDescriptionFromValue(shippingMethodDescriptionValue, partyPK); 230 } 231 } 232 } 233 } 234 235}