001// --------------------------------------------------------------------------------
002// Copyright 2002-2024 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.party.server.graphql;
018
019import com.echothree.model.control.graphql.server.util.BaseGraphQl;
020import com.echothree.model.data.party.server.entity.PartyRelationship;
021import graphql.annotations.annotationTypes.GraphQLDescription;
022import graphql.annotations.annotationTypes.GraphQLField;
023import graphql.annotations.annotationTypes.GraphQLName;
024import graphql.annotations.annotationTypes.GraphQLNonNull;
025import graphql.schema.DataFetchingEnvironment;
026
027@GraphQLDescription("party relationship object")
028@GraphQLName("PartyRelationship")
029public class PartyRelationshipObject
030        implements BaseGraphQl {
031
032    private final PartyRelationship partyRelationship; // Always Present
033    
034    public PartyRelationshipObject(PartyRelationship partyRelationship) {
035        this.partyRelationship = partyRelationship;
036    }
037
038    @GraphQLField
039    @GraphQLDescription("party relationship type")
040    @GraphQLNonNull
041    public PartyRelationshipTypeObject getPartyRelationshipType() {
042        return new PartyRelationshipTypeObject(partyRelationship.getPartyRelationshipType());
043    }
044
045    @GraphQLField
046    @GraphQLDescription("from party")
047    public PartyObject getFromParty(final DataFetchingEnvironment env) {
048        var fromParty = partyRelationship.getFromParty();
049
050        return PartySecurityUtils.getHasPartyAccess(env, fromParty) ? new PartyObject(fromParty) : null;
051    }
052
053    @GraphQLField
054    @GraphQLDescription("from role type")
055    @GraphQLNonNull
056    public RoleTypeObject getFromRoleType() {
057        return new RoleTypeObject(partyRelationship.getFromRoleType());
058    }
059
060    @GraphQLField
061    @GraphQLDescription("to party")
062    public PartyObject getToParty(final DataFetchingEnvironment env) {
063        var toParty = partyRelationship.getToParty();
064
065        return PartySecurityUtils.getHasPartyAccess(env, toParty) ? new PartyObject(toParty) : null;
066    }
067
068    @GraphQLField
069    @GraphQLDescription("to role type")
070    @GraphQLNonNull
071    public RoleTypeObject getToRoleType() {
072        return new RoleTypeObject(partyRelationship.getToRoleType());
073    }
074
075}