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.graphql.server.graphql.BaseEntityInstanceObject;
021import com.echothree.model.control.graphql.server.graphql.count.CountedObjects;
022import com.echothree.model.control.graphql.server.graphql.count.CountingDataConnectionFetcher;
023import com.echothree.model.control.graphql.server.graphql.count.CountingPaginatedData;
024import com.echothree.model.control.graphql.server.util.count.ObjectLimiter;
025import com.echothree.model.control.party.server.graphql.PartyObject;
026import com.echothree.model.control.party.server.graphql.PartySecurityUtils;
027import com.echothree.model.data.contact.common.PartyContactMechanismPurposeConstants;
028import com.echothree.model.data.contact.server.entity.PartyContactMechanism;
029import com.echothree.model.data.contact.server.entity.PartyContactMechanismDetail;
030import com.echothree.util.server.persistence.Session;
031import graphql.annotations.annotationTypes.GraphQLDescription;
032import graphql.annotations.annotationTypes.GraphQLField;
033import graphql.annotations.annotationTypes.GraphQLName;
034import graphql.annotations.annotationTypes.GraphQLNonNull;
035import graphql.annotations.connection.GraphQLConnection;
036import graphql.schema.DataFetchingEnvironment;
037import java.util.ArrayList;
038import java.util.stream.Collectors;
039
040@GraphQLDescription("partyContactMechanism object")
041@GraphQLName("PartyContactMechanism")
042public class PartyContactMechanismObject
043        extends BaseEntityInstanceObject {
044
045    private final PartyContactMechanism partyContactMechanism; // Always Present
046
047    public PartyContactMechanismObject(PartyContactMechanism partyContactMechanism) {
048        super(partyContactMechanism.getPrimaryKey());
049        
050        this.partyContactMechanism = partyContactMechanism;
051    }
052
053    private PartyContactMechanismDetail partyContactMechanismDetail; // Optional, use getPartyContactMechanismDetail()
054    
055    private PartyContactMechanismDetail getPartyContactMechanismDetail() {
056        if(partyContactMechanismDetail == null) {
057            partyContactMechanismDetail = partyContactMechanism.getLastDetail();
058        }
059        
060        return partyContactMechanismDetail;
061    }
062
063    @GraphQLField
064    @GraphQLDescription("party")
065    public PartyObject getParty(final DataFetchingEnvironment env) {
066        var party = partyContactMechanismDetail.getParty();
067
068        return PartySecurityUtils.getHasPartyAccess(env, party) ? new PartyObject(party) : null;
069    }
070
071    @GraphQLField
072    @GraphQLDescription("contact mechanism")
073    @GraphQLNonNull
074    public ContactMechanismObject getContactMechanism() {
075        return new ContactMechanismObject(getPartyContactMechanismDetail().getContactMechanism());
076    }
077
078    @GraphQLField
079    @GraphQLDescription("description")
080    public String getDescription(final DataFetchingEnvironment env) {
081        return getPartyContactMechanismDetail().getDescription();
082    }
083
084    @GraphQLField
085    @GraphQLDescription("is default")
086    @GraphQLNonNull
087    public boolean getIsDefault() {
088        return getPartyContactMechanismDetail().getIsDefault();
089    }
090    
091    @GraphQLField
092    @GraphQLDescription("sort order")
093    @GraphQLNonNull
094    public int getSortOrder() {
095        return getPartyContactMechanismDetail().getSortOrder();
096    }
097
098    @GraphQLField
099    @GraphQLDescription("party contact mechanism purposes")
100    @GraphQLNonNull
101    @GraphQLConnection(connectionFetcher = CountingDataConnectionFetcher.class)
102    public CountingPaginatedData<PartyContactMechanismPurposeObject> getPartyContactMechanismPurposes(final DataFetchingEnvironment env) {
103//        if(ContactSecurityUtils.getHasPartyContactMechanismPurposesAccess(env, party)) {
104        var contactControl = Session.getModelController(ContactControl.class);
105        var totalCount = contactControl.countPartyContactMechanismPurposesByPartyContactMechanism(partyContactMechanism);
106
107        try(var objectLimiter = new ObjectLimiter(env, PartyContactMechanismPurposeConstants.COMPONENT_VENDOR_NAME, PartyContactMechanismPurposeConstants.ENTITY_TYPE_NAME, totalCount)) {
108            var entities = contactControl.getPartyContactMechanismPurposesByPartyContactMechanism(partyContactMechanism);
109            var partyContactMechanismPurposees = entities.stream().map(PartyContactMechanismPurposeObject::new).collect(Collectors.toCollection(() -> new ArrayList<>(entities.size())));
110
111            return new CountedObjects<>(objectLimiter, partyContactMechanismPurposees);
112        }
113//        } else {
114//            return Connections.emptyConnection();
115//        }
116    }
117
118}