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.form.GetShippingMethodCarrierServicesForm; 020import com.echothree.control.user.shipping.common.result.GetShippingMethodCarrierServicesResult; 021import com.echothree.control.user.shipping.common.result.ShippingResultFactory; 022import com.echothree.model.control.carrier.server.control.CarrierControl; 023import com.echothree.model.control.party.common.PartyTypes; 024import com.echothree.model.control.security.common.SecurityRoleGroups; 025import com.echothree.model.control.security.common.SecurityRoles; 026import com.echothree.model.control.shipping.server.control.ShippingControl; 027import com.echothree.model.data.carrier.server.entity.Carrier; 028import com.echothree.model.data.carrier.server.entity.CarrierService; 029import com.echothree.model.data.party.server.entity.Party; 030import com.echothree.model.data.shipping.server.entity.ShippingMethod; 031import com.echothree.model.data.user.common.pk.UserVisitPK; 032import com.echothree.util.common.message.ExecutionErrors; 033import com.echothree.util.common.validation.FieldDefinition; 034import com.echothree.util.common.validation.FieldType; 035import com.echothree.util.common.command.BaseResult; 036import com.echothree.util.server.control.BaseSimpleCommand; 037import com.echothree.util.server.control.CommandSecurityDefinition; 038import com.echothree.util.server.control.PartyTypeDefinition; 039import com.echothree.util.server.control.SecurityRoleDefinition; 040import com.echothree.util.server.persistence.Session; 041import java.util.Arrays; 042import java.util.Collections; 043import java.util.List; 044 045public class GetShippingMethodCarrierServicesCommand 046 extends BaseSimpleCommand<GetShippingMethodCarrierServicesForm> { 047 048 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 049 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 050 051 static { 052 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList( 053 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 054 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList( 055 new SecurityRoleDefinition(SecurityRoleGroups.ShippingMethodCarrierService.name(), SecurityRoles.List.name()) 056 ))) 057 ))); 058 } 059 060 static { 061 FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 062 new FieldDefinition("ShippingMethodName", FieldType.ENTITY_NAME, false, null, null), 063 new FieldDefinition("CarrierName", FieldType.ENTITY_NAME, false, null, null), 064 new FieldDefinition("CarrierServiceName", FieldType.ENTITY_NAME, false, null, null) 065 )); 066 } 067 068 /** Creates a new instance of GetShippingMethodCarrierServicesCommand */ 069 public GetShippingMethodCarrierServicesCommand(UserVisitPK userVisitPK, GetShippingMethodCarrierServicesForm form) { 070 super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true); 071 } 072 073 @Override 074 protected BaseResult execute() { 075 var shippingControl = Session.getModelController(ShippingControl.class); 076 GetShippingMethodCarrierServicesResult result = ShippingResultFactory.getGetShippingMethodCarrierServicesResult(); 077 String shippingMethodName = form.getShippingMethodName(); 078 String carrierName = form.getCarrierName(); 079 String carrierServiceName = form.getCarrierServiceName(); 080 081 if(shippingMethodName != null && carrierName == null && carrierServiceName == null) { 082 ShippingMethod shippingMethod = shippingControl.getShippingMethodByName(shippingMethodName); 083 084 if(shippingMethod != null) { 085 result.setShippingMethod(shippingControl.getShippingMethodTransfer(getUserVisit(), shippingMethod)); 086 result.setShippingMethodCarrierServices(shippingControl.getShippingMethodCarrierServiceTransfersByShippingMethod(getUserVisit(), 087 shippingMethod)); 088 } else { 089 addExecutionError(ExecutionErrors.UnknownShippingMethodName.name(), shippingMethodName); 090 } 091 } else if(shippingMethodName == null && carrierName != null && carrierServiceName != null) { 092 var carrierControl = Session.getModelController(CarrierControl.class); 093 Carrier carrier = carrierControl.getCarrierByName(carrierName); 094 095 if(carrier != null) { 096 Party carrierParty = carrier.getParty(); 097 CarrierService carrierService = carrierControl.getCarrierServiceByName(carrierParty, carrierServiceName); 098 099 if(carrierService != null) { 100 result.setCarrierService(carrierControl.getCarrierServiceTransfer(getUserVisit(), carrierService)); 101 result.setShippingMethodCarrierServices(shippingControl.getShippingMethodCarrierServiceTransfersByCarrierService(getUserVisit(), 102 carrierService)); 103 } else { 104 addExecutionError(ExecutionErrors.UnknownCarrierServiceName.name(), carrierServiceName); 105 } 106 } else { 107 addExecutionError(ExecutionErrors.UnknownCarrierName.name(), carrierName); 108 } 109 110 } else { 111 addExecutionError(ExecutionErrors.InvalidParameterCount.name()); 112 } 113 114 return result; 115 } 116 117}