001// --------------------------------------------------------------------------------
002// Copyright 2002-2025 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    @GraphQLNonNull
066    public PartyObject getParty(final DataFetchingEnvironment env) {
067        var party = partyContactMechanismDetail.getParty();
068
069        return PartySecurityUtils.getHasPartyAccess(env, party) ? new PartyObject(party) : null;
070    }
071
072    @GraphQLField
073    @GraphQLDescription("contact mechanism")
074    @GraphQLNonNull
075    public ContactMechanismObject getContactMechanism() {
076        return new ContactMechanismObject(getPartyContactMechanismDetail().getContactMechanism());
077    }
078
079    @GraphQLField
080    @GraphQLDescription("description")
081    public String getDescription(final DataFetchingEnvironment env) {
082        return getPartyContactMechanismDetail().getDescription();
083    }
084
085    @GraphQLField
086    @GraphQLDescription("is default")
087    @GraphQLNonNull
088    public boolean getIsDefault() {
089        return getPartyContactMechanismDetail().getIsDefault();
090    }
091    
092    @GraphQLField
093    @GraphQLDescription("sort order")
094    @GraphQLNonNull
095    public int getSortOrder() {
096        return getPartyContactMechanismDetail().getSortOrder();
097    }
098
099    @GraphQLField
100    @GraphQLDescription("party contact mechanism purposes")
101    @GraphQLNonNull
102    @GraphQLConnection(connectionFetcher = CountingDataConnectionFetcher.class)
103    public CountingPaginatedData<PartyContactMechanismPurposeObject> getPartyContactMechanismPurposes(final DataFetchingEnvironment env) {
104//        if(ContactSecurityUtils.getHasPartyContactMechanismPurposesAccess(env, party)) {
105        var contactControl = Session.getModelController(ContactControl.class);
106        var totalCount = contactControl.countPartyContactMechanismPurposesByPartyContactMechanism(partyContactMechanism);
107
108        try(var objectLimiter = new ObjectLimiter(env, PartyContactMechanismPurposeConstants.COMPONENT_VENDOR_NAME, PartyContactMechanismPurposeConstants.ENTITY_TYPE_NAME, totalCount)) {
109            var entities = contactControl.getPartyContactMechanismPurposesByPartyContactMechanism(partyContactMechanism);
110            var partyContactMechanismPurposees = entities.stream().map(PartyContactMechanismPurposeObject::new).collect(Collectors.toCollection(() -> new ArrayList<>(entities.size())));
111
112            return new CountedObjects<>(objectLimiter, partyContactMechanismPurposees);
113        }
114//        } else {
115//            return Connections.emptyConnection();
116//        }
117    }
118
119}