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.contact.server.graphql;
018
019import com.echothree.model.control.contact.server.control.ContactControl;
020import com.echothree.model.control.contactlist.server.control.ContactListControl;
021import com.echothree.model.control.contactlist.server.graphql.ContactListContactMechanismPurposeObject;
022import com.echothree.model.control.contactlist.server.graphql.ContactListSecurityUtils;
023import com.echothree.model.control.graphql.server.graphql.BaseEntityInstanceObject;
024import com.echothree.model.control.graphql.server.graphql.count.Connections;
025import com.echothree.model.control.graphql.server.graphql.count.CountedObjects;
026import com.echothree.model.control.graphql.server.graphql.count.CountingDataConnectionFetcher;
027import com.echothree.model.control.graphql.server.graphql.count.CountingPaginatedData;
028import com.echothree.model.control.graphql.server.util.BaseGraphQl;
029import com.echothree.model.control.graphql.server.util.count.ObjectLimiter;
030import com.echothree.model.control.user.server.control.UserControl;
031import com.echothree.model.data.contact.server.entity.ContactMechanismPurpose;
032import com.echothree.model.data.contactlist.common.ContactListContactMechanismPurposeConstants;
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 mechanism purpose object")
044@GraphQLName("ContactMechanismPurpose")
045public class ContactMechanismPurposeObject
046        extends BaseEntityInstanceObject {
047    
048    private final ContactMechanismPurpose contactMechanismPurpose; // Always Present
049    
050    public ContactMechanismPurposeObject(ContactMechanismPurpose contactMechanismPurpose) {
051        super(contactMechanismPurpose.getPrimaryKey());
052
053        this.contactMechanismPurpose = contactMechanismPurpose;
054    }
055    
056    @GraphQLField
057    @GraphQLDescription("contact mechanism type name")
058    @GraphQLNonNull
059    public String getContactMechanismPurposeName() {
060        return contactMechanismPurpose.getContactMechanismPurposeName();
061    }
062
063    @GraphQLField
064    @GraphQLDescription("event subscriber")
065    @GraphQLNonNull
066    public Boolean getEventSubscriber() {
067        return contactMechanismPurpose.getEventSubscriber();
068    }
069
070    @GraphQLField
071    @GraphQLDescription("is default")
072    @GraphQLNonNull
073    public Boolean getIsDefault() {
074        return contactMechanismPurpose.getIsDefault();
075    }
076
077    @GraphQLField
078    @GraphQLDescription("sort order")
079    @GraphQLNonNull
080    public Integer getSortOrder() {
081        return contactMechanismPurpose.getSortOrder();
082    }
083
084    @GraphQLField
085    @GraphQLDescription("description")
086    @GraphQLNonNull
087    public String getDescription(final DataFetchingEnvironment env) {
088        var contactControl = Session.getModelController(ContactControl.class);
089        var userControl = Session.getModelController(UserControl.class);
090
091        return contactControl.getBestContactMechanismPurposeDescription(contactMechanismPurpose, userControl.getPreferredLanguageFromUserVisit(BaseGraphQl.getUserVisit(env)));
092    }
093
094    @GraphQLField
095    @GraphQLDescription("contact list contact mechanism purposes")
096    @GraphQLNonNull
097    @GraphQLConnection(connectionFetcher = CountingDataConnectionFetcher.class)
098    public CountingPaginatedData<ContactListContactMechanismPurposeObject> getContactListContactMechanismPurposes(final DataFetchingEnvironment env) {
099        if(ContactListSecurityUtils.getHasContactListContactMechanismPurposesAccess(env)) {
100            var contactListControl = Session.getModelController(ContactListControl.class);
101            var totalCount = contactListControl.countContactListContactMechanismPurposesByContactMechanismPurpose(contactMechanismPurpose);
102
103            try(var objectLimiter = new ObjectLimiter(env, ContactListContactMechanismPurposeConstants.COMPONENT_VENDOR_NAME, ContactListContactMechanismPurposeConstants.ENTITY_TYPE_NAME, totalCount)) {
104                var entities = contactListControl.getContactListContactMechanismPurposesByContactMechanismPurpose(contactMechanismPurpose);
105                var contactListContactMechanismPurposes = entities.stream().map(ContactListContactMechanismPurposeObject::new).collect(Collectors.toCollection(() -> new ArrayList<>(entities.size())));
106
107                return new CountedObjects<>(objectLimiter, contactListContactMechanismPurposes);
108            }
109        } else {
110            return Connections.emptyConnection();
111        }
112    }
113
114}