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