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