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.enterprise.inject.spi.CDI;
036import javax.inject.Inject;
037
038@ApplicationScoped
039public class PrinterGroupJobLogic
040        extends BaseLogic {
041
042    @Inject
043    DocumentControl documentControl;
044
045    @Inject
046    PrinterControl printerControl;
047
048    @Inject
049    WorkflowControl workflowControl;
050
051    @Inject
052    DocumentLogic documentLogic;
053
054    protected PrinterGroupJobLogic() {
055        super();
056    }
057
058    public static PrinterGroupJobLogic getInstance() {
059        return CDI.current().select(PrinterGroupJobLogic.class).get();
060    }
061    
062    private void insertPrinterGroupJobIntoWorkflow(final PrinterGroupJob printerGroupJob, final PartyPK createdBy) {
063        workflowControl.addEntityToWorkflowUsingNames(null, PrinterGroupJobStatusConstants.Workflow_PRINTER_GROUP_JOB_STATUS, null,
064                getEntityInstanceByBaseEntity(printerGroupJob), null, null, createdBy);
065    }
066
067    public PrinterGroupJob createPrinterGroupJob(final ExecutionErrorAccumulator ema, final PrinterGroup printerGroup, final Integer copies,
068            final Integer priority, final MimeType mimeType, final Language preferredLanguage, final String description, final ByteArray blob, final String clob,
069            final PartyPK createdBy) {
070        PrinterGroupJob printerGroupJob = null;
071        var documentType = documentControl.getDocumentTypeByName(DocumentConstants.DocumentType_PRINTER_GROUP_JOB);
072
073        if(documentType == null) {
074            addExecutionError(ema, ExecutionErrors.UnknownDocumentTypeName.name(), DocumentConstants.DocumentType_PRINTER_GROUP_JOB);
075        } else {
076            var document = documentLogic.createDocument(ema, documentType, mimeType, preferredLanguage, description, blob, clob, createdBy);
077
078            if(document != null) {
079                printerGroupJob = printerControl.createPrinterGroupJob(printerGroup, document, copies, priority, createdBy);
080                insertPrinterGroupJobIntoWorkflow(printerGroupJob, createdBy);
081            }
082        }
083
084        return printerGroupJob;
085    }
086
087    public void deletePrinterGroupJob(final ExecutionErrorAccumulator ema, final PrinterGroupJob printerGroupJob, final PartyPK deletedBy) {
088        var entityInstance = getEntityInstanceByBaseEntity(printerGroupJob);
089        var workflowEntityStatus = workflowControl.getWorkflowEntityStatusByEntityInstanceForUpdateUsingNames(PrinterGroupJobStatusConstants.Workflow_PRINTER_GROUP_JOB_STATUS, entityInstance);
090        var workflowStepName = workflowEntityStatus.getWorkflowStep().getLastDetail().getWorkflowStepName();
091
092        if(workflowStepName.equals(PrinterGroupJobStatusConstants.WorkflowStep_QUEUED)
093                || workflowStepName.equals(PrinterGroupJobStatusConstants.WorkflowStep_PRINTED)
094                || workflowStepName.equals(PrinterGroupJobStatusConstants.WorkflowStep_ERRORED)) {
095            var keepPrintedJobsTime = printerGroupJob.getLastDetail().getPrinterGroup().getLastDetail().getKeepPrintedJobsTime();
096
097            if(keepPrintedJobsTime == null) {
098                printerControl.removePrinterGroupJob(printerGroupJob);
099            } else {
100                var workflowDestinationName = workflowStepName + "_TO_DELETED";
101
102                workflowControl.transitionEntityInWorkflowUsingNames(null, workflowEntityStatus, workflowDestinationName, keepPrintedJobsTime, deletedBy);
103            }
104        } else {
105            addExecutionError(ema, ExecutionErrors.CannotDeletePrinterGroupJob.name(), workflowStepName);
106        }
107    }
108
109}