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 java.util.List;
044import javax.inject.Inject;
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    @Inject
075    LotAliasControl lotAliasControl;
076
077    @Inject
078    LotControl lotControl;
079
080    @Inject
081    ItemLogic itemLogic;
082
083    @Inject
084    LotLogic lotLogic;
085
086    /** Creates a new instance of EditLotAliasCommand */
087    public EditLotAliasCommand() {
088        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
089    }
090
091    @Override
092    public EditLotAliasResult getResult() {
093        return InventoryResultFactory.getEditLotAliasResult();
094    }
095
096    @Override
097    public LotAliasEdit getEdit() {
098        return InventoryEditFactory.getLotAliasEdit();
099    }
100
101    LotAliasType lotAliasType;
102
103    @Override
104    public LotAlias getEntity(EditLotAliasResult result) {
105        LotAlias lotAlias = null;
106        var item = itemLogic.getItemByName(this, spec.getItemName());
107
108        if(!hasExecutionErrors()) {
109            var lotIdentifier = spec.getLotIdentifier();
110            var lot = lotLogic.getLotByIdentifier(this, item, lotIdentifier);
111
112            if(!hasExecutionErrors()) {
113                var lotAliasTypeName = spec.getLotAliasTypeName();
114
115                lotAliasType = lotAliasControl.getLotAliasTypeByName(lotAliasTypeName);
116
117                if(lotAliasType != null) {
118                    if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
119                        lotAlias = lotAliasControl.getLotAlias(lot, lotAliasType);
120                    } else { // EditMode.UPDATE
121                        lotAlias = lotAliasControl.getLotAliasForUpdate(lot, lotAliasType);
122                    }
123
124                    if(lotAlias != null) {
125                        result.setLotAlias(lotAliasControl.getLotAliasTransfer(getUserVisit(), lotAlias));
126                    } else {
127                        addExecutionError(ExecutionErrors.UnknownLotAlias.name(), item.getLastDetail().getItemName(),
128                                lot.getLastDetail().getLotIdentifier(), lotAliasTypeName);
129                    }
130                } else {
131                    addExecutionError(ExecutionErrors.UnknownLotAliasTypeName.name(), lotAliasTypeName);
132                }
133            }
134        }
135
136        return lotAlias;
137    }
138
139    @Override
140    public LotAlias getLockEntity(LotAlias lotAlias) {
141        return lotAlias;
142    }
143
144    @Override
145    public void fillInResult(EditLotAliasResult result, LotAlias lotAlias) {
146        result.setLotAlias(lotAliasControl.getLotAliasTransfer(getUserVisit(), lotAlias));
147    }
148
149    @Override
150    public void doLock(LotAliasEdit edit, LotAlias lotAlias) {
151        edit.setAlias(lotAlias.getAlias());
152    }
153
154    @Override
155    public void canUpdate(LotAlias lotAlias) {
156        var alias = edit.getAlias();
157        var duplicateLotAlias = lotAliasControl.getLotAliasByAlias(lotAliasType, alias);
158
159        if(duplicateLotAlias != null && !lotAlias.equals(duplicateLotAlias)) {
160            var lotAliasTypeDetail = lotAlias.getLotAliasType().getLastDetail();
161
162            addExecutionError(ExecutionErrors.DuplicateLotAlias.name(), lotAliasTypeDetail.getLotAliasTypeName(), alias);
163        }
164    }
165
166    @Override
167    public void doUpdate(LotAlias lotAlias) {
168        var lotAliasValue = lotAliasControl.getLotAliasValue(lotAlias);
169
170        lotAliasValue.setAlias(edit.getAlias());
171
172        lotAliasControl.updateLotAliasFromValue(lotAliasValue, getPartyPK());
173    }
174
175}