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.model.control.printer.server.logic;
018
019import com.echothree.model.control.document.common.DocumentConstants;
020import com.echothree.model.control.document.server.control.DocumentControl;
021import com.echothree.model.control.document.server.logic.DocumentLogic;
022import com.echothree.model.control.printer.common.workflow.PrinterGroupJobStatusConstants;
023import com.echothree.model.control.printer.server.control.PrinterControl;
024import com.echothree.model.control.workflow.server.control.WorkflowControl;
025import com.echothree.model.data.core.server.entity.MimeType;
026import com.echothree.model.data.party.common.pk.PartyPK;
027import com.echothree.model.data.party.server.entity.Language;
028import com.echothree.model.data.printer.server.entity.PrinterGroup;
029import com.echothree.model.data.printer.server.entity.PrinterGroupJob;
030import com.echothree.util.common.message.ExecutionErrors;
031import com.echothree.util.common.persistence.type.ByteArray;
032import com.echothree.util.server.control.BaseLogic;
033import com.echothree.util.server.message.ExecutionErrorAccumulator;
034import javax.enterprise.context.ApplicationScoped;
035import javax.inject.Inject;
036
037@ApplicationScoped
038public class PrinterGroupJobLogic
039        extends BaseLogic {
040
041    @Inject
042    DocumentControl documentControl;
043
044    @Inject
045    PrinterControl printerControl;
046
047    @Inject
048    WorkflowControl workflowControl;
049
050    @Inject
051    DocumentLogic documentLogic;
052
053    protected PrinterGroupJobLogic() {
054        super();
055    }
056    
057    private void insertPrinterGroupJobIntoWorkflow(final PrinterGroupJob printerGroupJob, final PartyPK createdBy) {
058        workflowControl.addEntityToWorkflowUsingNames(null, PrinterGroupJobStatusConstants.Workflow_PRINTER_GROUP_JOB_STATUS, null,
059                getEntityInstanceByBaseEntity(printerGroupJob), null, null, createdBy);
060    }
061
062    public PrinterGroupJob createPrinterGroupJob(final ExecutionErrorAccumulator ema, final PrinterGroup printerGroup, final Integer copies,
063            final Integer priority, final MimeType mimeType, final Language preferredLanguage, final String description, final ByteArray blob, final String clob,
064            final PartyPK createdBy) {
065        PrinterGroupJob printerGroupJob = null;
066        var documentType = documentControl.getDocumentTypeByName(DocumentConstants.DocumentType_PRINTER_GROUP_JOB);
067
068        if(documentType == null) {
069            addExecutionError(ema, ExecutionErrors.UnknownDocumentTypeName.name(), DocumentConstants.DocumentType_PRINTER_GROUP_JOB);
070        } else {
071            var document = documentLogic.createDocument(ema, documentType, mimeType, preferredLanguage, description, blob, clob, createdBy);
072
073            if(document != null) {
074                printerGroupJob = printerControl.createPrinterGroupJob(printerGroup, document, copies, priority, createdBy);
075                insertPrinterGroupJobIntoWorkflow(printerGroupJob, createdBy);
076            }
077        }
078
079        return printerGroupJob;
080    }
081
082    public void deletePrinterGroupJob(final ExecutionErrorAccumulator ema, final PrinterGroupJob printerGroupJob, final PartyPK deletedBy) {
083        var entityInstance = getEntityInstanceByBaseEntity(printerGroupJob);
084        var workflowEntityStatus = workflowControl.getWorkflowEntityStatusByEntityInstanceForUpdateUsingNames(PrinterGroupJobStatusConstants.Workflow_PRINTER_GROUP_JOB_STATUS, entityInstance);
085        var workflowStepName = workflowEntityStatus.getWorkflowStep().getLastDetail().getWorkflowStepName();
086
087        if(workflowStepName.equals(PrinterGroupJobStatusConstants.WorkflowStep_QUEUED)
088                || workflowStepName.equals(PrinterGroupJobStatusConstants.WorkflowStep_PRINTED)
089                || workflowStepName.equals(PrinterGroupJobStatusConstants.WorkflowStep_ERRORED)) {
090            var keepPrintedJobsTime = printerGroupJob.getLastDetail().getPrinterGroup().getLastDetail().getKeepPrintedJobsTime();
091
092            if(keepPrintedJobsTime == null) {
093                printerControl.removePrinterGroupJob(printerGroupJob);
094            } else {
095                var workflowDestinationName = workflowStepName + "_TO_DELETED";
096
097                workflowControl.transitionEntityInWorkflowUsingNames(null, workflowEntityStatus, workflowDestinationName, keepPrintedJobsTime, deletedBy);
098            }
099        } else {
100            addExecutionError(ema, ExecutionErrors.CannotDeletePrinterGroupJob.name(), workflowStepName);
101        }
102    }
103
104}