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.form.GetShipmentAliasesForm;
020import com.echothree.control.user.shipment.common.result.ShipmentResultFactory;
021import com.echothree.control.user.shipment.server.command.util.ShipmentAliasUtil;
022import com.echothree.model.control.party.common.PartyTypes;
023import com.echothree.model.control.security.common.SecurityRoles;
024import com.echothree.model.control.shipment.server.control.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.ShipmentType;
028import com.echothree.model.data.shipment.server.factory.ShipmentAliasFactory;
029import com.echothree.util.common.command.BaseResult;
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.server.control.BasePaginatedMultipleEntitiesCommand;
034import com.echothree.util.server.control.CommandSecurityDefinition;
035import com.echothree.util.server.control.PartyTypeDefinition;
036import com.echothree.util.server.control.SecurityRoleDefinition;
037import java.util.Collection;
038import java.util.List;
039import javax.enterprise.context.Dependent;
040import javax.inject.Inject;
041
042@Dependent
043public class GetShipmentAliasesCommand
044        extends BasePaginatedMultipleEntitiesCommand<ShipmentAlias, GetShipmentAliasesForm> {
045
046    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
047
048    static {
049        FORM_FIELD_DEFINITIONS = List.of(
050                new FieldDefinition("ShipmentTypeName", FieldType.ENTITY_NAME, true, null, null),
051                new FieldDefinition("ShipmentName", FieldType.ENTITY_NAME, true, null, null)
052        );
053    }
054
055    @Inject
056    ShipmentControl shipmentControl;
057
058    /** Creates a new instance of GetShipmentAliasesCommand */
059    public GetShipmentAliasesCommand() {
060        super(null, FORM_FIELD_DEFINITIONS, true);
061    }
062
063    @Override
064    protected CommandSecurityDefinition getCommandSecurityDefinition() {
065        return new CommandSecurityDefinition(List.of(
066                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
067                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
068                        new SecurityRoleDefinition(ShipmentAliasUtil.getInstance().getSecurityRoleGroupNameByShipmentTypeSpec(form), SecurityRoles.List.name())
069                ))
070        ));
071    }
072
073    private Shipment shipment;
074
075    @Override
076    protected void handleForm() {
077        var shipmentTypeName = form.getShipmentTypeName();
078        var shipmentType = shipmentControl.getShipmentTypeByName(shipmentTypeName);
079
080        if(shipmentType != null) {
081            var shipmentName = form.getShipmentName();
082
083            shipment = shipmentControl.getShipmentByName(shipmentType, shipmentName);
084
085            if(shipment == null) {
086                addExecutionError(ExecutionErrors.UnknownShipmentName.name(), shipmentTypeName, shipmentName);
087            }
088        } else {
089            addExecutionError(ExecutionErrors.UnknownShipmentTypeName.name(), shipmentTypeName);
090        }
091    }
092
093    @Override
094    protected Long getTotalEntities() {
095        return hasExecutionErrors() ? null : shipmentControl.countShipmentAliasesByShipment(shipment);
096    }
097
098    @Override
099    protected Collection<ShipmentAlias> getEntities() {
100        return hasExecutionErrors() ? null : shipmentControl.getShipmentAliasesByShipment(shipment);
101    }
102
103    @Override
104    protected BaseResult getResult(Collection<ShipmentAlias> entities) {
105        var result = ShipmentResultFactory.getGetShipmentAliasesResult();
106
107        if(entities != null) {
108            var userVisit = getUserVisit();
109
110            result.setShipment(shipmentControl.getShipmentTransfer(userVisit, shipment));
111
112            if(session.hasLimit(ShipmentAliasFactory.class)) {
113                result.setShipmentAliasCount(getTotalEntities());
114            }
115
116            result.setShipmentAliases(shipmentControl.getShipmentAliasTransfers(userVisit, entities));
117        }
118
119        return result;
120    }
121
122}