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.shipping.server.command;
018
019import com.echothree.control.user.shipping.common.form.CreateShippingMethodForm;
020import com.echothree.control.user.shipping.common.result.ShippingResultFactory;
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.control.shipping.server.control.ShippingControl;
028import com.echothree.model.data.selector.server.entity.Selector;
029import com.echothree.model.data.shipping.server.entity.ShippingMethod;
030import com.echothree.model.data.user.common.pk.UserVisitPK;
031import com.echothree.util.common.command.BaseResult;
032import com.echothree.util.common.message.ExecutionErrors;
033import com.echothree.util.common.validation.FieldDefinition;
034import com.echothree.util.common.validation.FieldType;
035import com.echothree.util.server.control.BaseSimpleCommand;
036import com.echothree.util.server.control.CommandSecurityDefinition;
037import com.echothree.util.server.control.PartyTypeDefinition;
038import com.echothree.util.server.control.SecurityRoleDefinition;
039import java.util.List;
040import javax.enterprise.context.Dependent;
041import javax.inject.Inject;
042
043@Dependent
044public class CreateShippingMethodCommand
045        extends BaseSimpleCommand<CreateShippingMethodForm> {
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(List.of(
052                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
053                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
054                        new SecurityRoleDefinition(SecurityRoleGroups.ShippingMethod.name(), SecurityRoles.Create.name())
055                ))
056        ));
057
058        FORM_FIELD_DEFINITIONS = List.of(
059                new FieldDefinition("ShippingMethodName", FieldType.ENTITY_NAME, true, null, null),
060                new FieldDefinition("GeoCodeSelectorName", FieldType.ENTITY_NAME, false, null, null),
061                new FieldDefinition("ItemSelectorName", FieldType.ENTITY_NAME, false, null, null),
062                new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null),
063                new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L)
064        );
065    }
066
067    @Inject
068    SelectorControl selectorControl;
069
070    @Inject
071    ShippingControl shippingControl;
072
073    
074    /** Creates a new instance of CreateShippingMethodCommand */
075    public CreateShippingMethodCommand() {
076        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false);
077    }
078    
079    @Override
080    protected BaseResult execute() {
081        var result = ShippingResultFactory.getCreateShippingMethodResult();
082        var shippingMethodName = form.getShippingMethodName();
083        var shippingMethod = shippingControl.getShippingMethodByName(shippingMethodName);
084        
085        if(shippingMethod == null) {
086            var geoCodeSelectorName = form.getGeoCodeSelectorName();
087            Selector geoCodeSelector = null;
088
089            if(geoCodeSelectorName != null) {
090                var selectorKind = selectorControl.getSelectorKindByName(SelectorKinds.POSTAL_ADDRESS.name());
091
092                if(selectorKind != null) {
093                    var selectorType = selectorControl.getSelectorTypeByName(selectorKind,
094                            SelectorTypes.CARRIER.name());
095
096                    if(selectorType != null) {
097                        geoCodeSelector = selectorControl.getSelectorByName(selectorType, geoCodeSelectorName);
098                    } else {
099                        addExecutionError(ExecutionErrors.UnknownSelectorTypeName.name(), SelectorTypes.SHIPPING_METHOD.name());
100                    }
101                } else {
102                    addExecutionError(ExecutionErrors.UnknownSelectorKindName.name(), SelectorKinds.POSTAL_ADDRESS.name());
103                }
104            }
105
106            if(geoCodeSelectorName == null || geoCodeSelector != null) {
107                var itemSelectorName = form.getItemSelectorName();
108                Selector itemSelector = null;
109
110                if(itemSelectorName != null) {
111                    var selectorKind = selectorControl.getSelectorKindByName(SelectorKinds.ITEM.name());
112
113                    if(selectorKind != null) {
114                        var selectorType = selectorControl.getSelectorTypeByName(selectorKind,
115                                SelectorTypes.CARRIER.name());
116
117                        if(selectorType != null) {
118                            itemSelector = selectorControl.getSelectorByName(selectorType, itemSelectorName);
119                        } else {
120                            addExecutionError(ExecutionErrors.UnknownSelectorTypeName.name(), SelectorTypes.SHIPPING_METHOD.name());
121                        }
122                    } else {
123                        addExecutionError(ExecutionErrors.UnknownSelectorKindName.name(), SelectorKinds.ITEM.name());
124                    }
125                }
126
127                if(itemSelectorName == null || itemSelector != null) {
128                    var createdBy = getPartyPK();
129                    var sortOrder = Integer.valueOf(form.getSortOrder());
130                    var description = form.getDescription();
131
132                    shippingMethod = shippingControl.createShippingMethod(shippingMethodName, geoCodeSelector, itemSelector, sortOrder, createdBy);
133
134                    if(description != null) {
135                        shippingControl.createShippingMethodDescription(shippingMethod, getPreferredLanguage(), description,
136                                createdBy);
137                    }
138                } else {
139                    addExecutionError(ExecutionErrors.UnknownItemSelectorName.name(), itemSelectorName);
140                }
141            } else {
142                addExecutionError(ExecutionErrors.UnknownGeoCodeSelectorName.name(), geoCodeSelectorName);
143            }
144        } else {
145            addExecutionError(ExecutionErrors.DuplicateShippingMethodName.name(), shippingMethodName);
146        }
147        
148        if(shippingMethod != null) {
149            result.setEntityRef(shippingMethod.getPrimaryKey().getEntityRef());
150            result.setShippingMethodName(shippingMethod.getLastDetail().getShippingMethodName());
151        }
152
153        return result;
154    }
155    
156}