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.common.CustomerTypeContactListGroupConstants;
030import com.echothree.model.data.contactlist.common.PartyTypeContactListGroupConstants;
031import com.echothree.model.data.contactlist.server.entity.ContactListGroup;
032import com.echothree.model.data.contactlist.server.entity.ContactListGroupDetail;
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("contact list group object")
044@GraphQLName("ContactListGroup")
045public class ContactListGroupObject
046        extends BaseEntityInstanceObject {
047    
048    private final ContactListGroup contactListGroup; // Always Present
049    
050    public ContactListGroupObject(ContactListGroup contactListGroup) {
051        super(contactListGroup.getPrimaryKey());
052        
053        this.contactListGroup = contactListGroup;
054    }
055
056    private ContactListGroupDetail contactListGroupDetail; // Optional, use getContactListGroupDetail()
057    
058    private ContactListGroupDetail getContactListGroupDetail() {
059        if(contactListGroupDetail == null) {
060            contactListGroupDetail = contactListGroup.getLastDetail();
061        }
062        
063        return contactListGroupDetail;
064    }
065    
066    @GraphQLField
067    @GraphQLDescription("contact list group name")
068    @GraphQLNonNull
069    public String getContactListGroupName() {
070        return getContactListGroupDetail().getContactListGroupName();
071    }
072    
073    @GraphQLField
074    @GraphQLDescription("is default")
075    @GraphQLNonNull
076    public boolean getIsDefault() {
077        return getContactListGroupDetail().getIsDefault();
078    }
079    
080    @GraphQLField
081    @GraphQLDescription("sort order")
082    @GraphQLNonNull
083    public int getSortOrder() {
084        return getContactListGroupDetail().getSortOrder();
085    }
086    
087    @GraphQLField
088    @GraphQLDescription("description")
089    @GraphQLNonNull
090    public String getDescription(final DataFetchingEnvironment env) {
091        var contactListControl = Session.getModelController(ContactListControl.class);
092        var userControl = Session.getModelController(UserControl.class);
093
094        return contactListControl.getBestContactListGroupDescription(contactListGroup, userControl.getPreferredLanguageFromUserVisit(BaseGraphQl.getUserVisit(env)));
095    }
096
097    @GraphQLField
098    @GraphQLDescription("contact lists")
099    @GraphQLNonNull
100    @GraphQLConnection(connectionFetcher = CountingDataConnectionFetcher.class)
101    public CountingPaginatedData<ContactListObject> getContactLists(final DataFetchingEnvironment env) {
102        if(ContactListSecurityUtils.getHasContactListsAccess(env)) {
103            var chainControl = Session.getModelController(ContactListControl.class);
104            var totalCount = chainControl.countContactListsByContactListGroup(contactListGroup);
105
106            try(var objectLimiter = new ObjectLimiter(env, ContactListConstants.COMPONENT_VENDOR_NAME, ContactListConstants.ENTITY_TYPE_NAME, totalCount)) {
107                var entities = chainControl.getContactListsByContactListGroup(contactListGroup);
108                var contactLists = entities.stream().map(ContactListObject::new).collect(Collectors.toCollection(() -> new ArrayList<>(entities.size())));
109
110                return new CountedObjects<>(objectLimiter, contactLists);
111            }
112        } else {
113            return Connections.emptyConnection();
114        }
115    }
116
117    @GraphQLField
118    @GraphQLDescription("customer type contact list groups")
119    @GraphQLNonNull
120    @GraphQLConnection(connectionFetcher = CountingDataConnectionFetcher.class)
121    public CountingPaginatedData<CustomerTypeContactListGroupObject> getCustomerTypeContactListGroups(final DataFetchingEnvironment env) {
122        if(ContactListSecurityUtils.getHasCustomerTypeContactListGroupsAccess(env)) {
123            var contactListControl = Session.getModelController(ContactListControl.class);
124            var totalCount = contactListControl.countCustomerTypeContactListGroupsByContactListGroup(contactListGroup);
125
126            try(var objectLimiter = new ObjectLimiter(env, CustomerTypeContactListGroupConstants.COMPONENT_VENDOR_NAME, CustomerTypeContactListGroupConstants.ENTITY_TYPE_NAME, totalCount)) {
127                var entities = contactListControl.getCustomerTypeContactListGroupsByContactListGroup(contactListGroup);
128                var customerTypeContactListGroups = entities.stream().map(CustomerTypeContactListGroupObject::new).collect(Collectors.toCollection(() -> new ArrayList<>(entities.size())));
129
130                return new CountedObjects<>(objectLimiter, customerTypeContactListGroups);
131            }
132        } else {
133            return Connections.emptyConnection();
134        }
135    }
136
137    @GraphQLField
138    @GraphQLDescription("party type contact list groups")
139    @GraphQLNonNull
140    @GraphQLConnection(connectionFetcher = CountingDataConnectionFetcher.class)
141    public CountingPaginatedData<PartyTypeContactListGroupObject> getPartyTypeContactListGroups(final DataFetchingEnvironment env) {
142        if(ContactListSecurityUtils.getHasPartyTypeContactListGroupsAccess(env)) {
143            var contactListControl = Session.getModelController(ContactListControl.class);
144            var totalCount = contactListControl.countPartyTypeContactListGroupsByContactListGroup(contactListGroup);
145
146            try(var objectLimiter = new ObjectLimiter(env, PartyTypeContactListGroupConstants.COMPONENT_VENDOR_NAME, PartyTypeContactListGroupConstants.ENTITY_TYPE_NAME, totalCount)) {
147                var entities = contactListControl.getPartyTypeContactListGroupsByContactListGroup(contactListGroup);
148                var partyTypeContactListGroups = entities.stream().map(PartyTypeContactListGroupObject::new).collect(Collectors.toCollection(() -> new ArrayList<>(entities.size())));
149
150                return new CountedObjects<>(objectLimiter, partyTypeContactListGroups);
151            }
152        } else {
153            return Connections.emptyConnection();
154        }
155    }
156
157}