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