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.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 java.util.List; 042import javax.enterprise.context.Dependent; 043import javax.inject.Inject; 044 045@Dependent 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 @Inject 078 AccountingControl accountingControl; 079 080 @Inject 081 PartyControl partyControl; 082 083 @Inject 084 PrinterControl printerControl; 085 086 @Inject 087 WarehouseLogic warehouseLogic; 088 089 @Inject 090 WarehouseTypeLogic warehouseTypeLogic; 091 092 093 /** Creates a new instance of CreateWarehouseCommand */ 094 public CreateWarehouseCommand() { 095 super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false); 096 } 097 098 @Override 099 protected BaseResult execute() { 100 var result = WarehouseResultFactory.getCreateWarehouseResult(); 101 Warehouse warehouse = null; 102 var preferredLanguageIsoName = form.getPreferredLanguageIsoName(); 103 var preferredLanguage = preferredLanguageIsoName == null? null: partyControl.getLanguageByIsoName(preferredLanguageIsoName); 104 105 if(preferredLanguageIsoName == null || (preferredLanguage != null)) { 106 var preferredJavaTimeZoneName = form.getPreferredJavaTimeZoneName(); 107 var preferredTimeZone = preferredJavaTimeZoneName == null? null: partyControl.getTimeZoneByJavaName(preferredJavaTimeZoneName); 108 109 if(preferredJavaTimeZoneName == null || (preferredTimeZone != null)) { 110 var preferredDateTimeFormatName = form.getPreferredDateTimeFormatName(); 111 var preferredDateTimeFormat = preferredDateTimeFormatName == null? null: partyControl.getDateTimeFormatByName(preferredDateTimeFormatName); 112 113 if(preferredDateTimeFormatName == null || (preferredDateTimeFormat != null)) { 114 var preferredCurrencyIsoName = form.getPreferredCurrencyIsoName(); 115 Currency preferredCurrency; 116 117 if(preferredCurrencyIsoName == null) { 118 preferredCurrency = null; 119 } else { 120 preferredCurrency = accountingControl.getCurrencyByIsoName(preferredCurrencyIsoName); 121 } 122 123 if(preferredCurrencyIsoName == null || (preferredCurrency != null)) { 124 var inventoryMovePrinterGroupName = form.getInventoryMovePrinterGroupName(); 125 var inventoryMovePrinterGroup = printerControl.getPrinterGroupByName(inventoryMovePrinterGroupName); 126 127 if(inventoryMovePrinterGroup != null) { 128 var picklistPrinterGroupName = form.getPicklistPrinterGroupName(); 129 var picklistPrinterGroup = printerControl.getPrinterGroupByName(picklistPrinterGroupName); 130 131 if(picklistPrinterGroup != null) { 132 var packingListPrinterGroupName = form.getPackingListPrinterGroupName(); 133 var packingListPrinterGroup = printerControl.getPrinterGroupByName(packingListPrinterGroupName); 134 135 if(packingListPrinterGroup != null) { 136 var shippingManifestPrinterGroupName = form.getShippingManifestPrinterGroupName(); 137 var shippingManifestPrinterGroup = printerControl.getPrinterGroupByName(shippingManifestPrinterGroupName); 138 139 if(shippingManifestPrinterGroup != null) { 140 var warehouseType = warehouseTypeLogic.getWarehouseTypeByName(this, form.getWarehouseTypeName()); 141 142 if(!hasExecutionErrors()) { 143 var warehouseName = form.getWarehouseName(); 144 var name = form.getName(); 145 var isDefault = Boolean.valueOf(form.getIsDefault()); 146 var sortOrder = Integer.valueOf(form.getSortOrder()); 147 var createdBy = getPartyPK(); 148 149 warehouse = warehouseLogic.createWarehouse(this, warehouseName, 150 warehouseType, preferredLanguage, preferredCurrency, preferredTimeZone, 151 preferredDateTimeFormat, name, isDefault, sortOrder, createdBy); 152 153 if(!hasExecutionErrors()) { 154 var party = warehouse.getParty(); 155 var printerGroupUseType = printerControl.getPrinterGroupUseTypeByName(PrinterConstants.PrinterGroupUseType_WAREHOUSE_INVENTORY_MOVE); 156 printerControl.createPartyPrinterGroupUse(party, printerGroupUseType, inventoryMovePrinterGroup, createdBy); 157 158 printerGroupUseType = printerControl.getPrinterGroupUseTypeByName(PrinterConstants.PrinterGroupUseType_WAREHOUSE_PACKING_LIST); 159 printerControl.createPartyPrinterGroupUse(party, printerGroupUseType, packingListPrinterGroup, createdBy); 160 161 printerGroupUseType = printerControl.getPrinterGroupUseTypeByName(PrinterConstants.PrinterGroupUseType_WAREHOUSE_PICKLIST); 162 printerControl.createPartyPrinterGroupUse(party, printerGroupUseType, picklistPrinterGroup, createdBy); 163 164 printerGroupUseType = printerControl.getPrinterGroupUseTypeByName(PrinterConstants.PrinterGroupUseType_WAREHOUSE_SHIPPING_MANIFEST); 165 printerControl.createPartyPrinterGroupUse(party, printerGroupUseType, shippingManifestPrinterGroup, createdBy); 166 } 167 } 168 } else { 169 addExecutionError(ExecutionErrors.UnknownShippingManifestPrinterGroupName.name(), shippingManifestPrinterGroupName); 170 } 171 } else { 172 addExecutionError(ExecutionErrors.UnknownPackingListPrinterGroupName.name(), packingListPrinterGroupName); 173 } 174 } else { 175 addExecutionError(ExecutionErrors.UnknownPicklistPrinterGroupName.name(), picklistPrinterGroupName); 176 } 177 } else { 178 addExecutionError(ExecutionErrors.UnknownInventoryMovePrinterGroupName.name(), inventoryMovePrinterGroupName); 179 } 180 } else { 181 addExecutionError(ExecutionErrors.UnknownCurrencyIsoName.name(), preferredCurrencyIsoName); 182 } 183 } else { 184 addExecutionError(ExecutionErrors.UnknownDateTimeFormatName.name(), preferredDateTimeFormatName); 185 } 186 } else { 187 addExecutionError(ExecutionErrors.UnknownJavaTimeZoneName.name(), preferredJavaTimeZoneName); 188 } 189 } else { 190 addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), preferredLanguageIsoName); 191 } 192 193 if(warehouse != null) { 194 var party = warehouse.getParty(); 195 196 result.setEntityRef(party.getPrimaryKey().getEntityRef()); 197 result.setWarehouseName(warehouse.getWarehouseName()); 198 result.setPartyName(party.getLastDetail().getPartyName()); 199 } 200 201 return result; 202 } 203 204}