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.user.server.graphql;
018
019import com.echothree.model.control.graphql.server.graphql.BaseObject;
020import com.echothree.model.control.party.server.graphql.PartyObject;
021import com.echothree.model.control.party.server.graphql.PartyRelationshipObject;
022import com.echothree.model.control.party.server.graphql.PartySecurityUtils;
023import com.echothree.model.data.user.server.entity.UserKey;
024import com.echothree.model.data.user.server.entity.UserKeyDetail;
025import graphql.annotations.annotationTypes.GraphQLDescription;
026import graphql.annotations.annotationTypes.GraphQLField;
027import graphql.annotations.annotationTypes.GraphQLName;
028import graphql.annotations.annotationTypes.GraphQLNonNull;
029import graphql.schema.DataFetchingEnvironment;
030
031@GraphQLDescription("user key object")
032@GraphQLName("UserKey")
033public class UserKeyObject
034        extends BaseObject {
035    
036    private final UserKey userKey; // Always Present
037    
038    public UserKeyObject(UserKey userKey) {
039        this.userKey = userKey;
040    }
041
042    private UserKeyDetail userKeyDetail; // Optional, use getUserKeyDetail()
043    
044    private UserKeyDetail getUserKeyDetail() {
045        if(userKeyDetail == null) {
046            userKeyDetail = userKey.getLastDetail();
047        }
048        
049        return userKeyDetail;
050    }
051    
052    @GraphQLField
053    @GraphQLDescription("user visit group name")
054    @GraphQLNonNull
055    public String getUserKeyName() {
056        return getUserKeyDetail().getUserKeyName();
057    }
058
059    @GraphQLField
060    @GraphQLDescription("party")
061    public PartyObject getParty(final DataFetchingEnvironment env) {
062        var party = getUserKeyDetail().getParty();
063
064        return party != null && PartySecurityUtils.getHasPartyAccess(env, party) ?
065                new PartyObject(party) : null;
066    }
067
068    @GraphQLField
069    @GraphQLDescription("party relationship")
070    public PartyRelationshipObject getPartyRelationship(final DataFetchingEnvironment env) {
071        var partyRelationship = getUserKeyDetail().getPartyRelationship();
072
073        return partyRelationship != null && PartySecurityUtils.getHasPartyRelationshipAccess(env) ?
074                new PartyRelationshipObject(partyRelationship) : null;
075    }
076
077
078}