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.control.user.workflow.common.spec.WorkflowUniversalSpec; 020import com.echothree.model.control.core.common.ComponentVendors; 021import com.echothree.model.control.core.common.EntityTypes; 022import com.echothree.model.control.core.common.exception.InvalidParameterCountException; 023import com.echothree.model.control.core.server.logic.EntityInstanceLogic; 024import com.echothree.model.control.core.server.logic.EntityTypeLogic; 025import com.echothree.model.control.selector.server.logic.SelectorKindLogic; 026import com.echothree.model.control.workflow.common.exception.UnknownWorkflowEntityTypeException; 027import com.echothree.model.control.workflow.common.exception.UnknownWorkflowNameException; 028import com.echothree.model.control.workflow.common.exception.UnknownWorkflowSelectorKindException; 029import com.echothree.model.control.workflow.server.control.WorkflowControl; 030import com.echothree.model.data.workflow.server.entity.Workflow; 031import com.echothree.model.data.workflow.server.entity.WorkflowEntityType; 032import com.echothree.model.data.workflow.server.entity.WorkflowSelectorKind; 033import com.echothree.util.common.exception.BaseException; 034import com.echothree.util.common.message.ExecutionErrors; 035import com.echothree.util.server.control.BaseLogic; 036import com.echothree.util.server.message.ExecutionErrorAccumulator; 037import com.echothree.util.server.persistence.EntityPermission; 038import javax.enterprise.context.ApplicationScoped; 039import javax.enterprise.inject.spi.CDI; 040import javax.inject.Inject; 041 042@ApplicationScoped 043public class WorkflowLogic 044 extends BaseLogic { 045 046 @Inject 047 WorkflowControl workflowControl; 048 049 @Inject 050 EntityInstanceLogic entityInstanceLogic; 051 052 @Inject 053 EntityTypeLogic entityTypeLogic; 054 055 @Inject 056 SelectorKindLogic selectorKindLogic; 057 058 protected WorkflowLogic() { 059 super(); 060 } 061 062 public static WorkflowLogic getInstance() { 063 return CDI.current().select(WorkflowLogic.class).get(); 064 } 065 066 public Workflow getWorkflowByName(final Class<? extends BaseException> unknownException, final ExecutionErrors unknownExecutionError, 067 final ExecutionErrorAccumulator eea, final String workflowName, final EntityPermission entityPermission) { 068 var workflow = workflowControl.getWorkflowByName(workflowName, entityPermission); 069 070 if(workflow == null) { 071 handleExecutionError(unknownException, eea, unknownExecutionError.name(), workflowName); 072 } 073 074 return workflow; 075 } 076 077 public Workflow getWorkflowByName(final ExecutionErrorAccumulator eea, final String workflowName, 078 final EntityPermission entityPermission) { 079 return getWorkflowByName(UnknownWorkflowNameException.class, ExecutionErrors.UnknownWorkflowName, eea, 080 workflowName, entityPermission); 081 } 082 083 public Workflow getWorkflowByName(final ExecutionErrorAccumulator eea, final String workflowName) { 084 return getWorkflowByName(eea, workflowName, EntityPermission.READ_ONLY); 085 } 086 087 public Workflow getWorkflowByNameForUpdate(final ExecutionErrorAccumulator eea, final String workflowName) { 088 return getWorkflowByName(eea, workflowName, EntityPermission.READ_WRITE); 089 } 090 091 public Workflow getWorkflowByUniversalSpec(final ExecutionErrorAccumulator eea, 092 final WorkflowUniversalSpec universalSpec, final EntityPermission entityPermission) { 093 Workflow workflow = null; 094 var workflowName = universalSpec.getWorkflowName(); 095 var parameterCount = (workflowName == null ? 0 : 1) + entityInstanceLogic.countPossibleEntitySpecs(universalSpec); 096 097 switch(parameterCount) { 098 case 1 -> { 099 if(workflowName == null) { 100 var entityInstance = entityInstanceLogic.getEntityInstance(eea, universalSpec, 101 ComponentVendors.ECHO_THREE.name(), EntityTypes.Workflow.name()); 102 103 if(eea == null || !eea.hasExecutionErrors()) { 104 workflow = workflowControl.getWorkflowByEntityInstance(entityInstance, entityPermission); 105 } 106 } else { 107 workflow = getWorkflowByName(eea, workflowName, entityPermission); 108 } 109 } 110 default -> 111 handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name()); 112 } 113 114 return workflow; 115 } 116 117 public Workflow getWorkflowByUniversalSpec(final ExecutionErrorAccumulator eea, 118 final WorkflowUniversalSpec universalSpec) { 119 return getWorkflowByUniversalSpec(eea, universalSpec, EntityPermission.READ_ONLY); 120 } 121 122 public Workflow getWorkflowByUniversalSpecForUpdate(final ExecutionErrorAccumulator eea, 123 final WorkflowUniversalSpec universalSpec) { 124 return getWorkflowByUniversalSpec(eea, universalSpec, EntityPermission.READ_WRITE); 125 } 126 127 public WorkflowEntityType getWorkflowEntityTypeByName(final ExecutionErrorAccumulator eea, final String workflowName, 128 final String componentVendorName, final String entityTypeName) { 129 var workflow = getWorkflowByName(eea, workflowName); 130 var entityType = entityTypeLogic.getEntityTypeByName(eea, componentVendorName, entityTypeName); 131 WorkflowEntityType workflowEntityType = null; 132 133 if(eea != null && !eea.hasExecutionErrors()) { 134 workflowEntityType = workflowControl.getWorkflowEntityType(workflow, entityType); 135 136 if(workflowEntityType == null) { 137 var entityTypeDetail = entityType.getLastDetail(); 138 139 handleExecutionError(UnknownWorkflowEntityTypeException.class, eea, ExecutionErrors.UnknownWorkflowEntityType.name(), 140 workflow.getLastDetail(), 141 entityTypeDetail.getComponentVendor().getLastDetail().getComponentVendorName(), 142 entityTypeDetail.getEntityTypeName()); 143 } 144 } 145 146 return workflowEntityType; 147 } 148 149 public WorkflowSelectorKind getWorkflowSelectorKindByName(final ExecutionErrorAccumulator eea, final String workflowName, 150 final String selectorKindName) { 151 var workflow = getWorkflowByName(eea, workflowName); 152 var selectorKind = selectorKindLogic.getSelectorKindByName(eea, selectorKindName); 153 WorkflowSelectorKind workflowSelectorKind = null; 154 155 if(eea != null && !eea.hasExecutionErrors()) { 156 workflowSelectorKind = workflowControl.getWorkflowSelectorKind(workflow, selectorKind); 157 158 if(workflowSelectorKind == null) { 159 handleExecutionError(UnknownWorkflowSelectorKindException.class, eea, ExecutionErrors.UnknownWorkflowSelectorKind.name(), 160 workflow.getLastDetail(), 161 selectorKind.getLastDetail().getSelectorKindName()); 162 } 163 } 164 165 return workflowSelectorKind; 166 } 167 168}