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    /** Creates a new instance of GetCarrierServiceOptionsCommand */
068    public GetCarrierServiceOptionsCommand() {
069        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true);
070    }
071
072    private CarrierService carrierService;
073    private CarrierOption carrierOption;
074
075    @Override
076    protected void handleForm() {
077        var carrierName = form.getCarrierName();
078        var carrier = carrierControl.getCarrierByName(carrierName);
079
080        if(carrier != null) {
081            var carrierParty = carrier.getParty();
082            var carrierServiceName = form.getCarrierServiceName();
083            var carrierOptionName = form.getCarrierOptionName();
084            var parameterCount = (carrierServiceName == null ? 0 : 1) + (carrierOptionName == null ? 0 : 1);
085
086            if(parameterCount == 1) {
087                if(carrierServiceName != null) {
088                    carrierService = carrierControl.getCarrierServiceByName(carrierParty, carrierServiceName);
089
090                    if(carrierService == null) {
091                        addExecutionError(ExecutionErrors.UnknownCarrierServiceName.name(), carrierName, carrierServiceName);
092                    }
093                }
094
095                if(carrierOptionName != null) {
096                    carrierOption = carrierControl.getCarrierOptionByName(carrierParty, carrierOptionName);
097
098                    if(carrierOption == null) {
099                        addExecutionError(ExecutionErrors.UnknownCarrierOptionName.name(), carrierName, carrierOptionName);
100                    }
101                }
102            } else {
103                addExecutionError(ExecutionErrors.InvalidParameterCount.name());
104            }
105        } else {
106            addExecutionError(ExecutionErrors.UnknownCarrierName.name(), carrierName);
107        }
108    }
109
110    @Override
111    protected Long getTotalEntities() {
112        Long total = null;
113
114        if(!hasExecutionErrors()) {
115            if(carrierService != null) {
116                total = carrierControl.countCarrierServiceOptionsByCarrierService(carrierService);
117            } else if(carrierOption != null) {
118                total = carrierControl.countCarrierServiceOptionsByCarrierOption(carrierOption);
119            }
120        }
121
122        return total;
123    }
124
125    @Override
126    protected Collection<CarrierServiceOption> getEntities() {
127        Collection<CarrierServiceOption> entities = null;
128
129        if(!hasExecutionErrors()) {
130            if(carrierService != null) {
131                entities = carrierControl.getCarrierServiceOptionsByCarrierService(carrierService);
132            } else if(carrierOption != null) {
133                entities = carrierControl.getCarrierServiceOptionsByCarrierOption(carrierOption);
134            }
135        }
136
137        return entities;
138    }
139
140    @Override
141    protected BaseResult getResult(Collection<CarrierServiceOption> entities) {
142        var result = CarrierResultFactory.getGetCarrierServiceOptionsResult();
143
144        if(entities != null) {
145            var userVisit = getUserVisit();
146
147            if(session.hasLimit(CarrierServiceOptionFactory.class)) {
148                result.setCarrierServiceOptionCount(getTotalEntities());
149            }
150
151            if(carrierService != null) {
152                result.setCarrierService(carrierControl.getCarrierServiceTransfer(userVisit, carrierService));
153            } else if(carrierOption != null) {
154                result.setCarrierOption(carrierControl.getCarrierOptionTransfer(userVisit, carrierOption));
155            }
156
157            result.setCarrierServiceOptions(carrierControl.getCarrierServiceOptionTransfers(userVisit, entities));
158        }
159
160        return result;
161    }
162
163}