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.workflow.server.logic; 018 019import com.echothree.model.control.core.common.ComponentVendors; 020import com.echothree.model.control.core.common.EntityTypes; 021import com.echothree.model.control.order.server.trigger.OrderTrigger; 022import com.echothree.model.control.printer.server.trigger.PrinterGroupJobTrigger; 023import com.echothree.model.control.training.server.trigger.PartyTrainingClassTrigger; 024import com.echothree.model.control.workflow.server.control.WorkflowControl; 025import com.echothree.model.control.workflow.server.trigger.EntityTypeTrigger; 026import com.echothree.model.data.party.common.pk.PartyPK; 027import com.echothree.model.data.workflow.server.entity.WorkflowTrigger; 028import com.echothree.model.data.workflow.server.factory.WorkflowTriggerFactory; 029import com.echothree.util.common.message.ExecutionErrors; 030import com.echothree.util.server.message.ExecutionErrorAccumulator; 031import com.echothree.util.server.persistence.EntityPermission; 032import com.echothree.util.server.persistence.Session; 033import javax.enterprise.context.ApplicationScoped; 034import javax.inject.Inject; 035 036@ApplicationScoped 037public class WorkflowTriggerLogic { 038 039 @Inject 040 WorkflowTriggerFactory workflowTriggerFactory; 041 042 @Inject 043 WorkflowControl workflowControl; 044 045 protected WorkflowTriggerLogic() { 046 super(); 047 } 048 049 // TODO: configure using a property file. 050 private EntityTypeTrigger locateTrigger(final ExecutionErrorAccumulator eea, final String componentVendorName, final String entityTypeName) { 051 EntityTypeTrigger result = null; 052 053 if(componentVendorName.equals(ComponentVendors.ECHO_THREE.name())) { 054 if(entityTypeName.equals(EntityTypes.PartyTrainingClass.name())) { 055 result = new PartyTrainingClassTrigger(); 056 } else if(entityTypeName.equals(EntityTypes.PrinterGroupJob.name())) { 057 result = new PrinterGroupJobTrigger(); 058 } else if(entityTypeName.equals(EntityTypes.Order.name())) { 059 result = new OrderTrigger(); 060 } 061 } 062 063 if(result == null) { 064 eea.addExecutionError(ExecutionErrors.UnknownGeneralTrigger.name(), componentVendorName, entityTypeName); 065 } 066 067 return result; 068 } 069 070 private void processWorkflowTrigger(final Session session, final ExecutionErrorAccumulator eea, final WorkflowTrigger workflowTrigger, final PartyPK triggeredBy) { 071 var workflowEntityStatus = workflowTrigger.getWorkflowEntityStatusForUpdate(); 072 var entityInstance = workflowEntityStatus.getEntityInstance(); 073 var entityTypeDetail = entityInstance.getEntityType().getLastDetail(); 074 var entityTypeName = entityTypeDetail.getEntityTypeName(); 075 var componentVendorName = entityTypeDetail.getComponentVendor().getLastDetail().getComponentVendorName(); 076 var entityTypeTrigger = locateTrigger(eea, componentVendorName, entityTypeName); 077 078 if(eea == null || !eea.hasExecutionErrors()) { 079 entityTypeTrigger.handleTrigger(session, eea, workflowEntityStatus, triggeredBy); 080 } 081 } 082 083 public void processWorkflowTriggers(final Session session, final ExecutionErrorAccumulator eea, final PartyPK triggeredBy) { 084 var remainingTime = 1L * 50 * 1000; // 1 minute 085 086 for(var workflowTrigger : workflowControl.getWorkflowTriggersByTriggerTime(session.getStartTime())) { 087 var errorsOccurred = workflowTrigger.getErrorsOccurred(); 088 089 if(errorsOccurred == null || errorsOccurred == false) { 090 var startTime = System.currentTimeMillis(); 091 092 workflowTrigger = workflowTriggerFactory.getEntityFromPK(EntityPermission.READ_WRITE, workflowTrigger.getPrimaryKey()); 093 processWorkflowTrigger(session, eea, workflowTrigger, triggeredBy); 094 095 if(eea.hasExecutionErrors()) { 096 workflowTrigger.setErrorsOccurred(true); 097 break; 098 } else { 099 remainingTime -= System.currentTimeMillis() - startTime; 100 if(remainingTime < 0) { 101 break; 102 } 103 } 104 } 105 } 106 } 107 108}