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.edit.WarehouseEdit; 020import com.echothree.control.user.warehouse.common.edit.WarehouseEditFactory; 021import com.echothree.control.user.warehouse.common.form.EditWarehouseForm; 022import com.echothree.control.user.warehouse.common.result.EditWarehouseResult; 023import com.echothree.control.user.warehouse.common.result.WarehouseResultFactory; 024import com.echothree.control.user.warehouse.common.spec.WarehouseUniversalSpec; 025import com.echothree.model.control.accounting.server.control.AccountingControl; 026import com.echothree.model.control.party.common.PartyTypes; 027import com.echothree.model.control.party.server.control.PartyControl; 028import com.echothree.model.control.printer.common.PrinterConstants; 029import com.echothree.model.control.printer.server.control.PrinterControl; 030import com.echothree.model.control.security.common.SecurityRoleGroups; 031import com.echothree.model.control.security.common.SecurityRoles; 032import com.echothree.model.control.warehouse.server.control.WarehouseControl; 033import com.echothree.model.control.warehouse.server.logic.WarehouseLogic; 034import com.echothree.model.control.warehouse.server.logic.WarehouseTypeLogic; 035import com.echothree.model.data.accounting.server.entity.Currency; 036import com.echothree.model.data.party.server.entity.DateTimeFormat; 037import com.echothree.model.data.party.server.entity.Language; 038import com.echothree.model.data.party.server.entity.Party; 039import com.echothree.model.data.party.server.entity.TimeZone; 040import com.echothree.model.data.printer.server.entity.PrinterGroup; 041import com.echothree.model.data.user.common.pk.UserVisitPK; 042import com.echothree.model.data.warehouse.server.entity.WarehouseType; 043import com.echothree.util.common.message.ExecutionErrors; 044import com.echothree.util.common.validation.FieldDefinition; 045import com.echothree.util.common.validation.FieldType; 046import com.echothree.util.server.control.BaseAbstractEditCommand; 047import com.echothree.util.server.control.CommandSecurityDefinition; 048import com.echothree.util.server.control.PartyTypeDefinition; 049import com.echothree.util.server.control.SecurityRoleDefinition; 050import java.util.List; 051import javax.enterprise.context.Dependent; 052import javax.inject.Inject; 053 054@Dependent 055public class EditWarehouseCommand 056 extends BaseAbstractEditCommand<WarehouseUniversalSpec, WarehouseEdit, EditWarehouseResult, Party, Party> { 057 058 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 059 private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS; 060 private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS; 061 062 static { 063 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of( 064 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 065 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 066 new SecurityRoleDefinition(SecurityRoleGroups.Warehouse.name(), SecurityRoles.Edit.name()) 067 )) 068 )); 069 070 SPEC_FIELD_DEFINITIONS = List.of( 071 new FieldDefinition("WarehouseName", FieldType.ENTITY_NAME, false, null, null), 072 new FieldDefinition("PartyName", FieldType.ENTITY_NAME, false, null, null), 073 new FieldDefinition("EntityRef", FieldType.ENTITY_REF, false, null, null), 074 new FieldDefinition("Uuid", FieldType.UUID, false, null, null) 075 ); 076 077 EDIT_FIELD_DEFINITIONS = List.of( 078 new FieldDefinition("WarehouseName", FieldType.ENTITY_NAME, true, null, null), 079 new FieldDefinition("WarehouseTypeName", FieldType.ENTITY_NAME, true, null, null), 080 new FieldDefinition("Name", FieldType.STRING, false, 1L, 60L), 081 new FieldDefinition("PreferredLanguageIsoName", FieldType.ENTITY_NAME, false, null, null), 082 new FieldDefinition("PreferredCurrencyIsoName", FieldType.ENTITY_NAME, false, null, null), 083 new FieldDefinition("PreferredJavaTimeZoneName", FieldType.TIME_ZONE_NAME, false, null, null), 084 new FieldDefinition("PreferredDateTimeFormatName", FieldType.ENTITY_NAME, false, null, null), 085 new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null), 086 new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null), 087 new FieldDefinition("InventoryMovePrinterGroupName", FieldType.ENTITY_NAME, true, null, null), 088 new FieldDefinition("PicklistPrinterGroupName", FieldType.ENTITY_NAME, true, null, null), 089 new FieldDefinition("PackingListPrinterGroupName", FieldType.ENTITY_NAME, true, null, null), 090 new FieldDefinition("ShippingManifestPrinterGroupName", FieldType.ENTITY_NAME, true, null, null) 091 ); 092 } 093 094 @Inject 095 AccountingControl accountingControl; 096 097 @Inject 098 PartyControl partyControl; 099 100 @Inject 101 PrinterControl printerControl; 102 103 @Inject 104 WarehouseControl warehouseControl; 105 106 @Inject 107 WarehouseLogic warehouseLogic; 108 109 @Inject 110 WarehouseTypeLogic warehouseTypeLogic; 111 112 113 /** Creates a new instance of EditWarehouseCommand */ 114 public EditWarehouseCommand() { 115 super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 116 } 117 118 @Override 119 public EditWarehouseResult getResult() { 120 return WarehouseResultFactory.getEditWarehouseResult(); 121 } 122 123 @Override 124 public WarehouseEdit getEdit() { 125 return WarehouseEditFactory.getWarehouseEdit(); 126 } 127 128 @Override 129 public Party getEntity(EditWarehouseResult result) { 130 var warehouse = warehouseLogic.getWarehouseByUniversalSpec(this, spec, false, editModeToEntityPermission(editMode)); 131 132 return warehouse == null ? null : warehouse.getParty(); 133 } 134 135 @Override 136 public Party getLockEntity(Party party) { 137 return party; 138 } 139 140 @Override 141 public void fillInResult(EditWarehouseResult result, Party party) { 142 result.setWarehouse(warehouseControl.getWarehouseTransfer(getUserVisit(), party)); 143 } 144 145 @Override 146 public void doLock(WarehouseEdit edit, Party party) { 147 var warehouse = warehouseControl.getWarehouse(party); 148 var partyDetail = party.getLastDetail(); 149 var partyGroup = partyControl.getPartyGroup(party); 150 var preferredLanguage = partyDetail.getPreferredLanguage(); 151 var preferredCurrency = partyDetail.getPreferredCurrency(); 152 var preferredTimeZone = partyDetail.getPreferredTimeZone(); 153 var dateTimeFormat = partyDetail.getPreferredDateTimeFormat(); 154 155 edit.setWarehouseName(warehouse.getWarehouseName()); 156 edit.setWarehouseTypeName(warehouse.getWarehouseType().getLastDetail().getWarehouseTypeName()); 157 edit.setName(partyGroup == null? null: partyGroup.getName()); 158 edit.setPreferredLanguageIsoName(preferredLanguage == null ? null : preferredLanguage.getLanguageIsoName()); 159 edit.setPreferredCurrencyIsoName(preferredCurrency == null ? null : preferredCurrency.getCurrencyIsoName()); 160 edit.setPreferredJavaTimeZoneName(preferredTimeZone == null ? null : preferredTimeZone.getLastDetail().getJavaTimeZoneName()); 161 edit.setPreferredDateTimeFormatName(dateTimeFormat == null ? null : dateTimeFormat.getLastDetail().getDateTimeFormatName()); 162 edit.setIsDefault(warehouse.getIsDefault().toString()); 163 edit.setSortOrder(warehouse.getSortOrder().toString()); 164 edit.setInventoryMovePrinterGroupName(printerControl.getPartyPrinterGroupUseUsingNames(party, PrinterConstants.PrinterGroupUseType_WAREHOUSE_INVENTORY_MOVE).getPrinterGroup().getLastDetail().getPrinterGroupName()); 165 edit.setPicklistPrinterGroupName(printerControl.getPartyPrinterGroupUseUsingNames(party, PrinterConstants.PrinterGroupUseType_WAREHOUSE_PACKING_LIST).getPrinterGroup().getLastDetail().getPrinterGroupName()); 166 edit.setPackingListPrinterGroupName(printerControl.getPartyPrinterGroupUseUsingNames(party, PrinterConstants.PrinterGroupUseType_WAREHOUSE_PICKLIST).getPrinterGroup().getLastDetail().getPrinterGroupName()); 167 edit.setShippingManifestPrinterGroupName(printerControl.getPartyPrinterGroupUseUsingNames(party, PrinterConstants.PrinterGroupUseType_WAREHOUSE_SHIPPING_MANIFEST).getPrinterGroup().getLastDetail().getPrinterGroupName()); 168 } 169 170 WarehouseType warehouseType; 171 PrinterGroup inventoryMovePrinterGroup; 172 PrinterGroup picklistPrinterGroup; 173 PrinterGroup packingListPrinterGroup; 174 PrinterGroup shippingManifestPrinterGroup; 175 Language preferredLanguage; 176 TimeZone preferredTimeZone; 177 DateTimeFormat preferredDateTimeFormat; 178 Currency preferredCurrency; 179 180 @Override 181 public void canUpdate(Party party) { 182 var warehouse = warehouseControl.getWarehouseForUpdate(party); 183 var warehouseName = edit.getWarehouseName(); 184 var duplicateWarehouse = warehouseControl.getWarehouseByName(warehouseName); 185 186 warehouseType = warehouseTypeLogic.getWarehouseTypeByName(this, edit.getWarehouseTypeName()); 187 188 if(duplicateWarehouse == null || duplicateWarehouse.getPrimaryKey().equals(warehouse.getPrimaryKey())) { 189 var inventoryMovePrinterGroupName = edit.getInventoryMovePrinterGroupName(); 190 191 inventoryMovePrinterGroup = printerControl.getPrinterGroupByName(inventoryMovePrinterGroupName); 192 193 if(inventoryMovePrinterGroup != null) { 194 var picklistPrinterGroupName = edit.getPicklistPrinterGroupName(); 195 196 picklistPrinterGroup = printerControl.getPrinterGroupByName(picklistPrinterGroupName); 197 198 if(picklistPrinterGroup != null) { 199 var packingListPrinterGroupName = edit.getPackingListPrinterGroupName(); 200 201 packingListPrinterGroup = printerControl.getPrinterGroupByName(packingListPrinterGroupName); 202 203 if(packingListPrinterGroup != null) { 204 var shippingManifestPrinterGroupName = edit.getShippingManifestPrinterGroupName(); 205 206 shippingManifestPrinterGroup = printerControl.getPrinterGroupByName(shippingManifestPrinterGroupName); 207 208 if(shippingManifestPrinterGroup != null) { 209 var preferredLanguageIsoName = edit.getPreferredLanguageIsoName(); 210 211 preferredLanguage = preferredLanguageIsoName == null ? null : partyControl.getLanguageByIsoName(preferredLanguageIsoName); 212 213 if(preferredLanguageIsoName == null || (preferredLanguage != null)) { 214 var preferredJavaTimeZoneName = edit.getPreferredJavaTimeZoneName(); 215 216 preferredTimeZone = preferredJavaTimeZoneName == null ? null : partyControl.getTimeZoneByJavaName(preferredJavaTimeZoneName); 217 218 if(preferredJavaTimeZoneName == null || (preferredTimeZone != null)) { 219 var preferredDateTimeFormatName = edit.getPreferredDateTimeFormatName(); 220 221 preferredDateTimeFormat = preferredDateTimeFormatName == null ? null : partyControl.getDateTimeFormatByName(preferredDateTimeFormatName); 222 223 if(preferredDateTimeFormatName == null || (preferredDateTimeFormat != null)) { 224 var preferredCurrencyIsoName = edit.getPreferredCurrencyIsoName(); 225 226 if(preferredCurrencyIsoName == null) { 227 preferredCurrency = null; 228 } else { 229 preferredCurrency = accountingControl.getCurrencyByIsoName(preferredCurrencyIsoName); 230 } 231 232 if(preferredCurrencyIsoName != null && (preferredCurrency == null)) { 233 addExecutionError(ExecutionErrors.UnknownCurrencyIsoName.name(), preferredCurrencyIsoName); 234 } 235 } else { 236 addExecutionError(ExecutionErrors.UnknownDateTimeFormatName.name(), preferredDateTimeFormatName); 237 } 238 } else { 239 addExecutionError(ExecutionErrors.UnknownJavaTimeZoneName.name(), preferredJavaTimeZoneName); 240 } 241 } else { 242 addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), preferredLanguageIsoName); 243 } 244 } else { 245 addExecutionError(ExecutionErrors.UnknownShippingManifestPrinterGroupName.name(), shippingManifestPrinterGroupName); 246 } 247 } else { 248 addExecutionError(ExecutionErrors.UnknownPackingListPrinterGroupName.name(), packingListPrinterGroupName); 249 } 250 } else { 251 addExecutionError(ExecutionErrors.UnknownPicklistPrinterGroupName.name(), picklistPrinterGroupName); 252 } 253 } else { 254 addExecutionError(ExecutionErrors.UnknownInventoryMovePrinterGroupName.name(), inventoryMovePrinterGroupName); 255 } 256 } else { 257 addExecutionError(ExecutionErrors.DuplicateWarehouseName.name(), warehouseName); 258 } 259 } 260 261 @Override 262 public void doUpdate(Party party) { 263 var warehouse = warehouseControl.getWarehouseForUpdate(party); 264 var warehouseValue = warehouseControl.getWarehouseValue(warehouse); 265 var partyDetailValue = partyControl.getPartyDetailValueForUpdate(party); 266 var partyGroup = partyControl.getPartyGroupForUpdate(party); 267 var name = edit.getName(); 268 var updatedBy = getPartyPK(); 269 270 warehouseValue.setWarehouseName(edit.getWarehouseName()); 271 warehouseValue.setWarehouseTypePK(warehouseType.getPrimaryKey()); 272 warehouseValue.setIsDefault(Boolean.valueOf(edit.getIsDefault())); 273 warehouseValue.setSortOrder(Integer.valueOf(edit.getSortOrder())); 274 275 partyDetailValue.setPreferredLanguagePK(preferredLanguage == null ? null : preferredLanguage.getPrimaryKey()); 276 partyDetailValue.setPreferredTimeZonePK(preferredTimeZone == null ? null : preferredTimeZone.getPrimaryKey()); 277 partyDetailValue.setPreferredDateTimeFormatPK(preferredDateTimeFormat == null ? null : preferredDateTimeFormat.getPrimaryKey()); 278 partyDetailValue.setPreferredCurrencyPK(preferredCurrency == null ? null : preferredCurrency.getPrimaryKey()); 279 280 var printerGroupUseType = printerControl.getPrinterGroupUseTypeByName(PrinterConstants.PrinterGroupUseType_WAREHOUSE_INVENTORY_MOVE); 281 var partyPrinterGroupUseValue = printerControl.getPartyPrinterGroupUseValueForUpdate(party, printerGroupUseType); 282 partyPrinterGroupUseValue.setPrinterGroupPK(inventoryMovePrinterGroup.getPrimaryKey()); 283 printerControl.updatePartyPrinterGroupUseFromValue(partyPrinterGroupUseValue, updatedBy); 284 285 printerGroupUseType = printerControl.getPrinterGroupUseTypeByName(PrinterConstants.PrinterGroupUseType_WAREHOUSE_PACKING_LIST); 286 partyPrinterGroupUseValue = printerControl.getPartyPrinterGroupUseValueForUpdate(party, printerGroupUseType); 287 partyPrinterGroupUseValue.setPrinterGroupPK(packingListPrinterGroup.getPrimaryKey()); 288 printerControl.updatePartyPrinterGroupUseFromValue(partyPrinterGroupUseValue, updatedBy); 289 290 printerGroupUseType = printerControl.getPrinterGroupUseTypeByName(PrinterConstants.PrinterGroupUseType_WAREHOUSE_PICKLIST); 291 partyPrinterGroupUseValue = printerControl.getPartyPrinterGroupUseValueForUpdate(party, printerGroupUseType); 292 partyPrinterGroupUseValue.setPrinterGroupPK(picklistPrinterGroup.getPrimaryKey()); 293 printerControl.updatePartyPrinterGroupUseFromValue(partyPrinterGroupUseValue, updatedBy); 294 295 printerGroupUseType = printerControl.getPrinterGroupUseTypeByName(PrinterConstants.PrinterGroupUseType_WAREHOUSE_SHIPPING_MANIFEST); 296 partyPrinterGroupUseValue = printerControl.getPartyPrinterGroupUseValueForUpdate(party, printerGroupUseType); 297 partyPrinterGroupUseValue.setPrinterGroupPK(shippingManifestPrinterGroup.getPrimaryKey()); 298 printerControl.updatePartyPrinterGroupUseFromValue(partyPrinterGroupUseValue, updatedBy); 299 300 if(name != null) { 301 if(partyGroup != null) { 302 var partyGroupValue = partyControl.getPartyGroupValue(partyGroup); 303 304 partyGroupValue.setName(name); 305 partyControl.updatePartyGroupFromValue(partyGroupValue, updatedBy); 306 } else { 307 partyControl.createPartyGroup(party, name, updatedBy); 308 } 309 } else { 310 if(partyGroup != null) { 311 partyControl.deletePartyGroup(partyGroup, updatedBy); 312 } 313 } 314 315 warehouseLogic.updateWarehouseFromValue(this, warehouseValue, updatedBy); 316 partyControl.updatePartyFromValue(partyDetailValue, updatedBy); 317 } 318 319}