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.warehouse.server.command;
018
019import com.echothree.control.user.warehouse.common.form.CreateWarehouseForm;
020import com.echothree.control.user.warehouse.common.result.WarehouseResultFactory;
021import com.echothree.model.control.accounting.server.control.AccountingControl;
022import com.echothree.model.control.party.common.PartyTypes;
023import com.echothree.model.control.party.server.control.PartyControl;
024import com.echothree.model.control.printer.common.PrinterConstants;
025import com.echothree.model.control.printer.server.control.PrinterControl;
026import com.echothree.model.control.security.common.SecurityRoleGroups;
027import com.echothree.model.control.security.common.SecurityRoles;
028import com.echothree.model.control.warehouse.server.logic.WarehouseLogic;
029import com.echothree.model.control.warehouse.server.logic.WarehouseTypeLogic;
030import com.echothree.model.data.accounting.server.entity.Currency;
031import com.echothree.model.data.user.common.pk.UserVisitPK;
032import com.echothree.model.data.warehouse.server.entity.Warehouse;
033import com.echothree.util.common.command.BaseResult;
034import com.echothree.util.common.message.ExecutionErrors;
035import com.echothree.util.common.validation.FieldDefinition;
036import com.echothree.util.common.validation.FieldType;
037import com.echothree.util.server.control.BaseSimpleCommand;
038import com.echothree.util.server.control.CommandSecurityDefinition;
039import com.echothree.util.server.control.PartyTypeDefinition;
040import com.echothree.util.server.control.SecurityRoleDefinition;
041import com.echothree.util.server.persistence.Session;
042import java.util.List;
043import javax.enterprise.context.RequestScoped;
044
045@RequestScoped
046public class CreateWarehouseCommand
047        extends BaseSimpleCommand<CreateWarehouseForm> {
048
049    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
050    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
051    
052    static {
053        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
054                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
055                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
056                        new SecurityRoleDefinition(SecurityRoleGroups.Warehouse.name(), SecurityRoles.Create.name())
057                ))
058        ));
059
060        FORM_FIELD_DEFINITIONS = List.of(
061                new FieldDefinition("WarehouseName", FieldType.ENTITY_NAME, true, null, null),
062                new FieldDefinition("WarehouseTypeName", FieldType.ENTITY_NAME, true, null, null),
063                new FieldDefinition("Name", FieldType.STRING, false, 1L, 60L),
064                new FieldDefinition("PreferredLanguageIsoName", FieldType.ENTITY_NAME, false, null, null),
065                new FieldDefinition("PreferredCurrencyIsoName", FieldType.ENTITY_NAME, false, null, null),
066                new FieldDefinition("PreferredJavaTimeZoneName", FieldType.TIME_ZONE_NAME, false, null, null),
067                new FieldDefinition("PreferredDateTimeFormatName", FieldType.ENTITY_NAME, false, null, null),
068                new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null),
069                new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null),
070                new FieldDefinition("InventoryMovePrinterGroupName", FieldType.ENTITY_NAME, true, null, null),
071                new FieldDefinition("PicklistPrinterGroupName", FieldType.ENTITY_NAME, true, null, null),
072                new FieldDefinition("PackingListPrinterGroupName", FieldType.ENTITY_NAME, true, null, null),
073                new FieldDefinition("ShippingManifestPrinterGroupName", FieldType.ENTITY_NAME, true, null, null)
074        );
075    }
076    
077    /** Creates a new instance of CreateWarehouseCommand */
078    public CreateWarehouseCommand() {
079        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false);
080    }
081    
082    @Override
083    protected BaseResult execute() {
084        var result = WarehouseResultFactory.getCreateWarehouseResult();
085        Warehouse warehouse = null;
086        var partyControl = Session.getModelController(PartyControl.class);
087        var preferredLanguageIsoName = form.getPreferredLanguageIsoName();
088        var preferredLanguage = preferredLanguageIsoName == null? null: partyControl.getLanguageByIsoName(preferredLanguageIsoName);
089
090        if(preferredLanguageIsoName == null || (preferredLanguage != null)) {
091            var preferredJavaTimeZoneName = form.getPreferredJavaTimeZoneName();
092            var preferredTimeZone = preferredJavaTimeZoneName == null? null: partyControl.getTimeZoneByJavaName(preferredJavaTimeZoneName);
093
094            if(preferredJavaTimeZoneName == null || (preferredTimeZone != null)) {
095                var preferredDateTimeFormatName = form.getPreferredDateTimeFormatName();
096                var preferredDateTimeFormat = preferredDateTimeFormatName == null? null: partyControl.getDateTimeFormatByName(preferredDateTimeFormatName);
097
098                if(preferredDateTimeFormatName == null || (preferredDateTimeFormat != null)) {
099                    var preferredCurrencyIsoName = form.getPreferredCurrencyIsoName();
100                    Currency preferredCurrency;
101
102                    if(preferredCurrencyIsoName == null) {
103                        preferredCurrency = null;
104                    } else {
105                        var accountingControl = Session.getModelController(AccountingControl.class);
106
107                        preferredCurrency = accountingControl.getCurrencyByIsoName(preferredCurrencyIsoName);
108                    }
109
110                    if(preferredCurrencyIsoName == null || (preferredCurrency != null)) {
111                        var printerControl = Session.getModelController(PrinterControl.class);
112                        var inventoryMovePrinterGroupName = form.getInventoryMovePrinterGroupName();
113                        var inventoryMovePrinterGroup = printerControl.getPrinterGroupByName(inventoryMovePrinterGroupName);
114
115                        if(inventoryMovePrinterGroup != null) {
116                            var picklistPrinterGroupName = form.getPicklistPrinterGroupName();
117                            var picklistPrinterGroup = printerControl.getPrinterGroupByName(picklistPrinterGroupName);
118
119                            if(picklistPrinterGroup != null) {
120                                var packingListPrinterGroupName = form.getPackingListPrinterGroupName();
121                                var packingListPrinterGroup = printerControl.getPrinterGroupByName(packingListPrinterGroupName);
122
123                                if(packingListPrinterGroup != null) {
124                                    var shippingManifestPrinterGroupName = form.getShippingManifestPrinterGroupName();
125                                    var shippingManifestPrinterGroup = printerControl.getPrinterGroupByName(shippingManifestPrinterGroupName);
126
127                                    if(shippingManifestPrinterGroup != null) {
128                                        var warehouseType = WarehouseTypeLogic.getInstance().getWarehouseTypeByName(this, form.getWarehouseTypeName());
129
130                                        if(!hasExecutionErrors()) {
131                                            var warehouseName = form.getWarehouseName();
132                                            var name = form.getName();
133                                            var isDefault = Boolean.valueOf(form.getIsDefault());
134                                            var sortOrder = Integer.valueOf(form.getSortOrder());
135                                            var createdBy = getPartyPK();
136
137                                            warehouse = WarehouseLogic.getInstance().createWarehouse(this, warehouseName,
138                                                    warehouseType, preferredLanguage, preferredCurrency, preferredTimeZone,
139                                                    preferredDateTimeFormat, name, isDefault, sortOrder, createdBy);
140
141                                            if(!hasExecutionErrors()) {
142                                                var party = warehouse.getParty();
143                                                var printerGroupUseType = printerControl.getPrinterGroupUseTypeByName(PrinterConstants.PrinterGroupUseType_WAREHOUSE_INVENTORY_MOVE);
144                                                printerControl.createPartyPrinterGroupUse(party, printerGroupUseType, inventoryMovePrinterGroup, createdBy);
145
146                                                printerGroupUseType = printerControl.getPrinterGroupUseTypeByName(PrinterConstants.PrinterGroupUseType_WAREHOUSE_PACKING_LIST);
147                                                printerControl.createPartyPrinterGroupUse(party, printerGroupUseType, packingListPrinterGroup, createdBy);
148
149                                                printerGroupUseType = printerControl.getPrinterGroupUseTypeByName(PrinterConstants.PrinterGroupUseType_WAREHOUSE_PICKLIST);
150                                                printerControl.createPartyPrinterGroupUse(party, printerGroupUseType, picklistPrinterGroup, createdBy);
151
152                                                printerGroupUseType = printerControl.getPrinterGroupUseTypeByName(PrinterConstants.PrinterGroupUseType_WAREHOUSE_SHIPPING_MANIFEST);
153                                                printerControl.createPartyPrinterGroupUse(party, printerGroupUseType, shippingManifestPrinterGroup, createdBy);
154                                            }
155                                        }
156                                    } else {
157                                        addExecutionError(ExecutionErrors.UnknownShippingManifestPrinterGroupName.name(), shippingManifestPrinterGroupName);
158                                    }
159                                } else {
160                                    addExecutionError(ExecutionErrors.UnknownPackingListPrinterGroupName.name(), packingListPrinterGroupName);
161                                }
162                            } else {
163                                addExecutionError(ExecutionErrors.UnknownPicklistPrinterGroupName.name(), picklistPrinterGroupName);
164                            }
165                        } else {
166                            addExecutionError(ExecutionErrors.UnknownInventoryMovePrinterGroupName.name(), inventoryMovePrinterGroupName);
167                        }
168                    } else {
169                        addExecutionError(ExecutionErrors.UnknownCurrencyIsoName.name(), preferredCurrencyIsoName);
170                    }
171                } else {
172                    addExecutionError(ExecutionErrors.UnknownDateTimeFormatName.name(), preferredDateTimeFormatName);
173                }
174            } else {
175                addExecutionError(ExecutionErrors.UnknownJavaTimeZoneName.name(), preferredJavaTimeZoneName);
176            }
177        } else {
178            addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), preferredLanguageIsoName);
179        }
180
181        if(warehouse != null) {
182            var party = warehouse.getParty();
183            
184            result.setEntityRef(party.getPrimaryKey().getEntityRef());
185            result.setWarehouseName(warehouse.getWarehouseName());
186            result.setPartyName(party.getLastDetail().getPartyName());
187        }
188        
189        return result;
190    }
191    
192}