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.GetPrinterGroupJobsForm;
020import com.echothree.control.user.printer.common.result.PrinterResultFactory;
021import com.echothree.model.control.printer.common.workflow.PrinterGroupJobStatusConstants;
022import com.echothree.model.control.printer.server.control.PrinterControl;
023import com.echothree.model.control.workflow.server.control.WorkflowControl;
024import com.echothree.model.control.workflow.server.logic.WorkflowStepLogic;
025import com.echothree.model.data.printer.server.entity.PrinterGroup;
026import com.echothree.model.data.printer.server.entity.PrinterGroupJob;
027import com.echothree.model.data.printer.server.factory.PrinterGroupJobFactory;
028import com.echothree.model.data.workflow.server.entity.WorkflowStep;
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 java.util.Collection;
035import java.util.List;
036import javax.enterprise.context.Dependent;
037import javax.inject.Inject;
038
039@Dependent
040public class GetPrinterGroupJobsCommand
041        extends BasePaginatedMultipleEntitiesCommand<PrinterGroupJob, GetPrinterGroupJobsForm> {
042
043    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
044
045    static {
046        FORM_FIELD_DEFINITIONS = List.of(
047                new FieldDefinition("PrinterGroupName", FieldType.ENTITY_NAME, false, null, null),
048                new FieldDefinition("PrinterGroupJobStatusChoice", FieldType.ENTITY_NAME, false, null, null)
049        );
050    }
051
052    @Inject
053    PrinterControl printerControl;
054
055    @Inject
056    WorkflowControl workflowControl;
057
058    @Inject
059    WorkflowStepLogic workflowStepLogic;
060
061    /** Creates a new instance of GetPrinterGroupJobsCommand */
062    public GetPrinterGroupJobsCommand() {
063        super(null, FORM_FIELD_DEFINITIONS, true);
064    }
065
066    PrinterGroup printerGroup;
067    WorkflowStep workflowStep;
068
069    @Override
070    protected void handleForm() {
071        var printerGroupName = form.getPrinterGroupName();
072        var printerGroupJobStatusChoice = form.getPrinterGroupJobStatusChoice();
073        var parameterCount = (printerGroupName == null ? 0 : 1) + (printerGroupJobStatusChoice == null ? 0 : 1);
074
075        if(parameterCount == 1) {
076            if(printerGroupName != null) {
077                printerGroup = printerControl.getPrinterGroupByName(printerGroupName);
078
079                if(printerGroup == null) {
080                    addExecutionError(ExecutionErrors.UnknownPrinterGroupName.name(), printerGroupName);
081                }
082            } else {
083                var workflow = workflowControl.getWorkflowByName(PrinterGroupJobStatusConstants.Workflow_PRINTER_GROUP_JOB_STATUS);
084
085                workflowStep = workflowStepLogic.getWorkflowStepByName(this, workflow, printerGroupJobStatusChoice);
086            }
087        } else {
088            addExecutionError(ExecutionErrors.InvalidParameterCount.name());
089        }
090    }
091
092    @Override
093    protected Long getTotalEntities() {
094        return hasExecutionErrors() ? null :
095                printerGroup != null ? printerControl.countPrinterGroupJobsByPrinterGroup(printerGroup) :
096                        printerControl.countPrinterGroupJobsByPrinterGroupJobStatus(workflowStep);
097    }
098
099    @Override
100    protected Collection<PrinterGroupJob> getEntities() {
101        return hasExecutionErrors() ? null :
102                printerGroup != null ? printerControl.getPrinterGroupJobsByPrinterGroup(printerGroup) :
103                        printerControl.getPrinterGroupJobsByPrinterGroupJobStatus(workflowStep);
104    }
105
106    @Override
107    protected BaseResult getResult(Collection<PrinterGroupJob> entities) {
108        var result = PrinterResultFactory.getGetPrinterGroupJobsResult();
109
110        if(entities != null) {
111            if(printerGroup != null) {
112                result.setPrinterGroup(printerControl.getPrinterGroupTransfer(getUserVisit(), printerGroup));
113            }
114
115            if(session.hasLimit(PrinterGroupJobFactory.class)) {
116                result.setPrinterGroupJobCount(getTotalEntities());
117            }
118
119            result.setPrinterGroupJobs(printerControl.getPrinterGroupJobTransfers(getUserVisit(), entities));
120        }
121
122        return result;
123    }
124
125}