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 com.echothree.util.server.persistence.Session;
038import java.util.List;
039import javax.enterprise.context.Dependent;
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    /** Creates a new instance of CreateCarrierServiceOptionCommand */
074    public CreateCarrierServiceOptionCommand() {
075        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false);
076    }
077    
078    @Override
079    protected BaseResult execute() {
080        var carrierControl = Session.getModelController(CarrierControl.class);
081        var carrierName = form.getCarrierName();
082        var carrier = carrierControl.getCarrierByName(carrierName);
083        
084        if(carrier != null) {
085            var carrierParty = carrier.getParty();
086            var carrierServiceName = form.getCarrierServiceName();
087            var carrierService = carrierControl.getCarrierServiceByName(carrierParty, carrierServiceName);
088            
089            if(carrierService != null) {
090                var carrierOptionName = form.getCarrierOptionName();
091                var carrierOption = carrierControl.getCarrierOptionByName(carrierParty, carrierOptionName);
092                
093                if(carrierOption != null) {
094                    var carrierServiceOption = carrierControl.getCarrierServiceOption(carrierService, carrierOption);
095                    
096                    if(carrierServiceOption == null) {
097                        var selectorControl = Session.getModelController(SelectorControl.class);
098                        var selectorKind = selectorControl.getSelectorKindByName(SelectorKinds.POSTAL_ADDRESS.name());
099
100                        if(selectorKind != null) {
101                            var selectorType = selectorControl.getSelectorTypeByName(selectorKind, SelectorTypes.CARRIER_SERVICE_OPTION.name());
102
103                            if(selectorType != null) {
104                                var recommendedGeoCodeSelectorName = form.getRecommendedGeoCodeSelectorName();
105                                Selector recommendedGeoCodeSelector = null;
106
107                                if(recommendedGeoCodeSelectorName != null) {
108                                    recommendedGeoCodeSelector = selectorControl.getSelectorByName(selectorType, recommendedGeoCodeSelectorName);
109                                }
110
111                                if(recommendedGeoCodeSelectorName == null || recommendedGeoCodeSelector != null) {
112                                    var requiredGeoCodeSelectorName = form.getRequiredGeoCodeSelectorName();
113                                    Selector requiredGeoCodeSelector = null;
114
115                                    if(requiredGeoCodeSelectorName != null) {
116                                        requiredGeoCodeSelector = selectorControl.getSelectorByName(selectorType, requiredGeoCodeSelectorName);
117                                    }
118
119                                    if(requiredGeoCodeSelectorName == null || requiredGeoCodeSelector != null) {
120                                        selectorKind = selectorControl.getSelectorKindByName(SelectorKinds.ITEM.name());
121
122                                        if(selectorKind != null) {
123                                            selectorType = selectorControl.getSelectorTypeByName(selectorKind, SelectorTypes.CARRIER_SERVICE_OPTION.name());
124
125                                            if(selectorType != null) {
126                                                var recommendedItemSelectorName = form.getRecommendedItemSelectorName();
127                                                Selector recommendedItemSelector = null;
128
129                                                if(recommendedItemSelectorName != null) {
130                                                    recommendedItemSelector = selectorControl.getSelectorByName(selectorType, recommendedItemSelectorName);
131                                                }
132
133                                                if(recommendedItemSelectorName == null || recommendedItemSelector != null) {
134                                                    var requiredItemSelectorName = form.getRequiredItemSelectorName();
135                                                    Selector requiredItemSelector = null;
136
137                                                    if(requiredItemSelectorName != null) {
138                                                        requiredItemSelector = selectorControl.getSelectorByName(selectorType, requiredItemSelectorName);
139                                                    }
140
141                                                    if(requiredItemSelectorName == null || requiredItemSelector != null) {
142                                                        selectorKind = selectorControl.getSelectorKindByName(SelectorKinds.ORDER.name());
143
144                                                        if(selectorKind != null) {
145                                                            selectorType = selectorControl.getSelectorTypeByName(selectorKind, SelectorTypes.CARRIER_SERVICE_OPTION.name());
146
147                                                            if(selectorType != null) {
148                                                                var recommendedOrderSelectorName = form.getRecommendedOrderSelectorName();
149                                                                Selector recommendedOrderSelector = null;
150
151                                                                if(recommendedOrderSelectorName != null) {
152                                                                    recommendedOrderSelector = selectorControl.getSelectorByName(selectorType, recommendedOrderSelectorName);
153                                                                }
154
155                                                                if(recommendedOrderSelectorName == null || recommendedOrderSelector != null) {
156                                                                    var requiredOrderSelectorName = form.getRequiredOrderSelectorName();
157                                                                    Selector requiredOrderSelector = null;
158
159                                                                    if(requiredOrderSelectorName != null) {
160                                                                        requiredOrderSelector = selectorControl.getSelectorByName(selectorType, requiredOrderSelectorName);
161                                                                    }
162
163                                                                    if(requiredOrderSelectorName == null || requiredOrderSelector != null) {
164                                                                        selectorKind = selectorControl.getSelectorKindByName(SelectorKinds.SHIPMENT.name());
165
166                                                                        if(selectorKind != null) {
167                                                                            selectorType = selectorControl.getSelectorTypeByName(selectorKind, SelectorTypes.CARRIER_SERVICE_OPTION.name());
168
169                                                                            if(selectorType != null) {
170                                                                                var recommendedShipmentSelectorName = form.getRecommendedShipmentSelectorName();
171                                                                                Selector recommendedShipmentSelector = null;
172
173                                                                                if(recommendedShipmentSelectorName != null) {
174                                                                                    recommendedShipmentSelector = selectorControl.getSelectorByName(selectorType, recommendedShipmentSelectorName);
175                                                                                }
176
177                                                                                if(recommendedShipmentSelectorName == null || recommendedShipmentSelector != null) {
178                                                                                    var requiredShipmentSelectorName = form.getRequiredShipmentSelectorName();
179                                                                                    Selector requiredShipmentSelector = null;
180
181                                                                                    if(requiredShipmentSelectorName != null) {
182                                                                                        requiredShipmentSelector = selectorControl.getSelectorByName(selectorType, requiredShipmentSelectorName);
183                                                                                    }
184
185                                                                                    if(requiredShipmentSelectorName == null || requiredShipmentSelector != null) {
186                                                                                        var isRecommended = Boolean.valueOf(form.getIsRecommended());
187                                                                                        var isRequired = Boolean.valueOf(form.getIsRequired());
188
189                                                                                        carrierControl.createCarrierServiceOption(carrierService,
190                                                                                                carrierOption, isRecommended, isRequired,
191                                                                                                recommendedGeoCodeSelector, requiredGeoCodeSelector,
192                                                                                                recommendedItemSelector, requiredItemSelector,
193                                                                                                recommendedOrderSelector, requiredOrderSelector,
194                                                                                                recommendedShipmentSelector, requiredShipmentSelector,
195                                                                                                getPartyPK());
196                                                                                    } else {
197                                                                                        addExecutionError(ExecutionErrors.UnknownRequiredShipmentSelectorName.name(), requiredShipmentSelectorName);
198                                                                                    }
199                                                                                } else {
200                                                                                    addExecutionError(ExecutionErrors.UnknownRecommendedShipmentSelectorName.name(), recommendedShipmentSelectorName);
201                                                                                }
202                                                                            } else {
203                                                                                addExecutionError(ExecutionErrors.UnknownSelectorTypeName.name(),
204                                                                                        SelectorKinds.SHIPMENT.name(),
205                                                                                        SelectorTypes.CARRIER_SERVICE_OPTION.name());
206                                                                            }
207                                                                        } else {
208                                                                            addExecutionError(ExecutionErrors.UnknownSelectorKindName.name(),
209                                                                                    SelectorKinds.SHIPMENT.name());
210                                                                        }
211                                                                    } else {
212                                                                        addExecutionError(ExecutionErrors.UnknownRequiredOrderSelectorName.name(), requiredOrderSelectorName);
213                                                                    }
214                                                                } else {
215                                                                    addExecutionError(ExecutionErrors.UnknownRecommendedOrderSelectorName.name(), recommendedOrderSelectorName);
216                                                                }
217                                                            } else {
218                                                                addExecutionError(ExecutionErrors.UnknownSelectorTypeName.name(),
219                                                                        SelectorKinds.ORDER.name(),
220                                                                        SelectorTypes.CARRIER_SERVICE_OPTION.name());
221                                                            }
222                                                        } else {
223                                                            addExecutionError(ExecutionErrors.UnknownSelectorKindName.name(), SelectorKinds.ORDER.name());
224                                                        }
225                                                    } else {
226                                                        addExecutionError(ExecutionErrors.UnknownRequiredItemSelectorName.name(), requiredItemSelectorName);
227                                                    }
228                                                } else {
229                                                    addExecutionError(ExecutionErrors.UnknownRecommendedItemSelectorName.name(), recommendedItemSelectorName);
230                                                }
231                                            } else {
232                                                addExecutionError(ExecutionErrors.UnknownSelectorTypeName.name(), SelectorKinds.ITEM.name(),
233                                                        SelectorTypes.CARRIER_SERVICE_OPTION.name());
234                                            }
235                                        } else {
236                                            addExecutionError(ExecutionErrors.UnknownSelectorKindName.name(), SelectorKinds.ITEM.name());
237                                        }
238                                    } else {
239                                        addExecutionError(ExecutionErrors.UnknownRequiredGeoCodeSelectorName.name(), requiredGeoCodeSelectorName);
240                                    }
241                                } else {
242                                    addExecutionError(ExecutionErrors.UnknownRecommendedGeoCodeSelectorName.name(), recommendedGeoCodeSelectorName);
243                                }
244                            } else {
245                                addExecutionError(ExecutionErrors.UnknownSelectorTypeName.name(), SelectorKinds.POSTAL_ADDRESS.name(),
246                                        SelectorTypes.CARRIER_OPTION.name());
247                            }
248                        } else {
249                            addExecutionError(ExecutionErrors.UnknownSelectorKindName.name(), SelectorKinds.POSTAL_ADDRESS.name());
250                        }
251                    } else {
252                        addExecutionError(ExecutionErrors.DuplicateCarrierServiceOption.name(), carrierName, carrierServiceName, carrierOptionName);
253                    }
254                } else {
255                    addExecutionError(ExecutionErrors.UnknownCarrierOptionName.name(), carrierName, carrierOptionName);
256                }
257            } else {
258                addExecutionError(ExecutionErrors.UnknownCarrierServiceName.name(), carrierName, carrierServiceName);
259            }
260        } else {
261            addExecutionError(ExecutionErrors.UnknownCarrierName.name(), carrierName);
262        }
263        
264        return null;
265    }
266    
267}