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.CreateCarrierServiceForm;
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.control.selector.common.SelectorKinds;
026import com.echothree.model.control.selector.common.SelectorTypes;
027import com.echothree.model.control.selector.server.control.SelectorControl;
028import com.echothree.model.data.carrier.server.entity.CarrierService;
029import com.echothree.model.data.selector.server.entity.Selector;
030import com.echothree.model.data.user.common.pk.UserVisitPK;
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.BaseSimpleCommand;
036import com.echothree.util.server.control.CommandSecurityDefinition;
037import com.echothree.util.server.control.PartyTypeDefinition;
038import com.echothree.util.server.control.SecurityRoleDefinition;
039import java.util.List;
040import javax.enterprise.context.Dependent;
041import javax.inject.Inject;
042
043@Dependent
044public class CreateCarrierServiceCommand
045        extends BaseSimpleCommand<CreateCarrierServiceForm> {
046    
047    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
048    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
049    
050    static {
051        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
052                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
053                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
054                        new SecurityRoleDefinition(SecurityRoleGroups.CarrierService.name(), SecurityRoles.Create.name())
055                ))
056        ));
057
058        FORM_FIELD_DEFINITIONS = List.of(
059                new FieldDefinition("CarrierName", FieldType.ENTITY_NAME, true, null, null),
060                new FieldDefinition("CarrierServiceName", FieldType.ENTITY_NAME, true, null, null),
061                new FieldDefinition("GeoCodeSelectorName", FieldType.ENTITY_NAME, false, null, null),
062                new FieldDefinition("ItemSelectorName", FieldType.ENTITY_NAME, false, null, null),
063                new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null),
064                new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null),
065                new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L)
066        );
067    }
068
069    @Inject
070    CarrierControl carrierControl;
071
072    @Inject
073    SelectorControl selectorControl;
074
075    
076    /** Creates a new instance of CreateCarrierServiceCommand */
077    public CreateCarrierServiceCommand() {
078        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false);
079    }
080    
081    @Override
082    protected BaseResult execute() {
083        var result = CarrierResultFactory.getCreateCarrierServiceResult();
084        CarrierService carrierService = null;
085        var carrierName = form.getCarrierName();
086        var carrier = carrierControl.getCarrierByName(carrierName);
087        
088        if(carrier != null) {
089            var carrierParty = carrier.getParty();
090            var carrierServiceName = form.getCarrierServiceName();
091
092            carrierService = carrierControl.getCarrierServiceByName(carrierParty, carrierServiceName);
093            
094            if(carrierService == null) {
095                var geoCodeSelectorName = form.getGeoCodeSelectorName();
096                Selector geoCodeSelector = null;
097
098                if(geoCodeSelectorName != null) {
099                    var selectorKind = selectorControl.getSelectorKindByName(SelectorKinds.POSTAL_ADDRESS.name());
100
101                    if(selectorKind != null) {
102                        var selectorType = selectorControl.getSelectorTypeByName(selectorKind,
103                                SelectorTypes.CARRIER.name());
104
105                        if(selectorType != null) {
106                            geoCodeSelector = selectorControl.getSelectorByName(selectorType, geoCodeSelectorName);
107                        } else {
108                            addExecutionError(ExecutionErrors.UnknownSelectorTypeName.name(), SelectorKinds.POSTAL_ADDRESS.name(),
109                                    SelectorTypes.CARRIER_SERVICE.name());
110                        }
111                    } else {
112                        addExecutionError(ExecutionErrors.UnknownSelectorKindName.name(), SelectorKinds.POSTAL_ADDRESS.name());
113                    }
114                }
115
116                if(geoCodeSelectorName == null || geoCodeSelector != null) {
117                    var itemSelectorName = form.getItemSelectorName();
118                    Selector itemSelector = null;
119
120                    if(itemSelectorName != null) {
121                        var selectorKind = selectorControl.getSelectorKindByName(SelectorKinds.ITEM.name());
122
123                        if(selectorKind != null) {
124                            var selectorType = selectorControl.getSelectorTypeByName(selectorKind,
125                                    SelectorTypes.CARRIER.name());
126
127                            if(selectorType != null) {
128                                itemSelector = selectorControl.getSelectorByName(selectorType, itemSelectorName);
129                            } else {
130                                addExecutionError(ExecutionErrors.UnknownSelectorTypeName.name(), SelectorKinds.ITEM.name(),
131                                        SelectorTypes.CARRIER_SERVICE.name());
132                            }
133                        } else {
134                            addExecutionError(ExecutionErrors.UnknownSelectorKindName.name(), SelectorKinds.ITEM.name());
135                        }
136                    }
137
138                    if(itemSelectorName == null || itemSelector != null) {
139                        var createdBy = getPartyPK();
140                        var isDefault = Boolean.valueOf(form.getIsDefault());
141                        var sortOrder = Integer.valueOf(form.getSortOrder());
142                        var description = form.getDescription();
143
144                        carrierService = carrierControl.createCarrierService(carrierParty, carrierServiceName, geoCodeSelector, itemSelector,
145                                isDefault, sortOrder, createdBy);
146
147                        if(description != null) {
148                            carrierControl.createCarrierServiceDescription(carrierService, getPreferredLanguage(), description,
149                                    createdBy);
150                        }
151                    } else {
152                        addExecutionError(ExecutionErrors.UnknownItemSelectorName.name(), itemSelectorName);
153                    }
154                } else {
155                    addExecutionError(ExecutionErrors.UnknownGeoCodeSelectorName.name(), geoCodeSelectorName);
156                }
157            } else {
158                addExecutionError(ExecutionErrors.DuplicateCarrierServiceName.name(), carrierName, carrierServiceName);
159            }
160        } else {
161            addExecutionError(ExecutionErrors.UnknownCarrierName.name(), carrierName);
162        }
163        
164        if(carrierService != null) {
165            result.setEntityRef(carrierService.getPrimaryKey().getEntityRef());
166            result.setCarrierName(form.getCarrierName());
167            result.setCarrierServiceName(carrierService.getLastDetail().getCarrierServiceName());
168        }
169
170        return result;
171    }
172    
173}