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.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.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.core.server.entity.EntityInstance; 026import com.echothree.model.data.core.server.entity.MimeType; 027import com.echothree.model.data.document.server.entity.Document; 028import com.echothree.model.data.document.server.entity.DocumentType; 029import com.echothree.model.data.party.common.pk.PartyPK; 030import com.echothree.model.data.party.server.entity.Language; 031import com.echothree.model.data.printer.server.entity.PrinterGroup; 032import com.echothree.model.data.printer.server.entity.PrinterGroupJob; 033import com.echothree.model.data.workflow.server.entity.WorkflowEntityStatus; 034import com.echothree.util.common.message.ExecutionErrors; 035import com.echothree.util.common.persistence.type.ByteArray; 036import com.echothree.util.server.control.BaseLogic; 037import com.echothree.util.server.message.ExecutionErrorAccumulator; 038import com.echothree.util.server.persistence.Session; 039 040public class PrinterGroupJobLogic 041 extends BaseLogic { 042 043 private PrinterGroupJobLogic() { 044 super(); 045 } 046 047 private static class PrinterGroupJobLogicHolder { 048 static PrinterGroupJobLogic instance = new PrinterGroupJobLogic(); 049 } 050 051 public static PrinterGroupJobLogic getInstance() { 052 return PrinterGroupJobLogicHolder.instance; 053 } 054 055 private void insertPrinterGroupJobIntoWorkflow(final PrinterGroupJob printerGroupJob, final PartyPK createdBy) { 056 var workflowControl = Session.getModelController(WorkflowControl.class); 057 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 var documentControl = Session.getModelController(DocumentControl.class); 066 PrinterGroupJob printerGroupJob = null; 067 DocumentType documentType = documentControl.getDocumentTypeByName(DocumentConstants.DocumentType_PRINTER_GROUP_JOB); 068 069 if(documentType == null) { 070 addExecutionError(ema, ExecutionErrors.UnknownDocumentTypeName.name(), DocumentConstants.DocumentType_PRINTER_GROUP_JOB); 071 } else { 072 Document document = DocumentLogic.getInstance().createDocument(ema, documentType, mimeType, preferredLanguage, description, blob, clob, createdBy); 073 074 if(document != null) { 075 var printerControl = Session.getModelController(PrinterControl.class); 076 077 printerGroupJob = printerControl.createPrinterGroupJob(printerGroup, document, copies, priority, createdBy); 078 insertPrinterGroupJobIntoWorkflow(printerGroupJob, createdBy); 079 } 080 } 081 082 return printerGroupJob; 083 } 084 085 public void deletePrinterGroupJob(final ExecutionErrorAccumulator ema, final PrinterGroupJob printerGroupJob, final PartyPK deletedBy) { 086 var workflowControl = Session.getModelController(WorkflowControl.class); 087 EntityInstance entityInstance = getEntityInstanceByBaseEntity(printerGroupJob); 088 WorkflowEntityStatus workflowEntityStatus = workflowControl.getWorkflowEntityStatusByEntityInstanceForUpdateUsingNames(PrinterGroupJobStatusConstants.Workflow_PRINTER_GROUP_JOB_STATUS, entityInstance); 089 String workflowStepName = workflowEntityStatus.getWorkflowStep().getLastDetail().getWorkflowStepName(); 090 091 if(workflowStepName.equals(PrinterGroupJobStatusConstants.WorkflowStep_QUEUED) 092 || workflowStepName.equals(PrinterGroupJobStatusConstants.WorkflowStep_PRINTED) 093 || workflowStepName.equals(PrinterGroupJobStatusConstants.WorkflowStep_ERRORED)) { 094 Long keepPrintedJobsTime = printerGroupJob.getLastDetail().getPrinterGroup().getLastDetail().getKeepPrintedJobsTime(); 095 096 if(keepPrintedJobsTime == null) { 097 var printerControl = Session.getModelController(PrinterControl.class); 098 099 printerControl.removePrinterGroupJob(printerGroupJob); 100 } else { 101 String workflowDestinationName = new StringBuilder(workflowStepName).append("_TO_DELETED").toString(); 102 103 workflowControl.transitionEntityInWorkflowUsingNames(null, workflowEntityStatus, workflowDestinationName, keepPrintedJobsTime, deletedBy); 104 } 105 } else { 106 addExecutionError(ema, ExecutionErrors.CannotDeletePrinterGroupJob.name(), workflowStepName); 107 } 108 } 109 110}