001// --------------------------------------------------------------------------------
002// Copyright 2002-2024 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.GetPrinterGroupJobsResult;
021import com.echothree.control.user.printer.common.result.PrinterResultFactory;
022import com.echothree.model.control.printer.server.control.PrinterControl;
023import com.echothree.model.control.printer.common.workflow.PrinterGroupJobStatusConstants;
024import com.echothree.model.control.workflow.server.control.WorkflowControl;
025import com.echothree.model.data.printer.server.entity.PrinterGroup;
026import com.echothree.model.data.user.common.pk.UserVisitPK;
027import com.echothree.model.data.user.server.entity.UserVisit;
028import com.echothree.model.data.workflow.server.entity.WorkflowStep;
029import com.echothree.util.common.message.ExecutionErrors;
030import com.echothree.util.common.validation.FieldDefinition;
031import com.echothree.util.common.validation.FieldType;
032import com.echothree.util.common.command.BaseResult;
033import com.echothree.util.server.control.BaseSimpleCommand;
034import com.echothree.util.server.persistence.Session;
035import java.util.Arrays;
036import java.util.Collections;
037import java.util.List;
038
039public class GetPrinterGroupJobsCommand
040        extends BaseSimpleCommand<GetPrinterGroupJobsForm> {
041    
042   private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
043
044    static {
045        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
046                new FieldDefinition("PrinterGroupName", FieldType.ENTITY_NAME, false, null, null),
047                new FieldDefinition("PrinterGroupJobStatusChoice", FieldType.ENTITY_NAME, false, null, null)
048                ));
049    }
050
051    /** Creates a new instance of GetPrinterGroupJobsCommand */
052    public GetPrinterGroupJobsCommand(UserVisitPK userVisitPK, GetPrinterGroupJobsForm form) {
053        super(userVisitPK, form, null, FORM_FIELD_DEFINITIONS, true);
054    }
055    
056   @Override
057    protected BaseResult execute() {
058        GetPrinterGroupJobsResult result = PrinterResultFactory.getGetPrinterGroupJobsResult();
059        String printerGroupName = form.getPrinterGroupName();
060        String printerGroupJobStatusChoice = form.getPrinterGroupJobStatusChoice();
061        var parameterCount = (printerGroupName == null ? 0 : 1) + (printerGroupJobStatusChoice == null ? 0 : 1);
062
063        if(parameterCount == 1) {
064            var printerControl = Session.getModelController(PrinterControl.class);
065            UserVisit userVisit = getUserVisit();
066
067            if(printerGroupName != null) {
068                PrinterGroup printerGroup = printerControl.getPrinterGroupByName(printerGroupName);
069
070                if(printerGroup != null) {
071                    result.setPrinterGroup(printerControl.getPrinterGroupTransfer(userVisit, printerGroup));
072                    result.setPrinterGroupJobs(printerControl.getPrinterGroupJobTransfersByPrinterGroup(getUserVisit(), printerGroup));
073                } else {
074                    addExecutionError(ExecutionErrors.UnknownPrinterGroupName.name(), printerGroupName);
075                }
076            } else if(printerGroupJobStatusChoice != null) {
077                var workflowControl = Session.getModelController(WorkflowControl.class);
078                var workflowStep = workflowControl.getWorkflowStepByName(workflowControl.getWorkflowByName(PrinterGroupJobStatusConstants.Workflow_PRINTER_GROUP_JOB_STATUS),
079                        printerGroupJobStatusChoice);
080
081                if(workflowStep != null) {
082                    result.setPrinterGroupJobs(printerControl.getPrinterGroupJobTransfersByPrinterGroupJobStatus(userVisit, workflowStep));
083                } else {
084                    addExecutionError(ExecutionErrors.UnknownPrinterGroupJobStatusChoice.name(), printerGroupJobStatusChoice);
085                }
086            }
087        } else {
088            addExecutionError(ExecutionErrors.InvalidParameterCount.name());
089        }
090        
091        return result;
092    }
093    
094}