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.graphql.count.Connections;
021import com.echothree.model.control.graphql.server.graphql.count.CountedObjects;
022import com.echothree.model.control.graphql.server.graphql.count.CountingDataConnectionFetcher;
023import com.echothree.model.control.graphql.server.graphql.count.CountingPaginatedData;
024import com.echothree.model.control.graphql.server.util.BaseGraphQl;
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.WorkflowDestinationPartyTypeConstants;
029import com.echothree.model.data.workflow.common.WorkflowDestinationSelectorConstants;
030import com.echothree.model.data.workflow.common.WorkflowDestinationStepConstants;
031import com.echothree.model.data.workflow.server.entity.WorkflowDestination;
032import com.echothree.model.data.workflow.server.entity.WorkflowDestinationDetail;
033import com.echothree.util.server.persistence.Session;
034import graphql.annotations.annotationTypes.GraphQLDescription;
035import graphql.annotations.annotationTypes.GraphQLField;
036import graphql.annotations.annotationTypes.GraphQLName;
037import graphql.annotations.annotationTypes.GraphQLNonNull;
038import graphql.annotations.connection.GraphQLConnection;
039import graphql.schema.DataFetchingEnvironment;
040import java.util.ArrayList;
041import java.util.stream.Collectors;
042
043@GraphQLDescription("workflow destination object")
044@GraphQLName("WorkflowDestination")
045public class WorkflowDestinationObject
046        extends BaseEntityInstanceObject {
047    
048    private final WorkflowDestination workflowDestination; // Always Present
049    
050    public WorkflowDestinationObject(WorkflowDestination workflowDestination) {
051        super(workflowDestination.getPrimaryKey());
052        
053        this.workflowDestination = workflowDestination;
054    }
055
056    private WorkflowDestinationDetail workflowDestinationDetail; // Optional, use getWorkflowDestinationDetail()
057    
058    private WorkflowDestinationDetail getWorkflowDestinationDetail() {
059        if(workflowDestinationDetail == null) {
060            workflowDestinationDetail = workflowDestination.getLastDetail();
061        }
062        
063        return workflowDestinationDetail;
064    }
065
066    @GraphQLField
067    @GraphQLDescription("workflow step")
068    public WorkflowStepObject getWorkflowStep(final DataFetchingEnvironment env) {
069        return WorkflowSecurityUtils.getHasWorkflowStepAccess(env) ? new WorkflowStepObject(getWorkflowDestinationDetail().getWorkflowStep()) : null;
070    }
071
072    @GraphQLField
073    @GraphQLDescription("workflow destination name")
074    @GraphQLNonNull
075    public String getWorkflowDestinationName() {
076        return getWorkflowDestinationDetail().getWorkflowDestinationName();
077    }
078
079    @GraphQLField
080    @GraphQLDescription("is default")
081    @GraphQLNonNull
082    public boolean getIsDefault() {
083        return getWorkflowDestinationDetail().getIsDefault();
084    }
085
086    @GraphQLField
087    @GraphQLDescription("sort order")
088    @GraphQLNonNull
089    public int getSortOrder() {
090        return getWorkflowDestinationDetail().getSortOrder();
091    }
092    
093    @GraphQLField
094    @GraphQLDescription("description")
095    @GraphQLNonNull
096    public String getDescription(final DataFetchingEnvironment env) {
097        var workflowControl = Session.getModelController(WorkflowControl.class);
098        var userControl = Session.getModelController(UserControl.class);
099
100        return workflowControl.getBestWorkflowDestinationDescription(workflowDestination, userControl.getPreferredLanguageFromUserVisit(BaseGraphQl.getUserVisit(env)));
101    }
102
103    @GraphQLField
104    @GraphQLDescription("workflow destination steps")
105    @GraphQLNonNull
106    @GraphQLConnection(connectionFetcher = CountingDataConnectionFetcher.class)
107    public CountingPaginatedData<WorkflowDestinationStepObject> getWorkflowDestinationSteps(final DataFetchingEnvironment env) {
108        if(WorkflowSecurityUtils.getHasWorkflowDestinationStepsAccess(env)) {
109            var workflowControl = Session.getModelController(WorkflowControl.class);
110            var totalCount = workflowControl.countWorkflowDestinationStepsByWorkflowDestination(workflowDestination);
111
112            try(var objectLimiter = new ObjectLimiter(env, WorkflowDestinationStepConstants.COMPONENT_VENDOR_NAME, WorkflowDestinationStepConstants.ENTITY_TYPE_NAME, totalCount)) {
113                var entities = workflowControl.getWorkflowDestinationStepsByWorkflowDestination(workflowDestination);
114                var wishlistPriorities = entities.stream().map(WorkflowDestinationStepObject::new).collect(Collectors.toCollection(() -> new ArrayList<>(entities.size())));
115
116                return new CountedObjects<>(objectLimiter, wishlistPriorities);
117            }
118        } else {
119            return Connections.emptyConnection();
120        }
121    }
122
123    @GraphQLField
124    @GraphQLDescription("workflow destination party types")
125    @GraphQLNonNull
126    @GraphQLConnection(connectionFetcher = CountingDataConnectionFetcher.class)
127    public CountingPaginatedData<WorkflowDestinationPartyTypeObject> getWorkflowDestinationPartyTypes(final DataFetchingEnvironment env) {
128        if(WorkflowSecurityUtils.getHasWorkflowDestinationPartyTypesAccess(env)) {
129            var workflowControl = Session.getModelController(WorkflowControl.class);
130            var totalCount = workflowControl.countWorkflowDestinationPartyTypesByWorkflowDestination(workflowDestination);
131
132            try(var objectLimiter = new ObjectLimiter(env, WorkflowDestinationPartyTypeConstants.COMPONENT_VENDOR_NAME, WorkflowDestinationPartyTypeConstants.ENTITY_TYPE_NAME, totalCount)) {
133                var entities = workflowControl.getWorkflowDestinationPartyTypesByWorkflowDestination(workflowDestination);
134                var wishlistPriorities = entities.stream().map(WorkflowDestinationPartyTypeObject::new).collect(Collectors.toCollection(() -> new ArrayList<>(entities.size())));
135
136                return new CountedObjects<>(objectLimiter, wishlistPriorities);
137            }
138        } else {
139            return Connections.emptyConnection();
140        }
141    }
142
143    @GraphQLField
144    @GraphQLDescription("workflow destination selectors")
145    @GraphQLNonNull
146    @GraphQLConnection(connectionFetcher = CountingDataConnectionFetcher.class)
147    public CountingPaginatedData<WorkflowDestinationSelectorObject> getWorkflowDestinationSelectors(final DataFetchingEnvironment env) {
148        if(WorkflowSecurityUtils.getHasWorkflowDestinationSelectorsAccess(env)) {
149            var workflowControl = Session.getModelController(WorkflowControl.class);
150            var totalCount = workflowControl.countWorkflowDestinationSelectorsByWorkflowDestination(workflowDestination);
151
152            try(var objectLimiter = new ObjectLimiter(env, WorkflowDestinationSelectorConstants.COMPONENT_VENDOR_NAME, WorkflowDestinationSelectorConstants.ENTITY_TYPE_NAME, totalCount)) {
153                var entities = workflowControl.getWorkflowDestinationSelectorsByWorkflowDestination(workflowDestination);
154                var wishlistPriorities = entities.stream().map(WorkflowDestinationSelectorObject::new).collect(Collectors.toCollection(() -> new ArrayList<>(entities.size())));
155
156                return new CountedObjects<>(objectLimiter, wishlistPriorities);
157            }
158        } else {
159            return Connections.emptyConnection();
160        }
161    }
162
163}