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.selector.server.graphql;
018
019import com.echothree.model.control.graphql.server.graphql.BaseEntityInstanceObject;
020import com.echothree.model.control.graphql.server.util.BaseGraphQl;
021import com.echothree.model.control.selector.server.control.SelectorControl;
022import com.echothree.model.control.user.server.control.UserControl;
023import com.echothree.model.data.selector.server.entity.Selector;
024import com.echothree.model.data.selector.server.entity.SelectorDetail;
025import com.echothree.util.server.persistence.Session;
026import graphql.annotations.annotationTypes.GraphQLDescription;
027import graphql.annotations.annotationTypes.GraphQLField;
028import graphql.annotations.annotationTypes.GraphQLName;
029import graphql.annotations.annotationTypes.GraphQLNonNull;
030import graphql.schema.DataFetchingEnvironment;
031
032@GraphQLDescription("selector object")
033@GraphQLName("Selector")
034public class SelectorObject
035        extends BaseEntityInstanceObject {
036    
037    private final Selector selector; // Always Present
038    
039    public SelectorObject(Selector selector) {
040        super(selector.getPrimaryKey());
041        
042        this.selector = selector;
043    }
044
045    private SelectorDetail selectorDetail; // Optional, use getSelectorDetail()
046    
047    private SelectorDetail getSelectorDetail() {
048        if(selectorDetail == null) {
049            selectorDetail = selector.getLastDetail();
050        }
051        
052        return selectorDetail;
053    }
054
055    @GraphQLField
056    @GraphQLDescription("selector kind")
057    public SelectorTypeObject getSelectorType(final DataFetchingEnvironment env) {
058        return SelectorSecurityUtils.getHasSelectorTypeAccess(env) ? new SelectorTypeObject(getSelectorDetail().getSelectorType()) : null;
059    }
060
061    @GraphQLField
062    @GraphQLDescription("selector type name")
063    @GraphQLNonNull
064    public String getSelectorName() {
065        return getSelectorDetail().getSelectorName();
066    }
067    
068    @GraphQLField
069    @GraphQLDescription("is default")
070    @GraphQLNonNull
071    public boolean getIsDefault() {
072        return getSelectorDetail().getIsDefault();
073    }
074    
075    @GraphQLField
076    @GraphQLDescription("sort order")
077    @GraphQLNonNull
078    public int getSortOrder() {
079        return getSelectorDetail().getSortOrder();
080    }
081    
082    @GraphQLField
083    @GraphQLDescription("description")
084    @GraphQLNonNull
085    public String getDescription(final DataFetchingEnvironment env) {
086        var selectorControl = Session.getModelController(SelectorControl.class);
087        var userControl = Session.getModelController(UserControl.class);
088
089        return selectorControl.getBestSelectorDescription(selector, userControl.getPreferredLanguageFromUserVisit(BaseGraphQl.getUserVisit(env)));
090    }
091    
092}