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