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