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