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.form.GetShippingMethodCarrierServicesForm; 020import com.echothree.control.user.shipping.common.result.ShippingResultFactory; 021import com.echothree.model.control.carrier.server.control.CarrierControl; 022import com.echothree.model.control.party.common.PartyTypes; 023import com.echothree.model.control.security.common.SecurityRoleGroups; 024import com.echothree.model.control.security.common.SecurityRoles; 025import com.echothree.model.control.shipping.server.control.ShippingControl; 026import com.echothree.model.control.shipping.server.logic.ShippingMethodLogic; 027import com.echothree.model.data.carrier.server.entity.CarrierService; 028import com.echothree.model.data.shipping.server.entity.ShippingMethod; 029import com.echothree.model.data.shipping.server.entity.ShippingMethodCarrierService; 030import com.echothree.model.data.shipping.server.factory.ShippingMethodCarrierServiceFactory; 031import com.echothree.util.common.command.BaseResult; 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.server.control.BasePaginatedMultipleEntitiesCommand; 036import com.echothree.util.server.control.CommandSecurityDefinition; 037import com.echothree.util.server.control.PartyTypeDefinition; 038import com.echothree.util.server.control.SecurityRoleDefinition; 039import java.util.Collection; 040import java.util.List; 041import javax.enterprise.context.Dependent; 042import javax.inject.Inject; 043 044@Dependent 045public class GetShippingMethodCarrierServicesCommand 046 extends BasePaginatedMultipleEntitiesCommand<ShippingMethodCarrierService, 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(List.of( 053 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 054 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 055 new SecurityRoleDefinition(SecurityRoleGroups.ShippingMethodCarrierService.name(), SecurityRoles.List.name()) 056 )) 057 )); 058 059 FORM_FIELD_DEFINITIONS = List.of( 060 new FieldDefinition("ShippingMethodName", FieldType.ENTITY_NAME, false, null, null), 061 new FieldDefinition("CarrierName", FieldType.ENTITY_NAME, false, null, null), 062 new FieldDefinition("CarrierServiceName", FieldType.ENTITY_NAME, false, null, null) 063 ); 064 } 065 066 @Inject 067 CarrierControl carrierControl; 068 069 @Inject 070 ShippingControl shippingControl; 071 072 @Inject 073 ShippingMethodLogic shippingMethodLogic; 074 075 /** Creates a new instance of GetShippingMethodCarrierServicesCommand */ 076 public GetShippingMethodCarrierServicesCommand() { 077 super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true); 078 } 079 080 private ShippingMethod shippingMethod; 081 private CarrierService carrierService; 082 083 @Override 084 protected void handleForm() { 085 var shippingMethodName = form.getShippingMethodName(); 086 var carrierName = form.getCarrierName(); 087 var carrierServiceName = form.getCarrierServiceName(); 088 var parameterCount = (shippingMethodName == null ? 0 : 1) + (carrierName == null && carrierServiceName == null ? 0 : 1); 089 090 if(parameterCount == 1) { 091 if(shippingMethodName != null) { 092 shippingMethod = shippingMethodLogic.getShippingMethodByName(this, shippingMethodName); 093 } else { 094 var carrier = carrierControl.getCarrierByName(carrierName); 095 096 if(carrier != null) { 097 var carrierParty = carrier.getParty(); 098 carrierService = carrierControl.getCarrierServiceByName(carrierParty, carrierServiceName); 099 100 if(carrierService == null) { 101 addExecutionError(ExecutionErrors.UnknownCarrierServiceName.name(), carrierServiceName); 102 } 103 } else { 104 addExecutionError(ExecutionErrors.UnknownCarrierName.name(), carrierName); 105 } 106 } 107 } else { 108 addExecutionError(ExecutionErrors.InvalidParameterCount.name()); 109 } 110 } 111 112 @Override 113 protected Long getTotalEntities() { 114 Long total = null; 115 116 if(!hasExecutionErrors()) { 117 if(shippingMethod != null) { 118 total = shippingControl.countShippingMethodCarrierServicesByShippingMethod(shippingMethod); 119 } else { 120 total = shippingControl.countShippingMethodCarrierServicesByCarrierService(carrierService); 121 } 122 } 123 124 return total; 125 } 126 127 @Override 128 protected Collection<ShippingMethodCarrierService> getEntities() { 129 Collection<ShippingMethodCarrierService> entities = null; 130 131 if(!hasExecutionErrors()) { 132 if(shippingMethod != null) { 133 entities = shippingControl.getShippingMethodCarrierServicesByShippingMethod(shippingMethod); 134 } else { 135 entities = shippingControl.getShippingMethodCarrierServicesByCarrierService(carrierService); 136 } 137 } 138 139 return entities; 140 } 141 142 @Override 143 protected BaseResult getResult(Collection<ShippingMethodCarrierService> entities) { 144 var result = ShippingResultFactory.getGetShippingMethodCarrierServicesResult(); 145 146 if(entities != null) { 147 var userVisit = getUserVisit(); 148 149 if(shippingMethod != null) { 150 result.setShippingMethod(shippingControl.getShippingMethodTransfer(userVisit, shippingMethod)); 151 } else { 152 result.setCarrierService(carrierControl.getCarrierServiceTransfer(userVisit, carrierService)); 153 } 154 155 if(session.hasLimit(ShippingMethodCarrierServiceFactory.class)) { 156 result.setShippingMethodCarrierServiceCount(getTotalEntities()); 157 } 158 159 result.setShippingMethodCarrierServices(shippingControl.getShippingMethodCarrierServiceTransfers(userVisit, (List<ShippingMethodCarrierService>)entities)); 160 } 161 162 return result; 163 } 164 165}