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.CreateCarrierServiceForm;
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 CreateCarrierServiceCommand
045        extends BaseSimpleCommand<CreateCarrierServiceForm> {
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.CarrierService.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("GeoCodeSelectorName", FieldType.ENTITY_NAME, false, null, null),
062                new FieldDefinition("ItemSelectorName", FieldType.ENTITY_NAME, false, null, null),
063                new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null),
064                new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null),
065                new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L)
066                ));
067    }
068    
069    /** Creates a new instance of CreateCarrierServiceCommand */
070    public CreateCarrierServiceCommand() {
071        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false);
072    }
073    
074    @Override
075    protected BaseResult execute() {
076        var carrierControl = Session.getModelController(CarrierControl.class);
077        var carrierName = form.getCarrierName();
078        var carrier = carrierControl.getCarrierByName(carrierName);
079        
080        if(carrier != null) {
081            var carrierParty = carrier.getParty();
082            var carrierServiceName = form.getCarrierServiceName();
083            var carrierService = carrierControl.getCarrierServiceByName(carrierParty, carrierServiceName);
084            
085            if(carrierService == null) {
086                var geoCodeSelectorName = form.getGeoCodeSelectorName();
087                Selector geoCodeSelector = null;
088
089                if(geoCodeSelectorName != null) {
090                    var selectorControl = Session.getModelController(SelectorControl.class);
091                    var selectorKind = selectorControl.getSelectorKindByName(SelectorKinds.POSTAL_ADDRESS.name());
092
093                    if(selectorKind != null) {
094                        var selectorType = selectorControl.getSelectorTypeByName(selectorKind,
095                                SelectorTypes.CARRIER.name());
096
097                        if(selectorType != null) {
098                            geoCodeSelector = selectorControl.getSelectorByName(selectorType, geoCodeSelectorName);
099                        } else {
100                            addExecutionError(ExecutionErrors.UnknownSelectorTypeName.name(), SelectorKinds.POSTAL_ADDRESS.name(),
101                                    SelectorTypes.CARRIER_SERVICE.name());
102                        }
103                    } else {
104                        addExecutionError(ExecutionErrors.UnknownSelectorKindName.name(), SelectorKinds.POSTAL_ADDRESS.name());
105                    }
106                }
107
108                if(geoCodeSelectorName == null || geoCodeSelector != null) {
109                    var itemSelectorName = form.getItemSelectorName();
110                    Selector itemSelector = null;
111
112                    if(itemSelectorName != null) {
113                        var selectorControl = Session.getModelController(SelectorControl.class);
114                        var selectorKind = selectorControl.getSelectorKindByName(SelectorKinds.ITEM.name());
115
116                        if(selectorKind != null) {
117                            var selectorType = selectorControl.getSelectorTypeByName(selectorKind,
118                                    SelectorTypes.CARRIER.name());
119
120                            if(selectorType != null) {
121                                itemSelector = selectorControl.getSelectorByName(selectorType, itemSelectorName);
122                            } else {
123                                addExecutionError(ExecutionErrors.UnknownSelectorTypeName.name(), SelectorKinds.ITEM.name(),
124                                        SelectorTypes.CARRIER_SERVICE.name());
125                            }
126                        } else {
127                            addExecutionError(ExecutionErrors.UnknownSelectorKindName.name(), SelectorKinds.ITEM.name());
128                        }
129                    }
130
131                    if(itemSelectorName == null || itemSelector != null) {
132                        var createdBy = getPartyPK();
133                        var isDefault = Boolean.valueOf(form.getIsDefault());
134                        var sortOrder = Integer.valueOf(form.getSortOrder());
135                        var description = form.getDescription();
136
137                        carrierService = carrierControl.createCarrierService(carrierParty, carrierServiceName, geoCodeSelector, itemSelector,
138                                isDefault, sortOrder, createdBy);
139
140                        if(description != null) {
141                            carrierControl.createCarrierServiceDescription(carrierService, getPreferredLanguage(), description,
142                                    createdBy);
143                        }
144                    } else {
145                        addExecutionError(ExecutionErrors.UnknownItemSelectorName.name(), itemSelectorName);
146                    }
147                } else {
148                    addExecutionError(ExecutionErrors.UnknownGeoCodeSelectorName.name(), geoCodeSelectorName);
149                }
150            } else {
151                addExecutionError(ExecutionErrors.DuplicateCarrierServiceName.name(), carrierName, carrierServiceName);
152            }
153        } else {
154            addExecutionError(ExecutionErrors.UnknownCarrierName.name(), carrierName);
155        }
156        
157        return null;
158    }
159    
160}