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.CreateCarrierServiceOptionForm;
020import com.echothree.model.control.carrier.server.control.CarrierControl;
021import com.echothree.model.control.party.common.PartyTypes;
022import com.echothree.model.control.security.common.SecurityRoleGroups;
023import com.echothree.model.control.security.common.SecurityRoles;
024import com.echothree.model.control.selector.common.SelectorKinds;
025import com.echothree.model.control.selector.common.SelectorTypes;
026import com.echothree.model.control.selector.server.control.SelectorControl;
027import com.echothree.model.data.selector.server.entity.Selector;
028import com.echothree.model.data.user.common.pk.UserVisitPK;
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.BaseSimpleCommand;
034import com.echothree.util.server.control.CommandSecurityDefinition;
035import com.echothree.util.server.control.PartyTypeDefinition;
036import com.echothree.util.server.control.SecurityRoleDefinition;
037import java.util.List;
038import javax.enterprise.context.Dependent;
039import javax.inject.Inject;
040
041@Dependent
042public class CreateCarrierServiceOptionCommand
043        extends BaseSimpleCommand<CreateCarrierServiceOptionForm> {
044    
045    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
046    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
047    
048    static {
049        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
050                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
051                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
052                        new SecurityRoleDefinition(SecurityRoleGroups.CarrierServiceOption.name(), SecurityRoles.Create.name())
053                ))
054        ));
055
056        FORM_FIELD_DEFINITIONS = List.of(
057                new FieldDefinition("CarrierName", FieldType.ENTITY_NAME, true, null, null),
058                new FieldDefinition("CarrierServiceName", FieldType.ENTITY_NAME, true, null, null),
059                new FieldDefinition("CarrierOptionName", FieldType.ENTITY_NAME, true, null, null),
060                new FieldDefinition("IsRecommended", FieldType.BOOLEAN, true, null, null),
061                new FieldDefinition("IsRequired", FieldType.BOOLEAN, true, null, null),
062                new FieldDefinition("RecommendedGeoCodeSelectorName", FieldType.ENTITY_NAME, false, null, null),
063                new FieldDefinition("RequiredGeoCodeSelectorName", FieldType.ENTITY_NAME, false, null, null),
064                new FieldDefinition("RecommendedItemSelectorName", FieldType.ENTITY_NAME, false, null, null),
065                new FieldDefinition("RequiredItemSelectorName", FieldType.ENTITY_NAME, false, null, null),
066                new FieldDefinition("RecommendedOrderSelectorName", FieldType.ENTITY_NAME, false, null, null),
067                new FieldDefinition("RequiredOrderSelectorName", FieldType.ENTITY_NAME, false, null, null),
068                new FieldDefinition("RecommendedShipmentSelectorName", FieldType.ENTITY_NAME, false, null, null),
069                new FieldDefinition("RequiredShipmentSelectorName", FieldType.ENTITY_NAME, false, null, null)
070        );
071    }
072
073    @Inject
074    CarrierControl carrierControl;
075
076    @Inject
077    SelectorControl selectorControl;
078
079    
080    /** Creates a new instance of CreateCarrierServiceOptionCommand */
081    public CreateCarrierServiceOptionCommand() {
082        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false);
083    }
084    
085    @Override
086    protected BaseResult execute() {
087        var carrierName = form.getCarrierName();
088        var carrier = carrierControl.getCarrierByName(carrierName);
089        
090        if(carrier != null) {
091            var carrierParty = carrier.getParty();
092            var carrierServiceName = form.getCarrierServiceName();
093            var carrierService = carrierControl.getCarrierServiceByName(carrierParty, carrierServiceName);
094            
095            if(carrierService != null) {
096                var carrierOptionName = form.getCarrierOptionName();
097                var carrierOption = carrierControl.getCarrierOptionByName(carrierParty, carrierOptionName);
098                
099                if(carrierOption != null) {
100                    var carrierServiceOption = carrierControl.getCarrierServiceOption(carrierService, carrierOption);
101                    
102                    if(carrierServiceOption == null) {
103                        var selectorKind = selectorControl.getSelectorKindByName(SelectorKinds.POSTAL_ADDRESS.name());
104
105                        if(selectorKind != null) {
106                            var selectorType = selectorControl.getSelectorTypeByName(selectorKind, SelectorTypes.CARRIER_SERVICE_OPTION.name());
107
108                            if(selectorType != null) {
109                                var recommendedGeoCodeSelectorName = form.getRecommendedGeoCodeSelectorName();
110                                Selector recommendedGeoCodeSelector = null;
111
112                                if(recommendedGeoCodeSelectorName != null) {
113                                    recommendedGeoCodeSelector = selectorControl.getSelectorByName(selectorType, recommendedGeoCodeSelectorName);
114                                }
115
116                                if(recommendedGeoCodeSelectorName == null || recommendedGeoCodeSelector != null) {
117                                    var requiredGeoCodeSelectorName = form.getRequiredGeoCodeSelectorName();
118                                    Selector requiredGeoCodeSelector = null;
119
120                                    if(requiredGeoCodeSelectorName != null) {
121                                        requiredGeoCodeSelector = selectorControl.getSelectorByName(selectorType, requiredGeoCodeSelectorName);
122                                    }
123
124                                    if(requiredGeoCodeSelectorName == null || requiredGeoCodeSelector != null) {
125                                        selectorKind = selectorControl.getSelectorKindByName(SelectorKinds.ITEM.name());
126
127                                        if(selectorKind != null) {
128                                            selectorType = selectorControl.getSelectorTypeByName(selectorKind, SelectorTypes.CARRIER_SERVICE_OPTION.name());
129
130                                            if(selectorType != null) {
131                                                var recommendedItemSelectorName = form.getRecommendedItemSelectorName();
132                                                Selector recommendedItemSelector = null;
133
134                                                if(recommendedItemSelectorName != null) {
135                                                    recommendedItemSelector = selectorControl.getSelectorByName(selectorType, recommendedItemSelectorName);
136                                                }
137
138                                                if(recommendedItemSelectorName == null || recommendedItemSelector != null) {
139                                                    var requiredItemSelectorName = form.getRequiredItemSelectorName();
140                                                    Selector requiredItemSelector = null;
141
142                                                    if(requiredItemSelectorName != null) {
143                                                        requiredItemSelector = selectorControl.getSelectorByName(selectorType, requiredItemSelectorName);
144                                                    }
145
146                                                    if(requiredItemSelectorName == null || requiredItemSelector != null) {
147                                                        selectorKind = selectorControl.getSelectorKindByName(SelectorKinds.ORDER.name());
148
149                                                        if(selectorKind != null) {
150                                                            selectorType = selectorControl.getSelectorTypeByName(selectorKind, SelectorTypes.CARRIER_SERVICE_OPTION.name());
151
152                                                            if(selectorType != null) {
153                                                                var recommendedOrderSelectorName = form.getRecommendedOrderSelectorName();
154                                                                Selector recommendedOrderSelector = null;
155
156                                                                if(recommendedOrderSelectorName != null) {
157                                                                    recommendedOrderSelector = selectorControl.getSelectorByName(selectorType, recommendedOrderSelectorName);
158                                                                }
159
160                                                                if(recommendedOrderSelectorName == null || recommendedOrderSelector != null) {
161                                                                    var requiredOrderSelectorName = form.getRequiredOrderSelectorName();
162                                                                    Selector requiredOrderSelector = null;
163
164                                                                    if(requiredOrderSelectorName != null) {
165                                                                        requiredOrderSelector = selectorControl.getSelectorByName(selectorType, requiredOrderSelectorName);
166                                                                    }
167
168                                                                    if(requiredOrderSelectorName == null || requiredOrderSelector != null) {
169                                                                        selectorKind = selectorControl.getSelectorKindByName(SelectorKinds.SHIPMENT.name());
170
171                                                                        if(selectorKind != null) {
172                                                                            selectorType = selectorControl.getSelectorTypeByName(selectorKind, SelectorTypes.CARRIER_SERVICE_OPTION.name());
173
174                                                                            if(selectorType != null) {
175                                                                                var recommendedShipmentSelectorName = form.getRecommendedShipmentSelectorName();
176                                                                                Selector recommendedShipmentSelector = null;
177
178                                                                                if(recommendedShipmentSelectorName != null) {
179                                                                                    recommendedShipmentSelector = selectorControl.getSelectorByName(selectorType, recommendedShipmentSelectorName);
180                                                                                }
181
182                                                                                if(recommendedShipmentSelectorName == null || recommendedShipmentSelector != null) {
183                                                                                    var requiredShipmentSelectorName = form.getRequiredShipmentSelectorName();
184                                                                                    Selector requiredShipmentSelector = null;
185
186                                                                                    if(requiredShipmentSelectorName != null) {
187                                                                                        requiredShipmentSelector = selectorControl.getSelectorByName(selectorType, requiredShipmentSelectorName);
188                                                                                    }
189
190                                                                                    if(requiredShipmentSelectorName == null || requiredShipmentSelector != null) {
191                                                                                        var isRecommended = Boolean.valueOf(form.getIsRecommended());
192                                                                                        var isRequired = Boolean.valueOf(form.getIsRequired());
193
194                                                                                        carrierControl.createCarrierServiceOption(carrierService,
195                                                                                                carrierOption, isRecommended, isRequired,
196                                                                                                recommendedGeoCodeSelector, requiredGeoCodeSelector,
197                                                                                                recommendedItemSelector, requiredItemSelector,
198                                                                                                recommendedOrderSelector, requiredOrderSelector,
199                                                                                                recommendedShipmentSelector, requiredShipmentSelector,
200                                                                                                getPartyPK());
201                                                                                    } else {
202                                                                                        addExecutionError(ExecutionErrors.UnknownRequiredShipmentSelectorName.name(), requiredShipmentSelectorName);
203                                                                                    }
204                                                                                } else {
205                                                                                    addExecutionError(ExecutionErrors.UnknownRecommendedShipmentSelectorName.name(), recommendedShipmentSelectorName);
206                                                                                }
207                                                                            } else {
208                                                                                addExecutionError(ExecutionErrors.UnknownSelectorTypeName.name(),
209                                                                                        SelectorKinds.SHIPMENT.name(),
210                                                                                        SelectorTypes.CARRIER_SERVICE_OPTION.name());
211                                                                            }
212                                                                        } else {
213                                                                            addExecutionError(ExecutionErrors.UnknownSelectorKindName.name(),
214                                                                                    SelectorKinds.SHIPMENT.name());
215                                                                        }
216                                                                    } else {
217                                                                        addExecutionError(ExecutionErrors.UnknownRequiredOrderSelectorName.name(), requiredOrderSelectorName);
218                                                                    }
219                                                                } else {
220                                                                    addExecutionError(ExecutionErrors.UnknownRecommendedOrderSelectorName.name(), recommendedOrderSelectorName);
221                                                                }
222                                                            } else {
223                                                                addExecutionError(ExecutionErrors.UnknownSelectorTypeName.name(),
224                                                                        SelectorKinds.ORDER.name(),
225                                                                        SelectorTypes.CARRIER_SERVICE_OPTION.name());
226                                                            }
227                                                        } else {
228                                                            addExecutionError(ExecutionErrors.UnknownSelectorKindName.name(), SelectorKinds.ORDER.name());
229                                                        }
230                                                    } else {
231                                                        addExecutionError(ExecutionErrors.UnknownRequiredItemSelectorName.name(), requiredItemSelectorName);
232                                                    }
233                                                } else {
234                                                    addExecutionError(ExecutionErrors.UnknownRecommendedItemSelectorName.name(), recommendedItemSelectorName);
235                                                }
236                                            } else {
237                                                addExecutionError(ExecutionErrors.UnknownSelectorTypeName.name(), SelectorKinds.ITEM.name(),
238                                                        SelectorTypes.CARRIER_SERVICE_OPTION.name());
239                                            }
240                                        } else {
241                                            addExecutionError(ExecutionErrors.UnknownSelectorKindName.name(), SelectorKinds.ITEM.name());
242                                        }
243                                    } else {
244                                        addExecutionError(ExecutionErrors.UnknownRequiredGeoCodeSelectorName.name(), requiredGeoCodeSelectorName);
245                                    }
246                                } else {
247                                    addExecutionError(ExecutionErrors.UnknownRecommendedGeoCodeSelectorName.name(), recommendedGeoCodeSelectorName);
248                                }
249                            } else {
250                                addExecutionError(ExecutionErrors.UnknownSelectorTypeName.name(), SelectorKinds.POSTAL_ADDRESS.name(),
251                                        SelectorTypes.CARRIER_OPTION.name());
252                            }
253                        } else {
254                            addExecutionError(ExecutionErrors.UnknownSelectorKindName.name(), SelectorKinds.POSTAL_ADDRESS.name());
255                        }
256                    } else {
257                        addExecutionError(ExecutionErrors.DuplicateCarrierServiceOption.name(), carrierName, carrierServiceName, carrierOptionName);
258                    }
259                } else {
260                    addExecutionError(ExecutionErrors.UnknownCarrierOptionName.name(), carrierName, carrierOptionName);
261                }
262            } else {
263                addExecutionError(ExecutionErrors.UnknownCarrierServiceName.name(), carrierName, carrierServiceName);
264            }
265        } else {
266            addExecutionError(ExecutionErrors.UnknownCarrierName.name(), carrierName);
267        }
268        
269        return null;
270    }
271    
272}