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