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.sequence.server.graphql.SequenceObject;
028import com.echothree.model.control.sequence.server.graphql.SequenceSecurityUtils;
029import com.echothree.model.control.user.server.control.UserControl;
030import com.echothree.model.data.chain.common.ChainActionSetConstants;
031import com.echothree.model.data.chain.server.entity.Chain;
032import com.echothree.model.data.chain.server.entity.ChainDetail;
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("chain object")
044@GraphQLName("Chain")
045public class ChainObject
046        extends BaseEntityInstanceObject {
047    
048    private final Chain chain; // Always Present
049    
050    public ChainObject(Chain chain) {
051        super(chain.getPrimaryKey());
052        
053        this.chain = chain;
054    }
055
056    private ChainDetail chainDetail; // Optional, use getChainDetail()
057    
058    private ChainDetail getChainDetail() {
059        if(chainDetail == null) {
060            chainDetail = chain.getLastDetail();
061        }
062        
063        return chainDetail;
064    }
065
066    @GraphQLField
067    @GraphQLDescription("chain type")
068    public ChainTypeObject getChainType(final DataFetchingEnvironment env) {
069        return ChainSecurityUtils.getHasChainTypeAccess(env) ? new ChainTypeObject(getChainDetail().getChainType()) : null;
070    }
071
072    @GraphQLField
073    @GraphQLDescription("chain name")
074    @GraphQLNonNull
075    public String getChainName() {
076        return getChainDetail().getChainName();
077    }
078
079    @GraphQLField
080    @GraphQLDescription("chain instance sequence")
081    public SequenceObject getChainItemSequence(final DataFetchingEnvironment env) {
082        var sequence = getChainDetail().getChainInstanceSequence();
083
084        return sequence == null ? null : SequenceSecurityUtils.getHasSequenceAccess(env) ? new SequenceObject(sequence): null;
085    }
086
087    @GraphQLField
088    @GraphQLDescription("is default")
089    @GraphQLNonNull
090    public boolean getIsDefault() {
091        return getChainDetail().getIsDefault();
092    }
093    
094    @GraphQLField
095    @GraphQLDescription("sort order")
096    @GraphQLNonNull
097    public int getSortOrder() {
098        return getChainDetail().getSortOrder();
099    }
100    
101    @GraphQLField
102    @GraphQLDescription("description")
103    @GraphQLNonNull
104    public String getDescription(final DataFetchingEnvironment env) {
105        var chainControl = Session.getModelController(ChainControl.class);
106        var userControl = Session.getModelController(UserControl.class);
107
108        return chainControl.getBestChainDescription(chain, userControl.getPreferredLanguageFromUserVisit(BaseGraphQl.getUserVisit(env)));
109    }
110
111    @GraphQLField
112    @GraphQLDescription("chain action sets")
113    @GraphQLNonNull
114    @GraphQLConnection(connectionFetcher = CountingDataConnectionFetcher.class)
115    public CountingPaginatedData<ChainActionSetObject> getChainActionSets(final DataFetchingEnvironment env) {
116        if(ChainSecurityUtils.getHasChainActionSetsAccess(env)) {
117            var chainControl = Session.getModelController(ChainControl.class);
118            var totalCount = chainControl.countChainActionSetsByChain(chain);
119
120            try(var objectLimiter = new ObjectLimiter(env, ChainActionSetConstants.COMPONENT_VENDOR_NAME, ChainActionSetConstants.ENTITY_TYPE_NAME, totalCount)) {
121                var entities = chainControl.getChainActionSetsByChain(chain);
122                var chainActionSets = entities.stream().map(ChainActionSetObject::new).collect(Collectors.toCollection(() -> new ArrayList<>(entities.size())));
123
124                return new CountedObjects<>(objectLimiter, chainActionSets);
125            }
126        } else {
127            return Connections.emptyConnection();
128        }
129    }
130
131}