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.SelectorKind;
024import com.echothree.model.data.selector.server.entity.SelectorKindDetail;
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;
031import java.util.ArrayList;
032import java.util.Collection;
033
034@GraphQLDescription("selector kind object")
035@GraphQLName("SelectorKind")
036public class SelectorKindObject
037        extends BaseEntityInstanceObject {
038    
039    private final SelectorKind selectorKind; // Always Present
040    
041    public SelectorKindObject(SelectorKind selectorKind) {
042        super(selectorKind.getPrimaryKey());
043        
044        this.selectorKind = selectorKind;
045    }
046
047    private SelectorKindDetail selectorKindDetail; // Optional, use getSelectorKindDetail()
048    
049    private SelectorKindDetail getSelectorKindDetail() {
050        if(selectorKindDetail == null) {
051            selectorKindDetail = selectorKind.getLastDetail();
052        }
053        
054        return selectorKindDetail;
055    }
056    
057    @GraphQLField
058    @GraphQLDescription("selector kind name")
059    @GraphQLNonNull
060    public String getSelectorKindName() {
061        return getSelectorKindDetail().getSelectorKindName();
062    }
063    
064    @GraphQLField
065    @GraphQLDescription("is default")
066    @GraphQLNonNull
067    public boolean getIsDefault() {
068        return getSelectorKindDetail().getIsDefault();
069    }
070    
071    @GraphQLField
072    @GraphQLDescription("sort order")
073    @GraphQLNonNull
074    public int getSortOrder() {
075        return getSelectorKindDetail().getSortOrder();
076    }
077    
078    @GraphQLField
079    @GraphQLDescription("description")
080    @GraphQLNonNull
081    public String getDescription(final DataFetchingEnvironment env) {
082        var selectorControl = Session.getModelController(SelectorControl.class);
083        var userControl = Session.getModelController(UserControl.class);
084
085        return selectorControl.getBestSelectorKindDescription(selectorKind, userControl.getPreferredLanguageFromUserVisit(BaseGraphQl.getUserVisit(env)));
086    }
087
088    @GraphQLField
089    @GraphQLDescription("selector types")
090    public Collection<SelectorTypeObject> getSelectorTypes(final DataFetchingEnvironment env) {
091        Collection<SelectorTypeObject> selectorTypeObjects = null;
092
093        if(SelectorSecurityUtils.getHasSelectorTypesAccess(env)) {
094            var selectorControl = Session.getModelController(SelectorControl.class);
095            var selectorTypes = selectorControl.getSelectorTypes(selectorKind);
096
097            selectorTypeObjects = new ArrayList<>(selectorTypes.size());
098
099            selectorTypes.stream()
100                    .map(SelectorTypeObject::new)
101                    .forEachOrdered(selectorTypeObjects::add);
102        }
103
104        return selectorTypeObjects;
105    }
106
107}