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.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.enterprise.inject.spi.CDI; 035 036@ApplicationScoped 037public class WorkflowTriggerLogic { 038 039 protected WorkflowTriggerLogic() { 040 super(); 041 } 042 043 public static WorkflowTriggerLogic getInstance() { 044 return CDI.current().select(WorkflowTriggerLogic.class).get(); 045 } 046 047 // TODO: configure using a property file. 048 private EntityTypeTrigger locateTrigger(final ExecutionErrorAccumulator eea, final String componentVendorName, final String entityTypeName) { 049 EntityTypeTrigger result = null; 050 051 if(componentVendorName.equals(ComponentVendors.ECHO_THREE.name())) { 052 if(entityTypeName.equals(EntityTypes.PartyTrainingClass.name())) { 053 result = new PartyTrainingClassTrigger(); 054 } else if(entityTypeName.equals(EntityTypes.PrinterGroupJob.name())) { 055 result = new PrinterGroupJobTrigger(); 056 } else if(entityTypeName.equals(EntityTypes.Order.name())) { 057 result = new OrderTrigger(); 058 } 059 } 060 061 if(result == null) { 062 eea.addExecutionError(ExecutionErrors.UnknownGeneralTrigger.name(), componentVendorName, entityTypeName); 063 } 064 065 return result; 066 } 067 068 private void processWorkflowTrigger(final Session session, final ExecutionErrorAccumulator eea, final WorkflowTrigger workflowTrigger, final PartyPK triggeredBy) { 069 var workflowEntityStatus = workflowTrigger.getWorkflowEntityStatusForUpdate(); 070 var entityInstance = workflowEntityStatus.getEntityInstance(); 071 var entityTypeDetail = entityInstance.getEntityType().getLastDetail(); 072 var entityTypeName = entityTypeDetail.getEntityTypeName(); 073 var componentVendorName = entityTypeDetail.getComponentVendor().getLastDetail().getComponentVendorName(); 074 var entityTypeTrigger = locateTrigger(eea, componentVendorName, entityTypeName); 075 076 if(eea == null || !eea.hasExecutionErrors()) { 077 entityTypeTrigger.handleTrigger(session, eea, workflowEntityStatus, triggeredBy); 078 } 079 } 080 081 public void processWorkflowTriggers(final Session session, final ExecutionErrorAccumulator eea, final PartyPK triggeredBy) { 082 var workflowControl = Session.getModelController(WorkflowControl.class); 083 var remainingTime = 1L * 60 * 1000; // 1 minute 084 085 for(var workflowTrigger : workflowControl.getWorkflowTriggersByTriggerTime(session.START_TIME_LONG)) { 086 var errorsOccurred = workflowTrigger.getErrorsOccurred(); 087 088 if(errorsOccurred == null || errorsOccurred == false) { 089 var startTime = System.currentTimeMillis(); 090 091 workflowTrigger = WorkflowTriggerFactory.getInstance().getEntityFromPK(EntityPermission.READ_WRITE, workflowTrigger.getPrimaryKey()); 092 processWorkflowTrigger(session, eea, workflowTrigger, triggeredBy); 093 094 if(eea.hasExecutionErrors()) { 095 workflowTrigger.setErrorsOccurred(true); 096 break; 097 } else { 098 remainingTime -= System.currentTimeMillis() - startTime; 099 if(remainingTime < 0) { 100 break; 101 } 102 } 103 } 104 } 105 } 106 107}