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.CreateCarrierForm;
020import com.echothree.control.user.carrier.common.result.CarrierResultFactory;
021import com.echothree.control.user.carrier.common.result.CreateCarrierResult;
022import com.echothree.model.control.accounting.server.control.AccountingControl;
023import com.echothree.model.control.carrier.server.control.CarrierControl;
024import com.echothree.model.control.party.common.PartyTypes;
025import com.echothree.model.control.party.server.control.PartyControl;
026import com.echothree.model.control.security.common.SecurityRoleGroups;
027import com.echothree.model.control.security.common.SecurityRoles;
028import com.echothree.model.control.selector.common.SelectorKinds;
029import com.echothree.model.control.selector.common.SelectorTypes;
030import com.echothree.model.control.selector.server.control.SelectorControl;
031import com.echothree.model.data.accounting.server.entity.Currency;
032import com.echothree.model.data.carrier.server.entity.Carrier;
033import com.echothree.model.data.carrier.server.entity.CarrierType;
034import com.echothree.model.data.party.server.entity.DateTimeFormat;
035import com.echothree.model.data.party.server.entity.Language;
036import com.echothree.model.data.party.server.entity.Party;
037import com.echothree.model.data.party.server.entity.PartyType;
038import com.echothree.model.data.party.server.entity.TimeZone;
039import com.echothree.model.data.selector.server.entity.Selector;
040import com.echothree.model.data.selector.server.entity.SelectorKind;
041import com.echothree.model.data.selector.server.entity.SelectorType;
042import com.echothree.model.data.user.common.pk.UserVisitPK;
043import com.echothree.util.common.command.BaseResult;
044import com.echothree.util.common.message.ExecutionErrors;
045import com.echothree.util.common.persistence.BasePK;
046import com.echothree.util.common.validation.FieldDefinition;
047import com.echothree.util.common.validation.FieldType;
048import com.echothree.util.server.control.BaseSimpleCommand;
049import com.echothree.util.server.control.CommandSecurityDefinition;
050import com.echothree.util.server.control.PartyTypeDefinition;
051import com.echothree.util.server.control.SecurityRoleDefinition;
052import com.echothree.util.server.persistence.Session;
053import java.util.Arrays;
054import java.util.Collections;
055import java.util.List;
056
057public class CreateCarrierCommand
058        extends BaseSimpleCommand<CreateCarrierForm> {
059    
060    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
061    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
062    
063    static {
064        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList(
065                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
066                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList(
067                        new SecurityRoleDefinition(SecurityRoleGroups.Carrier.name(), SecurityRoles.Create.name())
068                        )))
069                )));
070
071        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
072                new FieldDefinition("CarrierName", FieldType.ENTITY_NAME, true, null, null),
073                new FieldDefinition("CarrierTypeName", FieldType.ENTITY_NAME, true, null, null),
074                new FieldDefinition("Name", FieldType.STRING, true, 1L, 60L),
075                new FieldDefinition("PreferredLanguageIsoName", FieldType.ENTITY_NAME, false, null, null),
076                new FieldDefinition("PreferredCurrencyIsoName", FieldType.ENTITY_NAME, false, null, null),
077                new FieldDefinition("PreferredJavaTimeZoneName", FieldType.TIME_ZONE_NAME, false, null, null),
078                new FieldDefinition("PreferredDateTimeFormatName", FieldType.ENTITY_NAME, false, null, null),
079                new FieldDefinition("GeoCodeSelectorName", FieldType.ENTITY_NAME, false, null, null),
080                new FieldDefinition("ItemSelectorName", FieldType.ENTITY_NAME, false, null, null),
081                new FieldDefinition("AccountValidationPattern", FieldType.REGULAR_EXPRESSION, false, null, null),
082                new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null),
083                new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null)
084                ));
085    }
086    
087    /** Creates a new instance of CreateCarrierCommand */
088    public CreateCarrierCommand(UserVisitPK userVisitPK, CreateCarrierForm form) {
089        super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false);
090    }
091    
092    @Override
093    protected BaseResult execute() {
094        var carrierControl = Session.getModelController(CarrierControl.class);
095        CreateCarrierResult result = CarrierResultFactory.getCreateCarrierResult();
096        String carrierName = form.getCarrierName();
097        Carrier carrier = carrierControl.getCarrierByName(carrierName);
098        
099        if(carrier == null) {
100            String carrierTypeName = form.getCarrierTypeName();
101            CarrierType carrierType = carrierControl.getCarrierTypeByName(carrierTypeName);
102            
103            if(carrierType != null) {
104                var partyControl = Session.getModelController(PartyControl.class);
105                String preferredLanguageIsoName = form.getPreferredLanguageIsoName();
106                Language preferredLanguage = preferredLanguageIsoName == null? null: partyControl.getLanguageByIsoName(preferredLanguageIsoName);
107                
108                if(preferredLanguageIsoName == null || (preferredLanguage != null)) {
109                    String preferredJavaTimeZoneName = form.getPreferredJavaTimeZoneName();
110                    TimeZone preferredTimeZone = preferredJavaTimeZoneName == null? null: partyControl.getTimeZoneByJavaName(preferredJavaTimeZoneName);
111                    
112                    if(preferredJavaTimeZoneName == null || (preferredTimeZone != null)) {
113                        String preferredDateTimeFormatName = form.getPreferredDateTimeFormatName();
114                        DateTimeFormat preferredDateTimeFormat = preferredDateTimeFormatName == null? null: partyControl.getDateTimeFormatByName(preferredDateTimeFormatName);
115                        
116                        if(preferredDateTimeFormatName == null || (preferredDateTimeFormat != null)) {
117                            String preferredCurrencyIsoName = form.getPreferredCurrencyIsoName();
118                            Currency preferredCurrency;
119                            
120                            if(preferredCurrencyIsoName == null)
121                                preferredCurrency = null;
122                            else {
123                                var accountingControl = Session.getModelController(AccountingControl.class);
124                                preferredCurrency = accountingControl.getCurrencyByIsoName(preferredCurrencyIsoName);
125                            }
126                            
127                            if(preferredCurrencyIsoName == null || (preferredCurrency != null)) {
128                                String geoCodeSelectorName = form.getGeoCodeSelectorName();
129                                Selector geoCodeSelector = null;
130
131                                if(geoCodeSelectorName != null) {
132                                    var selectorControl = Session.getModelController(SelectorControl.class);
133                                    SelectorKind selectorKind = selectorControl.getSelectorKindByName(SelectorKinds.POSTAL_ADDRESS.name());
134
135                                    if(selectorKind != null) {
136                                        SelectorType selectorType = selectorControl.getSelectorTypeByName(selectorKind,
137                                                SelectorTypes.CARRIER.name());
138
139                                        if(selectorType != null) {
140                                            geoCodeSelector = selectorControl.getSelectorByName(selectorType, geoCodeSelectorName);
141                                        } else {
142                                            addExecutionError(ExecutionErrors.UnknownSelectorTypeName.name(), SelectorKinds.POSTAL_ADDRESS.name(),
143                                                    SelectorTypes.CARRIER.name());
144                                        }
145                                    } else {
146                                        addExecutionError(ExecutionErrors.UnknownSelectorKindName.name(), SelectorKinds.POSTAL_ADDRESS.name());
147                                    }
148                                }
149
150                                if(geoCodeSelectorName == null || geoCodeSelector != null) {
151                                    String itemSelectorName = form.getItemSelectorName();
152                                    Selector itemSelector = null;
153
154                                    if(itemSelectorName != null) {
155                                        var selectorControl = Session.getModelController(SelectorControl.class);
156                                        SelectorKind selectorKind = selectorControl.getSelectorKindByName(SelectorKinds.ITEM.name());
157
158                                        if(selectorKind != null) {
159                                            SelectorType selectorType = selectorControl.getSelectorTypeByName(selectorKind,
160                                                    SelectorTypes.CARRIER.name());
161
162                                            if(selectorType != null) {
163                                                itemSelector = selectorControl.getSelectorByName(selectorType, itemSelectorName);
164                                            } else {
165                                                addExecutionError(ExecutionErrors.UnknownSelectorTypeName.name(), SelectorKinds.ITEM.name(),
166                                                        SelectorTypes.CARRIER.name());
167                                            }
168                                        } else {
169                                            addExecutionError(ExecutionErrors.UnknownSelectorKindName.name(), SelectorKinds.ITEM.name());
170                                        }
171                                    }
172
173                                    if(itemSelectorName == null || itemSelector != null) {
174                                        PartyType partyType = partyControl.getPartyTypeByName(PartyTypes.CARRIER.name());
175                                        BasePK createdBy = getPartyPK();
176                                        String name = form.getName();
177                                        String accountValidationPattern = form.getAccountValidationPattern();
178                                        var isDefault = Boolean.valueOf(form.getIsDefault());
179                                        var sortOrder = Integer.valueOf(form.getSortOrder());
180
181                                        Party party = partyControl.createParty(null, partyType, preferredLanguage, preferredCurrency, preferredTimeZone,
182                                                preferredDateTimeFormat, createdBy);
183                                        partyControl.createPartyGroup(party, name, createdBy);
184                                        carrier = carrierControl.createCarrier(party, carrierName, carrierType, geoCodeSelector, itemSelector,
185                                                accountValidationPattern, isDefault, sortOrder, createdBy);
186                                    } else {
187                                        addExecutionError(ExecutionErrors.UnknownItemSelectorName.name(), itemSelectorName);
188                                    }
189                                } else {
190                                    addExecutionError(ExecutionErrors.UnknownGeoCodeSelectorName.name(), geoCodeSelectorName);
191                                }
192                            } else {
193                                addExecutionError(ExecutionErrors.UnknownCurrencyIsoName.name(), preferredCurrencyIsoName);
194                            }
195                        } else {
196                            addExecutionError(ExecutionErrors.UnknownDateTimeFormatName.name(), preferredDateTimeFormatName);
197                        }
198                    } else {
199                        addExecutionError(ExecutionErrors.UnknownJavaTimeZoneName.name(), preferredJavaTimeZoneName);
200                    }
201                } else {
202                    addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), preferredLanguageIsoName);
203                }
204            } else {
205                addExecutionError(ExecutionErrors.UnknownCarrierTypeName.name(), carrierName, carrierTypeName);
206            }
207        } else {
208            addExecutionError(ExecutionErrors.DuplicateCarrierName.name(), carrierName);
209        }
210        
211        if(carrier != null) {
212            Party party = carrier.getParty();
213            
214            result.setEntityRef(party.getPrimaryKey().getEntityRef());
215            result.setCarrierName(carrier.getCarrierName());
216            result.setPartyName(party.getLastDetail().getPartyName());
217        }
218        
219        return result;
220    }
221    
222}