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