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.order.server.command;
018
019import com.echothree.control.user.order.common.form.GetOrderPrioritiesForm;
020import com.echothree.control.user.order.common.result.OrderResultFactory;
021import com.echothree.model.control.order.server.control.OrderPriorityControl;
022import com.echothree.model.control.order.server.control.OrderTypeControl;
023import com.echothree.model.control.order.server.logic.OrderTypeLogic;
024import com.echothree.model.control.party.common.PartyTypes;
025import com.echothree.model.control.security.common.SecurityRoleGroups;
026import com.echothree.model.control.security.common.SecurityRoles;
027import com.echothree.model.data.order.server.entity.OrderPriority;
028import com.echothree.model.data.order.server.entity.OrderType;
029import com.echothree.model.data.order.server.factory.OrderPriorityFactory;
030import com.echothree.util.common.command.BaseResult;
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 com.echothree.util.server.persistence.Session;
038import java.util.Collection;
039import java.util.List;
040import javax.enterprise.context.RequestScoped;
041
042@RequestScoped
043public class GetOrderPrioritiesCommand
044        extends BasePaginatedMultipleEntitiesCommand<OrderPriority, GetOrderPrioritiesForm> {
045    
046    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
047    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
048    
049    static {
050        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
051                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
052                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
053                        new SecurityRoleDefinition(SecurityRoleGroups.OrderPriority.name(), SecurityRoles.List.name())
054                ))
055        ));
056
057        FORM_FIELD_DEFINITIONS = List.of(
058                new FieldDefinition("OrderTypeName", FieldType.ENTITY_NAME, true, null, null)
059        );
060    }
061    
062    /** Creates a new instance of GetOrderPrioritiesCommand */
063    public GetOrderPrioritiesCommand() {
064        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true);
065    }
066
067    OrderType orderType;
068
069    @Override
070    protected void handleForm() {
071        var orderTypeName = form.getOrderTypeName();
072
073        orderType = OrderTypeLogic.getInstance().getOrderTypeByName(this, orderTypeName);
074    }
075
076    @Override
077    protected Long getTotalEntities() {
078        var orderPriorityControl = Session.getModelController(OrderPriorityControl.class);
079
080        return hasExecutionErrors() ? null : orderPriorityControl.countOrderPriorities(orderType);
081    }
082
083    @Override
084    protected Collection<OrderPriority> getEntities() {
085        Collection<OrderPriority> orderPriorities = null;
086
087        if(!hasExecutionErrors()) {
088            var orderPriorityControl = Session.getModelController(OrderPriorityControl.class);
089
090            orderPriorities = orderPriorityControl.getOrderPriorities(orderType);
091        }
092
093        return orderPriorities;
094    }
095
096    @Override
097    protected BaseResult getResult(Collection<OrderPriority> entities) {
098        var result = OrderResultFactory.getGetOrderPrioritiesResult();
099
100        if(entities != null) {
101            var orderTypeControl = Session.getModelController(OrderTypeControl.class);
102            var orderPriorityControl = Session.getModelController(OrderPriorityControl.class);
103            var userVisit = getUserVisit();
104
105            if(session.hasLimit(OrderPriorityFactory.class)) {
106                result.setOrderPriorityCount(getTotalEntities());
107            }
108
109            result.setOrderType(orderTypeControl.getOrderTypeTransfer(userVisit, orderType));
110            result.setOrderPriorities(orderPriorityControl.getOrderPriorityTransfers(userVisit, entities));
111        }
112
113        return result;
114    }
115    
116}