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.common.ChainActionTypes;
020import com.echothree.model.control.chain.server.control.ChainControl;
021import com.echothree.model.control.graphql.server.graphql.BaseEntityInstanceObject;
022import com.echothree.model.control.graphql.server.util.BaseGraphQl;
023import com.echothree.model.control.user.server.control.UserControl;
024import com.echothree.model.data.chain.server.entity.ChainAction;
025import com.echothree.model.data.chain.server.entity.ChainActionDetail;
026import com.echothree.util.server.persistence.Session;
027import graphql.annotations.annotationTypes.GraphQLDescription;
028import graphql.annotations.annotationTypes.GraphQLField;
029import graphql.annotations.annotationTypes.GraphQLName;
030import graphql.annotations.annotationTypes.GraphQLNonNull;
031import graphql.schema.DataFetchingEnvironment;
032
033@GraphQLDescription("chain action object")
034@GraphQLName("ChainAction")
035public class ChainActionObject
036        extends BaseEntityInstanceObject {
037    
038    private final ChainAction chainAction; // Always Present
039    
040    public ChainActionObject(ChainAction chainAction) {
041        super(chainAction.getPrimaryKey());
042        
043        this.chainAction = chainAction;
044    }
045
046    private ChainActionDetail chainActionDetail; // Optional, use getChainActionDetail()
047    
048    private ChainActionDetail getChainActionDetail() {
049        if(chainActionDetail == null) {
050            chainActionDetail = chainAction.getLastDetail();
051        }
052        
053        return chainActionDetail;
054    }
055
056    @GraphQLField
057    @GraphQLDescription("chain action set")
058    public ChainActionSetObject getChainActionSet(final DataFetchingEnvironment env) {
059        return ChainSecurityUtils.getHasChainActionSetAccess(env) ? new ChainActionSetObject(getChainActionDetail().getChainActionSet()) : null;
060    }
061
062    @GraphQLField
063    @GraphQLDescription("chain action name")
064    @GraphQLNonNull
065    public String getChainActionName() {
066        return getChainActionDetail().getChainActionName();
067    }
068
069    @GraphQLField
070    @GraphQLDescription("chain action type")
071    public ChainActionTypeObject getChainActionType(final DataFetchingEnvironment env) {
072        return ChainSecurityUtils.getHasChainActionTypeAccess(env) ? new ChainActionTypeObject(getChainActionDetail().getChainActionType()) : null;
073    }
074
075    @GraphQLField
076    @GraphQLDescription("sort order")
077    @GraphQLNonNull
078    public int getSortOrder() {
079        return getChainActionDetail().getSortOrder();
080    }
081    
082    @GraphQLField
083    @GraphQLDescription("description")
084    @GraphQLNonNull
085    public String getDescription(final DataFetchingEnvironment env) {
086        var chainControl = Session.getModelController(ChainControl.class);
087        var userControl = Session.getModelController(UserControl.class);
088
089        return chainControl.getBestChainActionDescription(chainAction, userControl.getPreferredLanguageFromUserVisit(BaseGraphQl.getUserVisit(env)));
090    }
091
092    private ChainActionTypes chainActionTypeEnum = null; // Optional, use getChainActionTypeEnum()
093
094    protected ChainActionTypes getChainActionTypeEnum() {
095        if(chainActionTypeEnum == null) {
096            chainActionTypeEnum = ChainActionTypes.valueOf(getChainActionDetail().getChainActionType().getLastDetail().getChainActionTypeName());
097        }
098
099        return chainActionTypeEnum;
100    }
101
102    @GraphQLField
103    @GraphQLDescription("chain action")
104    public ChainActionInterface getChainAction(final DataFetchingEnvironment env) {
105        var chainControl = Session.getModelController(ChainControl.class);
106
107        return switch(getChainActionTypeEnum()) {
108            case LETTER -> null;
109            case SURVEY -> null;
110            case CHAIN_ACTION_SET -> new ChainActionChainActionSetObject(chainControl.getChainActionChainActionSet(chainAction));
111        };
112    }
113    
114}