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.GetShipmentAliasesForm;
020import com.echothree.control.user.shipment.common.result.GetShipmentAliasesResult;
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.ShipmentType;
029import com.echothree.model.data.user.common.pk.UserVisitPK;
030import com.echothree.model.data.user.server.entity.UserVisit;
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.common.command.BaseResult;
035import com.echothree.util.server.control.BaseSimpleCommand;
036import com.echothree.util.server.control.CommandSecurityDefinition;
037import com.echothree.util.server.control.PartyTypeDefinition;
038import com.echothree.util.server.control.SecurityRoleDefinition;
039import com.echothree.util.server.persistence.Session;
040import java.util.Arrays;
041import java.util.Collections;
042import java.util.List;
043
044public class GetShipmentAliasesCommand
045        extends BaseSimpleCommand<GetShipmentAliasesForm> {
046    
047    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
048    
049    static {
050        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
051                new FieldDefinition("ShipmentTypeName", FieldType.ENTITY_NAME, true, null, null),
052                new FieldDefinition("ShipmentName", FieldType.ENTITY_NAME, true, null, null)
053                ));
054    }
055    
056    /** Creates a new instance of GetShipmentAliasesCommand */
057    public GetShipmentAliasesCommand(UserVisitPK userVisitPK, GetShipmentAliasesForm 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.List.name())
062                        )))
063                ))), FORM_FIELD_DEFINITIONS, false);
064    }
065    
066    @Override
067    protected BaseResult execute() {
068        var shipmentControl = Session.getModelController(ShipmentControl.class);
069        GetShipmentAliasesResult result = ShipmentResultFactory.getGetShipmentAliasesResult();
070        String shipmentTypeName = form.getShipmentTypeName();
071        ShipmentType shipmentType = shipmentControl.getShipmentTypeByName(shipmentTypeName);
072
073        if(shipmentType != null) {
074            String shipmentName = form.getShipmentName();
075            Shipment shipment = shipmentControl.getShipmentByName(shipmentType, shipmentName);
076
077            if(shipment != null) {
078                UserVisit userVisit = getUserVisit();
079
080                result.setShipment(shipmentControl.getShipmentTransfer(userVisit, shipment));
081                result.setShipmentAliases(shipmentControl.getShipmentAliasTransfersByShipment(userVisit, shipment));
082            } else {
083                addExecutionError(ExecutionErrors.UnknownShipmentName.name(), shipmentTypeName, shipmentName);
084            }
085        } else {
086            addExecutionError(ExecutionErrors.UnknownShipmentTypeName.name(), shipmentTypeName);
087        }
088
089        return result;
090    }
091    
092}