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.shipment.server.command;
018
019import com.echothree.control.user.shipment.common.edit.ShipmentAliasEdit;
020import com.echothree.control.user.shipment.common.edit.ShipmentEditFactory;
021import com.echothree.control.user.shipment.common.result.EditShipmentAliasResult;
022import com.echothree.control.user.shipment.common.result.ShipmentResultFactory;
023import com.echothree.control.user.shipment.common.spec.ShipmentAliasSpec;
024import com.echothree.control.user.shipment.server.command.util.ShipmentAliasUtil;
025import com.echothree.model.control.party.common.PartyTypes;
026import com.echothree.model.control.security.common.SecurityRoles;
027import com.echothree.model.control.shipment.server.control.ShipmentControl;
028import com.echothree.model.data.shipment.server.entity.ShipmentAlias;
029import com.echothree.model.data.shipment.server.entity.ShipmentAliasType;
030import com.echothree.util.common.command.EditMode;
031import com.echothree.util.common.message.ExecutionErrors;
032import com.echothree.util.common.validation.FieldDefinition;
033import com.echothree.util.common.validation.FieldType;
034import com.echothree.util.server.control.BaseAbstractEditCommand;
035import com.echothree.util.server.control.CommandSecurityDefinition;
036import com.echothree.util.server.control.PartyTypeDefinition;
037import com.echothree.util.server.control.SecurityRoleDefinition;
038import java.util.List;
039import javax.enterprise.context.Dependent;
040import javax.inject.Inject;
041
042@Dependent
043public class EditShipmentAliasCommand
044        extends BaseAbstractEditCommand<ShipmentAliasSpec, ShipmentAliasEdit, EditShipmentAliasResult, ShipmentAlias, ShipmentAlias> {
045    
046    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
047    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
048    
049    static {
050        SPEC_FIELD_DEFINITIONS = List.of(
051                new FieldDefinition("ShipmentTypeName", FieldType.ENTITY_NAME, true, null, null),
052                new FieldDefinition("ShipmentName", FieldType.ENTITY_NAME, true, null, null),
053                new FieldDefinition("ShipmentAliasTypeName", FieldType.ENTITY_NAME, true, null, null)
054        );
055        
056        EDIT_FIELD_DEFINITIONS = List.of(
057                new FieldDefinition("Alias", FieldType.ENTITY_NAME, true, null, null)
058        );
059    }
060
061    @Inject
062    ShipmentControl shipmentControl;
063
064    
065    /** Creates a new instance of EditShipmentAliasCommand */
066    public EditShipmentAliasCommand() {
067        super(null, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
068    }
069
070    @Override
071    protected CommandSecurityDefinition getCommandSecurityDefinition() {
072        return new CommandSecurityDefinition(List.of(
073                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
074                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
075                        new SecurityRoleDefinition(ShipmentAliasUtil.getInstance().getSecurityRoleGroupNameByShipmentTypeSpec(spec == null ? null : spec), SecurityRoles.Edit.name())
076                ))
077        ));
078    }
079
080    @Override
081    public EditShipmentAliasResult getResult() {
082        return ShipmentResultFactory.getEditShipmentAliasResult();
083    }
084
085    @Override
086    public ShipmentAliasEdit getEdit() {
087        return ShipmentEditFactory.getShipmentAliasEdit();
088    }
089
090    ShipmentAliasType shipmentAliasType;
091    
092    @Override
093    public ShipmentAlias getEntity(EditShipmentAliasResult result) {
094        ShipmentAlias shipmentAlias = null;
095        var shipmentTypeName = spec.getShipmentTypeName();
096        var shipmentType = shipmentControl.getShipmentTypeByName(shipmentTypeName);
097
098        if(shipmentType != null) {
099            var shipmentName = spec.getShipmentName();
100            var shipment = shipmentControl.getShipmentByName(shipmentType, shipmentName);
101
102            if(shipment != null) {
103                var shipmentAliasTypeName = spec.getShipmentAliasTypeName();
104
105                shipmentAliasType = shipmentControl.getShipmentAliasTypeByName(shipmentType, shipmentAliasTypeName);
106
107                if(shipmentAliasType != null) {
108                    if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
109                        shipmentAlias = shipmentControl.getShipmentAlias(shipment, shipmentAliasType);
110                    } else { // EditMode.UPDATE
111                        shipmentAlias = shipmentControl.getShipmentAliasForUpdate(shipment, shipmentAliasType);
112                    }
113
114                    if(shipmentAlias != null) {
115                        result.setShipmentAlias(shipmentControl.getShipmentAliasTransfer(getUserVisit(), shipmentAlias));
116                    } else {
117                        addExecutionError(ExecutionErrors.UnknownShipmentAlias.name(), shipmentTypeName, shipmentName, shipmentAliasTypeName);
118                    }
119                } else {
120                    addExecutionError(ExecutionErrors.UnknownShipmentAliasTypeName.name(), shipmentTypeName, shipmentAliasTypeName);
121                }
122            } else {
123                addExecutionError(ExecutionErrors.UnknownShipmentName.name(), shipmentTypeName, shipmentName);
124            }
125        } else {
126            addExecutionError(ExecutionErrors.UnknownShipmentTypeName.name(), shipmentTypeName);
127        }
128
129        return shipmentAlias;
130    }
131
132    @Override
133    public ShipmentAlias getLockEntity(ShipmentAlias shipmentAlias) {
134        return shipmentAlias;
135    }
136
137    @Override
138    public void fillInResult(EditShipmentAliasResult result, ShipmentAlias shipmentAlias) {
139        result.setShipmentAlias(shipmentControl.getShipmentAliasTransfer(getUserVisit(), shipmentAlias));
140    }
141
142    @Override
143    public void doLock(ShipmentAliasEdit edit, ShipmentAlias shipmentAlias) {
144        edit.setAlias(shipmentAlias.getAlias());
145    }
146
147    @Override
148    public void canUpdate(ShipmentAlias shipmentAlias) {
149        var alias = edit.getAlias();
150        var duplicateShipmentAlias = shipmentControl.getShipmentAliasByAlias(shipmentAliasType, alias);
151
152        if(duplicateShipmentAlias != null && !shipmentAlias.equals(duplicateShipmentAlias)) {
153            var shipmentAliasTypeDetail = shipmentAlias.getShipmentAliasType().getLastDetail();
154
155            addExecutionError(ExecutionErrors.DuplicateShipmentAlias.name(), shipmentAliasTypeDetail.getShipmentType().getLastDetail().getShipmentTypeName(),
156                    shipmentAliasTypeDetail.getShipmentAliasTypeName(), alias);
157        }
158    }
159
160    @Override
161    public void doUpdate(ShipmentAlias shipmentAlias) {
162        var shipmentAliasValue = shipmentControl.getShipmentAliasValue(shipmentAlias);
163
164        shipmentAliasValue.setAlias(edit.getAlias());
165
166        shipmentControl.updateShipmentAliasFromValue(shipmentAliasValue, getPartyPK());
167    }
168
169}