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