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.graphql;
018
019import com.echothree.model.control.graphql.server.graphql.BaseEntityInstanceObject;
020import com.echothree.model.control.graphql.server.util.BaseGraphQl;
021import com.echothree.model.control.graphql.server.graphql.count.Connections;
022import com.echothree.model.control.graphql.server.graphql.count.CountedObjects;
023import com.echothree.model.control.graphql.server.graphql.count.CountingDataConnectionFetcher;
024import com.echothree.model.control.graphql.server.graphql.count.CountingPaginatedData;
025import com.echothree.model.control.graphql.server.util.count.ObjectLimiter;
026import com.echothree.model.control.security.server.graphql.SecurityRoleGroupObject;
027import com.echothree.model.control.security.server.graphql.SecuritySecurityUtils;
028import com.echothree.model.control.selector.server.graphql.SelectorSecurityUtils;
029import com.echothree.model.control.selector.server.graphql.SelectorTypeObject;
030import com.echothree.model.control.user.server.control.UserControl;
031import com.echothree.model.control.workflow.server.control.WorkflowControl;
032import com.echothree.model.data.workflow.common.WorkflowEntityTypeConstants;
033import com.echothree.model.data.workflow.common.WorkflowEntranceConstants;
034import com.echothree.model.data.workflow.common.WorkflowSelectorKindConstants;
035import com.echothree.model.data.workflow.common.WorkflowStepConstants;
036import com.echothree.model.data.workflow.server.entity.Workflow;
037import com.echothree.model.data.workflow.server.entity.WorkflowDetail;
038import com.echothree.util.server.persistence.Session;
039import graphql.annotations.annotationTypes.GraphQLDescription;
040import graphql.annotations.annotationTypes.GraphQLField;
041import graphql.annotations.annotationTypes.GraphQLName;
042import graphql.annotations.annotationTypes.GraphQLNonNull;
043import graphql.annotations.connection.GraphQLConnection;
044import graphql.schema.DataFetchingEnvironment;
045import java.util.ArrayList;
046import java.util.stream.Collectors;
047
048@GraphQLDescription("workflow object")
049@GraphQLName("Workflow")
050public class WorkflowObject
051        extends BaseEntityInstanceObject {
052    
053    private final Workflow workflow; // Always Present
054    
055    public WorkflowObject(Workflow workflow) {
056        super(workflow.getPrimaryKey());
057        
058        this.workflow = workflow;
059    }
060
061    private WorkflowDetail workflowDetail; // Optional, use getWorkflowDetail()
062    
063    private WorkflowDetail getWorkflowDetail() {
064        if(workflowDetail == null) {
065            workflowDetail = workflow.getLastDetail();
066        }
067        
068        return workflowDetail;
069    }
070
071    @GraphQLField
072    @GraphQLDescription("workflow name")
073    @GraphQLNonNull
074    public String getWorkflowName() {
075        return getWorkflowDetail().getWorkflowName();
076    }
077
078    @GraphQLField
079    @GraphQLDescription("selector type")
080    public SelectorTypeObject getSelectorType(final DataFetchingEnvironment env) {
081        if(SelectorSecurityUtils.getHasSelectorTypeAccess(env)) {
082            var selectorType = getWorkflowDetail().getSelectorType();
083
084            return selectorType == null ? null : new SelectorTypeObject(selectorType);
085        } else {
086            return null;
087        }
088    }
089
090    @GraphQLField
091    @GraphQLDescription("security role group")
092    public SecurityRoleGroupObject getSecurityRoleGroup(final DataFetchingEnvironment env) {
093        if(SecuritySecurityUtils.getHasSecurityRoleGroupAccess(env)) {
094            var securityRoleGroup = getWorkflowDetail().getSecurityRoleGroup();
095
096            return securityRoleGroup == null ? null : new SecurityRoleGroupObject(securityRoleGroup);
097        } else {
098            return null;
099        }
100    }
101
102    @GraphQLField
103    @GraphQLDescription("sort order")
104    @GraphQLNonNull
105    public int getSortOrder() {
106        return getWorkflowDetail().getSortOrder();
107    }
108    
109    @GraphQLField
110    @GraphQLDescription("description")
111    @GraphQLNonNull
112    public String getDescription(final DataFetchingEnvironment env) {
113        var workflowControl = Session.getModelController(WorkflowControl.class);
114        var userControl = Session.getModelController(UserControl.class);
115
116        return workflowControl.getBestWorkflowDescription(workflow, userControl.getPreferredLanguageFromUserVisit(BaseGraphQl.getUserVisit(env)));
117    }
118
119    @GraphQLField
120    @GraphQLDescription("workflow entrances")
121    @GraphQLNonNull
122    @GraphQLConnection(connectionFetcher = CountingDataConnectionFetcher.class)
123    public CountingPaginatedData<WorkflowEntranceObject> getWorkflowEntrances(final DataFetchingEnvironment env) {
124        if(WorkflowSecurityUtils.getHasWorkflowEntrancesAccess(env)) {
125            var workflowControl = Session.getModelController(WorkflowControl.class);
126            var totalCount = workflowControl.countWorkflowEntrancesByWorkflow(workflow);
127
128            try(var objectLimiter = new ObjectLimiter(env, WorkflowEntranceConstants.COMPONENT_VENDOR_NAME, WorkflowEntranceConstants.ENTITY_TYPE_NAME, totalCount)) {
129                var entities = workflowControl.getWorkflowEntrancesByWorkflow(workflow);
130                var wishlistPriorities = entities.stream().map(WorkflowEntranceObject::new).collect(Collectors.toCollection(() -> new ArrayList<>(entities.size())));
131
132                return new CountedObjects<>(objectLimiter, wishlistPriorities);
133            }
134        } else {
135            return Connections.emptyConnection();
136        }
137    }
138
139    @GraphQLField
140    @GraphQLDescription("workflow steps")
141    @GraphQLNonNull
142    @GraphQLConnection(connectionFetcher = CountingDataConnectionFetcher.class)
143    public CountingPaginatedData<WorkflowStepObject> getWorkflowSteps(final DataFetchingEnvironment env) {
144        if(WorkflowSecurityUtils.getHasWorkflowStepsAccess(env)) {
145            var workflowControl = Session.getModelController(WorkflowControl.class);
146            var totalCount = workflowControl.countWorkflowStepsByWorkflow(workflow);
147
148            try(var objectLimiter = new ObjectLimiter(env, WorkflowStepConstants.COMPONENT_VENDOR_NAME, WorkflowStepConstants.ENTITY_TYPE_NAME, totalCount)) {
149                var entities = workflowControl.getWorkflowStepsByWorkflow(workflow);
150                var wishlistPriorities = entities.stream().map(WorkflowStepObject::new).collect(Collectors.toCollection(() -> new ArrayList<>(entities.size())));
151
152                return new CountedObjects<>(objectLimiter, wishlistPriorities);
153            }
154        } else {
155            return Connections.emptyConnection();
156        }
157    }
158
159    @GraphQLField
160    @GraphQLDescription("workflow entity types")
161    @GraphQLNonNull
162    @GraphQLConnection(connectionFetcher = CountingDataConnectionFetcher.class)
163    public CountingPaginatedData<WorkflowEntityTypeObject> getWorkflowEntityTypes(final DataFetchingEnvironment env) {
164        if(WorkflowSecurityUtils.getHasWorkflowEntityTypesAccess(env)) {
165            var workflowControl = Session.getModelController(WorkflowControl.class);
166            var totalCount = workflowControl.countWorkflowEntityTypesByWorkflow(workflow);
167
168            try(var objectLimiter = new ObjectLimiter(env, WorkflowEntityTypeConstants.COMPONENT_VENDOR_NAME, WorkflowEntityTypeConstants.ENTITY_TYPE_NAME, totalCount)) {
169                var entities = workflowControl.getWorkflowEntityTypesByWorkflow(workflow);
170                var wishlistPriorities = entities.stream().map(WorkflowEntityTypeObject::new).collect(Collectors.toCollection(() -> new ArrayList<>(entities.size())));
171
172                return new CountedObjects<>(objectLimiter, wishlistPriorities);
173            }
174        } else {
175            return Connections.emptyConnection();
176        }
177    }
178
179    @GraphQLField
180    @GraphQLDescription("workflow selector kinds")
181    @GraphQLNonNull
182    @GraphQLConnection(connectionFetcher = CountingDataConnectionFetcher.class)
183    public CountingPaginatedData<WorkflowSelectorKindObject> getWorkflowSelectorKinds(final DataFetchingEnvironment env) {
184        if(WorkflowSecurityUtils.getHasWorkflowSelectorKindsAccess(env)) {
185            var workflowControl = Session.getModelController(WorkflowControl.class);
186            var totalCount = workflowControl.countWorkflowSelectorKindsByWorkflow(workflow);
187
188            try(var objectLimiter = new ObjectLimiter(env, WorkflowSelectorKindConstants.COMPONENT_VENDOR_NAME, WorkflowSelectorKindConstants.ENTITY_TYPE_NAME, totalCount)) {
189                var entities = workflowControl.getWorkflowSelectorKindsByWorkflow(workflow);
190                var wishlistPriorities = entities.stream().map(WorkflowSelectorKindObject::new).collect(Collectors.toCollection(() -> new ArrayList<>(entities.size())));
191
192                return new CountedObjects<>(objectLimiter, wishlistPriorities);
193            }
194        } else {
195            return Connections.emptyConnection();
196        }
197    }
198
199}