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.party.server.graphql;
018
019import com.echothree.model.control.geo.server.control.GeoControl;
020import com.echothree.model.control.geo.server.graphql.GeoCodeLanguageObject;
021import com.echothree.model.control.geo.server.graphql.GeoSecurityUtils;
022import com.echothree.model.control.graphql.server.graphql.BaseEntityInstanceObject;
023import com.echothree.model.control.graphql.server.graphql.count.Connections;
024import com.echothree.model.control.graphql.server.graphql.count.CountedObjects;
025import com.echothree.model.control.graphql.server.graphql.count.CountingDataConnectionFetcher;
026import com.echothree.model.control.graphql.server.graphql.count.CountingPaginatedData;
027import com.echothree.model.control.graphql.server.util.BaseGraphQl;
028import com.echothree.model.control.graphql.server.util.count.ObjectLimiter;
029import com.echothree.model.control.party.server.control.PartyControl;
030import com.echothree.model.control.user.server.control.UserControl;
031import com.echothree.model.data.geo.common.GeoCodeLanguageConstants;
032import com.echothree.model.data.party.server.entity.Language;
033import com.echothree.util.server.persistence.Session;
034import graphql.annotations.annotationTypes.GraphQLDescription;
035import graphql.annotations.annotationTypes.GraphQLField;
036import graphql.annotations.annotationTypes.GraphQLName;
037import graphql.annotations.annotationTypes.GraphQLNonNull;
038import graphql.annotations.connection.GraphQLConnection;
039import graphql.schema.DataFetchingEnvironment;
040import java.util.ArrayList;
041import java.util.stream.Collectors;
042
043@GraphQLDescription("language object")
044@GraphQLName("Language")
045public class LanguageObject
046        extends BaseEntityInstanceObject {
047    
048    private final Language language; // Always Present
049    
050    public LanguageObject(Language language) {
051        super(language.getPrimaryKey());
052        
053        this.language = language;
054    }
055    
056    @GraphQLField
057    @GraphQLDescription("language iso name")
058    @GraphQLNonNull
059    public String getLanguageIsoName() {
060        return language.getLanguageIsoName();
061    }
062
063    @GraphQLField
064    @GraphQLDescription("is default")
065    @GraphQLNonNull
066    public Boolean getIsDefault() {
067        return language.getIsDefault();
068    }
069
070    @GraphQLField
071    @GraphQLDescription("sort order")
072    @GraphQLNonNull
073    public Integer getSortOrder() {
074        return language.getSortOrder();
075    }
076
077    @GraphQLField
078    @GraphQLDescription("description")
079    @GraphQLNonNull
080    public String getDescription(final DataFetchingEnvironment env) {
081        var partyControl = Session.getModelController(PartyControl.class);
082        var userControl = Session.getModelController(UserControl.class);
083
084        return partyControl.getBestLanguageDescription(language, userControl.getPreferredLanguageFromUserVisit(BaseGraphQl.getUserVisit(env)));
085    }
086
087    @GraphQLField
088    @GraphQLDescription("geo code languages")
089    @GraphQLNonNull
090    @GraphQLConnection(connectionFetcher = CountingDataConnectionFetcher.class)
091    public CountingPaginatedData<GeoCodeLanguageObject> getGeoCodeLanguages(final DataFetchingEnvironment env) {
092        if(GeoSecurityUtils.getHasGeoCodeLanguagesAccess(env)) {
093            var geoControl = Session.getModelController(GeoControl.class);
094            var totalCount = geoControl.countGeoCodeLanguagesByLanguage(language);
095
096            try(var objectLimiter = new ObjectLimiter(env, GeoCodeLanguageConstants.COMPONENT_VENDOR_NAME, GeoCodeLanguageConstants.ENTITY_TYPE_NAME, totalCount)) {
097                var entities = geoControl.getGeoCodeLanguagesByLanguage(language);
098                var items = entities.stream().map(GeoCodeLanguageObject::new).collect(Collectors.toCollection(() -> new ArrayList<>(entities.size())));
099
100                return new CountedObjects<>(objectLimiter, items);
101            }
102        } else {
103            return Connections.emptyConnection();
104        }
105    }
106
107}