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