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.item.server.graphql;
018
019import com.echothree.model.control.graphql.server.graphql.BaseEntityInstanceObject;
020import com.echothree.model.data.item.server.entity.RelatedItem;
021import com.echothree.model.data.item.server.entity.RelatedItemDetail;
022import graphql.annotations.annotationTypes.GraphQLDescription;
023import graphql.annotations.annotationTypes.GraphQLField;
024import graphql.annotations.annotationTypes.GraphQLName;
025import graphql.annotations.annotationTypes.GraphQLNonNull;
026import graphql.schema.DataFetchingEnvironment;
027
028@GraphQLDescription("related item object")
029@GraphQLName("RelatedItem")
030public class RelatedItemObject
031        extends BaseEntityInstanceObject {
032
033    private final RelatedItem relatedItem; // Always Present
034
035    public RelatedItemObject(RelatedItem relatedItem) {
036        super(relatedItem.getPrimaryKey());
037        
038        this.relatedItem = relatedItem;
039    }
040
041    private RelatedItemDetail relatedItemDetail; // Optional, use getRelatedItemTypeDetail()
042    
043    private RelatedItemDetail getRelatedItemDetail() {
044        if(relatedItemDetail == null) {
045            relatedItemDetail = relatedItem.getLastDetail();
046        }
047        
048        return relatedItemDetail;
049    }
050
051    @GraphQLField
052    @GraphQLDescription("related item type")
053    @GraphQLNonNull
054    public RelatedItemTypeObject getRelatedItemType(final DataFetchingEnvironment env) {
055        return ItemSecurityUtils.getHasItemAliasTypeAccess(env) ? new RelatedItemTypeObject(getRelatedItemDetail().getRelatedItemType(), null) : null;
056    }
057
058    @GraphQLField
059    @GraphQLDescription("from item")
060    public ItemObject getFromItem(final DataFetchingEnvironment env) {
061        return ItemSecurityUtils.getHasItemAccess(env) ? new ItemObject(getRelatedItemDetail().getFromItem()) : null;
062    }
063
064    @GraphQLField
065    @GraphQLDescription("toitem")
066    public ItemObject getToItem(final DataFetchingEnvironment env) {
067        return ItemSecurityUtils.getHasItemAccess(env) ? new ItemObject(getRelatedItemDetail().getToItem()) : null;
068    }
069
070    @GraphQLField
071    @GraphQLDescription("sort order")
072    @GraphQLNonNull
073    public int getSortOrder() {
074        return getRelatedItemDetail().getSortOrder();
075    }
076    
077}