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