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.printer.server.command;
018
019import com.echothree.control.user.printer.common.form.GetPrintersForm;
020import com.echothree.control.user.printer.common.result.PrinterResultFactory;
021import com.echothree.model.control.printer.server.control.PrinterControl;
022import com.echothree.model.data.printer.server.entity.Printer;
023import com.echothree.model.data.printer.server.entity.PrinterGroup;
024import com.echothree.model.data.printer.server.factory.PrinterFactory;
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 GetPrintersCommand
036        extends BasePaginatedMultipleEntitiesCommand<Printer, GetPrintersForm> {
037
038    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
039
040    static {
041        FORM_FIELD_DEFINITIONS = List.of(
042                new FieldDefinition("PrinterGroupName", FieldType.ENTITY_NAME, false, null, null)
043        );
044    }
045
046    @Inject
047    PrinterControl printerControl;
048
049    /** Creates a new instance of GetPrintersCommand */
050    public GetPrintersCommand() {
051        super(null, FORM_FIELD_DEFINITIONS, true);
052    }
053
054    PrinterGroup printerGroup;
055
056    @Override
057    protected void handleForm() {
058        var printerGroupName = form.getPrinterGroupName();
059
060        if(printerGroupName != null) {
061            printerGroup = printerControl.getPrinterGroupByName(printerGroupName);
062
063            if(printerGroup == null) {
064                addExecutionError(com.echothree.util.common.message.ExecutionErrors.UnknownPrinterGroupName.name(), printerGroupName);
065            }
066        }
067    }
068
069    @Override
070    protected Long getTotalEntities() {
071        return hasExecutionErrors() ? null :
072                printerGroup == null ? printerControl.countPrinters() : printerControl.countPrintersByPrinterGroup(printerGroup);
073    }
074
075    @Override
076    protected Collection<Printer> getEntities() {
077        return hasExecutionErrors() ? null :
078                printerGroup == null ? printerControl.getPrinters() : printerControl.getPrintersByPrinterGroup(printerGroup);
079    }
080
081    @Override
082    protected BaseResult getResult(Collection<Printer> entities) {
083        var result = PrinterResultFactory.getGetPrintersResult();
084
085        if(entities != null) {
086            if(printerGroup != null) {
087                result.setPrinterGroup(printerControl.getPrinterGroupTransfer(getUserVisit(), printerGroup));
088            }
089
090            if(session.hasLimit(PrinterFactory.class)) {
091                result.setPrinterCount(getTotalEntities());
092            }
093
094            result.setPrinters(printerControl.getPrinterTransfers(getUserVisit(), entities));
095        }
096
097        return result;
098    }
099
100}