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.selector.server.graphql;
018
019import com.echothree.model.control.graphql.server.graphql.BaseEntityInstanceObject;
020import com.echothree.model.control.graphql.server.graphql.count.Connections;
021import com.echothree.model.control.graphql.server.graphql.count.CountedObjects;
022import com.echothree.model.control.graphql.server.graphql.count.CountingDataConnectionFetcher;
023import com.echothree.model.control.graphql.server.graphql.count.CountingPaginatedData;
024import com.echothree.model.control.graphql.server.util.BaseGraphQl;
025import com.echothree.model.control.graphql.server.util.count.ObjectLimiter;
026import com.echothree.model.control.selector.server.control.SelectorControl;
027import com.echothree.model.control.user.server.control.UserControl;
028import com.echothree.model.data.selector.common.SelectorTypeConstants;
029import com.echothree.model.data.selector.server.entity.SelectorKind;
030import com.echothree.model.data.selector.server.entity.SelectorKindDetail;
031import com.echothree.util.server.persistence.Session;
032import graphql.annotations.annotationTypes.GraphQLDescription;
033import graphql.annotations.annotationTypes.GraphQLField;
034import graphql.annotations.annotationTypes.GraphQLName;
035import graphql.annotations.annotationTypes.GraphQLNonNull;
036import graphql.annotations.connection.GraphQLConnection;
037import graphql.schema.DataFetchingEnvironment;
038import java.util.ArrayList;
039import java.util.Collection;
040import java.util.stream.Collectors;
041
042@GraphQLDescription("selector kind object")
043@GraphQLName("SelectorKind")
044public class SelectorKindObject
045        extends BaseEntityInstanceObject {
046    
047    private final SelectorKind selectorKind; // Always Present
048    
049    public SelectorKindObject(SelectorKind selectorKind) {
050        super(selectorKind.getPrimaryKey());
051        
052        this.selectorKind = selectorKind;
053    }
054
055    private SelectorKindDetail selectorKindDetail; // Optional, use getSelectorKindDetail()
056    
057    private SelectorKindDetail getSelectorKindDetail() {
058        if(selectorKindDetail == null) {
059            selectorKindDetail = selectorKind.getLastDetail();
060        }
061        
062        return selectorKindDetail;
063    }
064    
065    @GraphQLField
066    @GraphQLDescription("selector kind name")
067    @GraphQLNonNull
068    public String getSelectorKindName() {
069        return getSelectorKindDetail().getSelectorKindName();
070    }
071    
072    @GraphQLField
073    @GraphQLDescription("is default")
074    @GraphQLNonNull
075    public boolean getIsDefault() {
076        return getSelectorKindDetail().getIsDefault();
077    }
078    
079    @GraphQLField
080    @GraphQLDescription("sort order")
081    @GraphQLNonNull
082    public int getSortOrder() {
083        return getSelectorKindDetail().getSortOrder();
084    }
085    
086    @GraphQLField
087    @GraphQLDescription("description")
088    @GraphQLNonNull
089    public String getDescription(final DataFetchingEnvironment env) {
090        var selectorControl = Session.getModelController(SelectorControl.class);
091        var userControl = Session.getModelController(UserControl.class);
092
093        return selectorControl.getBestSelectorKindDescription(selectorKind, userControl.getPreferredLanguageFromUserVisit(BaseGraphQl.getUserVisit(env)));
094    }
095
096    @GraphQLField
097    @GraphQLDescription("selector types")
098    @GraphQLNonNull
099    @GraphQLConnection(connectionFetcher = CountingDataConnectionFetcher.class)
100    public CountingPaginatedData<SelectorTypeObject> getSelectorTypes(final DataFetchingEnvironment env) {
101        if(SelectorSecurityUtils.getHasSelectorTypesAccess(env)) {
102            var selectorControl = Session.getModelController(SelectorControl.class);
103            var totalCount = selectorControl.countSelectorTypesBySelectorKind(selectorKind);
104
105            try(var objectLimiter = new ObjectLimiter(env, SelectorTypeConstants.COMPONENT_VENDOR_NAME, SelectorTypeConstants.ENTITY_TYPE_NAME, totalCount)) {
106                var entities = selectorControl.getSelectorTypes(selectorKind);
107                var selectorTypes = entities.stream().map(SelectorTypeObject::new).collect(Collectors.toCollection(() -> new ArrayList<>(entities.size())));
108
109                return new CountedObjects<>(objectLimiter, selectorTypes);
110            }
111        } else {
112            return Connections.emptyConnection();
113        }
114    }
115
116}