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 com.echothree.util.server.persistence.Session; 044import java.util.List; 045import javax.enterprise.context.Dependent; 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 /** Creates a new instance of EditShippingMethodCommand */ 077 public EditShippingMethodCommand() { 078 super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 079 } 080 081 @Override 082 public EditShippingMethodResult getResult() { 083 return ShippingResultFactory.getEditShippingMethodResult(); 084 } 085 086 @Override 087 public ShippingMethodEdit getEdit() { 088 return ShippingEditFactory.getShippingMethodEdit(); 089 } 090 091 @Override 092 public ShippingMethod getEntity(EditShippingMethodResult result) { 093 var shippingControl = Session.getModelController(ShippingControl.class); 094 ShippingMethod shippingMethod; 095 var shippingMethodName = spec.getShippingMethodName(); 096 097 if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) { 098 shippingMethod = shippingControl.getShippingMethodByName(shippingMethodName); 099 } else { // EditMode.UPDATE 100 shippingMethod = shippingControl.getShippingMethodByNameForUpdate(shippingMethodName); 101 } 102 103 if(shippingMethod != null) { 104 result.setShippingMethod(shippingControl.getShippingMethodTransfer(getUserVisit(), shippingMethod)); 105 } else { 106 addExecutionError(ExecutionErrors.UnknownShippingMethodName.name(), shippingMethodName); 107 } 108 109 return shippingMethod; 110 } 111 112 @Override 113 public ShippingMethod getLockEntity(ShippingMethod shippingMethod) { 114 return shippingMethod; 115 } 116 117 @Override 118 public void fillInResult(EditShippingMethodResult result, ShippingMethod shippingMethod) { 119 var shippingControl = Session.getModelController(ShippingControl.class); 120 121 result.setShippingMethod(shippingControl.getShippingMethodTransfer(getUserVisit(), shippingMethod)); 122 } 123 124 Selector geoCodeSelector = null; 125 Selector itemSelector = null; 126 127 @Override 128 public void doLock(ShippingMethodEdit edit, ShippingMethod shippingMethod) { 129 var shippingControl = Session.getModelController(ShippingControl.class); 130 var shippingMethodDescription = shippingControl.getShippingMethodDescription(shippingMethod, getPreferredLanguage()); 131 var shippingMethodDetail = shippingMethod.getLastDetail(); 132 133 geoCodeSelector = shippingMethodDetail.getGeoCodeSelector(); 134 shippingMethodDetail.getItemSelector(); 135 136 edit.setShippingMethodName(shippingMethodDetail.getShippingMethodName()); 137 edit.setGeoCodeSelectorName(itemSelector == null? null: geoCodeSelector.getLastDetail().getSelectorName()); 138 edit.setItemSelectorName(itemSelector == null? null: itemSelector.getLastDetail().getSelectorName()); 139 edit.setSortOrder(shippingMethodDetail.getSortOrder().toString()); 140 141 if(shippingMethodDescription != null) { 142 edit.setDescription(shippingMethodDescription.getDescription()); 143 } 144 } 145 146 @Override 147 public void canUpdate(ShippingMethod shippingMethod) { 148 var shippingControl = Session.getModelController(ShippingControl.class); 149 var shippingMethodName = edit.getShippingMethodName(); 150 var duplicateShippingMethod = shippingControl.getShippingMethodByName(shippingMethodName); 151 152 if(duplicateShippingMethod == null || shippingMethod.equals(duplicateShippingMethod)) { 153 var geoCodeSelectorName = edit.getGeoCodeSelectorName(); 154 155 if(geoCodeSelectorName != null) { 156 var selectorControl = Session.getModelController(SelectorControl.class); 157 var selectorKind = selectorControl.getSelectorKindByName(SelectorKinds.ITEM.name()); 158 159 if(selectorKind != null) { 160 var selectorType = selectorControl.getSelectorTypeByName(selectorKind, 161 SelectorTypes.CARRIER.name()); 162 163 if(selectorType != null) { 164 geoCodeSelector = selectorControl.getSelectorByName(selectorType, geoCodeSelectorName); 165 } else { 166 addExecutionError(ExecutionErrors.UnknownSelectorTypeName.name(), SelectorTypes.SHIPPING_METHOD.name()); 167 } 168 } else { 169 addExecutionError(ExecutionErrors.UnknownSelectorKindName.name(), SelectorKinds.ITEM.name()); 170 } 171 } 172 173 if(geoCodeSelectorName == null || geoCodeSelector != null) { 174 var itemSelectorName = edit.getItemSelectorName(); 175 176 if(itemSelectorName != null) { 177 var selectorControl = Session.getModelController(SelectorControl.class); 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 shippingControl = Session.getModelController(ShippingControl.class); 208 var partyPK = getPartyPK(); 209 var shippingMethodDetailValue = shippingControl.getShippingMethodDetailValueForUpdate(shippingMethod); 210 var shippingMethodDescription = shippingControl.getShippingMethodDescriptionForUpdate(shippingMethod, getPreferredLanguage()); 211 var description = edit.getDescription(); 212 213 shippingMethodDetailValue.setShippingMethodName(edit.getShippingMethodName()); 214 shippingMethodDetailValue.setGeoCodeSelectorPK(geoCodeSelector == null ? null : geoCodeSelector.getPrimaryKey()); 215 shippingMethodDetailValue.setItemSelectorPK(itemSelector == null ? null : itemSelector.getPrimaryKey()); 216 shippingMethodDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder())); 217 218 shippingControl.updateShippingMethodFromValue(shippingMethodDetailValue, partyPK); 219 220 if(shippingMethodDescription == null && description != null) { 221 shippingControl.createShippingMethodDescription(shippingMethod, getPreferredLanguage(), description, partyPK); 222 } else { 223 if(shippingMethodDescription != null && description == null) { 224 shippingControl.deleteShippingMethodDescription(shippingMethodDescription, partyPK); 225 } else { 226 if(shippingMethodDescription != null && description != null) { 227 var shippingMethodDescriptionValue = shippingControl.getShippingMethodDescriptionValue(shippingMethodDescription); 228 229 shippingMethodDescriptionValue.setDescription(description); 230 shippingControl.updateShippingMethodDescriptionFromValue(shippingMethodDescriptionValue, partyPK); 231 } 232 } 233 } 234 } 235 236}