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.shipment.server.command;
018
019import com.echothree.control.user.shipment.common.form.DeleteShipmentAliasForm;
020import com.echothree.control.user.shipment.server.command.util.ShipmentAliasUtil;
021import com.echothree.model.control.party.common.PartyTypes;
022import com.echothree.model.control.security.common.SecurityRoleGroups;
023import com.echothree.model.control.security.common.SecurityRoles;
024import com.echothree.model.control.shipment.server.ShipmentControl;
025import com.echothree.model.data.shipment.server.entity.Shipment;
026import com.echothree.model.data.shipment.server.entity.ShipmentAlias;
027import com.echothree.model.data.shipment.server.entity.ShipmentAliasType;
028import com.echothree.model.data.shipment.server.entity.ShipmentType;
029import com.echothree.model.data.user.common.pk.UserVisitPK;
030import com.echothree.util.common.message.ExecutionErrors;
031import com.echothree.util.common.validation.FieldDefinition;
032import com.echothree.util.common.validation.FieldType;
033import com.echothree.util.common.command.BaseResult;
034import com.echothree.util.server.control.BaseSimpleCommand;
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;
042
043public class DeleteShipmentAliasCommand
044        extends BaseSimpleCommand<DeleteShipmentAliasForm> {
045    
046    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
047    
048    static {
049        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
050                new FieldDefinition("ShipmentTypeName", FieldType.ENTITY_NAME, true, null, null),
051                new FieldDefinition("ShipmentName", FieldType.ENTITY_NAME, true, null, null),
052                new FieldDefinition("ShipmentAliasTypeName", FieldType.ENTITY_NAME, true, null, null)
053                ));
054    }
055    
056    /** Creates a new instance of DeleteShipmentAliasCommand */
057    public DeleteShipmentAliasCommand(UserVisitPK userVisitPK, DeleteShipmentAliasForm form) {
058        super(userVisitPK, form, new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList(
059                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
060                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList(
061                        new SecurityRoleDefinition(ShipmentAliasUtil.getInstance().getSecurityRoleGroupNameByShipmentTypeSpec(form), SecurityRoles.Delete.name())
062                        )))
063                ))), FORM_FIELD_DEFINITIONS, false);
064    }
065    
066    @Override
067    protected BaseResult execute() {
068        var shipmentControl = Session.getModelController(ShipmentControl.class);
069        String shipmentTypeName = form.getShipmentTypeName();
070        ShipmentType shipmentType = shipmentControl.getShipmentTypeByName(shipmentTypeName);
071
072        if(shipmentType != null) {
073            String shipmentName = form.getShipmentName();
074            Shipment shipment = shipmentControl.getShipmentByName(shipmentType, shipmentName);
075
076            if(shipment != null) {
077                String shipmentAliasTypeName = form.getShipmentAliasTypeName();
078                ShipmentAliasType shipmentAliasType = shipmentControl.getShipmentAliasTypeByName(shipmentType, shipmentAliasTypeName);
079
080                if(shipmentAliasType != null) {
081                    ShipmentAlias shipmentAlias = shipmentControl.getShipmentAliasForUpdate(shipment, shipmentAliasType);
082
083                    if(shipmentAlias != null) {
084                        shipmentControl.deleteShipmentAlias(shipmentAlias, getPartyPK());
085                    } else {
086                        addExecutionError(ExecutionErrors.UnknownShipmentAlias.name(), shipmentTypeName, shipmentName, shipmentAliasTypeName);
087                    }
088                } else {
089                    addExecutionError(ExecutionErrors.UnknownShipmentAliasTypeName.name(), shipmentTypeName, shipmentAliasTypeName);
090                }
091            } else {
092                addExecutionError(ExecutionErrors.UnknownShipmentName.name(), shipmentTypeName, shipmentName);
093            }
094        } else {
095            addExecutionError(ExecutionErrors.UnknownShipmentTypeName.name(), shipmentTypeName);
096        }
097
098        return null;
099    }
100    
101}