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