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.user.server.control.UserControl; 027import com.echothree.model.control.workflow.server.control.WorkflowControl; 028import com.echothree.model.data.workflow.common.WorkflowDestinationConstants; 029import com.echothree.model.data.workflow.common.WorkflowEntranceStepConstants; 030import com.echothree.model.data.workflow.server.entity.WorkflowStep; 031import com.echothree.model.data.workflow.server.entity.WorkflowStepDetail; 032import com.echothree.util.server.persistence.Session; 033import graphql.annotations.annotationTypes.GraphQLDescription; 034import graphql.annotations.annotationTypes.GraphQLField; 035import graphql.annotations.annotationTypes.GraphQLName; 036import graphql.annotations.annotationTypes.GraphQLNonNull; 037import graphql.annotations.connection.GraphQLConnection; 038import graphql.schema.DataFetchingEnvironment; 039import java.util.ArrayList; 040import java.util.stream.Collectors; 041 042@GraphQLDescription("workflow step object") 043@GraphQLName("WorkflowStep") 044public class WorkflowStepObject 045 extends BaseEntityInstanceObject { 046 047 private final WorkflowStep workflowStep; // Always Present 048 049 public WorkflowStepObject(WorkflowStep workflowStep) { 050 super(workflowStep.getPrimaryKey()); 051 052 this.workflowStep = workflowStep; 053 } 054 055 private WorkflowStepDetail workflowStepDetail; // Optional, use getWorkflowStepDetail() 056 057 private WorkflowStepDetail getWorkflowStepDetail() { 058 if(workflowStepDetail == null) { 059 workflowStepDetail = workflowStep.getLastDetail(); 060 } 061 062 return workflowStepDetail; 063 } 064 065 @GraphQLField 066 @GraphQLDescription("workflow") 067 public WorkflowObject getWorkflow(final DataFetchingEnvironment env) { 068 return WorkflowSecurityUtils.getHasWorkflowAccess(env) ? new WorkflowObject(getWorkflowStepDetail().getWorkflow()) : null; 069 } 070 071 @GraphQLField 072 @GraphQLDescription("workflow step name") 073 @GraphQLNonNull 074 public String getWorkflowStepName() { 075 return getWorkflowStepDetail().getWorkflowStepName(); 076 } 077 078 @GraphQLField 079 @GraphQLDescription("workflow step type") 080 public WorkflowStepTypeObject getWorkflowStepType(final DataFetchingEnvironment env) { 081 return WorkflowSecurityUtils.getHasWorkflowStepTypeAccess(env) ? new WorkflowStepTypeObject(getWorkflowStepDetail().getWorkflowStepType()) : null; 082 } 083 084 @GraphQLField 085 @GraphQLDescription("is default") 086 @GraphQLNonNull 087 public boolean getIsDefault() { 088 return getWorkflowStepDetail().getIsDefault(); 089 } 090 091 @GraphQLField 092 @GraphQLDescription("sort order") 093 @GraphQLNonNull 094 public int getSortOrder() { 095 return getWorkflowStepDetail().getSortOrder(); 096 } 097 098 @GraphQLField 099 @GraphQLDescription("description") 100 @GraphQLNonNull 101 public String getDescription(final DataFetchingEnvironment env) { 102 var workflowControl = Session.getModelController(WorkflowControl.class); 103 var userControl = Session.getModelController(UserControl.class); 104 105 return workflowControl.getBestWorkflowStepDescription(workflowStep, userControl.getPreferredLanguageFromUserVisit(BaseGraphQl.getUserVisit(env))); 106 } 107 108 @GraphQLField 109 @GraphQLDescription("workflow destinations") 110 @GraphQLNonNull 111 @GraphQLConnection(connectionFetcher = CountingDataConnectionFetcher.class) 112 public CountingPaginatedData<WorkflowDestinationObject> getWorkflowDestinations(final DataFetchingEnvironment env) { 113 if(WorkflowSecurityUtils.getHasWorkflowDestinationsAccess(env)) { 114 var workflowControl = Session.getModelController(WorkflowControl.class); 115 var totalCount = workflowControl.countWorkflowDestinationsByWorkflowStep(workflowStep); 116 117 try(var objectLimiter = new ObjectLimiter(env, WorkflowDestinationConstants.COMPONENT_VENDOR_NAME, WorkflowDestinationConstants.ENTITY_TYPE_NAME, totalCount)) { 118 var entities = workflowControl.getWorkflowDestinationsByWorkflowStep(workflowStep); 119 var wishlistPriorities = entities.stream().map(WorkflowDestinationObject::new).collect(Collectors.toCollection(() -> new ArrayList<>(entities.size()))); 120 121 return new CountedObjects<>(objectLimiter, wishlistPriorities); 122 } 123 } else { 124 return Connections.emptyConnection(); 125 } 126 } 127 128 @GraphQLField 129 @GraphQLDescription("workflow entrance steps") 130 @GraphQLNonNull 131 @GraphQLConnection(connectionFetcher = CountingDataConnectionFetcher.class) 132 public CountingPaginatedData<WorkflowEntranceStepObject> getWorkflowEntranceSteps(final DataFetchingEnvironment env) { 133 if(WorkflowSecurityUtils.getHasWorkflowEntranceStepsAccess(env)) { 134 var workflowControl = Session.getModelController(WorkflowControl.class); 135 var totalCount = workflowControl.countWorkflowEntranceStepsByWorkflowStep(workflowStep); 136 137 try(var objectLimiter = new ObjectLimiter(env, WorkflowEntranceStepConstants.COMPONENT_VENDOR_NAME, WorkflowEntranceStepConstants.ENTITY_TYPE_NAME, totalCount)) { 138 var entities = workflowControl.getWorkflowEntranceStepsByWorkflowStep(workflowStep); 139 var wishlistPriorities = entities.stream().map(WorkflowEntranceStepObject::new).collect(Collectors.toCollection(() -> new ArrayList<>(entities.size()))); 140 141 return new CountedObjects<>(objectLimiter, wishlistPriorities); 142 } 143 } else { 144 return Connections.emptyConnection(); 145 } 146 } 147}