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.core.server.command;
018
019import com.echothree.control.user.core.common.form.GetCommandsForm;
020import com.echothree.control.user.core.common.result.CoreResultFactory;
021import com.echothree.model.control.core.server.logic.ComponentVendorLogic;
022import com.echothree.model.data.core.server.entity.Command;
023import com.echothree.model.data.core.server.entity.ComponentVendor;
024import com.echothree.model.data.core.server.factory.CommandFactory;
025import com.echothree.util.common.command.BaseResult;
026import com.echothree.util.common.validation.FieldDefinition;
027import com.echothree.util.common.validation.FieldType;
028import com.echothree.util.server.control.BasePaginatedMultipleEntitiesCommand;
029import java.util.Collection;
030import java.util.List;
031import javax.enterprise.context.Dependent;
032import javax.inject.Inject;
033
034@Dependent
035public class GetCommandsCommand
036        extends BasePaginatedMultipleEntitiesCommand<Command, GetCommandsForm> {
037
038    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
039
040    static {
041        FORM_FIELD_DEFINITIONS = List.of(
042                new FieldDefinition("ComponentVendorName", FieldType.ENTITY_NAME, false, null, null)
043        );
044    }
045
046    @Inject
047    ComponentVendorLogic componentVendorLogic;
048
049    /** Creates a new instance of GetCommandsCommand */
050    public GetCommandsCommand() {
051        super(null, FORM_FIELD_DEFINITIONS, true);
052    }
053
054    ComponentVendor componentVendor;
055
056    @Override
057    protected void handleForm() {
058        var componentVendorName = form.getComponentVendorName();
059
060        if(componentVendorName != null) {
061            componentVendor = componentVendorLogic.getComponentVendorByName(this, componentVendorName);
062        }
063    }
064
065    @Override
066    protected Long getTotalEntities() {
067        return hasExecutionErrors() ? null :
068                componentVendor == null ?
069                        commandControl.countCommands() :
070                        commandControl.countCommandsByComponentVendor(componentVendor);
071    }
072
073    @Override
074    protected Collection<Command> getEntities() {
075        Collection<Command> entities = null;
076
077        if(!hasExecutionErrors()) {
078            entities = componentVendor == null ?
079                    commandControl.getCommands() :
080                    commandControl.getCommandsByComponentVendor(componentVendor);
081        }
082
083        return entities;
084    }
085
086    @Override
087    protected BaseResult getResult(Collection<Command> entities) {
088        var result = CoreResultFactory.getGetCommandsResult();
089
090        if(entities != null) {
091            var userVisit = getUserVisit();
092
093            if(componentVendor != null) {
094                result.setComponentVendor(componentControl.getComponentVendorTransfer(userVisit, componentVendor));
095            }
096
097            if(session.hasLimit(CommandFactory.class)) {
098                result.setCommandCount(getTotalEntities());
099            }
100
101            result.setCommands(commandControl.getCommandTransfers(userVisit, entities));
102        }
103
104        return result;
105    }
106
107}