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.GetCarrierServiceOptionForm;
020import com.echothree.control.user.carrier.common.result.CarrierResultFactory;
021import com.echothree.control.user.carrier.common.result.GetCarrierServiceOptionResult;
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.data.carrier.server.entity.Carrier;
027import com.echothree.model.data.carrier.server.entity.CarrierOption;
028import com.echothree.model.data.carrier.server.entity.CarrierService;
029import com.echothree.model.data.carrier.server.entity.CarrierServiceOption;
030import com.echothree.model.data.party.server.entity.Party;
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 GetCarrierServiceOptionCommand
046        extends BaseSimpleCommand<GetCarrierServiceOptionForm> {
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.CarrierServiceOption.name(), SecurityRoles.Review.name())
056                        )))
057                )));
058
059        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
060                new FieldDefinition("CarrierName", FieldType.ENTITY_NAME, true, null, null),
061                new FieldDefinition("CarrierServiceName", FieldType.ENTITY_NAME, true, null, null),
062                new FieldDefinition("CarrierOptionName", FieldType.ENTITY_NAME, true, null, null)
063                ));
064    }
065    
066    /** Creates a new instance of GetCarrierServiceOptionCommand */
067    public GetCarrierServiceOptionCommand(UserVisitPK userVisitPK, GetCarrierServiceOptionForm form) {
068        super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true);
069    }
070    
071    @Override
072    protected BaseResult execute() {
073        var carrierControl = Session.getModelController(CarrierControl.class);
074        GetCarrierServiceOptionResult result = CarrierResultFactory.getGetCarrierServiceOptionResult();
075        String carrierName = form.getCarrierName();
076        Carrier carrier = carrierControl.getCarrierByName(carrierName);
077        
078        if(carrier != null) {
079            Party carrierParty = carrier.getParty();
080            String carrierServiceName = form.getCarrierServiceName();
081            CarrierService carrierService = carrierControl.getCarrierServiceByName(carrierParty, carrierServiceName);
082            
083            result.setCarrier(carrierControl.getCarrierTransfer(getUserVisit(), carrier));
084            
085            if(carrierService != null) {
086                String carrierOptionName = form.getCarrierOptionName();
087                CarrierOption carrierOption = carrierControl.getCarrierOptionByName(carrierParty, carrierOptionName);
088                
089                result.setCarrierService(carrierControl.getCarrierServiceTransfer(getUserVisit(), carrierService));
090                
091                if(carrierOption != null) {
092                    CarrierServiceOption carrierServiceOption = carrierControl.getCarrierServiceOption(carrierService, carrierOption);
093                    
094                    result.setCarrierOption(carrierControl.getCarrierOptionTransfer(getUserVisit(), carrierOption));
095                    
096                    if(carrierServiceOption != null) {
097                        result.setCarrierServiceOption(carrierControl.getCarrierServiceOptionTransfer(getUserVisit(), carrierServiceOption));
098                    } else {
099                        addExecutionError(ExecutionErrors.UnknownCarrierServiceOption.name(), carrierName, carrierServiceName, carrierOptionName);
100                    }
101                } else {
102                    addExecutionError(ExecutionErrors.UnknownCarrierOptionName.name(), carrierName, carrierOptionName);
103                }
104            } else {
105                addExecutionError(ExecutionErrors.UnknownCarrierServiceName.name(), carrierName, carrierServiceName);
106            }
107        } else {
108            addExecutionError(ExecutionErrors.UnknownCarrierName.name(), carrierName);
109        }
110        
111        return result;
112    }
113    
114}