001// --------------------------------------------------------------------------------
002// Copyright 2002-2026 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.chain.server.graphql;
018
019import com.echothree.model.control.chain.server.control.ChainControl;
020import com.echothree.model.control.graphql.server.graphql.BaseEntityInstanceObject;
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.BaseGraphQl;
026import com.echothree.model.control.graphql.server.util.count.ObjectLimiter;
027import com.echothree.model.control.user.server.control.UserControl;
028import com.echothree.model.data.chain.common.ChainActionConstants;
029import com.echothree.model.data.chain.server.entity.ChainActionSet;
030import com.echothree.model.data.chain.server.entity.ChainActionSetDetail;
031import com.echothree.util.server.persistence.Session;
032import graphql.annotations.annotationTypes.GraphQLDescription;
033import graphql.annotations.annotationTypes.GraphQLField;
034import graphql.annotations.annotationTypes.GraphQLName;
035import graphql.annotations.annotationTypes.GraphQLNonNull;
036import graphql.annotations.connection.GraphQLConnection;
037import graphql.schema.DataFetchingEnvironment;
038import java.util.ArrayList;
039import java.util.stream.Collectors;
040
041@GraphQLDescription("chain action set object")
042@GraphQLName("ChainActionSet")
043public class ChainActionSetObject
044        extends BaseEntityInstanceObject {
045    
046    private final ChainActionSet chainActionSet; // Always Present
047    
048    public ChainActionSetObject(ChainActionSet chainActionSet) {
049        super(chainActionSet.getPrimaryKey());
050        
051        this.chainActionSet = chainActionSet;
052    }
053
054    private ChainActionSetDetail chainActionSetDetail; // Optional, use getChainActionSetDetail()
055    
056    private ChainActionSetDetail getChainActionSetDetail() {
057        if(chainActionSetDetail == null) {
058            chainActionSetDetail = chainActionSet.getLastDetail();
059        }
060        
061        return chainActionSetDetail;
062    }
063
064    @GraphQLField
065    @GraphQLDescription("chain")
066    public ChainObject getChain(final DataFetchingEnvironment env) {
067        return ChainSecurityUtils.getHasChainAccess(env) ? new ChainObject(getChainActionSetDetail().getChain()) : null;
068    }
069
070    @GraphQLField
071    @GraphQLDescription("chain action set name")
072    @GraphQLNonNull
073    public String getChainActionSetName() {
074        return getChainActionSetDetail().getChainActionSetName();
075    }
076    
077    @GraphQLField
078    @GraphQLDescription("is default")
079    @GraphQLNonNull
080    public boolean getIsDefault() {
081        return getChainActionSetDetail().getIsDefault();
082    }
083    
084    @GraphQLField
085    @GraphQLDescription("sort order")
086    @GraphQLNonNull
087    public int getSortOrder() {
088        return getChainActionSetDetail().getSortOrder();
089    }
090    
091    @GraphQLField
092    @GraphQLDescription("description")
093    @GraphQLNonNull
094    public String getDescription(final DataFetchingEnvironment env) {
095        var chainControl = Session.getModelController(ChainControl.class);
096        var userControl = Session.getModelController(UserControl.class);
097
098        return chainControl.getBestChainActionSetDescription(chainActionSet, userControl.getPreferredLanguageFromUserVisit(BaseGraphQl.getUserVisit(env)));
099    }
100
101    @GraphQLField
102    @GraphQLDescription("chain actions")
103    @GraphQLNonNull
104    @GraphQLConnection(connectionFetcher = CountingDataConnectionFetcher.class)
105    public CountingPaginatedData<ChainActionObject> getChainActions(final DataFetchingEnvironment env) {
106        if(ChainSecurityUtils.getHasChainActionsAccess(env)) {
107            var chainControl = Session.getModelController(ChainControl.class);
108            var totalCount = chainControl.countChainActionsByChainActionSet(chainActionSet);
109
110            try(var objectLimiter = new ObjectLimiter(env, ChainActionConstants.COMPONENT_VENDOR_NAME, ChainActionConstants.ENTITY_TYPE_NAME, totalCount)) {
111                var entities = chainControl.getChainActionsByChainActionSet(chainActionSet);
112                var chainActions = entities.stream().map(ChainActionObject::new).collect(Collectors.toCollection(() -> new ArrayList<>(entities.size())));
113
114                return new CountedObjects<>(objectLimiter, chainActions);
115            }
116        } else {
117            return Connections.emptyConnection();
118        }
119    }
120
121}