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