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.carrier.server.command; 018 019import com.echothree.control.user.carrier.common.form.CreateCarrierServiceOptionForm; 020import com.echothree.model.control.carrier.server.control.CarrierControl; 021import com.echothree.model.control.party.common.PartyTypes; 022import com.echothree.model.control.security.common.SecurityRoleGroups; 023import com.echothree.model.control.security.common.SecurityRoles; 024import com.echothree.model.control.selector.common.SelectorKinds; 025import com.echothree.model.control.selector.common.SelectorTypes; 026import com.echothree.model.control.selector.server.control.SelectorControl; 027import com.echothree.model.data.carrier.server.entity.Carrier; 028import com.echothree.model.data.carrier.server.entity.CarrierOption; 029import com.echothree.model.data.carrier.server.entity.CarrierService; 030import com.echothree.model.data.carrier.server.entity.CarrierServiceOption; 031import com.echothree.model.data.party.server.entity.Party; 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.user.common.pk.UserVisitPK; 036import com.echothree.util.common.command.BaseResult; 037import com.echothree.util.common.message.ExecutionErrors; 038import com.echothree.util.common.validation.FieldDefinition; 039import com.echothree.util.common.validation.FieldType; 040import com.echothree.util.server.control.BaseSimpleCommand; 041import com.echothree.util.server.control.CommandSecurityDefinition; 042import com.echothree.util.server.control.PartyTypeDefinition; 043import com.echothree.util.server.control.SecurityRoleDefinition; 044import com.echothree.util.server.persistence.Session; 045import java.util.Arrays; 046import java.util.Collections; 047import java.util.List; 048 049public class CreateCarrierServiceOptionCommand 050 extends BaseSimpleCommand<CreateCarrierServiceOptionForm> { 051 052 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 053 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 054 055 static { 056 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList( 057 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 058 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList( 059 new SecurityRoleDefinition(SecurityRoleGroups.CarrierServiceOption.name(), SecurityRoles.Create.name()) 060 ))) 061 ))); 062 063 FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 064 new FieldDefinition("CarrierName", FieldType.ENTITY_NAME, true, null, null), 065 new FieldDefinition("CarrierServiceName", FieldType.ENTITY_NAME, true, null, null), 066 new FieldDefinition("CarrierOptionName", FieldType.ENTITY_NAME, true, null, null), 067 new FieldDefinition("IsRecommended", FieldType.BOOLEAN, true, null, null), 068 new FieldDefinition("IsRequired", FieldType.BOOLEAN, true, null, null), 069 new FieldDefinition("RecommendedGeoCodeSelectorName", FieldType.ENTITY_NAME, false, null, null), 070 new FieldDefinition("RequiredGeoCodeSelectorName", FieldType.ENTITY_NAME, false, null, null), 071 new FieldDefinition("RecommendedItemSelectorName", FieldType.ENTITY_NAME, false, null, null), 072 new FieldDefinition("RequiredItemSelectorName", FieldType.ENTITY_NAME, false, null, null), 073 new FieldDefinition("RecommendedOrderSelectorName", FieldType.ENTITY_NAME, false, null, null), 074 new FieldDefinition("RequiredOrderSelectorName", FieldType.ENTITY_NAME, false, null, null), 075 new FieldDefinition("RecommendedShipmentSelectorName", FieldType.ENTITY_NAME, false, null, null), 076 new FieldDefinition("RequiredShipmentSelectorName", FieldType.ENTITY_NAME, false, null, null) 077 )); 078 } 079 080 /** Creates a new instance of CreateCarrierServiceOptionCommand */ 081 public CreateCarrierServiceOptionCommand(UserVisitPK userVisitPK, CreateCarrierServiceOptionForm form) { 082 super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false); 083 } 084 085 @Override 086 protected BaseResult execute() { 087 var carrierControl = Session.getModelController(CarrierControl.class); 088 String carrierName = form.getCarrierName(); 089 Carrier carrier = carrierControl.getCarrierByName(carrierName); 090 091 if(carrier != null) { 092 Party carrierParty = carrier.getParty(); 093 String carrierServiceName = form.getCarrierServiceName(); 094 CarrierService carrierService = carrierControl.getCarrierServiceByName(carrierParty, carrierServiceName); 095 096 if(carrierService != null) { 097 String carrierOptionName = form.getCarrierOptionName(); 098 CarrierOption carrierOption = carrierControl.getCarrierOptionByName(carrierParty, carrierOptionName); 099 100 if(carrierOption != null) { 101 CarrierServiceOption carrierServiceOption = carrierControl.getCarrierServiceOption(carrierService, carrierOption); 102 103 if(carrierServiceOption == null) { 104 var selectorControl = Session.getModelController(SelectorControl.class); 105 SelectorKind selectorKind = selectorControl.getSelectorKindByName(SelectorKinds.POSTAL_ADDRESS.name()); 106 107 if(selectorKind != null) { 108 SelectorType selectorType = selectorControl.getSelectorTypeByName(selectorKind, SelectorTypes.CARRIER_SERVICE_OPTION.name()); 109 110 if(selectorType != null) { 111 String recommendedGeoCodeSelectorName = form.getRecommendedGeoCodeSelectorName(); 112 Selector recommendedGeoCodeSelector = null; 113 114 if(recommendedGeoCodeSelectorName != null) { 115 recommendedGeoCodeSelector = selectorControl.getSelectorByName(selectorType, recommendedGeoCodeSelectorName); 116 } 117 118 if(recommendedGeoCodeSelectorName == null || recommendedGeoCodeSelector != null) { 119 String requiredGeoCodeSelectorName = form.getRequiredGeoCodeSelectorName(); 120 Selector requiredGeoCodeSelector = null; 121 122 if(requiredGeoCodeSelectorName != null) { 123 requiredGeoCodeSelector = selectorControl.getSelectorByName(selectorType, requiredGeoCodeSelectorName); 124 } 125 126 if(requiredGeoCodeSelectorName == null || requiredGeoCodeSelector != null) { 127 selectorKind = selectorControl.getSelectorKindByName(SelectorKinds.ITEM.name()); 128 129 if(selectorKind != null) { 130 selectorType = selectorControl.getSelectorTypeByName(selectorKind, SelectorTypes.CARRIER_SERVICE_OPTION.name()); 131 132 if(selectorType != null) { 133 String recommendedItemSelectorName = form.getRecommendedItemSelectorName(); 134 Selector recommendedItemSelector = null; 135 136 if(recommendedItemSelectorName != null) { 137 recommendedItemSelector = selectorControl.getSelectorByName(selectorType, recommendedItemSelectorName); 138 } 139 140 if(recommendedItemSelectorName == null || recommendedItemSelector != null) { 141 String requiredItemSelectorName = form.getRequiredItemSelectorName(); 142 Selector requiredItemSelector = null; 143 144 if(requiredItemSelectorName != null) { 145 requiredItemSelector = selectorControl.getSelectorByName(selectorType, requiredItemSelectorName); 146 } 147 148 if(requiredItemSelectorName == null || requiredItemSelector != null) { 149 selectorKind = selectorControl.getSelectorKindByName(SelectorKinds.ORDER.name()); 150 151 if(selectorKind != null) { 152 selectorType = selectorControl.getSelectorTypeByName(selectorKind, SelectorTypes.CARRIER_SERVICE_OPTION.name()); 153 154 if(selectorType != null) { 155 String recommendedOrderSelectorName = form.getRecommendedOrderSelectorName(); 156 Selector recommendedOrderSelector = null; 157 158 if(recommendedOrderSelectorName != null) { 159 recommendedOrderSelector = selectorControl.getSelectorByName(selectorType, recommendedOrderSelectorName); 160 } 161 162 if(recommendedOrderSelectorName == null || recommendedOrderSelector != null) { 163 String requiredOrderSelectorName = form.getRequiredOrderSelectorName(); 164 Selector requiredOrderSelector = null; 165 166 if(requiredOrderSelectorName != null) { 167 requiredOrderSelector = selectorControl.getSelectorByName(selectorType, requiredOrderSelectorName); 168 } 169 170 if(requiredOrderSelectorName == null || requiredOrderSelector != null) { 171 selectorKind = selectorControl.getSelectorKindByName(SelectorKinds.SHIPMENT.name()); 172 173 if(selectorKind != null) { 174 selectorType = selectorControl.getSelectorTypeByName(selectorKind, SelectorTypes.CARRIER_SERVICE_OPTION.name()); 175 176 if(selectorType != null) { 177 String recommendedShipmentSelectorName = form.getRecommendedShipmentSelectorName(); 178 Selector recommendedShipmentSelector = null; 179 180 if(recommendedShipmentSelectorName != null) { 181 recommendedShipmentSelector = selectorControl.getSelectorByName(selectorType, recommendedShipmentSelectorName); 182 } 183 184 if(recommendedShipmentSelectorName == null || recommendedShipmentSelector != null) { 185 String requiredShipmentSelectorName = form.getRequiredShipmentSelectorName(); 186 Selector requiredShipmentSelector = null; 187 188 if(requiredShipmentSelectorName != null) { 189 requiredShipmentSelector = selectorControl.getSelectorByName(selectorType, requiredShipmentSelectorName); 190 } 191 192 if(requiredShipmentSelectorName == null || requiredShipmentSelector != null) { 193 Boolean isRecommended = Boolean.valueOf(form.getIsRecommended()); 194 Boolean isRequired = Boolean.valueOf(form.getIsRequired()); 195 196 carrierControl.createCarrierServiceOption(carrierService, 197 carrierOption, isRecommended, isRequired, 198 recommendedGeoCodeSelector, requiredGeoCodeSelector, 199 recommendedItemSelector, requiredItemSelector, 200 recommendedOrderSelector, requiredOrderSelector, 201 recommendedShipmentSelector, requiredShipmentSelector, 202 getPartyPK()); 203 } else { 204 addExecutionError(ExecutionErrors.UnknownRequiredShipmentSelectorName.name(), requiredShipmentSelectorName); 205 } 206 } else { 207 addExecutionError(ExecutionErrors.UnknownRecommendedShipmentSelectorName.name(), recommendedShipmentSelectorName); 208 } 209 } else { 210 addExecutionError(ExecutionErrors.UnknownSelectorTypeName.name(), 211 SelectorKinds.SHIPMENT.name(), 212 SelectorTypes.CARRIER_SERVICE_OPTION.name()); 213 } 214 } else { 215 addExecutionError(ExecutionErrors.UnknownSelectorKindName.name(), 216 SelectorKinds.SHIPMENT.name()); 217 } 218 } else { 219 addExecutionError(ExecutionErrors.UnknownRequiredOrderSelectorName.name(), requiredOrderSelectorName); 220 } 221 } else { 222 addExecutionError(ExecutionErrors.UnknownRecommendedOrderSelectorName.name(), recommendedOrderSelectorName); 223 } 224 } else { 225 addExecutionError(ExecutionErrors.UnknownSelectorTypeName.name(), 226 SelectorKinds.ORDER.name(), 227 SelectorTypes.CARRIER_SERVICE_OPTION.name()); 228 } 229 } else { 230 addExecutionError(ExecutionErrors.UnknownSelectorKindName.name(), SelectorKinds.ORDER.name()); 231 } 232 } else { 233 addExecutionError(ExecutionErrors.UnknownRequiredItemSelectorName.name(), requiredItemSelectorName); 234 } 235 } else { 236 addExecutionError(ExecutionErrors.UnknownRecommendedItemSelectorName.name(), recommendedItemSelectorName); 237 } 238 } else { 239 addExecutionError(ExecutionErrors.UnknownSelectorTypeName.name(), SelectorKinds.ITEM.name(), 240 SelectorTypes.CARRIER_SERVICE_OPTION.name()); 241 } 242 } else { 243 addExecutionError(ExecutionErrors.UnknownSelectorKindName.name(), SelectorKinds.ITEM.name()); 244 } 245 } else { 246 addExecutionError(ExecutionErrors.UnknownRequiredGeoCodeSelectorName.name(), requiredGeoCodeSelectorName); 247 } 248 } else { 249 addExecutionError(ExecutionErrors.UnknownRecommendedGeoCodeSelectorName.name(), recommendedGeoCodeSelectorName); 250 } 251 } else { 252 addExecutionError(ExecutionErrors.UnknownSelectorTypeName.name(), SelectorKinds.POSTAL_ADDRESS.name(), 253 SelectorTypes.CARRIER_OPTION.name()); 254 } 255 } else { 256 addExecutionError(ExecutionErrors.UnknownSelectorKindName.name(), SelectorKinds.POSTAL_ADDRESS.name()); 257 } 258 } else { 259 addExecutionError(ExecutionErrors.DuplicateCarrierServiceOption.name(), carrierName, carrierServiceName, carrierOptionName); 260 } 261 } else { 262 addExecutionError(ExecutionErrors.UnknownCarrierOptionName.name(), carrierName, carrierOptionName); 263 } 264 } else { 265 addExecutionError(ExecutionErrors.UnknownCarrierServiceName.name(), carrierName, carrierServiceName); 266 } 267 } else { 268 addExecutionError(ExecutionErrors.UnknownCarrierName.name(), carrierName); 269 } 270 271 return null; 272 } 273 274}