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.ContactListFrequency;
030import com.echothree.model.data.contactlist.server.entity.ContactListFrequencyDetail;
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 frequency object")
042@GraphQLName("ContactListFrequency")
043public class ContactListFrequencyObject
044        extends BaseEntityInstanceObject {
045    
046    private final ContactListFrequency contactListFrequency; // Always Present
047    
048    public ContactListFrequencyObject(ContactListFrequency contactListFrequency) {
049        super(contactListFrequency.getPrimaryKey());
050        
051        this.contactListFrequency = contactListFrequency;
052    }
053
054    private ContactListFrequencyDetail contactListFrequencyDetail; // Optional, use getContactListFrequencyDetail()
055    
056    private ContactListFrequencyDetail getContactListFrequencyDetail() {
057        if(contactListFrequencyDetail == null) {
058            contactListFrequencyDetail = contactListFrequency.getLastDetail();
059        }
060        
061        return contactListFrequencyDetail;
062    }
063    
064    @GraphQLField
065    @GraphQLDescription("contact list frequency name")
066    @GraphQLNonNull
067    public String getContactListFrequencyName() {
068        return getContactListFrequencyDetail().getContactListFrequencyName();
069    }
070    
071    @GraphQLField
072    @GraphQLDescription("is default")
073    @GraphQLNonNull
074    public boolean getIsDefault() {
075        return getContactListFrequencyDetail().getIsDefault();
076    }
077    
078    @GraphQLField
079    @GraphQLDescription("sort order")
080    @GraphQLNonNull
081    public int getSortOrder() {
082        return getContactListFrequencyDetail().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.getBestContactListFrequencyDescription(contactListFrequency, 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.countContactListsByContactListFrequency(contactListFrequency);
103
104            try(var objectLimiter = new ObjectLimiter(env, ContactListConstants.COMPONENT_VENDOR_NAME, ContactListConstants.ENTITY_TYPE_NAME, totalCount)) {
105                var entities = chainControl.getContactListsByContactListFrequency(contactListFrequency);
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}