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