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.contactlist.server.graphql;
018
019import com.echothree.model.control.contactlist.server.control.ContactListControl;
020import com.echothree.model.control.graphql.server.graphql.BaseEntityInstanceObject;
021import com.echothree.model.control.graphql.server.graphql.count.Connections;
022import com.echothree.model.control.graphql.server.graphql.count.CountedObjects;
023import com.echothree.model.control.graphql.server.graphql.count.CountingDataConnectionFetcher;
024import com.echothree.model.control.graphql.server.graphql.count.CountingPaginatedData;
025import com.echothree.model.control.graphql.server.util.BaseGraphQl;
026import com.echothree.model.control.graphql.server.util.count.ObjectLimiter;
027import com.echothree.model.control.user.server.control.UserControl;
028import com.echothree.model.data.contactlist.common.ContactListConstants;
029import com.echothree.model.data.contactlist.server.entity.ContactListGroup;
030import com.echothree.model.data.contactlist.server.entity.ContactListGroupDetail;
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.stream.Collectors;
040
041@GraphQLDescription("contact list group object")
042@GraphQLName("ContactListGroup")
043public class ContactListGroupObject
044        extends BaseEntityInstanceObject {
045    
046    private final ContactListGroup contactListGroup; // Always Present
047    
048    public ContactListGroupObject(ContactListGroup contactListGroup) {
049        super(contactListGroup.getPrimaryKey());
050        
051        this.contactListGroup = contactListGroup;
052    }
053
054    private ContactListGroupDetail contactListGroupDetail; // Optional, use getContactListGroupDetail()
055    
056    private ContactListGroupDetail getContactListGroupDetail() {
057        if(contactListGroupDetail == null) {
058            contactListGroupDetail = contactListGroup.getLastDetail();
059        }
060        
061        return contactListGroupDetail;
062    }
063    
064    @GraphQLField
065    @GraphQLDescription("contact list group name")
066    @GraphQLNonNull
067    public String getContactListGroupName() {
068        return getContactListGroupDetail().getContactListGroupName();
069    }
070    
071    @GraphQLField
072    @GraphQLDescription("is default")
073    @GraphQLNonNull
074    public boolean getIsDefault() {
075        return getContactListGroupDetail().getIsDefault();
076    }
077    
078    @GraphQLField
079    @GraphQLDescription("sort order")
080    @GraphQLNonNull
081    public int getSortOrder() {
082        return getContactListGroupDetail().getSortOrder();
083    }
084    
085    @GraphQLField
086    @GraphQLDescription("description")
087    @GraphQLNonNull
088    public String getDescription(final DataFetchingEnvironment env) {
089        var contactListControl = Session.getModelController(ContactListControl.class);
090        var userControl = Session.getModelController(UserControl.class);
091
092        return contactListControl.getBestContactListGroupDescription(contactListGroup, userControl.getPreferredLanguageFromUserVisit(BaseGraphQl.getUserVisit(env)));
093    }
094
095    @GraphQLField
096    @GraphQLDescription("contact lists")
097    @GraphQLNonNull
098    @GraphQLConnection(connectionFetcher = CountingDataConnectionFetcher.class)
099    public CountingPaginatedData<ContactListObject> getContactLists(final DataFetchingEnvironment env) {
100        if(ContactListSecurityUtils.getHasContactListsAccess(env)) {
101            var chainControl = Session.getModelController(ContactListControl.class);
102            var totalCount = chainControl.countContactListsByContactListGroup(contactListGroup);
103
104            try(var objectLimiter = new ObjectLimiter(env, ContactListConstants.COMPONENT_VENDOR_NAME, ContactListConstants.ENTITY_TYPE_NAME, totalCount)) {
105                var entities = chainControl.getContactListsByContactListGroup(contactListGroup);
106                var contactLists = entities.stream().map(ContactListObject::new).collect(Collectors.toCollection(() -> new ArrayList<>(entities.size())));
107
108                return new CountedObjects<>(objectLimiter, contactLists);
109            }
110        } else {
111            return Connections.emptyConnection();
112        }
113    }
114
115}