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.carrier.server.command; 018 019import com.echothree.control.user.carrier.common.form.GetCarrierServiceOptionsForm; 020import com.echothree.control.user.carrier.common.result.CarrierResultFactory; 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.data.carrier.server.entity.CarrierOption; 026import com.echothree.model.data.carrier.server.entity.CarrierService; 027import com.echothree.model.data.carrier.server.entity.CarrierServiceOption; 028import com.echothree.model.data.carrier.server.factory.CarrierServiceOptionFactory; 029import com.echothree.util.common.command.BaseResult; 030import com.echothree.util.common.message.ExecutionErrors; 031import com.echothree.util.common.validation.FieldDefinition; 032import com.echothree.util.common.validation.FieldType; 033import com.echothree.util.server.control.BasePaginatedMultipleEntitiesCommand; 034import com.echothree.util.server.control.CommandSecurityDefinition; 035import com.echothree.util.server.control.PartyTypeDefinition; 036import com.echothree.util.server.control.SecurityRoleDefinition; 037import java.util.Collection; 038import java.util.List; 039import javax.enterprise.context.Dependent; 040import javax.inject.Inject; 041 042@Dependent 043public class GetCarrierServiceOptionsCommand 044 extends BasePaginatedMultipleEntitiesCommand<CarrierServiceOption, GetCarrierServiceOptionsForm> { 045 046 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 047 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 048 049 static { 050 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of( 051 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 052 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 053 new SecurityRoleDefinition(SecurityRoleGroups.CarrierServiceOption.name(), SecurityRoles.List.name()) 054 )) 055 )); 056 057 FORM_FIELD_DEFINITIONS = List.of( 058 new FieldDefinition("CarrierName", FieldType.ENTITY_NAME, true, null, null), 059 new FieldDefinition("CarrierServiceName", FieldType.ENTITY_NAME, false, null, null), 060 new FieldDefinition("CarrierOptionName", FieldType.ENTITY_NAME, false, null, null) 061 ); 062 } 063 064 @Inject 065 CarrierControl carrierControl; 066 067 068 /** Creates a new instance of GetCarrierServiceOptionsCommand */ 069 public GetCarrierServiceOptionsCommand() { 070 super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true); 071 } 072 073 private CarrierService carrierService; 074 private CarrierOption carrierOption; 075 076 @Override 077 protected void handleForm() { 078 var carrierName = form.getCarrierName(); 079 var carrier = carrierControl.getCarrierByName(carrierName); 080 081 if(carrier != null) { 082 var carrierParty = carrier.getParty(); 083 var carrierServiceName = form.getCarrierServiceName(); 084 var carrierOptionName = form.getCarrierOptionName(); 085 var parameterCount = (carrierServiceName == null ? 0 : 1) + (carrierOptionName == null ? 0 : 1); 086 087 if(parameterCount == 1) { 088 if(carrierServiceName != null) { 089 carrierService = carrierControl.getCarrierServiceByName(carrierParty, carrierServiceName); 090 091 if(carrierService == null) { 092 addExecutionError(ExecutionErrors.UnknownCarrierServiceName.name(), carrierName, carrierServiceName); 093 } 094 } 095 096 if(carrierOptionName != null) { 097 carrierOption = carrierControl.getCarrierOptionByName(carrierParty, carrierOptionName); 098 099 if(carrierOption == null) { 100 addExecutionError(ExecutionErrors.UnknownCarrierOptionName.name(), carrierName, carrierOptionName); 101 } 102 } 103 } else { 104 addExecutionError(ExecutionErrors.InvalidParameterCount.name()); 105 } 106 } else { 107 addExecutionError(ExecutionErrors.UnknownCarrierName.name(), carrierName); 108 } 109 } 110 111 @Override 112 protected Long getTotalEntities() { 113 Long total = null; 114 115 if(!hasExecutionErrors()) { 116 if(carrierService != null) { 117 total = carrierControl.countCarrierServiceOptionsByCarrierService(carrierService); 118 } else if(carrierOption != null) { 119 total = carrierControl.countCarrierServiceOptionsByCarrierOption(carrierOption); 120 } 121 } 122 123 return total; 124 } 125 126 @Override 127 protected Collection<CarrierServiceOption> getEntities() { 128 Collection<CarrierServiceOption> entities = null; 129 130 if(!hasExecutionErrors()) { 131 if(carrierService != null) { 132 entities = carrierControl.getCarrierServiceOptionsByCarrierService(carrierService); 133 } else if(carrierOption != null) { 134 entities = carrierControl.getCarrierServiceOptionsByCarrierOption(carrierOption); 135 } 136 } 137 138 return entities; 139 } 140 141 @Override 142 protected BaseResult getResult(Collection<CarrierServiceOption> entities) { 143 var result = CarrierResultFactory.getGetCarrierServiceOptionsResult(); 144 145 if(entities != null) { 146 var userVisit = getUserVisit(); 147 148 if(session.hasLimit(CarrierServiceOptionFactory.class)) { 149 result.setCarrierServiceOptionCount(getTotalEntities()); 150 } 151 152 if(carrierService != null) { 153 result.setCarrierService(carrierControl.getCarrierServiceTransfer(userVisit, carrierService)); 154 } else if(carrierOption != null) { 155 result.setCarrierOption(carrierControl.getCarrierOptionTransfer(userVisit, carrierOption)); 156 } 157 158 result.setCarrierServiceOptions(carrierControl.getCarrierServiceOptionTransfers(userVisit, entities)); 159 } 160 161 return result; 162 } 163 164}