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.inventory.server.command; 018 019import com.echothree.control.user.inventory.common.edit.InventoryEditFactory; 020import com.echothree.control.user.inventory.common.edit.LotAliasEdit; 021import com.echothree.control.user.inventory.common.form.EditLotAliasForm; 022import com.echothree.control.user.inventory.common.result.EditLotAliasResult; 023import com.echothree.control.user.inventory.common.result.InventoryResultFactory; 024import com.echothree.control.user.inventory.common.spec.LotAliasSpec; 025import com.echothree.model.control.inventory.server.control.LotAliasControl; 026import com.echothree.model.control.inventory.server.control.LotControl; 027import com.echothree.model.control.inventory.server.logic.LotLogic; 028import com.echothree.model.control.item.server.logic.ItemLogic; 029import com.echothree.model.control.party.common.PartyTypes; 030import com.echothree.model.control.security.common.SecurityRoleGroups; 031import com.echothree.model.control.security.common.SecurityRoles; 032import com.echothree.model.data.inventory.server.entity.LotAlias; 033import com.echothree.model.data.inventory.server.entity.LotAliasType; 034import com.echothree.model.data.user.common.pk.UserVisitPK; 035import com.echothree.util.common.command.EditMode; 036import com.echothree.util.common.message.ExecutionErrors; 037import com.echothree.util.common.validation.FieldDefinition; 038import com.echothree.util.common.validation.FieldType; 039import com.echothree.util.server.control.BaseAbstractEditCommand; 040import com.echothree.util.server.control.CommandSecurityDefinition; 041import com.echothree.util.server.control.PartyTypeDefinition; 042import com.echothree.util.server.control.SecurityRoleDefinition; 043import com.echothree.util.server.persistence.Session; 044import java.util.List; 045import javax.enterprise.context.Dependent; 046 047@Dependent 048public class EditLotAliasCommand 049 extends BaseAbstractEditCommand<LotAliasSpec, LotAliasEdit, EditLotAliasResult, LotAlias, LotAlias> { 050 051 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 052 private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS; 053 private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS; 054 055 static { 056 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of( 057 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 058 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 059 new SecurityRoleDefinition(SecurityRoleGroups.LotAliasType.name(), SecurityRoles.Edit.name()) 060 )) 061 )); 062 063 SPEC_FIELD_DEFINITIONS = List.of( 064 new FieldDefinition("ItemName", FieldType.ENTITY_NAME, true, null, null), 065 new FieldDefinition("LotIdentifier", FieldType.STRING, true, 1L, 40L), 066 new FieldDefinition("LotAliasTypeName", FieldType.ENTITY_NAME, true, null, null) 067 ); 068 069 EDIT_FIELD_DEFINITIONS = List.of( 070 new FieldDefinition("Alias", FieldType.ENTITY_NAME, true, null, null) 071 ); 072 } 073 074 /** Creates a new instance of EditLotAliasCommand */ 075 public EditLotAliasCommand() { 076 super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 077 } 078 079 @Override 080 public EditLotAliasResult getResult() { 081 return InventoryResultFactory.getEditLotAliasResult(); 082 } 083 084 @Override 085 public LotAliasEdit getEdit() { 086 return InventoryEditFactory.getLotAliasEdit(); 087 } 088 089 LotAliasType lotAliasType; 090 091 @Override 092 public LotAlias getEntity(EditLotAliasResult result) { 093 LotAlias lotAlias = null; 094 var item = ItemLogic.getInstance().getItemByName(this, spec.getItemName()); 095 096 if(!hasExecutionErrors()) { 097 var lotIdentifier = spec.getLotIdentifier(); 098 var lot = LotLogic.getInstance().getLotByIdentifier(this, item, lotIdentifier); 099 100 if(!hasExecutionErrors()) { 101 var lotAliasControl = Session.getModelController(LotAliasControl.class); 102 var lotAliasTypeName = spec.getLotAliasTypeName(); 103 104 lotAliasType = lotAliasControl.getLotAliasTypeByName(lotAliasTypeName); 105 106 if(lotAliasType != null) { 107 if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) { 108 lotAlias = lotAliasControl.getLotAlias(lot, lotAliasType); 109 } else { // EditMode.UPDATE 110 lotAlias = lotAliasControl.getLotAliasForUpdate(lot, lotAliasType); 111 } 112 113 if(lotAlias != null) { 114 result.setLotAlias(lotAliasControl.getLotAliasTransfer(getUserVisit(), lotAlias)); 115 } else { 116 addExecutionError(ExecutionErrors.UnknownLotAlias.name(), item.getLastDetail().getItemName(), 117 lot.getLastDetail().getLotIdentifier(), lotAliasTypeName); 118 } 119 } else { 120 addExecutionError(ExecutionErrors.UnknownLotAliasTypeName.name(), lotAliasTypeName); 121 } 122 } 123 } 124 125 return lotAlias; 126 } 127 128 @Override 129 public LotAlias getLockEntity(LotAlias lotAlias) { 130 return lotAlias; 131 } 132 133 @Override 134 public void fillInResult(EditLotAliasResult result, LotAlias lotAlias) { 135 var lotAliasControl = Session.getModelController(LotAliasControl.class); 136 137 result.setLotAlias(lotAliasControl.getLotAliasTransfer(getUserVisit(), lotAlias)); 138 } 139 140 @Override 141 public void doLock(LotAliasEdit edit, LotAlias lotAlias) { 142 edit.setAlias(lotAlias.getAlias()); 143 } 144 145 @Override 146 public void canUpdate(LotAlias lotAlias) { 147 var lotAliasControl = Session.getModelController(LotAliasControl.class); 148 var alias = edit.getAlias(); 149 var duplicateLotAlias = lotAliasControl.getLotAliasByAlias(lotAliasType, alias); 150 151 if(duplicateLotAlias != null && !lotAlias.equals(duplicateLotAlias)) { 152 var lotAliasTypeDetail = lotAlias.getLotAliasType().getLastDetail(); 153 154 addExecutionError(ExecutionErrors.DuplicateLotAlias.name(), lotAliasTypeDetail.getLotAliasTypeName(), alias); 155 } 156 } 157 158 @Override 159 public void doUpdate(LotAlias lotAlias) { 160 var lotAliasControl = Session.getModelController(LotAliasControl.class); 161 var lotAliasValue = lotAliasControl.getLotAliasValue(lotAlias); 162 163 lotAliasValue.setAlias(edit.getAlias()); 164 165 lotAliasControl.updateLotAliasFromValue(lotAliasValue, getPartyPK()); 166 } 167 168}